-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathtest_strings.rs
391 lines (334 loc) · 9.41 KB
/
test_strings.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#![feature(is_sorted)]
#![allow(non_snake_case)]
use algo::strings::palindrome;
use algo::strings::{brute_force, Quick3String, Quick3Way, TrieST, KMP, LSD, MSD, TST};
use std::collections::HashMap;
const WORDS3: &'static str = include_str!("../res/strings/words3.txt");
const SHELLS: &'static str = include_str!("../res/strings/shells.txt");
const SHELLS_ST: &'static str = include_str!("../res/strings/shellsST.txt");
#[test]
fn LSD_radix_sort() {
let i = WORDS3;
let mut a = extract_words(i);
let w = a[0].len();
LSD::sort(&mut a, w);
assert!(a.is_sorted());
// license plate data
let mut a = vec![
"4PGC938", "2IYE230", "3CIO720", "1ICK750", "1OHV845", "4JZY524", "1ICK750", "3CIO720",
"1OHV845", "1OHV845", "2RLA629", "2RLA629", "3ATW723",
];
let w = a[0].len();
LSD::sort(&mut a, w);
assert!(a.is_sorted());
}
#[test]
fn LSD_radix_sort_i32() {
let mut a: Vec<i32> = (0..10).rev().collect();
LSD::sort_i32(&mut a);
assert!(a.is_sorted());
let mut a = vec![1, 2, 3, -1, -2, -3];
LSD::sort_i32(&mut a);
assert!(a.is_sorted());
}
#[test]
fn MSD_radix_sort() {
// empty
let mut data: Vec<&str> = vec![];
MSD::sort(&mut data);
assert!(data.is_sorted());
// normal
let i = SHELLS;
let mut data = extract_words(i);
MSD::sort(&mut data);
assert!(data.is_sorted());
}
#[test]
fn quick3str() {
// empty
let mut data: Vec<&str> = vec![];
Quick3String::sort(&mut data);
assert!(data.is_sorted());
// normal
let i = SHELLS;
let mut data = extract_words(i);
Quick3String::sort(&mut data);
assert!(data.is_sorted());
}
#[test]
fn quick3way() {
// empty
let mut data: Vec<&str> = vec![];
Quick3Way::sort(&mut data);
assert!(data.is_sorted());
// normal
let i = SHELLS;
let mut data = extract_words(i);
Quick3Way::sort(&mut data);
assert!(data.is_sorted());
}
// also fine for sorted data
#[test]
fn sorted_data() {
let mut a = vec![
"4PGC938", "2IYE230", "3CIO720", "1ICK750", "1OHV845", "4JZY524", "1ICK750", "3CIO720",
"1OHV845", "1OHV845", "2RLA629", "2RLA629", "3ATW723",
];
a.sort();
// lsd
let w = a[0].len();
LSD::sort(&mut a, w);
assert!(a.is_sorted());
// msd
MSD::sort(&mut a);
assert!(a.is_sorted());
// three-way quick
Quick3String::sort(&mut a);
assert!(a.is_sorted());
// lsd32
let mut a: Vec<i32> = (0..10).collect();
LSD::sort_i32(&mut a);
assert!(a.is_sorted());
}
#[test]
fn trie_st() {
let mut st = TrieST::default();
// test len & empty
assert!(st.is_empty());
assert_eq!(0, st.len());
let i = SHELLS_ST;
let mut hm = HashMap::new();
let a = extract_words(i);
for (i, &s) in a.iter().enumerate() {
hm.insert(s, i);
// test put
st.put(s, Some(i));
}
// test len & empty
assert!(!st.is_empty());
assert_eq!(7, st.len());
for (&k, v) in hm.iter() {
// test get & contains
assert_eq!(st.get(k), Some(v));
assert!(st.contains(k));
}
// test keys
// TrieST keys contained in HashMap
let keys = st.keys();
assert_eq!(hm.keys().len(), st.len());
for k in keys.iter() {
assert!(hm.contains_key(k.as_str()));
}
// HashMap keys contained in TrieST
let keys = hm.keys();
for &k in keys {
assert!(st.contains(k));
}
// test keys_with_prefix
let mut matches = st.keys_with_prefix("shor");
assert_eq!(1, matches.len());
assert_eq!(Some("shore"), matches.dequeue().as_deref());
// test keys_that_match
let mut matches = st.keys_that_match(".he.l.");
assert_eq!(Some("shells"), matches.dequeue().as_deref());
// test longest_prefix_of
assert_eq!(Some("shells"), st.longest_prefix_of("shellsort"));
assert_eq!(Some("she"), st.longest_prefix_of("shell"));
assert_eq!(None, st.longest_prefix_of("quicksort"));
// test delete
assert!(st.contains("shells"));
st.delete("shells");
assert_eq!(6, st.len());
assert!(!st.contains("shells"));
// test put(xx, None) can delete too
assert!(st.contains("shore"));
st.put("shore", None);
assert_eq!(5, st.len());
assert!(!st.contains("shore"));
}
#[test]
fn tst() {
let mut st = TST::default();
// test len & empty
assert!(st.is_empty());
assert_eq!(0, st.len());
let i = SHELLS_ST;
let mut hm = HashMap::new();
let a = extract_words(i);
for (i, &s) in a.iter().enumerate() {
hm.insert(s, i);
// test put
st.put(s, Some(i));
}
// test len & empty
assert!(!st.is_empty());
assert_eq!(7, st.len());
for (&k, v) in hm.iter() {
// test get & contains
assert_eq!(st.get(k), Some(v));
assert!(st.contains(k));
}
// test keys
// TST keys contained in HashMap
let keys = st.keys();
assert_eq!(hm.keys().len(), keys.len());
for k in keys.iter() {
assert!(hm.contains_key(k.as_str()));
}
// HashMap keys contained in TST
let keys = hm.keys();
for &k in keys {
assert!(st.contains(k));
}
// test keys_with_prefix
let mut matches = st.keys_with_prefix("shor");
assert_eq!(1, matches.len());
assert_eq!(Some("shore"), matches.dequeue().as_deref());
// test keys_that_match
let mut matches = st.keys_that_match(".he.l.");
assert_eq!(Some("shells"), matches.dequeue().as_deref());
// test longest_prefix_of
assert_eq!(Some("shells"), st.longest_prefix_of("shellsort"));
assert_eq!(Some("she"), st.longest_prefix_of("shell"));
assert_eq!(None, st.longest_prefix_of("quicksort"));
// test delete
assert!(st.contains("shells"));
st.put("shells", None);
assert_eq!(6, st.len());
assert!(!st.contains("shells"));
}
#[test]
fn trie_st_drop() {
use algo::common::drop::{self, Elem};
let mut st = TrieST::default();
drop::with(|ctx| {
st.put("aaa", Some(Elem));
st.put("bbb", Some(Elem));
st.put("ccc", Some(Elem));
st.put("ddd", Some(Elem));
drop(st);
assert_eq!(4, ctx.get());
});
// test overwrite "aaa"
let mut st = TrieST::default();
drop::with(|ctx| {
st.put("aaa", Some(Elem));
st.put("bbb", Some(Elem));
st.put("ccc", Some(Elem));
st.put("ddd", Some(Elem));
assert_eq!(0, ctx.get());
st.put("aaa", Some(Elem)); // do overwrite
assert_eq!(1, ctx.get());
drop(st);
assert_eq!(5, ctx.get());
});
}
#[test]
fn trie_st_drop_with_delete() {
use algo::common::drop::{self, Elem};
let mut st = TrieST::default();
drop::with(|ctx| {
st.put("aaa", Some(Elem));
st.put("bbb", Some(Elem));
st.put("ccc", Some(Elem));
st.put("ddd", Some(Elem));
st.delete("aaa");
st.delete("bbb");
assert_eq!(2, ctx.get());
drop(st);
assert_eq!(4, ctx.get());
});
}
#[test]
fn trie_st_drop_with_put() {
use algo::common::drop::{self, Elem};
let mut st = TrieST::default();
drop::with(|ctx| {
st.put("aaa", Some(Elem));
st.put("bbb", Some(Elem));
st.put("ccc", Some(Elem));
st.put("ddd", Some(Elem));
st.put("aaa", None);
st.put("bbb", None);
assert_eq!(2, ctx.get());
drop(st);
assert_eq!(4, ctx.get());
});
}
#[test]
fn tst_drop() {
use algo::common::drop::{self, Elem};
let mut st = TST::default();
drop::with(|ctx| {
st.put("aaa", Some(Elem));
st.put("bbb", Some(Elem));
st.put("ccc", Some(Elem));
st.put("ddd", Some(Elem));
// do drop
drop(st);
assert_eq!(4, ctx.get());
});
}
#[test]
fn tst_drop_with_put_none() {
use algo::common::drop::{self, Elem};
// init
let mut st = TST::default();
drop::with(|ctx| {
st.put("aaa", Some(Elem));
st.put("bbb", Some(Elem));
st.put("ccc", Some(Elem));
// do drops
st.put("aaa", None);
assert_eq!(1, ctx.get());
st.put("bbb", None);
assert_eq!(2, ctx.get());
st.put("ccc", None);
assert_eq!(3, ctx.get());
});
}
#[test]
fn brute_force_search() {
let data = substr_data();
for (pat, txt, pos) in data {
assert_eq!(pos, brute_force::search1(pat, txt));
assert_eq!(pos, brute_force::search2(pat, txt));
}
}
#[test]
fn kmp() {
let data = substr_data();
for (pat, txt, pos) in data {
let kmp = KMP::from(pat);
assert_eq!(pos, kmp.search(txt));
}
}
#[test]
fn palindrome() {
assert!(palindrome::is_palindrome(""));
assert!(palindrome::is_palindrome("a"));
assert!(palindrome::is_palindrome("aba"));
assert!(palindrome::is_palindrome("abba"));
assert!(!palindrome::is_palindrome("abc"));
assert!(!palindrome::is_palindrome("abab"));
}
fn extract_words(i: &str) -> Vec<&str> {
i.split_whitespace().collect()
}
fn substr_data() -> Vec<(&'static str, &'static str, Option<usize>)> {
vec![
(
"abracadabra",
"abacadabrabracabracadabrabrabracad",
Some(14),
),
("rab", "abacadabrabracabracadabrabrabracad", Some(8)),
("bcara", "abacadabrabracabracadabrabrabracad", None),
(
"rabrabracad",
"abacadabrabracabracadabrabrabracad",
Some(23),
),
("abacad", "abacadabrabracabracadabrabrabracad", Some(0)),
]
}