1 //! Randomized tests to try to catch crlf seam errors.
2
3 extern crate rand;
4 extern crate ropey;
5
6 use rand::Rng;
7 use ropey::Rope;
8
9 #[test]
crlf_inserts()10 fn crlf_inserts() {
11 let mut rng = rand::thread_rng();
12 let mut tree = Rope::new();
13
14 // Do a bunch of random incoherent inserts of CRLF
15 // pairs.
16 for _ in 0..(1 << 12) {
17 let len = tree.len_chars().max(1);
18 tree.insert(rng.gen::<usize>() % len, "\r\n\r\n");
19 tree.insert(rng.gen::<usize>() % len, "\n\r\n\r");
20 tree.insert(rng.gen::<usize>() % len, "\r\n\r\n");
21 tree.insert(rng.gen::<usize>() % len, "\n\r\n\r");
22 tree.insert(rng.gen::<usize>() % len, "\r\n\r\n");
23 tree.insert(rng.gen::<usize>() % len, "こんいちは、");
24 tree.insert(rng.gen::<usize>() % len, "\n\r\n\r");
25 tree.insert(rng.gen::<usize>() % len, "\r\n\r\n");
26 tree.insert(rng.gen::<usize>() % len, "\n\r\n\r");
27 tree.insert(rng.gen::<usize>() % len, "\r\n\r\n");
28 tree.insert(rng.gen::<usize>() % len, "\n\r\n\r");
29 tree.insert(rng.gen::<usize>() % len, "みんなさん!");
30
31 // Make sure the tree is sound
32 tree.assert_invariants();
33 }
34 }
35
36 #[test]
crlf_removals()37 fn crlf_removals() {
38 let mut rng = rand::thread_rng();
39 let mut tree = Rope::new();
40
41 // Build tree.
42 for _ in 0..(1 << 9) {
43 let len = tree.len_chars().max(1);
44 tree.insert(rng.gen::<usize>() % len, "\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\nこんいちは、\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\nこんいちは、r\n\r\n\r\n\r\nみんなさん!\n\r\n\r\n\r\nこんいちは、\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\nみんなさん!\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\r\n\r\n\r\n\r\n\r\n\r\nみんなさん!\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\rみんなさん!\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r");
45 }
46
47 // Do a bunch of random incoherent removals
48 for _ in 0..(1 << 11) {
49 let start = rng.gen::<usize>() % tree.len_chars().max(1);
50 let end = (start + 5).min(tree.len_chars());
51 tree.remove(start..end);
52
53 let start = rng.gen::<usize>() % tree.len_chars().max(1);
54 let end = (start + 9).min(tree.len_chars());
55 tree.remove(start..end);
56
57 // Make sure the tree is sound
58 tree.assert_invariants();
59 }
60 }
61