Skip to content

Commit 96b6f35

Browse files
committed
add test cases for Lsn math and AtomicLsn
1 parent 648755a commit 96b6f35

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

zenith_utils/src/lsn.rs

+40
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,44 @@ mod tests {
196196
assert_eq!(Lsn::from_hex("0"), Ok(Lsn(0)));
197197
assert_eq!(Lsn::from_hex("F12345678AAAA5555"), Err(LsnParseError));
198198
}
199+
200+
#[test]
201+
fn test_lsn_math() {
202+
assert_eq!(Lsn(1234) + 11u64, Lsn(1245));
203+
204+
assert_eq!(
205+
{
206+
let mut lsn = Lsn(1234);
207+
lsn += 11u64;
208+
lsn
209+
},
210+
Lsn(1245)
211+
);
212+
213+
assert_eq!(Lsn(1234).checked_sub(1233u64), Some(Lsn(1)));
214+
assert_eq!(Lsn(1234).checked_sub(1235u64), None);
215+
216+
let seg_sz = 16u64 * 1024 * 1024;
217+
assert_eq!(Lsn(0x1000007).segment_offset(seg_sz), 7u64);
218+
assert_eq!(Lsn(0x1000007).segment_number(seg_sz), 1u64);
219+
220+
assert_eq!(Lsn(0x4007).block_offset(), 7u64);
221+
assert_eq!(Lsn(0x4000).block_offset(), 0u64);
222+
assert_eq!(Lsn(0x4007).remaining_in_block(), 8185u64);
223+
assert_eq!(Lsn(0x4000).remaining_in_block(), 8192u64);
224+
225+
assert_eq!(Lsn(0xffff01).calc_padding(seg_sz), 255u64);
226+
assert_eq!(Lsn(0x2000000).calc_padding(seg_sz), 0u64);
227+
assert_eq!(Lsn(0xffff01).calc_padding(8u32), 7u64);
228+
assert_eq!(Lsn(0xffff00).calc_padding(8u32), 0u64);
229+
}
230+
231+
#[test]
232+
fn test_atomic_lsn() {
233+
let lsn = AtomicLsn::new(0);
234+
assert_eq!(lsn.fetch_add(1234), Lsn(0));
235+
assert_eq!(lsn.load(), Lsn(1234));
236+
lsn.store(Lsn(5678));
237+
assert_eq!(lsn.load(), Lsn(5678));
238+
}
199239
}

0 commit comments

Comments
 (0)