-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlib.rs
194 lines (156 loc) · 4.06 KB
/
lib.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
extern crate elias_fano;
use elias_fano::EliasFano;
#[test]
fn test_membership() {
const NUM: u64 = 1000;
let mut ef = EliasFano::new(NUM, NUM);
let array: Vec<u64> = vec![0; NUM as usize]
.iter()
.enumerate()
.map(|(idx, _)| idx as u64)
.collect();
ef.compress(array.iter());
for (idx, v) in array.iter().enumerate() {
if ef.value() != *v {
panic!("{} is not the same as {}", ef.value(), v);
}
match ef.next() {
Ok(_) => (),
Err(_) => {
if idx != array.len() - 1 {
panic!("Error returned when not at end of items");
}
}
}
}
}
#[test]
fn test_position() {
const NUM: u64 = 1000;
let mut ef = EliasFano::new(NUM, NUM);
let array: Vec<u64> = vec![0; NUM as usize]
.iter()
.enumerate()
.map(|(idx, _)| idx as u64)
.collect();
ef.compress(array.iter());
for i in 0..NUM {
if ef.position() != i {
panic!("Index is returning wrong position for linear increment")
}
let _ = ef.next();
}
}
#[test]
fn test_skip() {
const NUM: u64 = 1000;
let mut ef = EliasFano::new(NUM, NUM);
let array: Vec<u64> = vec![0; NUM as usize]
.iter()
.enumerate()
.map(|(idx, _)| idx as u64)
.collect();
ef.compress(array.iter());
ef.skip(500);
assert_eq!(ef.value(), 500);
ef.skip(350);
assert_eq!(ef.value(), 850);
assert!(ef.skip(149).is_ok());
assert!(ef.skip(150).is_err());
}
#[test]
fn test_reset() {
const NUM: u64 = 1000;
let mut ef = EliasFano::new(NUM, NUM);
let array: Vec<u64> = vec![0; NUM as usize]
.iter()
.enumerate()
.map(|(idx, _)| idx as u64)
.collect();
ef.compress(array.iter());
if ef.position() != 0 {
panic!("Initial position is not equal to 0");
}
let _ = ef.next();
ef.reset();
if ef.position() != 0 {
panic!("Position was not reset correctly");
}
if ef.value() != 0 {
panic!("Initial value is incorrect");
}
}
#[test]
fn test_move() {
const NUM: u64 = 1000;
let mut ef = EliasFano::new(NUM, NUM);
let array: Vec<u64> = vec![0; NUM as usize]
.iter()
.enumerate()
.map(|(idx, _)| idx as u64)
.collect();
ef.compress(array.iter());
if ef.position() != 0 {
panic!("Initial position is not equal to 0");
}
for (idx, val) in array.iter().enumerate() {
let _ = ef.visit(idx as u64);
if ef.value() != *val {
panic!("Received unexpected value after visit");
}
}
for i in 0..NUM {
let _ = ef.visit((array.len() - i as usize - 1) as u64);
if ef.value() != array[array.len() - i as usize - 1] {
panic!("Incorrect value found while visiting backwards");
}
}
}
#[test]
fn test_generic() {
let mut ef = EliasFano::new(1000, 5);
ef.compress([0, 5, 9, 800, 1000].iter());
if ef.value() != 0 {
panic!("Incorrect start value");
}
let _ = ef.visit(0);
if ef.value() != 0 {
panic!("0 visit returns different value");
}
let _ = ef.visit(4);
if ef.value() != 1000 {
panic!(
"Visit returning incorrect value, expected: {}, received: {}",
1000,
ef.value()
);
}
ef.reset();
if ef.value() != 0 {
panic!("Incorrect behaviour on reset");
}
let _ = ef.next();
if ef.value() != 5 {
panic!(
"Next value is incorrect, expected: {}, received: {}",
5,
ef.value()
);
}
let _ = ef.next();
if ef.value() != 9 {
panic!(
"Next value is incorrect, expected: {}, received: {}",
9,
ef.value()
);
}
let _ = ef.visit(1);
if ef.value() != 5 {
panic!(
"Visit returning incorrect value, expected: {}, received: {}",
5,
ef.value()
);
}
}