Skip to content
This repository was archived by the owner on Nov 6, 2024. It is now read-only.

Commit 21b0c73

Browse files
wllenyjjiangliu
authored andcommitted
fam_wrappers: implement PartialEq
Implementing PartialEq as helpful for testing. Since the FamStructWrapper type implementation of PartialEq requires the type T to implement PartialEq, it is necessary here to compare the other fields of the type T except entries. entries will be compare within FamStructWrapper's PartialEq. Signed-off-by: wllenyj <wllenyj@linux.alibaba.com>
1 parent 47d4a91 commit 21b0c73

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

coverage_config_x86_64.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"coverage_score": 66.3,
2+
"coverage_score": 67.5,
33
"exclude_path": "",
44
"crate_features": "fam-wrappers"
55
}

src/arm64/fam_wrappers.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ const ARM64_REGS_MAX: usize = 500;
1212
// Implement the FamStruct trait for kvm_reg_list.
1313
generate_fam_struct_impl!(kvm_reg_list, u64, reg, u64, n, ARM64_REGS_MAX);
1414

15+
// Implement the PartialEq trait for kvm_reg_list.
16+
impl PartialEq for kvm_reg_list {
17+
fn eq(&self, other: &kvm_reg_list) -> bool {
18+
// No need to call entries's eq, FamStructWrapper's PartialEq will do it for you
19+
self.n == other.n
20+
}
21+
}
22+
1523
/// Wrapper over the `kvm_reg_list` structure.
1624
///
1725
/// The `kvm_reg_list` structure contains a flexible array member. For details check the
@@ -20,3 +28,22 @@ generate_fam_struct_impl!(kvm_reg_list, u64, reg, u64, n, ARM64_REGS_MAX);
2028
/// the array elements, this type is implemented using
2129
/// [FamStructWrapper](../vmm_sys_util/fam/struct.FamStructWrapper.html).
2230
pub type RegList = FamStructWrapper<kvm_reg_list>;
31+
32+
#[cfg(test)]
33+
mod tests {
34+
use super::RegList;
35+
36+
#[test]
37+
fn test_reg_list_eq() {
38+
let mut wrapper = RegList::new(1).unwrap();
39+
assert_eq!(wrapper.as_slice().len(), 1);
40+
41+
let mut wrapper2 = wrapper.clone();
42+
assert!(wrapper == wrapper2);
43+
44+
wrapper.as_mut_slice()[0] = 1;
45+
assert!(wrapper != wrapper2);
46+
wrapper2.as_mut_slice()[0] = 1;
47+
assert!(wrapper == wrapper2);
48+
}
49+
}

src/x86/fam_wrappers.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ generate_fam_struct_impl!(
2323
KVM_MAX_CPUID_ENTRIES
2424
);
2525

26+
// Implement the PartialEq trait for kvm_cpuid2.
27+
//
28+
// Note:
29+
// This PartialEq implementation should not be used directly, instead FamStructWrapper
30+
// should be used. FamStructWrapper<T> provides us with an PartialEq implementation,
31+
// and it will determine the entire contents of the entries array. But requires
32+
// type T to implement `Default + FamStruct + PartialEq`, so we implement PartialEq here
33+
// and only need to determine the header field.
34+
impl PartialEq for kvm_cpuid2 {
35+
fn eq(&self, other: &kvm_cpuid2) -> bool {
36+
// No need to call entries's eq, FamStructWrapper's PartialEq will do it for you
37+
self.nent == other.nent && self.padding == other.padding
38+
}
39+
}
40+
2641
/// Wrapper over the `kvm_cpuid2` structure.
2742
///
2843
/// The `kvm_cpuid2` structure contains a flexible array member. For details check the
@@ -42,6 +57,14 @@ generate_fam_struct_impl!(
4257
KVM_MAX_MSR_ENTRIES
4358
);
4459

60+
// Implement the PartialEq trait for kvm_msrs.
61+
impl PartialEq for kvm_msrs {
62+
fn eq(&self, other: &kvm_msrs) -> bool {
63+
// No need to call entries's eq, FamStructWrapper's PartialEq will do it for you
64+
self.nmsrs == other.nmsrs && self.pad == other.pad
65+
}
66+
}
67+
4568
/// Wrapper over the `kvm_msrs` structure.
4669
///
4770
/// The `kvm_msrs` structure contains a flexible array member. For details check the
@@ -54,6 +77,14 @@ pub type Msrs = FamStructWrapper<kvm_msrs>;
5477
// Implement the FamStruct trait for kvm_msr_list.
5578
generate_fam_struct_impl!(kvm_msr_list, u32, indices, u32, nmsrs, KVM_MAX_MSR_ENTRIES);
5679

80+
// Implement the PartialEq trait for kvm_msr_list.
81+
impl PartialEq for kvm_msr_list {
82+
fn eq(&self, other: &kvm_msr_list) -> bool {
83+
// No need to call entries's eq, FamStructWrapper's PartialEq will do it for you
84+
self.nmsrs == other.nmsrs
85+
}
86+
}
87+
5788
/// Wrapper over the `kvm_msr_list` structure.
5889
///
5990
/// The `kvm_msr_list` structure contains a flexible array member. For details check the
@@ -62,3 +93,57 @@ generate_fam_struct_impl!(kvm_msr_list, u32, indices, u32, nmsrs, KVM_MAX_MSR_EN
6293
/// the array elements, this type is implemented using
6394
/// [FamStructWrapper](../vmm_sys_util/fam/struct.FamStructWrapper.html).
6495
pub type MsrList = FamStructWrapper<kvm_msr_list>;
96+
97+
#[cfg(test)]
98+
mod tests {
99+
use super::{CpuId, MsrList, Msrs};
100+
use x86::bindings::kvm_cpuid_entry2;
101+
102+
#[test]
103+
fn test_cpuid_eq() {
104+
let entries = &[kvm_cpuid_entry2::default(); 2];
105+
let mut wrapper = CpuId::from_entries(entries).unwrap();
106+
assert_eq!(wrapper.as_slice().len(), 2);
107+
108+
let mut wrapper2 = wrapper.clone();
109+
assert!(wrapper == wrapper2);
110+
111+
wrapper.as_mut_slice()[1].index = 1;
112+
assert!(wrapper != wrapper2);
113+
wrapper2.as_mut_slice()[1].index = 1;
114+
assert!(wrapper == wrapper2);
115+
}
116+
#[test]
117+
fn test_msrs_eq() {
118+
let mut wrapper = Msrs::new(2).unwrap();
119+
assert_eq!(wrapper.as_slice().len(), 2);
120+
121+
let mut wrapper2 = wrapper.clone();
122+
assert!(wrapper == wrapper2);
123+
124+
wrapper.as_mut_fam_struct().pad = 1;
125+
assert!(wrapper != wrapper2);
126+
wrapper2.as_mut_fam_struct().pad = 1;
127+
assert!(wrapper == wrapper2);
128+
129+
wrapper.as_mut_slice()[1].data = 1;
130+
assert!(wrapper != wrapper2);
131+
assert!(wrapper.as_slice() != wrapper2.as_slice());
132+
wrapper2.as_mut_slice()[1].data = 1;
133+
assert!(wrapper == wrapper2);
134+
assert!(wrapper.as_slice() == wrapper2.as_slice());
135+
}
136+
#[test]
137+
fn test_msrs_list_eq() {
138+
let mut wrapper = MsrList::new(1).unwrap();
139+
assert_eq!(wrapper.as_slice().len(), 1);
140+
141+
let mut wrapper2 = wrapper.clone();
142+
assert!(wrapper == wrapper2);
143+
144+
wrapper.as_mut_slice()[0] = 1;
145+
assert!(wrapper != wrapper2);
146+
wrapper2.as_mut_slice()[0] = 1;
147+
assert!(wrapper == wrapper2);
148+
}
149+
}

0 commit comments

Comments
 (0)