PageRenderTime 15ms CodeModel.GetById 1ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/run-pass/binops.rs

http://github.com/jruderman/rust
Rust | 162 lines | 136 code | 21 blank | 5 comment | 40 complexity | 21c92a84548487f36fa095467a356f0b MD5 | raw file
  1// Binop corner cases
  2
  3use std;
  4import unsafe::reinterpret_cast;
  5import task;
  6import comm;
  7
  8fn test_nil() {
  9    assert (() == ());
 10    assert (!(() != ()));
 11    assert (!(() < ()));
 12    assert (() <= ());
 13    assert (!(() > ()));
 14    assert (() >= ());
 15}
 16
 17fn test_bool() {
 18    assert (!(true < false));
 19    assert (!(true <= false));
 20    assert (true > false);
 21    assert (true >= false);
 22
 23    assert (false < true);
 24    assert (false <= true);
 25    assert (!(false > true));
 26    assert (!(false >= true));
 27
 28    // Bools support bitwise binops
 29    assert (false & false == false);
 30    assert (true & false == false);
 31    assert (true & true == true);
 32    assert (false | false == false);
 33    assert (true | false == true);
 34    assert (true | true == true);
 35    assert (false ^ false == false);
 36    assert (true ^ false == true);
 37    assert (true ^ true == false);
 38}
 39
 40fn test_char() {
 41    let ch10 = 10 as char;
 42    let ch4 = 4 as char;
 43    let ch2 = 2 as char;
 44    assert (ch10 + ch4 == 14 as char);
 45    assert (ch10 - ch4 == 6 as char);
 46    assert (ch10 * ch4 == 40 as char);
 47    assert (ch10 / ch4 == ch2);
 48    assert (ch10 % ch4 == ch2);
 49    assert (ch10 >> ch2 == ch2);
 50    assert (ch10 << ch4 == 160 as char);
 51    assert (ch10 | ch4 == 14 as char);
 52    assert (ch10 & ch2 == ch2);
 53    assert (ch10 ^ ch2 == 8 as char);
 54}
 55
 56fn test_box() {
 57    assert (@10 == @10);
 58    assert (@{a: 1, b: 3} < @{a: 1, b: 4});
 59    assert (@{a: 'x'} != @{a: 'y'});
 60}
 61
 62fn test_port() {
 63    let p1 = comm::port::<int>();
 64    let p2 = comm::port::<int>();
 65
 66    assert (p1 == p1);
 67    assert (p1 != p2);
 68}
 69
 70fn test_chan() {
 71    let p: comm::port<int> = comm::port();
 72    let ch1 = comm::chan(p);
 73    let ch2 = comm::chan(p);
 74
 75    assert (ch1 == ch1);
 76    // Chans are equal because they are just task:port addresses.
 77    assert (ch1 == ch2);
 78}
 79
 80fn test_ptr() unsafe {
 81    let p1: *u8 = unsafe::reinterpret_cast(0);
 82    let p2: *u8 = unsafe::reinterpret_cast(0);
 83    let p3: *u8 = unsafe::reinterpret_cast(1);
 84
 85    assert p1 == p2;
 86    assert p1 != p3;
 87    assert p1 < p3;
 88    assert p1 <= p3;
 89    assert p3 > p1;
 90    assert p3 >= p3;
 91    assert p1 <= p2;
 92    assert p1 >= p2;
 93}
 94
 95fn test_fn() {
 96    fn f() { }
 97    fn g() { }
 98    fn h(_i: int) { }
 99    let f1 = f;
100    let f2 = f;
101    let g1 = g;
102    let h1 = h;
103    let h2 = h;
104    assert (f1 == f2);
105    assert (f1 == f);
106
107    assert (f1 != g1);
108    assert (h1 == h2);
109    assert (!(f1 != f2));
110    assert (!(h1 < h2));
111    assert (h1 <= h2);
112    assert (!(h1 > h2));
113    assert (h1 >= h2);
114}
115
116#[abi = "cdecl"]
117#[nolink]
118extern mod test {
119    fn rust_get_sched_id() -> libc::intptr_t;
120    fn get_task_id() -> libc::intptr_t;
121}
122
123fn test_foreign_fn() {
124    assert test::rust_get_sched_id != test::get_task_id;
125    assert test::rust_get_sched_id == test::rust_get_sched_id;
126}
127
128class p {
129  let mut x: int;
130  let mut y: int;
131  new(x: int, y: int) { self.x = x; self.y = y; }
132}
133
134fn test_class() {
135  let q = p(1, 2);
136  let r = p(1, 2);
137  
138  unsafe {
139  error!{"q = %x, r = %x",
140         (unsafe::reinterpret_cast::<*p, uint>(ptr::addr_of(q))),
141         (unsafe::reinterpret_cast::<*p, uint>(ptr::addr_of(r)))};
142  }
143  assert(q == r);
144  r.y = 17;
145  assert(r.y != q.y);
146  assert(r.y == 17);
147  assert(q != r);
148}
149
150fn main() {
151    test_nil();
152    test_bool();
153    test_char();
154    test_box();
155    // FIXME: test_port causes valgrind errors (#2724)
156    //test_port();
157    test_chan();
158    test_ptr();
159    test_fn();
160    test_foreign_fn();
161    test_class();
162}