-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermutation-tests.rs
206 lines (179 loc) · 6.3 KB
/
permutation-tests.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
extern crate test;
extern crate rand;
use rand::{task_rng, Rng};
#[test]
fn test_permutations() {
/* Test simple permutations */
let mut v : &mut[int] = &mut[1, 2, 3, 4, 5];
assert!(v.prev_permutation() == false);
assert!(v.next_permutation());
assert_eq!(v, &mut[1, 2, 3, 5, 4]);
assert!(v.prev_permutation());
assert_eq!(v, &mut[1, 2, 3, 4, 5]);
assert!(v.next_permutation());
assert!(v.next_permutation());
assert_eq!(v, &mut[1, 2, 4, 3, 5]);
assert!(v.next_permutation());
assert_eq!(v, &mut[1, 2, 4, 5, 3]);
let mut v: &mut[int] = &mut[1, 0, 0, 0];
assert!(v.next_permutation() == false);
assert!(v.prev_permutation());
assert_eq!(v, &mut[0, 1, 0, 0]);
assert!(v.prev_permutation());
assert_eq!(v, &mut[0, 0, 1, 0]);
assert!(v.prev_permutation());
assert_eq!(v, &mut[0, 0, 0, 1]);
assert!(v.prev_permutation() == false);
}
#[test]
fn test_permutations_empty_and_short() {
/* Test 0-length, 1-length and 2-length slices */
let mut empty : &mut[int] = &mut[];
assert!(empty.next_permutation() == false);
assert_eq!(empty, &mut[]);
assert!(empty.prev_permutation() == false);
assert_eq!(empty, &mut[]);
let mut small : &mut[int] = &mut[1, 2];
assert!(small.prev_permutation() == false);
assert_eq!(small, &mut[1, 2]);
assert!(small.next_permutation());
assert_eq!(small, &mut[2, 1]);
assert!(small.next_permutation() == false);
assert_eq!(small, &mut[2, 1]);
assert!(small.prev_permutation());
assert_eq!(small, &mut[1, 2]);
assert!(small.prev_permutation() == false);
assert_eq!(small, &mut[1, 2]);
}
#[test]
fn test_permutations_custom_type() {
/* Test a custom struct (w/ nonsense contents) */
struct TS { big: [uint, ..4], small: Option<uint> }
impl Ord for TS {
fn lt(&self, other: &TS) -> bool {
return self.small.unwrap() < other.small.unwrap();
}
}
impl TotalOrd for TS {
fn cmp(&self, other: &TS) -> Ordering {
return self.small.unwrap().cmp(&other.small.unwrap());
}
}
impl Eq for TS {
fn eq(&self, other: &TS) -> bool {
return self.small.unwrap() == other.small.unwrap();
}
}
impl TotalEq for TS { }
impl std::fmt::Show for TS {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f.buf, "{}", self.small.unwrap())
}
}
let mut tmp : Vec<TS> = Vec::new();
for i in range(0u, 3) {
tmp.push(TS { big: [i, ..4], small: Some(i) });
}
let mut v2 : &mut[TS] = tmp.as_mut_slice();
assert!(v2.prev_permutation() == false);
assert!(v2.next_permutation());
assert_eq!(v2, &mut[TS{ big: [0, ..4], small: Some(0) }, TS{ big: [0, ..4], small: Some(2) }, TS{ big: [0, ..4], small: Some(1) }]);
assert!(v2.prev_permutation());
assert_eq!(v2, &mut[TS{ big: [0, ..4], small: Some(0) }, TS{ big: [0, ..4], small: Some(1) }, TS{ big: [0, ..4], small: Some(2) }]);
assert!(v2.next_permutation());
assert_eq!(v2, &mut[TS{ big: [0, ..4], small: Some(0) }, TS{ big: [0, ..4], small: Some(2) }, TS{ big: [0, ..4], small: Some(1) }]);
assert!(v2.next_permutation());
assert_eq!(v2, &mut[TS{ big: [0, ..4], small: Some(1) }, TS{ big: [0, ..4], small: Some(0) }, TS{ big: [0, ..4], small: Some(2) }]);
}
#[test]
fn test_euler_24() {
// Project euler #24
let mut v : &mut[int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
for _ in range(0, 999999) {
v.next_permutation();
}
assert_eq!(v, &mut[2, 7, 8, 3, 9, 1, 5, 4, 6, 0]);
}
#[test]
fn test_string() {
let s = "ABCD";
let mut tmp = s.chars().collect::<Vec<char>>();
let mut t = tmp.as_mut_slice();
assert!(t.prev_permutation() == false);
assert!(t.next_permutation());
assert_eq!(t, &mut['A', 'B', 'D', 'C']);
assert!(t.prev_permutation());
assert_eq!(t, &mut['A', 'B', 'C', 'D']);
assert!(t.next_permutation());
assert_eq!(t, &mut['A', 'B', 'D', 'C']);
assert!(t.next_permutation());
assert_eq!(t, &mut['A', 'C', 'B', 'D']);
assert!(t.next_permutation());
assert_eq!(t, &mut['A', 'C', 'D', 'B']);
assert!(t.prev_permutation());
assert_eq!(t, &mut['A', 'C', 'B', 'D']);
}
fn test_all_permutations(input: &mut &mut[int]) {
// This was designed as a test for prev_permutation, after it was known that
// next_permutation worked. It generates *and stores* all permutations of the input,
// in two ways, and verifies that they generate the same result.
// Only useable for inputs of length 10 or less, approximately.
// (That's 2*10! = 7.25 million permutations, each stored, each at least 80 bytes on 64-bit.)
// Length 11 would be 2*11! = 79.8 million, meaning at LEAST 2*11!*11*8 = 6.5 GiB RAM used.
// unshift() is probably the main reason it gets slow, however.
let mut x = Vec::from_slice(*input);
x.sort();
let mut y = x.clone(); y.reverse();
let mut forward = x.as_mut_slice();
let mut backward = y.as_mut_slice();
let mut results_forward : Vec<Vec<int>> = Vec::new();
let mut results_backward : Vec<Vec<int>> = Vec::new();
loop {
results_forward.push(Vec::from_slice(forward));
if !forward.next_permutation() { break; }
}
loop {
results_backward.push(Vec::from_slice(backward));
if !backward.prev_permutation() { break; }
}
results_backward.reverse();
assert_eq!(results_forward.len(), results_backward.len()); /* makes for much better error output when this is the case */
assert_eq!(results_forward, results_backward);
}
#[bench]
fn bench_my_permutations_bulk(b: &mut test::test::Bencher) {
b.iter(|| { let mut data : &mut[int] = &mut[0, 1, 2, 3, 4, 5, 6]; while data.next_permutation() { } });
}
#[bench]
fn bench_rust_permutations_bulk(b: &mut test::test::Bencher) {
b.iter(|| { let mut data : &mut[int] = &mut[0, 1, 2, 3, 4, 5, 6]; for _ in data.permutations() { } } );
}
#[bench]
fn bench_my_permutations_one_iter(b: &mut test::test::Bencher) {
let mut data : &mut[int] = &mut[0, 0, 0, 1, 1, 1, 3, 3, 4, 5, 6, 7, 8, 8, 9];
b.iter(|| { data.next_permutation(); });
}
#[bench]
fn bench_rust_permutations_one_iter(b: &mut test::test::Bencher) {
let mut data : &mut[int] = &mut[0, 0, 0, 1, 1, 1, 3, 3, 4, 5, 6, 7, 8, 8, 9];
let mut p = data.permutations();
b.iter(|| { p.next(); });
}
fn infinite_loop_random_test() {
let mut rng = task_rng();
let mut num_passed : uint = 0;
loop {
let mut v : Vec<int> = Vec::new();
// create a random vec, of random length
for _ in range(0, rng.gen_range(3u, 8)) {
v.push(rng.gen_range(0, 14));
}
v.sort();
test_all_permutations(&mut v.as_mut_slice());
num_passed += 1;
println!("{} random tests passed", num_passed);
}
}
fn main() {
infinite_loop_random_test();
}