-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathtest_sort.rs
107 lines (94 loc) · 2.36 KB
/
test_sort.rs
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#![feature(is_sorted)]
use algo::common;
use algo::sort;
macro_rules! case {
// to test fn is "sort::$p::sort"
// testcase name is "fn $p()"
($p: ident) => {
#[test]
fn $p() {
let mut data = gen_vec_data();
for t in data.iter_mut() {
sort::$p::sort(t);
assert!(t.is_sorted());
}
}
};
// to test fn is "sort::$p::$f"
// testcase name is "fn $f()"
($p: ident, $f: ident) => {
#[test]
fn $f() {
let mut data = gen_vec_data();
for t in data.iter_mut() {
sort::$p::$f(t);
assert!(t.is_sorted());
}
}
};
}
case!(bubble);
case!(insert);
case!(selection);
case!(selection, sort_cocktail);
case!(quick);
case!(shell);
case!(floyd);
#[test]
fn insert_sort_dth() {
let mut data = common::util::vec_alphabet(1);
common::util::shuffle(&mut data);
let len = data.len();
sort::insert::sort_dth(&mut data, 0, len - 1, 0);
assert!(data.is_sorted());
// 0-th char equal
let mut data = vec!["af", "ae", "ad", "ac", "ab", "aa"];
let len = data.len();
sort::insert::sort_dth(&mut data, 0, len - 1, 0);
assert!(data.is_sorted());
// length
let mut data = vec!["aaaaaa", "aaaaa", "aaaa", "aaa", "aa", "a"];
let len = data.len();
sort::insert::sort_dth(&mut data, 0, len - 1, 0);
assert!(data.is_sorted());
}
#[test]
fn tournament_tree() {
let mut data = gen_vec_data();
for t in data.iter_mut() {
let t = sort::tree_selection::sort_desc(t);
assert!(t.iter().rev().is_sorted());
}
}
#[test]
fn merge() {
let mut data = gen_vec_data();
for t in data.iter_mut() {
let t = sort::merge::v1::sort(t);
assert!(t.is_sorted());
}
let mut data = gen_vec_data();
for t in data.iter_mut() {
sort::merge::v2::sort(t);
assert!(t.is_sorted());
}
let mut data = gen_vec_data();
for t in data.iter_mut() {
sort::merge::v3::sort(t);
assert!(t.is_sorted());
}
}
// utils
fn gen_vec_data() -> Vec<Vec<i32>> {
let sorted: Vec<i32> = (1..100).collect();
let mut unsorted = sorted.clone();
common::util::shuffle(&mut unsorted);
vec![
//empty
vec![],
//only 1
vec![1],
sorted,
unsorted,
]
}