]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/u128.rs
Rollup merge of #39212 - redox-os:master, r=brson
[rust.git] / src / test / run-pass / u128.rs
1 // Copyright 2016 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 // ignore-stage0
12 // ignore-stage1
13
14 // ignore-emscripten
15
16 #![feature(i128_type, test)]
17
18 extern crate test;
19 use test::black_box as b;
20
21 fn main() {
22     let x: u128 = 0xFFFF_FFFF_FFFF_FFFF__FFFF_FFFF_FFFF_FFFF;
23     assert_eq!(0, !x);
24     assert_eq!(0, !x);
25     let y: u128 = 0xFFFF_FFFF_FFFF_FFFF__FFFF_FFFF_FFFF_FFFE;
26     assert_eq!(!1, y);
27     assert_eq!(x, y | 1);
28     assert_eq!(0xFAFF_0000_FF8F_0000__FFFF_0000_FFFF_FFFE,
29                y &
30                0xFAFF_0000_FF8F_0000__FFFF_0000_FFFF_FFFF);
31     let z: u128 = 0xABCD_EF;
32     assert_eq!(z * z, 0x734C_C2F2_A521);
33     assert_eq!(z * z * z * z, 0x33EE_0E2A_54E2_59DA_A0E7_8E41);
34     assert_eq!(z + z + z + z, 0x2AF3_7BC);
35     let k: u128 = 0x1234_5678_9ABC_DEFF_EDCB_A987_6543_210;
36     assert_eq!(k + k, 0x2468_ACF1_3579_BDFF_DB97_530E_CA86_420);
37     assert_eq!(0, k - k);
38     assert_eq!(0x1234_5678_9ABC_DEFF_EDCB_A987_5A86_421, k - z);
39     assert_eq!(0x1000_0000_0000_0000_0000_0000_0000_000,
40                k - 0x234_5678_9ABC_DEFF_EDCB_A987_6543_210);
41     assert_eq!(0x6EF5_DE4C_D3BC_2AAA_3BB4_CC5D_D6EE_8, k / 42);
42     assert_eq!(0, k % 42);
43     assert_eq!(15, z % 42);
44     assert_eq!(0x169D_A8020_CEC18, k % 0x3ACB_FE49_FF24_AC);
45     assert_eq!(0x91A2_B3C4_D5E6_F7, k >> 65);
46     assert_eq!(0xFDB9_7530_ECA8_6420_0000_0000_0000_0000, k << 65);
47     assert!(k > z);
48     assert!(y > k);
49     assert!(y < x);
50     assert_eq!(x as u64, !0);
51     assert_eq!(z as u64, 0xABCD_EF);
52     assert_eq!(k as u64, 0xFEDC_BA98_7654_3210);
53     assert_eq!(k as i128, 0x1234_5678_9ABC_DEFF_EDCB_A987_6543_210);
54     assert_eq!((z as f64) as u128, z);
55     assert_eq!((z as f32) as u128, z);
56     assert_eq!((z as f64 * 16.0) as u128, z * 16);
57     assert_eq!((z as f32 * 16.0) as u128, z * 16);
58     let l :u128 = 432 << 100;
59     assert_eq!((l as f32) as u128, l);
60     assert_eq!((l as f64) as u128, l);
61     // formatting
62     let j: u128 = 1 << 67;
63     assert_eq!("147573952589676412928", format!("{}", j));
64     assert_eq!("80000000000000000", format!("{:x}", j));
65     assert_eq!("20000000000000000000000", format!("{:o}", j));
66     assert_eq!("10000000000000000000000000000000000000000000000000000000000000000000",
67                format!("{:b}", j));
68     assert_eq!("340282366920938463463374607431768211455",
69         format!("{}", u128::max_value()));
70     assert_eq!("147573952589676412928", format!("{:?}", j));
71     // common traits
72     assert_eq!(x, b(x.clone()));
73     // overflow checks
74     assert_eq!((z).checked_mul(z), Some(0x734C_C2F2_A521));
75     assert_eq!((k).checked_mul(k), None);
76     let l: u128 = b(u128::max_value() - 10);
77     let o: u128 = b(17);
78     assert_eq!(l.checked_add(b(11)), None);
79     assert_eq!(l.checked_sub(l), Some(0));
80     assert_eq!(o.checked_sub(b(18)), None);
81     assert_eq!(b(1u128).checked_shl(b(127)), Some(1 << 127));
82     assert_eq!(o.checked_shl(b(128)), None);
83 }