]> git.proxmox.com Git - rustc.git/blame - src/libcollectionstest/slice.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / libcollectionstest / slice.rs
CommitLineData
c34b1796
AL
1// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11use std::cmp::Ordering::{Equal, Greater, Less};
c34b1796 12use std::mem;
9346a6ac 13use std::__rand::{Rng, thread_rng};
c34b1796 14use std::rc::Rc;
c34b1796
AL
15
16fn square(n: usize) -> usize { n * n }
17
18fn is_odd(n: &usize) -> bool { *n % 2 == 1 }
19
20#[test]
21fn test_from_fn() {
22 // Test on-stack from_fn.
23 let mut v: Vec<_> = (0..3).map(square).collect();
24 {
25 let v = v;
26 assert_eq!(v.len(), 3);
27 assert_eq!(v[0], 0);
28 assert_eq!(v[1], 1);
29 assert_eq!(v[2], 4);
30 }
31
32 // Test on-heap from_fn.
33 v = (0..5).map(square).collect();
34 {
35 let v = v;
36 assert_eq!(v.len(), 5);
37 assert_eq!(v[0], 0);
38 assert_eq!(v[1], 1);
39 assert_eq!(v[2], 4);
40 assert_eq!(v[3], 9);
41 assert_eq!(v[4], 16);
42 }
43}
44
45#[test]
46fn test_from_elem() {
47 // Test on-stack from_elem.
48 let mut v = vec![10, 10];
49 {
50 let v = v;
51 assert_eq!(v.len(), 2);
52 assert_eq!(v[0], 10);
53 assert_eq!(v[1], 10);
54 }
55
56 // Test on-heap from_elem.
57 v = vec![20; 6];
58 {
59 let v = &v[..];
60 assert_eq!(v[0], 20);
61 assert_eq!(v[1], 20);
62 assert_eq!(v[2], 20);
63 assert_eq!(v[3], 20);
64 assert_eq!(v[4], 20);
65 assert_eq!(v[5], 20);
66 }
67}
68
69#[test]
70fn test_is_empty() {
71 let xs: [i32; 0] = [];
72 assert!(xs.is_empty());
73 assert!(![0].is_empty());
74}
75
76#[test]
77fn test_len_divzero() {
78 type Z = [i8; 0];
79 let v0 : &[Z] = &[];
80 let v1 : &[Z] = &[[]];
81 let v2 : &[Z] = &[[], []];
82 assert_eq!(mem::size_of::<Z>(), 0);
83 assert_eq!(v0.len(), 0);
84 assert_eq!(v1.len(), 1);
85 assert_eq!(v2.len(), 2);
86}
87
88#[test]
89fn test_get() {
90 let mut a = vec![11];
91 assert_eq!(a.get(1), None);
92 a = vec![11, 12];
93 assert_eq!(a.get(1).unwrap(), &12);
94 a = vec![11, 12, 13];
95 assert_eq!(a.get(1).unwrap(), &12);
96}
97
98#[test]
99fn test_first() {
100 let mut a = vec![];
101 assert_eq!(a.first(), None);
102 a = vec![11];
103 assert_eq!(a.first().unwrap(), &11);
104 a = vec![11, 12];
105 assert_eq!(a.first().unwrap(), &11);
106}
107
108#[test]
109fn test_first_mut() {
110 let mut a = vec![];
111 assert_eq!(a.first_mut(), None);
112 a = vec![11];
113 assert_eq!(*a.first_mut().unwrap(), 11);
114 a = vec![11, 12];
115 assert_eq!(*a.first_mut().unwrap(), 11);
116}
117
118#[test]
c1a9b12d 119fn test_split_first() {
c34b1796
AL
120 let mut a = vec![11];
121 let b: &[i32] = &[];
c1a9b12d
SL
122 assert!(b.split_first().is_none());
123 assert_eq!(a.split_first(), Some((&11, b)));
c34b1796
AL
124 a = vec![11, 12];
125 let b: &[i32] = &[12];
c1a9b12d 126 assert_eq!(a.split_first(), Some((&11, b)));
c34b1796
AL
127}
128
129#[test]
c1a9b12d 130fn test_split_first_mut() {
c34b1796
AL
131 let mut a = vec![11];
132 let b: &mut [i32] = &mut [];
c1a9b12d
SL
133 assert!(b.split_first_mut().is_none());
134 assert!(a.split_first_mut() == Some((&mut 11, b)));
c34b1796
AL
135 a = vec![11, 12];
136 let b: &mut [_] = &mut [12];
c1a9b12d 137 assert!(a.split_first_mut() == Some((&mut 11, b)));
c34b1796
AL
138}
139
140#[test]
c1a9b12d 141fn test_split_last() {
c34b1796
AL
142 let mut a = vec![11];
143 let b: &[i32] = &[];
c1a9b12d
SL
144 assert!(b.split_last().is_none());
145 assert_eq!(a.split_last(), Some((&11, b)));
c34b1796
AL
146 a = vec![11, 12];
147 let b: &[_] = &[11];
c1a9b12d 148 assert_eq!(a.split_last(), Some((&12, b)));
c34b1796
AL
149}
150
151#[test]
c1a9b12d 152fn test_split_last_mut() {
c34b1796
AL
153 let mut a = vec![11];
154 let b: &mut [i32] = &mut [];
c1a9b12d
SL
155 assert!(b.split_last_mut().is_none());
156 assert!(a.split_last_mut() == Some((&mut 11, b)));
157
c34b1796
AL
158 a = vec![11, 12];
159 let b: &mut [_] = &mut [11];
c1a9b12d 160 assert!(a.split_last_mut() == Some((&mut 12, b)));
c34b1796
AL
161}
162
163#[test]
164fn test_last() {
165 let mut a = vec![];
166 assert_eq!(a.last(), None);
167 a = vec![11];
168 assert_eq!(a.last().unwrap(), &11);
169 a = vec![11, 12];
170 assert_eq!(a.last().unwrap(), &12);
171}
172
173#[test]
174fn test_last_mut() {
175 let mut a = vec![];
176 assert_eq!(a.last_mut(), None);
177 a = vec![11];
178 assert_eq!(*a.last_mut().unwrap(), 11);
179 a = vec![11, 12];
180 assert_eq!(*a.last_mut().unwrap(), 12);
181}
182
183#[test]
184fn test_slice() {
185 // Test fixed length vector.
186 let vec_fixed = [1, 2, 3, 4];
187 let v_a = vec_fixed[1..vec_fixed.len()].to_vec();
188 assert_eq!(v_a.len(), 3);
189
190 assert_eq!(v_a[0], 2);
191 assert_eq!(v_a[1], 3);
192 assert_eq!(v_a[2], 4);
193
194 // Test on stack.
195 let vec_stack: &[_] = &[1, 2, 3];
196 let v_b = vec_stack[1..3].to_vec();
197 assert_eq!(v_b.len(), 2);
198
199 assert_eq!(v_b[0], 2);
200 assert_eq!(v_b[1], 3);
201
202 // Test `Box<[T]>`
203 let vec_unique = vec![1, 2, 3, 4, 5, 6];
204 let v_d = vec_unique[1..6].to_vec();
205 assert_eq!(v_d.len(), 5);
206
207 assert_eq!(v_d[0], 2);
208 assert_eq!(v_d[1], 3);
209 assert_eq!(v_d[2], 4);
210 assert_eq!(v_d[3], 5);
211 assert_eq!(v_d[4], 6);
212}
213
214#[test]
215fn test_slice_from() {
216 let vec: &[_] = &[1, 2, 3, 4];
217 assert_eq!(&vec[..], vec);
218 let b: &[_] = &[3, 4];
219 assert_eq!(&vec[2..], b);
220 let b: &[_] = &[];
221 assert_eq!(&vec[4..], b);
222}
223
224#[test]
225fn test_slice_to() {
226 let vec: &[_] = &[1, 2, 3, 4];
227 assert_eq!(&vec[..4], vec);
228 let b: &[_] = &[1, 2];
229 assert_eq!(&vec[..2], b);
230 let b: &[_] = &[];
231 assert_eq!(&vec[..0], b);
232}
233
234
235#[test]
236fn test_pop() {
237 let mut v = vec![5];
238 let e = v.pop();
239 assert_eq!(v.len(), 0);
240 assert_eq!(e, Some(5));
241 let f = v.pop();
242 assert_eq!(f, None);
243 let g = v.pop();
244 assert_eq!(g, None);
245}
246
247#[test]
248fn test_swap_remove() {
249 let mut v = vec![1, 2, 3, 4, 5];
250 let mut e = v.swap_remove(0);
251 assert_eq!(e, 1);
252 assert_eq!(v, [5, 2, 3, 4]);
253 e = v.swap_remove(3);
254 assert_eq!(e, 4);
255 assert_eq!(v, [5, 2, 3]);
256}
257
258#[test]
259#[should_panic]
260fn test_swap_remove_fail() {
261 let mut v = vec![1];
262 let _ = v.swap_remove(0);
263 let _ = v.swap_remove(0);
264}
265
266#[test]
267fn test_swap_remove_noncopyable() {
268 // Tests that we don't accidentally run destructors twice.
269 let mut v: Vec<Box<_>> = Vec::new();
54a0048b
SL
270 v.push(box 0);
271 v.push(box 0);
272 v.push(box 0);
c34b1796
AL
273 let mut _e = v.swap_remove(0);
274 assert_eq!(v.len(), 2);
275 _e = v.swap_remove(1);
276 assert_eq!(v.len(), 1);
277 _e = v.swap_remove(0);
278 assert_eq!(v.len(), 0);
279}
280
281#[test]
282fn test_push() {
283 // Test on-stack push().
284 let mut v = vec![];
285 v.push(1);
286 assert_eq!(v.len(), 1);
287 assert_eq!(v[0], 1);
288
289 // Test on-heap push().
290 v.push(2);
291 assert_eq!(v.len(), 2);
292 assert_eq!(v[0], 1);
293 assert_eq!(v[1], 2);
294}
295
296#[test]
297fn test_truncate() {
298 let mut v: Vec<Box<_>> = vec![box 6,box 5,box 4];
299 v.truncate(1);
300 let v = v;
301 assert_eq!(v.len(), 1);
302 assert_eq!(*(v[0]), 6);
303 // If the unsafe block didn't drop things properly, we blow up here.
304}
305
306#[test]
307fn test_clear() {
308 let mut v: Vec<Box<_>> = vec![box 6,box 5,box 4];
309 v.clear();
310 assert_eq!(v.len(), 0);
311 // If the unsafe block didn't drop things properly, we blow up here.
312}
313
314#[test]
315fn test_dedup() {
316 fn case(a: Vec<i32>, b: Vec<i32>) {
317 let mut v = a;
318 v.dedup();
319 assert_eq!(v, b);
320 }
321 case(vec![], vec![]);
322 case(vec![1], vec![1]);
323 case(vec![1,1], vec![1]);
324 case(vec![1,2,3], vec![1,2,3]);
325 case(vec![1,1,2,3], vec![1,2,3]);
326 case(vec![1,2,2,3], vec![1,2,3]);
327 case(vec![1,2,3,3], vec![1,2,3]);
328 case(vec![1,1,2,2,2,3,3], vec![1,2,3]);
329}
330
331#[test]
332fn test_dedup_unique() {
333 let mut v0: Vec<Box<_>> = vec![box 1, box 1, box 2, box 3];
334 v0.dedup();
335 let mut v1: Vec<Box<_>> = vec![box 1, box 2, box 2, box 3];
336 v1.dedup();
337 let mut v2: Vec<Box<_>> = vec![box 1, box 2, box 3, box 3];
338 v2.dedup();
339 /*
340 * If the boxed pointers were leaked or otherwise misused, valgrind
341 * and/or rt should raise errors.
342 */
343}
344
345#[test]
346fn test_dedup_shared() {
347 let mut v0: Vec<Box<_>> = vec![box 1, box 1, box 2, box 3];
348 v0.dedup();
349 let mut v1: Vec<Box<_>> = vec![box 1, box 2, box 2, box 3];
350 v1.dedup();
351 let mut v2: Vec<Box<_>> = vec![box 1, box 2, box 3, box 3];
352 v2.dedup();
353 /*
354 * If the pointers were leaked or otherwise misused, valgrind and/or
355 * rt should raise errors.
356 */
357}
358
359#[test]
360fn test_retain() {
361 let mut v = vec![1, 2, 3, 4, 5];
362 v.retain(is_odd);
363 assert_eq!(v, [1, 3, 5]);
364}
365
c34b1796
AL
366#[test]
367fn test_binary_search() {
368 assert_eq!([1,2,3,4,5].binary_search(&5).ok(), Some(4));
369 assert_eq!([1,2,3,4,5].binary_search(&4).ok(), Some(3));
370 assert_eq!([1,2,3,4,5].binary_search(&3).ok(), Some(2));
371 assert_eq!([1,2,3,4,5].binary_search(&2).ok(), Some(1));
372 assert_eq!([1,2,3,4,5].binary_search(&1).ok(), Some(0));
373
374 assert_eq!([2,4,6,8,10].binary_search(&1).ok(), None);
375 assert_eq!([2,4,6,8,10].binary_search(&5).ok(), None);
376 assert_eq!([2,4,6,8,10].binary_search(&4).ok(), Some(1));
377 assert_eq!([2,4,6,8,10].binary_search(&10).ok(), Some(4));
378
379 assert_eq!([2,4,6,8].binary_search(&1).ok(), None);
380 assert_eq!([2,4,6,8].binary_search(&5).ok(), None);
381 assert_eq!([2,4,6,8].binary_search(&4).ok(), Some(1));
382 assert_eq!([2,4,6,8].binary_search(&8).ok(), Some(3));
383
384 assert_eq!([2,4,6].binary_search(&1).ok(), None);
385 assert_eq!([2,4,6].binary_search(&5).ok(), None);
386 assert_eq!([2,4,6].binary_search(&4).ok(), Some(1));
387 assert_eq!([2,4,6].binary_search(&6).ok(), Some(2));
388
389 assert_eq!([2,4].binary_search(&1).ok(), None);
390 assert_eq!([2,4].binary_search(&5).ok(), None);
391 assert_eq!([2,4].binary_search(&2).ok(), Some(0));
392 assert_eq!([2,4].binary_search(&4).ok(), Some(1));
393
394 assert_eq!([2].binary_search(&1).ok(), None);
395 assert_eq!([2].binary_search(&5).ok(), None);
396 assert_eq!([2].binary_search(&2).ok(), Some(0));
397
398 assert_eq!([].binary_search(&1).ok(), None);
399 assert_eq!([].binary_search(&5).ok(), None);
400
401 assert!([1,1,1,1,1].binary_search(&1).ok() != None);
402 assert!([1,1,1,1,2].binary_search(&1).ok() != None);
403 assert!([1,1,1,2,2].binary_search(&1).ok() != None);
404 assert!([1,1,2,2,2].binary_search(&1).ok() != None);
405 assert_eq!([1,2,2,2,2].binary_search(&1).ok(), Some(0));
406
407 assert_eq!([1,2,3,4,5].binary_search(&6).ok(), None);
408 assert_eq!([1,2,3,4,5].binary_search(&0).ok(), None);
409}
410
411#[test]
412fn test_reverse() {
413 let mut v = vec![10, 20];
414 assert_eq!(v[0], 10);
415 assert_eq!(v[1], 20);
416 v.reverse();
417 assert_eq!(v[0], 20);
418 assert_eq!(v[1], 10);
419
420 let mut v3 = Vec::<i32>::new();
421 v3.reverse();
422 assert!(v3.is_empty());
423}
424
425#[test]
426fn test_sort() {
427 for len in 4..25 {
428 for _ in 0..100 {
429 let mut v: Vec<_> = thread_rng().gen_iter::<i32>().take(len).collect();
430 let mut v1 = v.clone();
431
432 v.sort();
433 assert!(v.windows(2).all(|w| w[0] <= w[1]));
434
435 v1.sort_by(|a, b| a.cmp(b));
436 assert!(v1.windows(2).all(|w| w[0] <= w[1]));
437
438 v1.sort_by(|a, b| b.cmp(a));
439 assert!(v1.windows(2).all(|w| w[0] >= w[1]));
440 }
441 }
442
443 // shouldn't panic
444 let mut v: [i32; 0] = [];
445 v.sort();
446
447 let mut v = [0xDEADBEEFu64];
448 v.sort();
449 assert!(v == [0xDEADBEEF]);
450}
451
452#[test]
453fn test_sort_stability() {
454 for len in 4..25 {
455 for _ in 0..10 {
456 let mut counts = [0; 10];
457
458 // create a vector like [(6, 1), (5, 1), (6, 2), ...],
459 // where the first item of each tuple is random, but
460 // the second item represents which occurrence of that
461 // number this element is, i.e. the second elements
462 // will occur in sorted order.
463 let mut v: Vec<_> = (0..len).map(|_| {
464 let n = thread_rng().gen::<usize>() % 10;
465 counts[n] += 1;
466 (n, counts[n])
467 }).collect();
468
469 // only sort on the first element, so an unstable sort
470 // may mix up the counts.
471 v.sort_by(|&(a,_), &(b,_)| a.cmp(&b));
472
473 // this comparison includes the count (the second item
474 // of the tuple), so elements with equal first items
475 // will need to be ordered with increasing
476 // counts... i.e. exactly asserting that this sort is
477 // stable.
478 assert!(v.windows(2).all(|w| w[0] <= w[1]));
479 }
480 }
481}
482
483#[test]
484fn test_concat() {
485 let v: [Vec<i32>; 0] = [];
486 let c = v.concat();
487 assert_eq!(c, []);
488 let d = [vec![1], vec![2, 3]].concat();
489 assert_eq!(d, [1, 2, 3]);
490
491 let v: &[&[_]] = &[&[1], &[2, 3]];
c1a9b12d 492 assert_eq!(v.join(&0), [1, 0, 2, 3]);
c34b1796 493 let v: &[&[_]] = &[&[1], &[2], &[3]];
c1a9b12d 494 assert_eq!(v.join(&0), [1, 0, 2, 0, 3]);
c34b1796
AL
495}
496
497#[test]
c1a9b12d 498fn test_join() {
c34b1796 499 let v: [Vec<i32>; 0] = [];
c1a9b12d
SL
500 assert_eq!(v.join(&0), []);
501 assert_eq!([vec![1], vec![2, 3]].join(&0), [1, 0, 2, 3]);
502 assert_eq!([vec![1], vec![2], vec![3]].join(&0), [1, 0, 2, 0, 3]);
c34b1796
AL
503
504 let v: [&[_]; 2] = [&[1], &[2, 3]];
c1a9b12d 505 assert_eq!(v.join(&0), [1, 0, 2, 3]);
c34b1796 506 let v: [&[_]; 3] = [&[1], &[2], &[3]];
c1a9b12d 507 assert_eq!(v.join(&0), [1, 0, 2, 0, 3]);
c34b1796
AL
508}
509
510#[test]
511fn test_insert() {
512 let mut a = vec![1, 2, 4];
513 a.insert(2, 3);
514 assert_eq!(a, [1, 2, 3, 4]);
515
516 let mut a = vec![1, 2, 3];
517 a.insert(0, 0);
518 assert_eq!(a, [0, 1, 2, 3]);
519
520 let mut a = vec![1, 2, 3];
521 a.insert(3, 4);
522 assert_eq!(a, [1, 2, 3, 4]);
523
524 let mut a = vec![];
525 a.insert(0, 1);
526 assert_eq!(a, [1]);
527}
528
529#[test]
530#[should_panic]
531fn test_insert_oob() {
532 let mut a = vec![1, 2, 3];
533 a.insert(4, 5);
534}
535
536#[test]
537fn test_remove() {
538 let mut a = vec![1, 2, 3, 4];
539
540 assert_eq!(a.remove(2), 3);
541 assert_eq!(a, [1, 2, 4]);
542
543 assert_eq!(a.remove(2), 4);
544 assert_eq!(a, [1, 2]);
545
546 assert_eq!(a.remove(0), 1);
547 assert_eq!(a, [2]);
548
549 assert_eq!(a.remove(0), 2);
550 assert_eq!(a, []);
551}
552
553#[test]
554#[should_panic]
555fn test_remove_fail() {
556 let mut a = vec![1];
557 let _ = a.remove(0);
558 let _ = a.remove(0);
559}
560
561#[test]
562fn test_capacity() {
563 let mut v = vec![0];
564 v.reserve_exact(10);
565 assert!(v.capacity() >= 11);
566}
567
568#[test]
569fn test_slice_2() {
570 let v = vec![1, 2, 3, 4, 5];
571 let v = &v[1..3];
572 assert_eq!(v.len(), 2);
573 assert_eq!(v[0], 2);
574 assert_eq!(v[1], 3);
575}
576
54a0048b
SL
577macro_rules! assert_order {
578 (Greater, $a:expr, $b:expr) => {
579 assert_eq!($a.cmp($b), Greater);
580 assert!($a > $b);
581 };
582 (Less, $a:expr, $b:expr) => {
583 assert_eq!($a.cmp($b), Less);
584 assert!($a < $b);
585 };
586 (Equal, $a:expr, $b:expr) => {
587 assert_eq!($a.cmp($b), Equal);
588 assert_eq!($a, $b);
589 }
590}
591
592#[test]
593fn test_total_ord_u8() {
594 let c = &[1u8, 2, 3];
595 assert_order!(Greater, &[1u8, 2, 3, 4][..], &c[..]);
596 let c = &[1u8, 2, 3, 4];
597 assert_order!(Less, &[1u8, 2, 3][..], &c[..]);
598 let c = &[1u8, 2, 3, 6];
599 assert_order!(Equal, &[1u8, 2, 3, 6][..], &c[..]);
600 let c = &[1u8, 2, 3, 4, 5, 6];
601 assert_order!(Less, &[1u8, 2, 3, 4, 5, 5, 5, 5][..], &c[..]);
602 let c = &[1u8, 2, 3, 4];
603 assert_order!(Greater, &[2u8, 2][..], &c[..]);
604}
605
606
c34b1796 607#[test]
54a0048b 608fn test_total_ord_i32() {
c34b1796 609 let c = &[1, 2, 3];
54a0048b 610 assert_order!(Greater, &[1, 2, 3, 4][..], &c[..]);
c34b1796 611 let c = &[1, 2, 3, 4];
54a0048b 612 assert_order!(Less, &[1, 2, 3][..], &c[..]);
c34b1796 613 let c = &[1, 2, 3, 6];
54a0048b 614 assert_order!(Equal, &[1, 2, 3, 6][..], &c[..]);
c34b1796 615 let c = &[1, 2, 3, 4, 5, 6];
54a0048b 616 assert_order!(Less, &[1, 2, 3, 4, 5, 5, 5, 5][..], &c[..]);
c34b1796 617 let c = &[1, 2, 3, 4];
54a0048b 618 assert_order!(Greater, &[2, 2][..], &c[..]);
c34b1796
AL
619}
620
621#[test]
622fn test_iterator() {
623 let xs = [1, 2, 5, 10, 11];
624 let mut it = xs.iter();
625 assert_eq!(it.size_hint(), (5, Some(5)));
626 assert_eq!(it.next().unwrap(), &1);
627 assert_eq!(it.size_hint(), (4, Some(4)));
628 assert_eq!(it.next().unwrap(), &2);
629 assert_eq!(it.size_hint(), (3, Some(3)));
630 assert_eq!(it.next().unwrap(), &5);
631 assert_eq!(it.size_hint(), (2, Some(2)));
632 assert_eq!(it.next().unwrap(), &10);
633 assert_eq!(it.size_hint(), (1, Some(1)));
634 assert_eq!(it.next().unwrap(), &11);
635 assert_eq!(it.size_hint(), (0, Some(0)));
636 assert!(it.next().is_none());
637}
638
c34b1796
AL
639#[test]
640fn test_iter_size_hints() {
641 let mut xs = [1, 2, 5, 10, 11];
642 assert_eq!(xs.iter().size_hint(), (5, Some(5)));
643 assert_eq!(xs.iter_mut().size_hint(), (5, Some(5)));
644}
645
646#[test]
647fn test_iter_clone() {
648 let xs = [1, 2, 5];
649 let mut it = xs.iter();
650 it.next();
651 let mut jt = it.clone();
652 assert_eq!(it.next(), jt.next());
653 assert_eq!(it.next(), jt.next());
654 assert_eq!(it.next(), jt.next());
655}
656
657#[test]
658fn test_mut_iterator() {
659 let mut xs = [1, 2, 3, 4, 5];
660 for x in &mut xs {
661 *x += 1;
662 }
663 assert!(xs == [2, 3, 4, 5, 6])
664}
665
666#[test]
667fn test_rev_iterator() {
668
669 let xs = [1, 2, 5, 10, 11];
670 let ys = [11, 10, 5, 2, 1];
671 let mut i = 0;
672 for &x in xs.iter().rev() {
673 assert_eq!(x, ys[i]);
674 i += 1;
675 }
676 assert_eq!(i, 5);
677}
678
679#[test]
680fn test_mut_rev_iterator() {
681 let mut xs = [1, 2, 3, 4, 5];
682 for (i,x) in xs.iter_mut().rev().enumerate() {
683 *x += i;
684 }
685 assert!(xs == [5, 5, 5, 5, 5])
686}
687
688#[test]
689fn test_move_iterator() {
690 let xs = vec![1,2,3,4,5];
691 assert_eq!(xs.into_iter().fold(0, |a: usize, b: usize| 10*a + b), 12345);
692}
693
694#[test]
695fn test_move_rev_iterator() {
696 let xs = vec![1,2,3,4,5];
697 assert_eq!(xs.into_iter().rev().fold(0, |a: usize, b: usize| 10*a + b), 54321);
698}
699
700#[test]
701fn test_splitator() {
702 let xs = &[1,2,3,4,5];
703
704 let splits: &[&[_]] = &[&[1], &[3], &[5]];
705 assert_eq!(xs.split(|x| *x % 2 == 0).collect::<Vec<_>>(),
706 splits);
707 let splits: &[&[_]] = &[&[], &[2,3,4,5]];
708 assert_eq!(xs.split(|x| *x == 1).collect::<Vec<_>>(),
709 splits);
710 let splits: &[&[_]] = &[&[1,2,3,4], &[]];
711 assert_eq!(xs.split(|x| *x == 5).collect::<Vec<_>>(),
712 splits);
713 let splits: &[&[_]] = &[&[1,2,3,4,5]];
714 assert_eq!(xs.split(|x| *x == 10).collect::<Vec<_>>(),
715 splits);
716 let splits: &[&[_]] = &[&[], &[], &[], &[], &[], &[]];
717 assert_eq!(xs.split(|_| true).collect::<Vec<&[i32]>>(),
718 splits);
719
720 let xs: &[i32] = &[];
721 let splits: &[&[i32]] = &[&[]];
722 assert_eq!(xs.split(|x| *x == 5).collect::<Vec<&[i32]>>(), splits);
723}
724
725#[test]
726fn test_splitnator() {
727 let xs = &[1,2,3,4,5];
728
729 let splits: &[&[_]] = &[&[1,2,3,4,5]];
730 assert_eq!(xs.splitn(1, |x| *x % 2 == 0).collect::<Vec<_>>(),
731 splits);
732 let splits: &[&[_]] = &[&[1], &[3,4,5]];
733 assert_eq!(xs.splitn(2, |x| *x % 2 == 0).collect::<Vec<_>>(),
734 splits);
735 let splits: &[&[_]] = &[&[], &[], &[], &[4,5]];
736 assert_eq!(xs.splitn(4, |_| true).collect::<Vec<_>>(),
737 splits);
738
739 let xs: &[i32] = &[];
740 let splits: &[&[i32]] = &[&[]];
741 assert_eq!(xs.splitn(2, |x| *x == 5).collect::<Vec<_>>(), splits);
742}
743
744#[test]
745fn test_splitnator_mut() {
746 let xs = &mut [1,2,3,4,5];
747
748 let splits: &[&mut[_]] = &[&mut [1,2,3,4,5]];
749 assert_eq!(xs.splitn_mut(1, |x| *x % 2 == 0).collect::<Vec<_>>(),
750 splits);
751 let splits: &[&mut[_]] = &[&mut [1], &mut [3,4,5]];
752 assert_eq!(xs.splitn_mut(2, |x| *x % 2 == 0).collect::<Vec<_>>(),
753 splits);
754 let splits: &[&mut[_]] = &[&mut [], &mut [], &mut [], &mut [4,5]];
755 assert_eq!(xs.splitn_mut(4, |_| true).collect::<Vec<_>>(),
756 splits);
757
758 let xs: &mut [i32] = &mut [];
759 let splits: &[&mut[i32]] = &[&mut []];
760 assert_eq!(xs.splitn_mut(2, |x| *x == 5).collect::<Vec<_>>(),
761 splits);
762}
763
764#[test]
765fn test_rsplitator() {
766 let xs = &[1,2,3,4,5];
767
768 let splits: &[&[_]] = &[&[5], &[3], &[1]];
769 assert_eq!(xs.split(|x| *x % 2 == 0).rev().collect::<Vec<_>>(),
770 splits);
771 let splits: &[&[_]] = &[&[2,3,4,5], &[]];
772 assert_eq!(xs.split(|x| *x == 1).rev().collect::<Vec<_>>(),
773 splits);
774 let splits: &[&[_]] = &[&[], &[1,2,3,4]];
775 assert_eq!(xs.split(|x| *x == 5).rev().collect::<Vec<_>>(),
776 splits);
777 let splits: &[&[_]] = &[&[1,2,3,4,5]];
778 assert_eq!(xs.split(|x| *x == 10).rev().collect::<Vec<_>>(),
779 splits);
780
781 let xs: &[i32] = &[];
782 let splits: &[&[i32]] = &[&[]];
783 assert_eq!(xs.split(|x| *x == 5).rev().collect::<Vec<&[i32]>>(), splits);
784}
785
786#[test]
787fn test_rsplitnator() {
788 let xs = &[1,2,3,4,5];
789
790 let splits: &[&[_]] = &[&[1,2,3,4,5]];
791 assert_eq!(xs.rsplitn(1, |x| *x % 2 == 0).collect::<Vec<_>>(),
792 splits);
793 let splits: &[&[_]] = &[&[5], &[1,2,3]];
794 assert_eq!(xs.rsplitn(2, |x| *x % 2 == 0).collect::<Vec<_>>(),
795 splits);
796 let splits: &[&[_]] = &[&[], &[], &[], &[1,2]];
797 assert_eq!(xs.rsplitn(4, |_| true).collect::<Vec<_>>(),
798 splits);
799
800 let xs: &[i32] = &[];
801 let splits: &[&[i32]] = &[&[]];
802 assert_eq!(xs.rsplitn(2, |x| *x == 5).collect::<Vec<&[i32]>>(), splits);
803 assert!(xs.rsplitn(0, |x| *x % 2 == 0).next().is_none());
804}
805
806#[test]
807fn test_windowsator() {
808 let v = &[1,2,3,4];
809
810 let wins: &[&[_]] = &[&[1,2], &[2,3], &[3,4]];
811 assert_eq!(v.windows(2).collect::<Vec<_>>(), wins);
812
813 let wins: &[&[_]] = &[&[1,2,3], &[2,3,4]];
814 assert_eq!(v.windows(3).collect::<Vec<_>>(), wins);
815 assert!(v.windows(6).next().is_none());
816
817 let wins: &[&[_]] = &[&[3,4], &[2,3], &[1,2]];
818 assert_eq!(v.windows(2).rev().collect::<Vec<&[_]>>(), wins);
c34b1796
AL
819}
820
821#[test]
822#[should_panic]
823fn test_windowsator_0() {
824 let v = &[1,2,3,4];
825 let _it = v.windows(0);
826}
827
828#[test]
829fn test_chunksator() {
830 let v = &[1,2,3,4,5];
831
832 assert_eq!(v.chunks(2).len(), 3);
833
834 let chunks: &[&[_]] = &[&[1,2], &[3,4], &[5]];
835 assert_eq!(v.chunks(2).collect::<Vec<_>>(), chunks);
836 let chunks: &[&[_]] = &[&[1,2,3], &[4,5]];
837 assert_eq!(v.chunks(3).collect::<Vec<_>>(), chunks);
838 let chunks: &[&[_]] = &[&[1,2,3,4,5]];
839 assert_eq!(v.chunks(6).collect::<Vec<_>>(), chunks);
840
841 let chunks: &[&[_]] = &[&[5], &[3,4], &[1,2]];
842 assert_eq!(v.chunks(2).rev().collect::<Vec<_>>(), chunks);
c34b1796
AL
843}
844
845#[test]
846#[should_panic]
847fn test_chunksator_0() {
848 let v = &[1,2,3,4];
849 let _it = v.chunks(0);
850}
851
c34b1796
AL
852#[test]
853fn test_reverse_part() {
854 let mut values = [1,2,3,4,5];
855 values[1..4].reverse();
856 assert!(values == [1,4,3,2,5]);
857}
858
859#[test]
860fn test_show() {
861 macro_rules! test_show_vec {
862 ($x:expr, $x_str:expr) => ({
863 let (x, x_str) = ($x, $x_str);
864 assert_eq!(format!("{:?}", x), x_str);
865 assert_eq!(format!("{:?}", x), x_str);
866 })
867 }
868 let empty = Vec::<i32>::new();
869 test_show_vec!(empty, "[]");
870 test_show_vec!(vec![1], "[1]");
871 test_show_vec!(vec![1, 2, 3], "[1, 2, 3]");
872 test_show_vec!(vec![vec![], vec![1], vec![1, 1]],
873 "[[], [1], [1, 1]]");
874
875 let empty_mut: &mut [i32] = &mut[];
876 test_show_vec!(empty_mut, "[]");
877 let v = &mut[1];
878 test_show_vec!(v, "[1]");
879 let v = &mut[1, 2, 3];
880 test_show_vec!(v, "[1, 2, 3]");
881 let v: &mut[&mut[_]] = &mut[&mut[], &mut[1], &mut[1, 1]];
882 test_show_vec!(v, "[[], [1], [1, 1]]");
883}
884
885#[test]
886fn test_vec_default() {
887 macro_rules! t {
888 ($ty:ty) => {{
889 let v: $ty = Default::default();
890 assert!(v.is_empty());
891 }}
892 }
893
894 t!(&[i32]);
895 t!(Vec<i32>);
896}
897
c34b1796
AL
898#[test]
899#[should_panic]
900fn test_overflow_does_not_cause_segfault() {
901 let mut v = vec![];
902 v.reserve_exact(!0);
903 v.push(1);
904 v.push(2);
905}
906
907#[test]
908#[should_panic]
909fn test_overflow_does_not_cause_segfault_managed() {
910 let mut v = vec![Rc::new(1)];
911 v.reserve_exact(!0);
912 v.push(Rc::new(2));
913}
914
915#[test]
916fn test_mut_split_at() {
54a0048b 917 let mut values = [1,2,3,4,5];
c34b1796
AL
918 {
919 let (left, right) = values.split_at_mut(2);
920 {
921 let left: &[_] = left;
922 assert!(left[..left.len()] == [1, 2]);
923 }
924 for p in left {
925 *p += 1;
926 }
927
928 {
929 let right: &[_] = right;
930 assert!(right[..right.len()] == [3, 4, 5]);
931 }
932 for p in right {
933 *p += 2;
934 }
935 }
936
937 assert!(values == [2, 3, 5, 6, 7]);
938}
939
940#[derive(Clone, PartialEq)]
941struct Foo;
942
943#[test]
944fn test_iter_zero_sized() {
945 let mut v = vec![Foo, Foo, Foo];
946 assert_eq!(v.len(), 3);
947 let mut cnt = 0;
948
949 for f in &v {
950 assert!(*f == Foo);
951 cnt += 1;
952 }
953 assert_eq!(cnt, 3);
954
955 for f in &v[1..3] {
956 assert!(*f == Foo);
957 cnt += 1;
958 }
959 assert_eq!(cnt, 5);
960
961 for f in &mut v {
962 assert!(*f == Foo);
963 cnt += 1;
964 }
965 assert_eq!(cnt, 8);
966
967 for f in v {
968 assert!(f == Foo);
969 cnt += 1;
970 }
971 assert_eq!(cnt, 11);
972
973 let xs: [Foo; 3] = [Foo, Foo, Foo];
974 cnt = 0;
975 for f in &xs {
976 assert!(*f == Foo);
977 cnt += 1;
978 }
979 assert!(cnt == 3);
980}
981
982#[test]
983fn test_shrink_to_fit() {
984 let mut xs = vec![0, 1, 2, 3];
985 for i in 4..100 {
986 xs.push(i)
987 }
988 assert_eq!(xs.capacity(), 128);
989 xs.shrink_to_fit();
990 assert_eq!(xs.capacity(), 100);
991 assert_eq!(xs, (0..100).collect::<Vec<_>>());
992}
993
994#[test]
995fn test_starts_with() {
996 assert!(b"foobar".starts_with(b"foo"));
997 assert!(!b"foobar".starts_with(b"oob"));
998 assert!(!b"foobar".starts_with(b"bar"));
999 assert!(!b"foo".starts_with(b"foobar"));
1000 assert!(!b"bar".starts_with(b"foobar"));
1001 assert!(b"foobar".starts_with(b"foobar"));
1002 let empty: &[u8] = &[];
1003 assert!(empty.starts_with(empty));
1004 assert!(!empty.starts_with(b"foo"));
1005 assert!(b"foobar".starts_with(empty));
1006}
1007
1008#[test]
1009fn test_ends_with() {
1010 assert!(b"foobar".ends_with(b"bar"));
1011 assert!(!b"foobar".ends_with(b"oba"));
1012 assert!(!b"foobar".ends_with(b"foo"));
1013 assert!(!b"foo".ends_with(b"foobar"));
1014 assert!(!b"bar".ends_with(b"foobar"));
1015 assert!(b"foobar".ends_with(b"foobar"));
1016 let empty: &[u8] = &[];
1017 assert!(empty.ends_with(empty));
1018 assert!(!empty.ends_with(b"foo"));
1019 assert!(b"foobar".ends_with(empty));
1020}
1021
1022#[test]
1023fn test_mut_splitator() {
1024 let mut xs = [0,1,0,2,3,0,0,4,5,0];
1025 assert_eq!(xs.split_mut(|x| *x == 0).count(), 6);
1026 for slice in xs.split_mut(|x| *x == 0) {
1027 slice.reverse();
1028 }
1029 assert!(xs == [0,1,0,3,2,0,0,5,4,0]);
1030
1031 let mut xs = [0,1,0,2,3,0,0,4,5,0,6,7];
1032 for slice in xs.split_mut(|x| *x == 0).take(5) {
1033 slice.reverse();
1034 }
1035 assert!(xs == [0,1,0,3,2,0,0,5,4,0,6,7]);
1036}
1037
1038#[test]
1039fn test_mut_splitator_rev() {
1040 let mut xs = [1,2,0,3,4,0,0,5,6,0];
1041 for slice in xs.split_mut(|x| *x == 0).rev().take(4) {
1042 slice.reverse();
1043 }
1044 assert!(xs == [1,2,0,4,3,0,0,6,5,0]);
1045}
1046
1047#[test]
1048fn test_get_mut() {
1049 let mut v = [0,1,2];
1050 assert_eq!(v.get_mut(3), None);
1051 v.get_mut(1).map(|e| *e = 7);
1052 assert_eq!(v[1], 7);
1053 let mut x = 2;
1054 assert_eq!(v.get_mut(2), Some(&mut x));
1055}
1056
1057#[test]
1058fn test_mut_chunks() {
1059 let mut v = [0, 1, 2, 3, 4, 5, 6];
1060 assert_eq!(v.chunks_mut(2).len(), 4);
1061 for (i, chunk) in v.chunks_mut(3).enumerate() {
1062 for x in chunk {
1063 *x = i as u8;
1064 }
1065 }
1066 let result = [0, 0, 0, 1, 1, 1, 2];
1067 assert!(v == result);
1068}
1069
1070#[test]
1071fn test_mut_chunks_rev() {
1072 let mut v = [0, 1, 2, 3, 4, 5, 6];
1073 for (i, chunk) in v.chunks_mut(3).rev().enumerate() {
1074 for x in chunk {
1075 *x = i as u8;
1076 }
1077 }
1078 let result = [2, 2, 2, 1, 1, 1, 0];
1079 assert!(v == result);
1080}
1081
1082#[test]
1083#[should_panic]
1084fn test_mut_chunks_0() {
1085 let mut v = [1, 2, 3, 4];
1086 let _it = v.chunks_mut(0);
1087}
1088
1089#[test]
1090fn test_mut_last() {
1091 let mut x = [1, 2, 3, 4, 5];
1092 let h = x.last_mut();
1093 assert_eq!(*h.unwrap(), 5);
1094
1095 let y: &mut [i32] = &mut [];
1096 assert!(y.last_mut().is_none());
1097}
1098
1099#[test]
1100fn test_to_vec() {
1101 let xs: Box<_> = box [1, 2, 3];
1102 let ys = xs.to_vec();
1103 assert_eq!(ys, [1, 2, 3]);
1104}
1105
c1a9b12d
SL
1106#[test]
1107fn test_box_slice_clone() {
1108 let data = vec![vec![0, 1], vec![0], vec![1]];
1109 let data2 = data.clone().into_boxed_slice().clone().to_vec();
1110
1111 assert_eq!(data, data2);
1112}
1113
1114#[test]
1115fn test_box_slice_clone_panics() {
1116 use std::sync::Arc;
1117 use std::sync::atomic::{AtomicUsize, Ordering};
1118 use std::thread::spawn;
1119
1120 struct Canary {
1121 count: Arc<AtomicUsize>,
1122 panics: bool
1123 }
1124
1125 impl Drop for Canary {
1126 fn drop(&mut self) {
1127 self.count.fetch_add(1, Ordering::SeqCst);
1128 }
1129 }
1130
1131 impl Clone for Canary {
1132 fn clone(&self) -> Self {
1133 if self.panics { panic!() }
1134
1135 Canary {
1136 count: self.count.clone(),
1137 panics: self.panics
1138 }
1139 }
1140 }
1141
1142 let drop_count = Arc::new(AtomicUsize::new(0));
1143 let canary = Canary { count: drop_count.clone(), panics: false };
1144 let panic = Canary { count: drop_count.clone(), panics: true };
1145
1146 spawn(move || {
1147 // When xs is dropped, +5.
1148 let xs = vec![canary.clone(), canary.clone(), canary.clone(),
1149 panic, canary].into_boxed_slice();
1150
1151 // When panic is cloned, +3.
1152 xs.clone();
1153 }).join().unwrap_err();
1154
1155 // Total = 8
1156 assert_eq!(drop_count.load(Ordering::SeqCst), 8);
1157}
1158
7453a54e
SL
1159#[test]
1160fn test_copy_from_slice() {
1161 let src = [0, 1, 2, 3, 4, 5];
1162 let mut dst = [0; 6];
1163 dst.copy_from_slice(&src);
1164 assert_eq!(src, dst)
1165}
1166
1167#[test]
1168#[should_panic(expected = "destination and source slices have different lengths")]
1169fn test_copy_from_slice_dst_longer() {
1170 let src = [0, 1, 2, 3];
1171 let mut dst = [0; 5];
1172 dst.copy_from_slice(&src);
1173}
1174
1175#[test]
1176#[should_panic(expected = "destination and source slices have different lengths")]
1177fn test_copy_from_slice_dst_shorter() {
1178 let src = [0, 1, 2, 3];
1179 let mut dst = [0; 3];
1180 dst.copy_from_slice(&src);
1181}
1182
c34b1796 1183mod bench {
c34b1796 1184 use std::{mem, ptr};
9346a6ac 1185 use std::__rand::{Rng, thread_rng};
c34b1796
AL
1186
1187 use test::{Bencher, black_box};
1188
1189 #[bench]
1190 fn iterator(b: &mut Bencher) {
1191 // peculiar numbers to stop LLVM from optimising the summation
1192 // out.
1193 let v: Vec<_> = (0..100).map(|i| i ^ (i << 1) ^ (i >> 1)).collect();
1194
1195 b.iter(|| {
1196 let mut sum = 0;
1197 for x in &v {
1198 sum += *x;
1199 }
1200 // sum == 11806, to stop dead code elimination.
1201 if sum == 0 {panic!()}
1202 })
1203 }
1204
1205 #[bench]
1206 fn mut_iterator(b: &mut Bencher) {
c1a9b12d 1207 let mut v = vec![0; 100];
c34b1796
AL
1208
1209 b.iter(|| {
1210 let mut i = 0;
1211 for x in &mut v {
1212 *x = i;
1213 i += 1;
1214 }
1215 })
1216 }
1217
1218 #[bench]
1219 fn concat(b: &mut Bencher) {
1220 let xss: Vec<Vec<i32>> =
1221 (0..100).map(|i| (0..i).collect()).collect();
1222 b.iter(|| {
1223 xss.concat();
1224 });
1225 }
1226
1227 #[bench]
c1a9b12d 1228 fn join(b: &mut Bencher) {
c34b1796
AL
1229 let xss: Vec<Vec<i32>> =
1230 (0..100).map(|i| (0..i).collect()).collect();
1231 b.iter(|| {
c1a9b12d 1232 xss.join(&0)
c34b1796
AL
1233 });
1234 }
1235
1236 #[bench]
1237 fn push(b: &mut Bencher) {
1238 let mut vec = Vec::<i32>::new();
1239 b.iter(|| {
1240 vec.push(0);
1241 black_box(&vec);
1242 });
1243 }
1244
1245 #[bench]
1246 fn starts_with_same_vector(b: &mut Bencher) {
1247 let vec: Vec<_> = (0..100).collect();
1248 b.iter(|| {
1249 vec.starts_with(&vec)
1250 })
1251 }
1252
1253 #[bench]
1254 fn starts_with_single_element(b: &mut Bencher) {
1255 let vec: Vec<_> = vec![0];
1256 b.iter(|| {
1257 vec.starts_with(&vec)
1258 })
1259 }
1260
1261 #[bench]
1262 fn starts_with_diff_one_element_at_end(b: &mut Bencher) {
1263 let vec: Vec<_> = (0..100).collect();
1264 let mut match_vec: Vec<_> = (0..99).collect();
1265 match_vec.push(0);
1266 b.iter(|| {
1267 vec.starts_with(&match_vec)
1268 })
1269 }
1270
1271 #[bench]
1272 fn ends_with_same_vector(b: &mut Bencher) {
1273 let vec: Vec<_> = (0..100).collect();
1274 b.iter(|| {
1275 vec.ends_with(&vec)
1276 })
1277 }
1278
1279 #[bench]
1280 fn ends_with_single_element(b: &mut Bencher) {
1281 let vec: Vec<_> = vec![0];
1282 b.iter(|| {
1283 vec.ends_with(&vec)
1284 })
1285 }
1286
1287 #[bench]
1288 fn ends_with_diff_one_element_at_beginning(b: &mut Bencher) {
1289 let vec: Vec<_> = (0..100).collect();
1290 let mut match_vec: Vec<_> = (0..100).collect();
1291 match_vec[0] = 200;
1292 b.iter(|| {
1293 vec.starts_with(&match_vec)
1294 })
1295 }
1296
1297 #[bench]
1298 fn contains_last_element(b: &mut Bencher) {
1299 let vec: Vec<_> = (0..100).collect();
1300 b.iter(|| {
1301 vec.contains(&99)
1302 })
1303 }
1304
1305 #[bench]
1306 fn zero_1kb_from_elem(b: &mut Bencher) {
1307 b.iter(|| {
c1a9b12d 1308 vec![0u8; 1024]
c34b1796
AL
1309 });
1310 }
1311
1312 #[bench]
1313 fn zero_1kb_set_memory(b: &mut Bencher) {
1314 b.iter(|| {
1315 let mut v = Vec::<u8>::with_capacity(1024);
1316 unsafe {
1317 let vp = v.as_mut_ptr();
1318 ptr::write_bytes(vp, 0, 1024);
1319 v.set_len(1024);
1320 }
1321 v
1322 });
1323 }
1324
1325 #[bench]
1326 fn zero_1kb_loop_set(b: &mut Bencher) {
1327 b.iter(|| {
1328 let mut v = Vec::<u8>::with_capacity(1024);
1329 unsafe {
1330 v.set_len(1024);
1331 }
1332 for i in 0..1024 {
1333 v[i] = 0;
1334 }
1335 });
1336 }
1337
1338 #[bench]
1339 fn zero_1kb_mut_iter(b: &mut Bencher) {
1340 b.iter(|| {
1341 let mut v = Vec::<u8>::with_capacity(1024);
1342 unsafe {
1343 v.set_len(1024);
1344 }
1345 for x in &mut v {
1346 *x = 0;
1347 }
1348 v
1349 });
1350 }
1351
1352 #[bench]
1353 fn random_inserts(b: &mut Bencher) {
9346a6ac 1354 let mut rng = thread_rng();
c34b1796 1355 b.iter(|| {
c1a9b12d 1356 let mut v = vec![(0, 0); 30];
c34b1796
AL
1357 for _ in 0..100 {
1358 let l = v.len();
1359 v.insert(rng.gen::<usize>() % (l + 1),
1360 (1, 1));
1361 }
1362 })
1363 }
1364 #[bench]
1365 fn random_removes(b: &mut Bencher) {
9346a6ac 1366 let mut rng = thread_rng();
c34b1796 1367 b.iter(|| {
c1a9b12d 1368 let mut v = vec![(0, 0); 130];
c34b1796
AL
1369 for _ in 0..100 {
1370 let l = v.len();
1371 v.remove(rng.gen::<usize>() % l);
1372 }
1373 })
1374 }
1375
1376 #[bench]
1377 fn sort_random_small(b: &mut Bencher) {
9346a6ac 1378 let mut rng = thread_rng();
c34b1796
AL
1379 b.iter(|| {
1380 let mut v: Vec<_> = rng.gen_iter::<u64>().take(5).collect();
1381 v.sort();
1382 });
1383 b.bytes = 5 * mem::size_of::<u64>() as u64;
1384 }
1385
1386 #[bench]
1387 fn sort_random_medium(b: &mut Bencher) {
9346a6ac 1388 let mut rng = thread_rng();
c34b1796
AL
1389 b.iter(|| {
1390 let mut v: Vec<_> = rng.gen_iter::<u64>().take(100).collect();
1391 v.sort();
1392 });
1393 b.bytes = 100 * mem::size_of::<u64>() as u64;
1394 }
1395
1396 #[bench]
1397 fn sort_random_large(b: &mut Bencher) {
9346a6ac 1398 let mut rng = thread_rng();
c34b1796
AL
1399 b.iter(|| {
1400 let mut v: Vec<_> = rng.gen_iter::<u64>().take(10000).collect();
1401 v.sort();
1402 });
1403 b.bytes = 10000 * mem::size_of::<u64>() as u64;
1404 }
1405
1406 #[bench]
1407 fn sort_sorted(b: &mut Bencher) {
1408 let mut v: Vec<_> = (0..10000).collect();
1409 b.iter(|| {
1410 v.sort();
1411 });
1412 b.bytes = (v.len() * mem::size_of_val(&v[0])) as u64;
1413 }
1414
1415 type BigSortable = (u64, u64, u64, u64);
1416
1417 #[bench]
1418 fn sort_big_random_small(b: &mut Bencher) {
9346a6ac 1419 let mut rng = thread_rng();
c34b1796
AL
1420 b.iter(|| {
1421 let mut v = rng.gen_iter::<BigSortable>().take(5)
1422 .collect::<Vec<BigSortable>>();
1423 v.sort();
1424 });
1425 b.bytes = 5 * mem::size_of::<BigSortable>() as u64;
1426 }
1427
1428 #[bench]
1429 fn sort_big_random_medium(b: &mut Bencher) {
9346a6ac 1430 let mut rng = thread_rng();
c34b1796
AL
1431 b.iter(|| {
1432 let mut v = rng.gen_iter::<BigSortable>().take(100)
1433 .collect::<Vec<BigSortable>>();
1434 v.sort();
1435 });
1436 b.bytes = 100 * mem::size_of::<BigSortable>() as u64;
1437 }
1438
1439 #[bench]
1440 fn sort_big_random_large(b: &mut Bencher) {
9346a6ac 1441 let mut rng = thread_rng();
c34b1796
AL
1442 b.iter(|| {
1443 let mut v = rng.gen_iter::<BigSortable>().take(10000)
1444 .collect::<Vec<BigSortable>>();
1445 v.sort();
1446 });
1447 b.bytes = 10000 * mem::size_of::<BigSortable>() as u64;
1448 }
1449
1450 #[bench]
1451 fn sort_big_sorted(b: &mut Bencher) {
1452 let mut v: Vec<BigSortable> = (0..10000).map(|i| (i, i, i, i)).collect();
1453 b.iter(|| {
1454 v.sort();
1455 });
1456 b.bytes = (v.len() * mem::size_of_val(&v[0])) as u64;
1457 }
1458}