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
pub mod cb;
pub mod spsc;

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn option_i32() {
    let (mut tx, mut _rx) = spsc::channel(2);
    tx.put( |v| *v = Some(1) );
  }

  #[test]
  fn string_literal() {
    let (mut tx, mut _rx) = spsc::channel(2);
    tx.put( |v| *v = Some("world") );
  }

  #[test]
  fn boxed_string_obj() {
    let (mut tx, mut _rx) = spsc::channel(2);
    tx.put( |v| *v = Some(String::from("world")) );
  }

  #[test]
  fn with_spawn() {
    use std::thread;
    let (mut tx, mut rx) = spsc::channel(2);
    let t = thread::spawn(move|| {
      for i in 1..4 {
        tx.put(|v| *v = Some(i));
      }
    });
    t.join().unwrap();
    let sum = rx.iter().fold(0, |acc, num| acc + num);
    assert_eq!(sum, 5);
  }

  #[test]
  fn at_most_once() {
    let (mut tx, mut rx) = spsc::channel(20);
    tx.put(|v| *v = Some(1));
    tx.put(|v| *v = Some(2));
    tx.put(|v| *v = Some(3));
    {
      // first iterator processes the single element
      // assumes I process everything else in the iterator
      let mut it = rx.iter();
      assert_eq!(Some(1), it.next());
    }
    {
      // the second iterator gets nothing, since the first
      // iterator received the whole range no matter if it
      // has really called a next on them ot not
      let mut it = rx.iter();
      assert_eq!(None, it.next());
    }
  }

  #[test]
  fn range() {
    use std::thread;
    use cb::IterRange;

    let (mut tx, mut rx) = spsc::channel(2);
    let t = thread::spawn(move|| {
      for i in 1..4 {
        tx.put(|v| *v = Some(i));
      }
    });
    t.join().unwrap();
    let i = rx.iter();
    let (from,to) = i.get_range();
    assert_eq!(from, 1);
    assert_eq!(to, 3);
    assert_eq!(Some(1),i.next_id());
  }
}