]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/binops.rs
9c1b64004a2bfe148dec7a5d3251fa8578b44194
[rust.git] / src / test / run-pass / binops.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Binop corner cases
12
13 #![feature(managed_boxes)]
14
15 use std::gc::GC;
16
17 fn test_nil() {
18     assert_eq!((), ());
19     assert!((!(() != ())));
20     assert!((!(() < ())));
21     assert!((() <= ()));
22     assert!((!(() > ())));
23     assert!((() >= ()));
24 }
25
26 fn test_bool() {
27     assert!((!(true < false)));
28     assert!((!(true <= false)));
29     assert!((true > false));
30     assert!((true >= false));
31
32     assert!((false < true));
33     assert!((false <= true));
34     assert!((!(false > true)));
35     assert!((!(false >= true)));
36
37     // Bools support bitwise binops
38     assert_eq!(false & false, false);
39     assert_eq!(true & false, false);
40     assert_eq!(true & true, true);
41     assert_eq!(false | false, false);
42     assert_eq!(true | false, true);
43     assert_eq!(true | true, true);
44     assert_eq!(false ^ false, false);
45     assert_eq!(true ^ false, true);
46     assert_eq!(true ^ true, false);
47 }
48
49 fn test_box() {
50     assert_eq!(box(GC) 10, box(GC) 10);
51 }
52
53 fn test_ptr() {
54     unsafe {
55         let p1: *u8 = ::std::mem::transmute(0);
56         let p2: *u8 = ::std::mem::transmute(0);
57         let p3: *u8 = ::std::mem::transmute(1);
58
59         assert_eq!(p1, p2);
60         assert!(p1 != p3);
61         assert!(p1 < p3);
62         assert!(p1 <= p3);
63         assert!(p3 > p1);
64         assert!(p3 >= p3);
65         assert!(p1 <= p2);
66         assert!(p1 >= p2);
67     }
68 }
69
70 #[deriving(PartialEq, Show)]
71 struct p {
72   x: int,
73   y: int,
74 }
75
76 fn p(x: int, y: int) -> p {
77     p {
78         x: x,
79         y: y
80     }
81 }
82
83 fn test_class() {
84   let q = p(1, 2);
85   let mut r = p(1, 2);
86
87   unsafe {
88   println!("q = {:x}, r = {:x}",
89          (::std::mem::transmute::<*p, uint>(&q)),
90          (::std::mem::transmute::<*p, uint>(&r)));
91   }
92   assert_eq!(q, r);
93   r.y = 17;
94   assert!((r.y != q.y));
95   assert_eq!(r.y, 17);
96   assert!((q != r));
97 }
98
99 pub fn main() {
100     test_nil();
101     test_bool();
102     test_box();
103     test_ptr();
104     test_class();
105 }