Skip to content

Commit 9e7ce54

Browse files
committed
test: reflections' equality
1 parent 67e707e commit 9e7ce54

2 files changed

Lines changed: 177 additions & 1 deletion

File tree

src/hardlink/hardlink_list.rs

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ where
8686
Size: size::Size,
8787
{
8888
/// Add an entry to the record.
89-
#[cfg_attr(not(unix), expect(unused))]
89+
#[cfg_attr(not(unix), cfg(test))] // this function isn't used on non-POSIX except in tests
9090
pub(crate) fn add(
9191
&self,
9292
ino: InodeNumber,
@@ -115,3 +115,124 @@ where
115115
size_assertion.map_err(AddError::SizeConflict)
116116
}
117117
}
118+
119+
#[cfg(test)]
120+
mod tests {
121+
use super::HardlinkList;
122+
use crate::size::Bytes;
123+
use pipe_trait::Pipe;
124+
use pretty_assertions::{assert_eq, assert_ne};
125+
126+
const TABLE: &[(u64, u64, u64, &str)] = &[
127+
(241, 3652, 1, "a"),
128+
(569, 2210, 1, "b"),
129+
(110, 2350, 3, "c"),
130+
(110, 2350, 3, "c1"),
131+
(778, 1110, 1, "d"),
132+
(274, 6060, 2, "e"),
133+
(274, 6060, 2, "e1"),
134+
(883, 4530, 1, "f"),
135+
];
136+
137+
fn add<const ROW: usize>(list: HardlinkList<Bytes>) -> HardlinkList<Bytes> {
138+
let values = TABLE[ROW];
139+
let (ino, size, links, path) = values;
140+
if let Err(error) = list.add(ino.into(), size.into(), links, path.as_ref()) {
141+
panic!("Failed to add {values:?} (index: {ROW}) to the list: {error}");
142+
}
143+
list
144+
}
145+
146+
#[test]
147+
fn insertion_order_is_irrelevant_to_equality() {
148+
let a = HardlinkList::new()
149+
.pipe(add::<3>)
150+
.pipe(add::<1>)
151+
.pipe(add::<4>)
152+
.pipe(add::<6>)
153+
.pipe(add::<5>)
154+
.pipe(add::<0>)
155+
.pipe(add::<7>)
156+
.pipe(add::<2>)
157+
.into_reflection();
158+
159+
let b = HardlinkList::new()
160+
.pipe(add::<5>)
161+
.pipe(add::<6>)
162+
.pipe(add::<2>)
163+
.pipe(add::<0>)
164+
.pipe(add::<1>)
165+
.pipe(add::<3>)
166+
.pipe(add::<7>)
167+
.pipe(add::<4>)
168+
.into_reflection();
169+
170+
let c = HardlinkList::new()
171+
.pipe(add::<0>)
172+
.pipe(add::<1>)
173+
.pipe(add::<2>)
174+
.pipe(add::<3>)
175+
.pipe(add::<4>)
176+
.pipe(add::<5>)
177+
.pipe(add::<6>)
178+
.pipe(add::<7>)
179+
.into_reflection();
180+
181+
assert_eq!(a, b);
182+
assert_eq!(b, c);
183+
assert_eq!(a, c);
184+
}
185+
186+
#[test]
187+
fn omitting_insertion_cause_inequality() {
188+
let a = HardlinkList::new()
189+
.pipe(add::<0>)
190+
.pipe(add::<1>)
191+
.pipe(add::<2>)
192+
.pipe(add::<3>)
193+
.pipe(add::<4>)
194+
.pipe(add::<5>)
195+
.pipe(add::<6>)
196+
.pipe(add::<7>)
197+
.into_reflection();
198+
199+
let b = HardlinkList::new()
200+
.pipe(add::<0>)
201+
.pipe(add::<1>)
202+
.pipe(add::<2>)
203+
.pipe(add::<3>)
204+
.pipe(add::<4>)
205+
.pipe(add::<5>)
206+
.pipe(add::<7>)
207+
.into_reflection();
208+
209+
assert_ne!(a, b);
210+
assert_ne!(b, a);
211+
}
212+
213+
#[test]
214+
fn insertion_difference_cause_inequality() {
215+
let a = HardlinkList::new()
216+
.pipe(add::<0>)
217+
.pipe(add::<1>)
218+
.pipe(add::<2>)
219+
.pipe(add::<3>)
220+
.pipe(add::<4>)
221+
.pipe(add::<5>)
222+
.pipe(add::<6>)
223+
.into_reflection();
224+
225+
let b = HardlinkList::new()
226+
.pipe(add::<0>)
227+
.pipe(add::<1>)
228+
.pipe(add::<2>)
229+
.pipe(add::<3>)
230+
.pipe(add::<4>)
231+
.pipe(add::<5>)
232+
.pipe(add::<7>)
233+
.into_reflection();
234+
235+
assert_ne!(a, b);
236+
assert_ne!(b, a);
237+
}
238+
}

src/hardlink/link_path_list.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ impl LinkPathList {
2323
LinkPathList(vec![path])
2424
}
2525

26+
/// Create a list of many paths.
27+
#[cfg(test)]
28+
pub(crate) fn many(paths: impl IntoIterator<Item: Into<PathBuf>>) -> Self {
29+
let paths: Vec<_> = paths.into_iter().map(Into::into).collect();
30+
assert!(!paths.is_empty(), "paths must not be empty");
31+
LinkPathList(paths)
32+
}
33+
2634
/// Add a path to the list.
2735
pub(crate) fn add(&mut self, path: PathBuf) {
2836
self.0.push(path)
@@ -43,3 +51,50 @@ impl LinkPathList {
4351
self.into()
4452
}
4553
}
54+
55+
#[cfg(test)]
56+
mod tests {
57+
use super::LinkPathList;
58+
use pipe_trait::Pipe;
59+
use pretty_assertions::{assert_eq, assert_ne};
60+
61+
#[test]
62+
fn item_order_is_irrelevant_to_equality() {
63+
let a = ["3", "4", "0", "2", "1"]
64+
.pipe(LinkPathList::many)
65+
.into_reflection();
66+
let b = ["4", "0", "3", "2", "1"]
67+
.pipe(LinkPathList::many)
68+
.into_reflection();
69+
let c = ["0", "1", "2", "3", "4"]
70+
.pipe(LinkPathList::many)
71+
.into_reflection();
72+
assert_eq!(a, b);
73+
assert_eq!(b, c);
74+
assert_eq!(a, c);
75+
}
76+
77+
#[test]
78+
fn item_absent_cause_inequality() {
79+
let a = ["0", "1", "2", "3"]
80+
.pipe(LinkPathList::many)
81+
.into_reflection();
82+
let b = ["0", "1", "2", "3", "4"]
83+
.pipe(LinkPathList::many)
84+
.into_reflection();
85+
assert_ne!(a, b);
86+
assert_ne!(b, a);
87+
}
88+
89+
#[test]
90+
fn item_difference_cause_inequality() {
91+
let a = ["0", "1", "2", "3", "5"]
92+
.pipe(LinkPathList::many)
93+
.into_reflection();
94+
let b = ["0", "1", "2", "3", "4"]
95+
.pipe(LinkPathList::many)
96+
.into_reflection();
97+
assert_ne!(a, b);
98+
assert_ne!(b, a);
99+
}
100+
}

0 commit comments

Comments
 (0)