Debian Codegraph @

src/test.rs

raw symbols SCIP
1 use super::*;
2 use std::cell::Cell;
3 use std::mem;
4 use std::panic::{self, AssertUnwindSafe};
5 use std::ptr;
6
7 struct DropTracker<'a>(&'a Cell<u32>);
8 impl<'a> Drop for DropTracker<'a> {
9 fn drop(&mut self) {
10 self.0.set(self.0.get() + 1);
11 }
12 }
13
14 struct Node<'a, 'b: 'a>(Option<&'a Node<'a, 'b>>, u32, DropTracker<'b>);
15
16 #[test]
17 fn arena_as_intended() {
18 let drop_counter = Cell::new(0);
19 {
20 let arena = Arena::with_capacity(2);
21
22 let mut node: &Node = arena.alloc(Node(None, 1, DropTracker(&drop_counter)));
23 assert_eq!(arena.chunks.borrow().rest.len(), 0);
24
25 node = arena.alloc(Node(Some(node), 2, DropTracker(&drop_counter)));
26 assert_eq!(arena.chunks.borrow().rest.len(), 0);
27
28 node = arena.alloc(Node(Some(node), 3, DropTracker(&drop_counter)));
29 assert_eq!(arena.chunks.borrow().rest.len(), 1);
30
31 node = arena.alloc(Node(Some(node), 4, DropTracker(&drop_counter)));
32 assert_eq!(arena.chunks.borrow().rest.len(), 1);
33
34 assert_eq!(node.1, 4);
35 assert_eq!(node.0.unwrap().1, 3);
36 assert_eq!(node.0.unwrap().0.unwrap().1, 2);
37 assert_eq!(node.0.unwrap().0.unwrap().0.unwrap().1, 1);
38 assert!(node.0.unwrap().0.unwrap().0.unwrap().0.is_none());
39
40 assert_eq!(arena.len(), 4);
41
42 mem::drop(node);
43 assert_eq!(drop_counter.get(), 0);
44
45 let mut node: &Node = arena.alloc(Node(None, 5, DropTracker(&drop_counter)));
46 assert_eq!(arena.chunks.borrow().rest.len(), 1);
47
48 node = arena.alloc(Node(Some(node), 6, DropTracker(&drop_counter)));
49 assert_eq!(arena.chunks.borrow().rest.len(), 1);
50
51 node = arena.alloc(Node(Some(node), 7, DropTracker(&drop_counter)));
52 assert_eq!(arena.chunks.borrow().rest.len(), 2);
53
54 assert_eq!(drop_counter.get(), 0);
55
56 assert_eq!(node.1, 7);
57 assert_eq!(node.0.unwrap().1, 6);
58 assert_eq!(node.0.unwrap().0.unwrap().1, 5);
59 assert!(node.0.unwrap().0.unwrap().0.is_none());
60
61 assert_eq!(drop_counter.get(), 0);
62 }
63 assert_eq!(drop_counter.get(), 7);
64 }
65
66 #[test]
67 fn ensure_into_vec_maintains_order_of_allocation() {
68 let arena = Arena::with_capacity(1); // force multiple inner vecs
69 for &s in &["t", "e", "s", "t"] {
70 arena.alloc(String::from(s));
71 }
72 let vec = arena.into_vec();
73 assert_eq!(vec, vec!["t", "e", "s", "t"]);
74 }
75
76 #[test]
77 fn test_zero_cap() {
78 let arena = Arena::with_capacity(0);
79 let a = arena.alloc(1);
80 let b = arena.alloc(2);
81 assert_eq!(*a, 1);
82 assert_eq!(*b, 2);
83 assert_eq!(arena.len(), 2);
84 }
85
86 #[test]
87 fn test_alloc_extend() {
88 let arena = Arena::with_capacity(2);
89 for i in 0..15 {
90 let slice = arena.alloc_extend(0..i);
91 for (j, &elem) in slice.iter().enumerate() {
92 assert_eq!(j, elem);
93 }
94 }
95 }
96
97 #[test]
98 fn test_alloc_uninitialized() {
99 const LIMIT: usize = 15;
100 let drop_counter = Cell::new(0);
101 unsafe {
102 let arena: Arena<Node> = Arena::with_capacity(4);
103 for i in 0..LIMIT {
104 let slice = arena.alloc_uninitialized(i);
105 for (j, elem) in slice.iter_mut().enumerate() {
106 ptr::write(
107 elem.as_mut_ptr(),
108 Node(None, j as u32, DropTracker(&drop_counter)),
109 );
110 }
111 assert_eq!(drop_counter.get(), 0);
112 }
113 }
114 assert_eq!(drop_counter.get(), (0..LIMIT).fold(0, |a, e| a + e) as u32);
115 }
116
117 #[test]
118 fn test_alloc_extend_with_drop_counter() {
119 let drop_counter = Cell::new(0);
120 {
121 let arena = Arena::with_capacity(2);
122 let iter = (0..100).map(|j| Node(None, j as u32, DropTracker(&drop_counter)));
123 let older_ref = Some(&arena.alloc_extend(iter)[0]);
124 assert_eq!(drop_counter.get(), 0);
125 let iter = (0..100).map(|j| Node(older_ref, j as u32, DropTracker(&drop_counter)));
126 arena.alloc_extend(iter);
127 assert_eq!(drop_counter.get(), 0);
128 }
129 assert_eq!(drop_counter.get(), 200);
130 }
131
132 /// Test with bools.
133 ///
134 /// Bools, unlike integers, have invalid bit patterns. Therefore, ever having an uninitialized bool
135 /// is insta-UB. Make sure miri doesn't find any such thing.
136 #[test]
137 fn test_alloc_uninitialized_bools() {
138 const LEN: usize = 20;
139 unsafe {
140 let arena: Arena<bool> = Arena::with_capacity(2);
141 let slice = arena.alloc_uninitialized(LEN);
142 for elem in slice.iter_mut() {
143 ptr::write(elem.as_mut_ptr(), true);
144 }
145 // Now it is fully initialized, we can safely transmute the slice.
146 let slice: &mut [bool] = mem::transmute(slice);
147 assert_eq!(&[true; LEN], slice);
148 }
149 }
150
151 /// Check nothing bad happens by panicking during initialization of borrowed slice.
152 #[test]
153 fn alloc_uninitialized_with_panic() {
154 struct Dropper(bool);
155
156 impl Drop for Dropper {
157 fn drop(&mut self) {
158 // Just make sure we touch the value, to make sure miri would bite if it was
159 // unitialized
160 if self.0 {
161 panic!();
162 }
163 }
164 }
165 let mut reached_first_init = false;
166 panic::catch_unwind(AssertUnwindSafe(|| unsafe {
167 let arena: Arena<Dropper> = Arena::new();
168 arena.reserve_extend(2);
169 let uninitialized = arena.uninitialized_array();
170 assert!((*uninitialized).len() >= 2);
171 ptr::write((*uninitialized)[0].as_mut_ptr(), Dropper(false));
172 reached_first_init = true;
173 panic!("To drop the arena");
174 // If it didn't panic, we would continue by initializing the second one and confirming by
175 // .alloc_uninitialized();
176 }))
177 .unwrap_err();
178 assert!(reached_first_init);
179 }
180
181 #[test]
182 fn test_uninitialized_array() {
183 let arena = Arena::with_capacity(2);
184 let uninit = arena.uninitialized_array();
185 arena.alloc_extend(0..2);
186 unsafe {
187 for (&a, b) in (&*uninit).iter().zip(0..2) {
188 assert_eq!(a.assume_init(), b);
189 }
190 assert!((&*arena.uninitialized_array()).as_ptr() != (&*uninit).as_ptr());
191 arena.alloc(0);
192 let uninit = arena.uninitialized_array();
193 assert_eq!((&*uninit).len(), 3);
194 }
195 }
196
197 #[test]
198 fn dont_trust_the_iterator_size() {
199 use std::iter::repeat;
200
201 struct WrongSizeIter<I>(I);
202 impl<I> Iterator for WrongSizeIter<I>
203 where
204 I: Iterator,
205 {
206 type Item = I::Item;
207
208 fn next(&mut self) -> Option<Self::Item> {
209 self.0.next()
210 }
211
212 fn size_hint(&self) -> (usize, Option<usize>) {
213 (0, Some(0))
214 }
215 }
216
217 impl<I> ExactSizeIterator for WrongSizeIter<I> where I: Iterator {}
218
219 let arena = Arena::with_capacity(2);
220 arena.alloc(0);
221 let slice = arena.alloc_extend(WrongSizeIter(repeat(1).take(1_000)));
222 // Allocation of 1000 elements should have created a new chunk
223 assert_eq!(arena.chunks.borrow().rest.len(), 1);
224 assert_eq!(slice.len(), 1000);
225 }
226
227 #[test]
228 fn arena_is_send() {
229 fn assert_is_send<T: Send>(_: T) {}
230
231 // If `T` is `Send`, ...
232 assert_is_send(42_u32);
233
234 // Then `Arena<T>` is also `Send`.
235 let arena: Arena<u32> = Arena::new();
236 assert_is_send(arena);
237 }
238
239 #[test]
240 fn iter_mut_low_capacity() {
241 #[derive(Debug, PartialEq, Eq)]
242 struct NonCopy(usize);
243
244 const MAX: usize = 1_000;
245 const CAP: usize = 16;
246
247 let mut arena = Arena::with_capacity(CAP);
248 for i in 1..MAX {
249 arena.alloc(NonCopy(i));
250 }
251
252 assert!(
253 arena.chunks.borrow().rest.len() > 1,
254 "expected multiple chunks"
255 );
256
257 let mut iter = arena.iter_mut();
258 for i in 1..MAX {
259 assert_eq!(Some(&mut NonCopy(i)), iter.next());
260 }
261
262 assert_eq!(None, iter.next());
263 }
264
265 #[test]
266 fn iter_mut_high_capacity() {
267 #[derive(Debug, PartialEq, Eq)]
268 struct NonCopy(usize);
269
270 const MAX: usize = 1_000;
271 const CAP: usize = 8192;
272
273 let mut arena = Arena::with_capacity(CAP);
274 for i in 1..MAX {
275 arena.alloc(NonCopy(i));
276 }
277
278 assert!(
279 arena.chunks.borrow().rest.is_empty(),
280 "expected single chunk"
281 );
282
283 let mut iter = arena.iter_mut();
284 for i in 1..MAX {
285 assert_eq!(Some(&mut NonCopy(i)), iter.next());
286 }
287
288 assert_eq!(None, iter.next());
289 }
290
291 fn assert_size_hint<T>(arena_len: usize, iter: IterMut<'_, T>) {
292 let (min, max) = iter.size_hint();
293
294 assert!(max.is_some());
295 let max = max.unwrap();
296
297 // Check that the actual arena length lies between the estimated min and max
298 assert!(min <= arena_len);
299 assert!(max >= arena_len);
300
301 // Check that the min and max estimates are within a factor of 3
302 assert!(min >= arena_len / 3);
303 assert!(max <= arena_len * 3);
304 }
305
306 #[test]
307 fn size_hint() {
308 #[derive(Debug, PartialEq, Eq)]
309 struct NonCopy(usize);
310
311 const MAX: usize = 32;
312 const CAP: usize = 0;
313
314 for cap in CAP..(CAP + 16/* check some non-power-of-two capacities */) {
315 let mut arena = Arena::with_capacity(cap);
316 for i in 1..MAX {
317 arena.alloc(NonCopy(i));
318 let iter = arena.iter_mut();
319 assert_size_hint(i, iter);
320 }
321 }
322 }
323
324 // Ensure that `alloc_extend` doesn't violate provenance of
325 // existing references. (Note: This test is pointless except
326 // under miri).
327 #[test]
328 fn check_extend_provenance() {
329 let arena = Arena::new();
330 let a = arena.alloc(0);
331 arena.alloc_extend([0]);
332 *a = 1;
333 }
334
335 #[test]
336 fn size_hint_low_initial_capacities() {
337 #[derive(Debug, PartialEq, Eq)]
338 struct NonCopy(usize);
339
340 const MAX: usize = if cfg!(miri) { 100 } else { 25_000 };
341 const CAP: usize = 0;
342
343 for cap in CAP..(CAP + 128/* check some non-power-of-two capacities */) {
344 let mut arena = Arena::with_capacity(cap);
345 for i in 1..MAX {
346 arena.alloc(NonCopy(i));
347 let iter = arena.iter_mut();
348 assert_size_hint(i, iter);
349 }
350 }
351 }
352
353 #[test]
354 fn size_hint_high_initial_capacities() {
355 #[derive(Debug, PartialEq, Eq)]
356 struct NonCopy(usize);
357
358 const MAX: usize = if cfg!(miri) { 100 } else { 25_000 };
359 const CAP: usize = 8164;
360
361 for cap in CAP..(CAP + 128/* check some non-power-of-two capacities */) {
362 let mut arena = Arena::with_capacity(cap);
363 for i in 1..MAX {
364 arena.alloc(NonCopy(i));
365 let iter = arena.iter_mut();
366 assert_size_hint(i, iter);
367 }
368 }
369 }
370
371 #[test]
372 fn size_hint_many_items() {
373 #[derive(Debug, PartialEq, Eq)]
374 struct NonCopy(usize);
375
376 const MAX: usize = if cfg!(miri) { 500 } else { 5_000_000 };
377 const CAP: usize = 16;
378
379 let mut arena = Arena::with_capacity(CAP);
380 for i in 1..MAX {
381 arena.alloc(NonCopy(i));
382 let iter = arena.iter_mut();
383 assert_size_hint(i, iter);
384 }
385 }