1 extern crate ropey;
2
3 use std::sync::mpsc;
4 use std::thread;
5
6 use std::iter::Iterator;
7
8 use ropey::Rope;
9
10 const TEXT: &str = include_str!("test_text.txt");
11
12 #[test]
clone_rope_to_thread()13 fn clone_rope_to_thread() {
14 let mut rope1 = Rope::from_str(TEXT);
15 let rope2 = rope1.clone();
16
17 // Spawn a thread for modifying the clone
18 let (tx1, rx1) = mpsc::channel::<Rope>();
19 let (tx2, rx2) = mpsc::channel::<Rope>();
20 thread::spawn(move || {
21 // Modify rope2
22 let mut rope = rx1.recv().unwrap();
23 rope.insert(432, "Hello ");
24 rope.insert(2345, "world! ");
25 rope.insert(5256, "How are ");
26 rope.insert(53, "you ");
27 rope.insert(768, "doing?\r\n");
28
29 // Send it back
30 tx2.send(rope).unwrap();
31
32 // Modify it again
33 let mut rope = rx1.recv().unwrap();
34 rope.insert(3891, "I'm doing fine, thanks!");
35 tx2.send(rope).unwrap();
36 });
37
38 // Send the clone to the other thread for modification
39 tx1.send(rope2).unwrap();
40
41 // Make identical modifications to rope1 as are being made
42 // to rope2 in the other thread.
43 rope1.insert(432, "Hello ");
44 rope1.insert(2345, "world! ");
45 rope1.insert(5256, "How are ");
46 rope1.insert(53, "you ");
47 rope1.insert(768, "doing?\r\n");
48
49 // Get rope2 back and make sure they match
50 let rope2 = rx2.recv().unwrap();
51 let matches = Iterator::zip(rope1.chars(), rope2.chars())
52 .map(|(a, b)| a == b)
53 .all(|n| n);
54 assert_eq!(matches, true);
55
56 // Send rope2 to the other thread again for more modifications.
57 tx1.send(rope2).unwrap();
58
59 // Get rope2 back again and make sure they don't match now.
60 let rope2 = rx2.recv().unwrap();
61 let matches = Iterator::zip(rope1.chars(), rope2.chars())
62 .map(|(a, b)| a == b)
63 .all(|n| n);
64 assert_eq!(matches, false);
65 }
66