-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtests.rs
50 lines (47 loc) · 1.57 KB
/
tests.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use crate::*;
#[test]
fn test_bit_vec_count_ones() {
let mut bitvec = BitVec::ones(1000);
assert_eq!(bitvec.count_ones(), 1000);
assert_eq!(bitvec.count_ones_before(500), 500);
bitvec.set(1500, true);
assert_eq!(bitvec.count_ones(), 1001);
assert_eq!(bitvec.count_ones_before(500), 500);
}
#[test]
fn test_bitvec_set_raw_copy() {
let v = vec![7];
let buf = v.as_ptr();
let mut bitvec = unsafe { BitVec::from_raw_copy(buf, 1, 64) };
let ptr = bitvec.storage.as_mut_ptr();
let buffer_len = bitvec.storage.len();
let mut bitvec2 = BitVec::zeros(1);
unsafe {
bitvec2.set_raw_copy(ptr, buffer_len, bitvec.nbits);
}
assert_eq!(v.len(), 1); // ensure v lives long enough
assert_eq!(bitvec2.get(0), Some(true));
assert_eq!(bitvec2.get(1), Some(true));
assert_eq!(bitvec2.get(2), Some(true));
assert_eq!(bitvec2.get(3), Some(false));
assert_eq!(bitvec2.get(63), Some(false));
assert_eq!(bitvec2.get(64), None);
}
#[test]
fn test_bitvec_set_raw() {
let mut bitvec = BitVec::ones(1025);
let ptr = bitvec.storage.as_mut_ptr();
let buffer_len = bitvec.storage.len();
let capacity = bitvec.storage.capacity();
let nbits = bitvec.nbits;
let spilled = bitvec.storage.spilled();
let mut bitvec2 = BitVec::zeros(1);
unsafe {
std::mem::forget(bitvec);
assert!(spilled);
bitvec2.set_raw(ptr, buffer_len, capacity, nbits);
assert_eq!(bitvec2.get(0), Some(true));
assert_eq!(bitvec2.get(1024), Some(true));
assert_eq!(bitvec2.get(1025), None);
}
}