]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/const-int-overflowing.rs
Auto merge of #53409 - GuillaumeGomez:associated-const-value, r=QuietMisdreavus
[rust.git] / src / test / run-pass / const-int-overflowing.rs
1 // Copyright 2018 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 #![feature(const_int_overflowing)]
12
13 const ADD_A: (u32, bool) = 5u32.overflowing_add(2);
14 const ADD_B: (u32, bool) = u32::max_value().overflowing_add(1);
15
16 const SUB_A: (u32, bool) = 5u32.overflowing_sub(2);
17 const SUB_B: (u32, bool) = 0u32.overflowing_sub(1);
18
19 const MUL_A: (u32, bool) = 5u32.overflowing_mul(2);
20 const MUL_B: (u32, bool) = 1_000_000_000u32.overflowing_mul(10);
21
22 const SHL_A: (u32, bool) = 0x1u32.overflowing_shl(4);
23 const SHL_B: (u32, bool) = 0x1u32.overflowing_shl(132);
24
25 const SHR_A: (u32, bool) = 0x10u32.overflowing_shr(4);
26 const SHR_B: (u32, bool) = 0x10u32.overflowing_shr(132);
27
28 fn ident<T>(ident: T) -> T {
29     ident
30 }
31
32 fn main() {
33     assert_eq!(ADD_A, ident((7, false)));
34     assert_eq!(ADD_B, ident((0, true)));
35
36     assert_eq!(SUB_A, ident((3, false)));
37     assert_eq!(SUB_B, ident((u32::max_value(), true)));
38
39     assert_eq!(MUL_A, ident((10, false)));
40     assert_eq!(MUL_B, ident((1410065408, true)));
41
42     assert_eq!(SHL_A, ident((0x10, false)));
43     assert_eq!(SHL_B, ident((0x10, true)));
44
45     assert_eq!(SHR_A, ident((0x1, false)));
46     assert_eq!(SHR_B, ident((0x1, true)));
47 }