1 // Copyright 2013 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.
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.
12 use std::rand::{task_rng, Rng};
14 static MAX_LEN: uint = 20;
15 static mut drop_counts: [uint, .. MAX_LEN] = [0, .. MAX_LEN];
16 static mut clone_count: uint = 0;
18 #[deriving(Rand, Eq, Ord, TotalEq, TotalOrd)]
19 struct DropCounter { x: uint, clone_num: uint }
21 impl Clone for DropCounter {
22 fn clone(&self) -> DropCounter {
23 let num = unsafe { clone_count };
24 unsafe { clone_count += 1; }
32 impl Drop for DropCounter {
35 // Rand creates some with arbitrary clone_nums
36 if self.clone_num < MAX_LEN {
37 drop_counts[self.clone_num] += 1;
44 // len can't go above 64.
45 for len in range(2u, MAX_LEN) {
46 for _ in range(0, 10) {
47 let main = task_rng().gen_vec::<DropCounter>(len);
49 // work out the total number of comparisons required to sort
52 main.clone().sort_by(|a, b| { count += 1; a.cmp(b) });
54 // ... and then fail on each and every single one.
55 for fail_countdown in range(0, count) {
56 // refresh the counters.
58 drop_counts = [0, .. MAX_LEN];
66 let mut fail_countdown = fail_countdown;
68 if fail_countdown == 0 {
76 // check that the number of things dropped is exactly
77 // what we expect (i.e. the contents of `v`).
79 for (i, &c) in drop_counts.iter().enumerate() {
80 let expected = if i < len {1} else {0};
81 assert!(c == expected,
82 "found drop count == {} for i == {}, len == {}",