sort/
sort.rs

1// This is a buggy quick sort implementation, QuickCheck will find the bug for
2// you.
3
4extern crate quickcheck;
5
6use quickcheck::quickcheck;
7
8fn smaller_than<T: Clone + Ord>(xs: &[T], pivot: &T) -> Vec<T> {
9    return xs.iter().filter(|&x| *x < *pivot).map(|x| x.clone()).collect();
10}
11
12fn larger_than<T: Clone + Ord>(xs: &[T], pivot: &T) -> Vec<T> {
13    return xs.iter().filter(|&x| *x > *pivot).map(|x| x.clone()).collect();
14}
15
16fn sortk<T: Clone + Ord>(x: &T, xs: &[T]) -> Vec<T> {
17    let mut result : Vec<T> = sort(&*smaller_than(xs, x));
18    let last_part = sort(&*larger_than(xs, x));
19    result.push(x.clone());
20    result.extend(last_part.iter().map(|x| x.clone()));
21    result
22}
23
24fn sort<T: Clone + Ord>(list: &[T]) -> Vec<T> {
25    if list.is_empty() {
26        vec![]
27    } else {
28        sortk(&list[0], &list[1..])
29    }
30}
31
32fn main() {
33    fn is_sorted(xs: Vec<isize>) -> bool {
34        for win in xs.windows(2) {
35            if win[0] > win[1] {
36                return false
37            }
38        }
39        true
40    }
41
42    fn keeps_length(xs: Vec<isize>) -> bool {
43        xs.len() == sort(&*xs).len()
44    }
45    quickcheck(keeps_length as fn(Vec<isize>) -> bool);
46
47    quickcheck(is_sorted as fn(Vec<isize>) -> bool)
48}