]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/binops.rs
auto merge of #19648 : mquandalle/rust/patch-1, r=alexcrichton
[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 fn test_nil() {
14     assert_eq!((), ());
15     assert!((!(() != ())));
16     assert!((!(() < ())));
17     assert!((() <= ()));
18     assert!((!(() > ())));
19     assert!((() >= ()));
20 }
21
22 fn test_bool() {
23     assert!((!(true < false)));
24     assert!((!(true <= false)));
25     assert!((true > false));
26     assert!((true >= false));
27
28     assert!((false < true));
29     assert!((false <= true));
30     assert!((!(false > true)));
31     assert!((!(false >= true)));
32
33     // Bools support bitwise binops
34     assert_eq!(false & false, false);
35     assert_eq!(true & false, false);
36     assert_eq!(true & true, true);
37     assert_eq!(false | false, false);
38     assert_eq!(true | false, true);
39     assert_eq!(true | true, true);
40     assert_eq!(false ^ false, false);
41     assert_eq!(true ^ false, true);
42     assert_eq!(true ^ true, false);
43 }
44
45 fn test_ptr() {
46     unsafe {
47         let p1: *const u8 = ::std::mem::transmute(0u);
48         let p2: *const u8 = ::std::mem::transmute(0u);
49         let p3: *const u8 = ::std::mem::transmute(1u);
50
51         assert_eq!(p1, p2);
52         assert!(p1 != p3);
53         assert!(p1 < p3);
54         assert!(p1 <= p3);
55         assert!(p3 > p1);
56         assert!(p3 >= p3);
57         assert!(p1 <= p2);
58         assert!(p1 >= p2);
59     }
60 }
61
62 #[deriving(PartialEq, Show)]
63 struct p {
64   x: int,
65   y: int,
66 }
67
68 fn p(x: int, y: int) -> p {
69     p {
70         x: x,
71         y: y
72     }
73 }
74
75 fn test_class() {
76   let q = p(1, 2);
77   let mut r = p(1, 2);
78
79   unsafe {
80   println!("q = {:x}, r = {:x}",
81          (::std::mem::transmute::<*const p, uint>(&q)),
82          (::std::mem::transmute::<*const p, uint>(&r)));
83   }
84   assert_eq!(q, r);
85   r.y = 17;
86   assert!((r.y != q.y));
87   assert_eq!(r.y, 17);
88   assert!((q != r));
89 }
90
91 pub fn main() {
92     test_nil();
93     test_bool();
94     test_ptr();
95     test_class();
96 }