snarkvm_console_program/data/record/
equal.rs1use super::*;
17
18impl<N: Network, Private: Visibility<Boolean = Boolean<N>>> Eq for Record<N, Private> {}
19
20impl<N: Network, Private: Visibility<Boolean = Boolean<N>>> PartialEq for Record<N, Private> {
21 fn eq(&self, other: &Self) -> bool {
23 *self.is_equal(other)
24 }
25}
26
27impl<N: Network, Private: Visibility<Boolean = Boolean<N>>> Equal<Self> for Record<N, Private> {
28 type Output = Boolean<N>;
29
30 fn is_equal(&self, other: &Self) -> Self::Output {
32 if self.data.len() != other.data.len() {
34 return Boolean::new(false);
35 }
36
37 if !(*self.owner.is_equal(&other.owner) && *self.nonce.is_equal(&other.nonce)) {
39 return Boolean::new(false);
40 }
41
42 if self
44 .data
45 .iter()
46 .zip_eq(other.data.iter())
47 .all(|((name_a, entry_a), (name_b, entry_b))| *name_a.is_equal(name_b) && *entry_a.is_equal(entry_b))
48 {
49 Boolean::new(true)
50 } else {
51 Boolean::new(false)
52 }
53 }
54
55 fn is_not_equal(&self, other: &Self) -> Self::Output {
57 !self.is_equal(other)
58 }
59}
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64 use snarkvm_console_network::MainnetV0;
65
66 type CurrentNetwork = MainnetV0;
67
68 fn sample_record() -> Record<CurrentNetwork, Plaintext<CurrentNetwork>> {
69 Record::<CurrentNetwork, Plaintext<CurrentNetwork>>::from_str(
70 r"{
71 owner: aleo14tlamssdmg3d0p5zmljma573jghe2q9n6wz29qf36re2glcedcpqfg4add.private,
72 a: true.private,
73 b: 123456789field.public,
74 c: 0group.private,
75 d: {
76 e: true.private,
77 f: 123456789field.private,
78 g: 0group.private
79 },
80 _nonce: 0group.public
81}",
82 )
83 .unwrap()
84 }
85
86 fn sample_mismatched_record() -> Record<CurrentNetwork, Plaintext<CurrentNetwork>> {
87 Record::<CurrentNetwork, Plaintext<CurrentNetwork>>::from_str(
88 r"{
89 owner: aleo14tlamssdmg3d0p5zmljma573jghe2q9n6wz29qf36re2glcedcpqfg4add.private,
90 a: true.public,
91 b: 123456789field.public,
92 c: 0group.private,
93 d: {
94 e: true.private,
95 f: 123456789field.private,
96 g: 0group.private
97 },
98 _nonce: 0group.public
99}",
100 )
101 .unwrap()
102 }
103
104 fn check_is_equal() {
105 let record = sample_record();
107 let mismatched_record = sample_mismatched_record();
108
109 let candidate = record.is_equal(&record);
110 assert!(*candidate);
111
112 let candidate = record.is_equal(&mismatched_record);
113 assert!(!*candidate);
114 }
115
116 fn check_is_not_equal() {
117 let record = sample_record();
119 let mismatched_record = sample_mismatched_record();
120
121 let candidate = record.is_not_equal(&mismatched_record);
122 assert!(*candidate);
123
124 let candidate = record.is_not_equal(&record);
125 assert!(!*candidate);
126 }
127
128 #[test]
129 fn test_is_equal() {
130 check_is_equal()
131 }
132
133 #[test]
134 fn test_is_not_equal() {
135 check_is_not_equal()
136 }
137}