about summary refs log tree commit diff
path: root/src/test/run-pass/vector-sort-failure-safe.rs
blob: e4e419e4988c17923f92ed81564dca08e95006af (plain)
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
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::task;
use std::rand::{task_rng, Rng};

static MAX_LEN: uint = 20;
static mut drop_counts: [uint, .. MAX_LEN] = [0, .. MAX_LEN];
static mut clone_count: uint = 0;

#[deriving(Rand, PartialEq, PartialOrd, TotalEq, TotalOrd)]
struct DropCounter { x: uint, clone_num: uint }

impl Clone for DropCounter {
    fn clone(&self) -> DropCounter {
        let num = unsafe { clone_count };
        unsafe { clone_count += 1; }
        DropCounter {
            x: self.x,
            clone_num: num
        }
    }
}

impl Drop for DropCounter {
    fn drop(&mut self) {
        unsafe {
            // Rand creates some with arbitrary clone_nums
            if self.clone_num < MAX_LEN {
                drop_counts[self.clone_num] += 1;
            }
        }
    }
}

pub fn main() {
    // len can't go above 64.
    for len in range(2u, MAX_LEN) {
        for _ in range(0, 10) {
            let main = task_rng().gen_iter::<DropCounter>()
                                 .take(len)
                                 .collect::<Vec<DropCounter>>();

            // work out the total number of comparisons required to sort
            // this array...
            let mut count = 0;
            main.clone().as_mut_slice().sort_by(|a, b| { count += 1; a.cmp(b) });

            // ... and then fail on each and every single one.
            for fail_countdown in range(0, count) {
                // refresh the counters.
                unsafe {
                    drop_counts = [0, .. MAX_LEN];
                    clone_count = 0;
                }

                let v = main.clone();

                task::try(proc() {
                        let mut v = v;
                        let mut fail_countdown = fail_countdown;
                        v.as_mut_slice().sort_by(|a, b| {
                                if fail_countdown == 0 {
                                    fail!()
                                }
                                fail_countdown -= 1;
                                a.cmp(b)
                            })
                    });

                // check that the number of things dropped is exactly
                // what we expect (i.e. the contents of `v`).
                unsafe {
                    for (i, &c) in drop_counts.iter().enumerate() {
                        let expected = if i < len {1} else {0};
                        assert!(c == expected,
                                "found drop count == {} for i == {}, len == {}",
                                c, i, len);
                    }
                }
            }
        }
    }
}