-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.rs
265 lines (211 loc) · 5.8 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use generational_vector::GenerationalVector;
#[test]
fn default() {
let gv: GenerationalVector<&str> = Default::default();
assert_eq!(gv.len(), 0);
assert!(gv.is_empty());
assert_eq!(gv.count_num_free(), 0);
}
#[test]
fn new() {
let gv: GenerationalVector<&str> = GenerationalVector::new();
assert_eq!(gv.len(), 0);
assert!(gv.is_empty());
assert_eq!(gv.count_num_free(), 0);
}
#[test]
fn insert() {
let mut gv = GenerationalVector::default();
let a = gv.push("a");
let b = gv.push("b");
let c = gv.push("c");
assert_eq!(gv.get(&a), Some(&"a"));
assert_eq!(gv.get(&b), Some(&"b"));
assert_eq!(gv.get(&c), Some(&"c"));
assert_eq!(gv.len(), 3);
assert!(!gv.is_empty());
assert_eq!(gv.count_num_free(), 0);
}
#[test]
fn new_from_vec() {
let gv: GenerationalVector<_> = vec!["a", "b", "c"].into();
assert_eq!(gv.len(), 3);
}
#[test]
fn new_from_iter() {
let vec = ["a", "b", "c"];
let gv: GenerationalVector<_> = GenerationalVector::new_from_iter(vec);
assert_eq!(gv.len(), 3);
}
#[test]
fn into_iter() {
let gv: GenerationalVector<_> = vec!["a", "b", "c"].into();
let vec: Vec<_> = gv.into_iter().collect();
assert_eq!(vec.len(), 3);
assert!(vec.contains(&"a"));
assert!(vec.contains(&"b"));
assert!(vec.contains(&"c"));
}
#[test]
fn iter() {
let gv: GenerationalVector<_> = vec![10, 20, 30, 40, 50].into();
let vec: Vec<_> = gv
.iter()
.filter(|&x| *x > 20 && *x < 50)
.map(|x| x * 2)
.collect();
assert_eq!(vec.len(), 2);
assert!(vec.contains(&60));
assert!(vec.contains(&80));
}
#[test]
fn iter_mut() {
let mut gv: GenerationalVector<_> = vec![20, 30, 40, 50].into();
for value in gv.iter_mut().filter(|&&mut x| x > 20 && x < 50) {
*value *= 2;
}
let vec: Vec<_> = gv.into_iter().collect();
assert_eq!(vec.len(), 4);
assert!(vec.contains(&20));
assert!(vec.contains(&60));
assert!(vec.contains(&80));
assert!(vec.contains(&50));
}
#[test]
fn for_loop() {
let gv: GenerationalVector<_> = vec!["a", "b", "c"].into();
let mut vec = Vec::default();
for entry in gv {
vec.push(entry);
}
assert_eq!(vec.len(), 3);
assert!(vec.contains(&"a"));
assert!(vec.contains(&"b"));
assert!(vec.contains(&"c"));
}
#[test]
fn for_loop_with_ref() {
let gv: GenerationalVector<_> = vec!["a", "b", "c"].into();
let gv_ref = &gv;
let mut vec = Vec::default();
for &entry in gv_ref.into_iter() {
vec.push(entry);
}
assert_eq!(vec.len(), 3);
assert!(vec.contains(&"a"));
assert!(vec.contains(&"b"));
assert!(vec.contains(&"c"));
}
#[test]
fn for_loop_with_mut_ref() {
let mut gv: GenerationalVector<_> = vec![10, 20, 30].into();
let gv_ref = &mut gv;
let mut vec = Vec::default();
for entry in gv_ref.into_iter() {
*entry = *entry + 1;
}
let gv_ref = &gv;
for &entry in gv_ref.into_iter() {
vec.push(entry);
}
assert_eq!(vec.len(), 3);
assert!(vec.contains(&11));
assert!(vec.contains(&21));
assert!(vec.contains(&31));
}
#[test]
fn remove() {
let mut gv = GenerationalVector::default();
let a = gv.push("a");
let _ = gv.push("b");
let _ = gv.push("c");
gv.remove(&a);
assert_eq!(gv.get(&a), None);
assert_eq!(gv.len(), 2);
assert!(!gv.is_empty());
// Since one element was deleted, there is exactly one free slot.
assert_eq!(gv.count_num_free(), 1);
// The internal vector stays expanded.
assert_eq!(gv.capacity(), 4);
}
#[test]
fn insert_after_delete() {
let mut gv = GenerationalVector::default();
let a = gv.push("a");
let _ = gv.push("b");
let _ = gv.push("c");
gv.remove(&a);
let d = gv.push("d");
// The index of element "a" was re-assigned to "d",
// however the generation differs.
assert_ne!(a, d);
// The vector still has three elements however.
assert_eq!(gv.len(), 3);
assert!(!gv.is_empty());
// No free slots.
assert_eq!(gv.count_num_free(), 0);
// The internal vector was expanded.
assert_eq!(gv.capacity(), 4);
}
#[test]
fn insert_after_delete_twice() {
let mut gv = GenerationalVector::default();
let a = gv.push("a");
let _ = gv.push("b");
let _ = gv.push("c");
gv.remove(&a);
let d = gv.push("d");
gv.remove(&d);
let e = gv.push("e");
// The index of element "a" was re-assigned to "e",
// however the generation was incremented twice.
assert_ne!(a, e);
}
#[test]
fn delete_all() {
let mut gv = GenerationalVector::default();
let a = gv.push("a");
let b = gv.push("b");
let c = gv.push("c");
gv.remove(&a);
gv.remove(&b);
gv.remove(&c);
assert_eq!(gv.len(), 0);
assert!(gv.is_empty());
// Number of free elements is three, however
// the internal list capacity is still higher.
assert_eq!(gv.count_num_free(), 3);
assert_eq!(gv.capacity(), 4);
}
#[test]
fn delete_all_reverse() {
let mut gv = GenerationalVector::default();
let a = gv.push("a");
let b = gv.push("b");
let c = gv.push("c");
gv.remove(&c);
gv.remove(&b);
gv.remove(&a);
assert_eq!(gv.len(), 0);
assert!(gv.is_empty());
// Number of free elements is three, however
// the internal list capacity is still higher.
assert_eq!(gv.count_num_free(), 3);
assert_eq!(gv.capacity(), 4);
}
#[test]
fn delete_all_and_insert() {
let mut gv = GenerationalVector::default();
let a = gv.push("a");
let b = gv.push("b");
let c = gv.push("c");
gv.remove(&a);
gv.remove(&b);
gv.remove(&c);
let _d = gv.push("d");
let _e = gv.push("e");
// The last deleted element is assigned first.
assert_eq!(gv.len(), 2);
assert!(!gv.is_empty());
assert_eq!(gv.count_num_free(), 1);
}