/src/test/run-pass/task-comm-16.rs

http://github.com/jruderman/rust · Rust · 79 lines · 67 code · 9 blank · 3 comment · 14 complexity · 929b92dea327afd7932d7de94b525362 MD5 · raw file

  1. // -*- rust -*-
  2. use std;
  3. import pipes;
  4. import pipes::send;
  5. import pipes::port;
  6. import pipes::recv;
  7. import pipes::chan;
  8. // Tests of ports and channels on various types
  9. fn test_rec() {
  10. type r = {val0: int, val1: u8, val2: char};
  11. let (ch, po) = pipes::stream();
  12. let r0: r = {val0: 0, val1: 1u8, val2: '2'};
  13. ch.send(r0);
  14. let mut r1: r;
  15. r1 = po.recv();
  16. assert (r1.val0 == 0);
  17. assert (r1.val1 == 1u8);
  18. assert (r1.val2 == '2');
  19. }
  20. fn test_vec() {
  21. let (ch, po) = pipes::stream();
  22. let v0: ~[int] = ~[0, 1, 2];
  23. ch.send(v0);
  24. let v1 = po.recv();
  25. assert (v1[0] == 0);
  26. assert (v1[1] == 1);
  27. assert (v1[2] == 2);
  28. }
  29. fn test_str() {
  30. let (ch, po) = pipes::stream();
  31. let s0 = ~"test";
  32. ch.send(s0);
  33. let s1 = po.recv();
  34. assert (s1[0] == 't' as u8);
  35. assert (s1[1] == 'e' as u8);
  36. assert (s1[2] == 's' as u8);
  37. assert (s1[3] == 't' as u8);
  38. }
  39. fn test_tag() {
  40. enum t { tag1, tag2(int), tag3(int, u8, char), }
  41. let (ch, po) = pipes::stream();
  42. ch.send(tag1);
  43. ch.send(tag2(10));
  44. ch.send(tag3(10, 11u8, 'A'));
  45. let mut t1: t;
  46. t1 = po.recv();
  47. assert (t1 == tag1);
  48. t1 = po.recv();
  49. assert (t1 == tag2(10));
  50. t1 = po.recv();
  51. assert (t1 == tag3(10, 11u8, 'A'));
  52. }
  53. fn test_chan() {
  54. let (ch, po) = pipes::stream();
  55. let (ch0, po0) = pipes::stream();
  56. ch.send(ch0);
  57. let ch1 = po.recv();
  58. // Does the transmitted channel still work?
  59. ch1.send(10);
  60. let mut i: int;
  61. i = po0.recv();
  62. assert (i == 10);
  63. }
  64. fn main() {
  65. test_rec();
  66. test_vec();
  67. test_str();
  68. test_tag();
  69. test_chan();
  70. }