]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/shift-near-oflo.rs
Merge tag 'upstream/1.5.0+dfsg1'
[rustc.git] / src / test / run-pass / shift-near-oflo.rs
1 // Copyright 2015 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 // compile-flags: -C debug-assertions
12
13 // Check that we do *not* overflow on a number of edge cases.
14 // (compare with test/run-fail/overflowing-{lsh,rsh}*.rs)
15
16 fn main() {
17 test_left_shift();
18 test_right_shift();
19 }
20
21 fn test_left_shift() {
22 // negative rhs can panic, but values in [0,N-1] are okay for iN
23
24 macro_rules! tests {
25 ($iN:ty, $uN:ty, $max_rhs:expr, $expect_i:expr, $expect_u:expr) => { {
26 let x = (1 as $iN) << 0;
27 assert_eq!(x, 1);
28 let x = (1 as $uN) << 0;
29 assert_eq!(x, 1);
30 let x = (1 as $iN) << $max_rhs;
31 assert_eq!(x, $expect_i);
32 let x = (1 as $uN) << $max_rhs;
33 assert_eq!(x, $expect_u);
34 // high-order bits on LHS are silently discarded without panic.
35 let x = (3 as $iN) << $max_rhs;
36 assert_eq!(x, $expect_i);
37 let x = (3 as $uN) << $max_rhs;
38 assert_eq!(x, $expect_u);
39 } }
40 }
41
42 let x = 1_i8 << 0;
43 assert_eq!(x, 1);
44 let x = 1_u8 << 0;
45 assert_eq!(x, 1);
46 let x = 1_i8 << 7;
47 assert_eq!(x, std::i8::MIN);
48 let x = 1_u8 << 7;
49 assert_eq!(x, 0x80);
50 // high-order bits on LHS are silently discarded without panic.
51 let x = 3_i8 << 7;
52 assert_eq!(x, std::i8::MIN);
53 let x = 3_u8 << 7;
54 assert_eq!(x, 0x80);
55
56 // above is (approximately) expanded from:
57 tests!(i8, u8, 7, std::i8::MIN, 0x80_u8);
58
59 tests!(i16, u16, 15, std::i16::MIN, 0x8000_u16);
60 tests!(i32, u32, 31, std::i32::MIN, 0x8000_0000_u32);
61 tests!(i64, u64, 63, std::i64::MIN, 0x8000_0000_0000_0000_u64);
62 }
63
64 fn test_right_shift() {
65 // negative rhs can panic, but values in [0,N-1] are okay for iN
66
67 macro_rules! tests {
68 ($iN:ty, $uN:ty, $max_rhs:expr,
69 $signbit_i:expr, $highbit_i:expr, $highbit_u:expr) =>
70 { {
71 let x = (1 as $iN) >> 0;
72 assert_eq!(x, 1);
73 let x = (1 as $uN) >> 0;
74 assert_eq!(x, 1);
75 let x = ($highbit_i) >> $max_rhs-1;
76 assert_eq!(x, 1);
77 let x = ($highbit_u) >> $max_rhs;
78 assert_eq!(x, 1);
79 // sign-bit is carried by arithmetic right shift
80 let x = ($signbit_i) >> $max_rhs;
81 assert_eq!(x, -1);
82 // low-order bits on LHS are silently discarded without panic.
83 let x = ($highbit_i + 1) >> $max_rhs-1;
84 assert_eq!(x, 1);
85 let x = ($highbit_u + 1) >> $max_rhs;
86 assert_eq!(x, 1);
87 let x = ($signbit_i + 1) >> $max_rhs;
88 assert_eq!(x, -1);
89 } }
90 }
91
92 tests!(i8, u8, 7, std::i8::MIN, 0x40_i8, 0x80_u8);
93 tests!(i16, u16, 15, std::i16::MIN, 0x4000_u16, 0x8000_u16);
94 tests!(i32, u32, 31, std::i32::MIN, 0x4000_0000_u32, 0x8000_0000_u32);
95 tests!(i64, u64, 63, std::i64::MIN,
96 0x4000_0000_0000_0000_u64, 0x8000_0000_0000_0000_u64);
97 }