2018-08-30 12:18:55 +00:00
|
|
|
// run-pass
|
2018-09-25 21:51:35 +00:00
|
|
|
#![allow(unused_mut)]
|
|
|
|
#![allow(unused_parens)]
|
2018-08-31 13:02:01 +00:00
|
|
|
#![allow(non_camel_case_types)]
|
|
|
|
|
2014-12-23 19:53:35 +00:00
|
|
|
use std::sync::mpsc::channel;
|
2013-05-25 02:35:29 +00:00
|
|
|
use std::cmp;
|
2011-03-22 01:13:08 +00:00
|
|
|
|
2011-06-15 18:19:50 +00:00
|
|
|
// Tests of ports and channels on various types
|
|
|
|
fn test_rec() {
|
2015-03-26 00:06:52 +00:00
|
|
|
struct R {val0: isize, val1: u8, val2: char}
|
2011-06-15 18:19:50 +00:00
|
|
|
|
2014-03-09 21:58:32 +00:00
|
|
|
let (tx, rx) = channel();
|
2015-03-03 08:42:26 +00:00
|
|
|
let r0: R = R {val0: 0, val1: 1, val2: '2'};
|
2014-12-23 19:53:35 +00:00
|
|
|
tx.send(r0).unwrap();
|
2013-01-26 06:46:32 +00:00
|
|
|
let mut r1: R;
|
2014-12-23 19:53:35 +00:00
|
|
|
r1 = rx.recv().unwrap();
|
2013-05-19 02:02:45 +00:00
|
|
|
assert_eq!(r1.val0, 0);
|
2015-03-03 08:42:26 +00:00
|
|
|
assert_eq!(r1.val1, 1);
|
2013-05-19 02:02:45 +00:00
|
|
|
assert_eq!(r1.val2, '2');
|
2011-03-22 01:13:08 +00:00
|
|
|
}
|
|
|
|
|
2011-04-19 20:35:49 +00:00
|
|
|
fn test_vec() {
|
2014-03-09 21:58:32 +00:00
|
|
|
let (tx, rx) = channel();
|
2016-10-29 21:54:04 +00:00
|
|
|
let v0: Vec<isize> = vec![0, 1, 2];
|
2014-12-23 19:53:35 +00:00
|
|
|
tx.send(v0).unwrap();
|
|
|
|
let v1 = rx.recv().unwrap();
|
2014-10-15 06:05:01 +00:00
|
|
|
assert_eq!(v1[0], 0);
|
|
|
|
assert_eq!(v1[1], 1);
|
|
|
|
assert_eq!(v1[2], 2);
|
2011-03-22 01:13:08 +00:00
|
|
|
}
|
|
|
|
|
2011-04-19 20:35:49 +00:00
|
|
|
fn test_str() {
|
2014-03-09 21:58:32 +00:00
|
|
|
let (tx, rx) = channel();
|
2014-05-25 10:10:11 +00:00
|
|
|
let s0 = "test".to_string();
|
2014-12-23 19:53:35 +00:00
|
|
|
tx.send(s0).unwrap();
|
|
|
|
let s1 = rx.recv().unwrap();
|
2014-06-20 01:22:33 +00:00
|
|
|
assert_eq!(s1.as_bytes()[0], 't' as u8);
|
|
|
|
assert_eq!(s1.as_bytes()[1], 'e' as u8);
|
|
|
|
assert_eq!(s1.as_bytes()[2], 's' as u8);
|
|
|
|
assert_eq!(s1.as_bytes()[3], 't' as u8);
|
2011-03-22 02:25:34 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 13:34:18 +00:00
|
|
|
#[derive(Debug)]
|
2012-08-27 23:26:35 +00:00
|
|
|
enum t {
|
|
|
|
tag1,
|
2015-03-26 00:06:52 +00:00
|
|
|
tag2(isize),
|
|
|
|
tag3(isize, u8, char)
|
2012-08-27 23:26:35 +00:00
|
|
|
}
|
|
|
|
|
2014-05-30 00:45:07 +00:00
|
|
|
impl cmp::PartialEq for t {
|
2013-03-22 18:23:21 +00:00
|
|
|
fn eq(&self, other: &t) -> bool {
|
2012-11-15 02:59:30 +00:00
|
|
|
match *self {
|
2014-11-06 08:05:53 +00:00
|
|
|
t::tag1 => {
|
2012-09-20 01:00:26 +00:00
|
|
|
match (*other) {
|
2014-11-06 08:05:53 +00:00
|
|
|
t::tag1 => true,
|
2012-08-27 23:26:35 +00:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
2014-11-06 08:05:53 +00:00
|
|
|
t::tag2(e0a) => {
|
2012-09-20 01:00:26 +00:00
|
|
|
match (*other) {
|
2014-11-06 08:05:53 +00:00
|
|
|
t::tag2(e0b) => e0a == e0b,
|
2012-08-27 23:26:35 +00:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
2014-11-06 08:05:53 +00:00
|
|
|
t::tag3(e0a, e1a, e2a) => {
|
2012-09-20 01:00:26 +00:00
|
|
|
match (*other) {
|
2014-11-06 08:05:53 +00:00
|
|
|
t::tag3(e0b, e1b, e2b) =>
|
2012-08-27 23:26:35 +00:00
|
|
|
e0a == e0b && e1a == e1b && e2a == e2b,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-22 18:23:21 +00:00
|
|
|
fn ne(&self, other: &t) -> bool { !(*self).eq(other) }
|
2012-08-27 23:26:35 +00:00
|
|
|
}
|
|
|
|
|
2011-04-19 20:35:49 +00:00
|
|
|
fn test_tag() {
|
2014-03-09 21:58:32 +00:00
|
|
|
let (tx, rx) = channel();
|
2014-12-23 19:53:35 +00:00
|
|
|
tx.send(t::tag1).unwrap();
|
|
|
|
tx.send(t::tag2(10)).unwrap();
|
2015-03-03 08:42:26 +00:00
|
|
|
tx.send(t::tag3(10, 11, 'A')).unwrap();
|
2012-03-22 15:39:41 +00:00
|
|
|
let mut t1: t;
|
2014-12-23 19:53:35 +00:00
|
|
|
t1 = rx.recv().unwrap();
|
2014-11-06 08:05:53 +00:00
|
|
|
assert_eq!(t1, t::tag1);
|
2014-12-23 19:53:35 +00:00
|
|
|
t1 = rx.recv().unwrap();
|
2014-11-06 08:05:53 +00:00
|
|
|
assert_eq!(t1, t::tag2(10));
|
2014-12-23 19:53:35 +00:00
|
|
|
t1 = rx.recv().unwrap();
|
2015-03-03 08:42:26 +00:00
|
|
|
assert_eq!(t1, t::tag3(10, 11, 'A'));
|
2011-03-22 01:13:08 +00:00
|
|
|
}
|
|
|
|
|
2011-04-19 20:35:49 +00:00
|
|
|
fn test_chan() {
|
2014-03-09 21:58:32 +00:00
|
|
|
let (tx1, rx1) = channel();
|
|
|
|
let (tx2, rx2) = channel();
|
2014-12-23 19:53:35 +00:00
|
|
|
tx1.send(tx2).unwrap();
|
|
|
|
let tx2 = rx1.recv().unwrap();
|
2011-06-15 18:19:50 +00:00
|
|
|
// Does the transmitted channel still work?
|
|
|
|
|
2014-12-23 19:53:35 +00:00
|
|
|
tx2.send(10).unwrap();
|
2015-03-26 00:06:52 +00:00
|
|
|
let mut i: isize;
|
2014-12-23 19:53:35 +00:00
|
|
|
i = rx2.recv().unwrap();
|
2013-05-19 02:02:45 +00:00
|
|
|
assert_eq!(i, 10);
|
2011-03-22 01:13:08 +00:00
|
|
|
}
|
|
|
|
|
2013-02-02 03:43:17 +00:00
|
|
|
pub fn main() {
|
2012-07-25 21:05:06 +00:00
|
|
|
test_rec();
|
|
|
|
test_vec();
|
|
|
|
test_str();
|
|
|
|
test_tag();
|
|
|
|
test_chan();
|
|
|
|
}
|