]>
git.proxmox.com Git - rustc.git/blob - 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.
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.
11 // compile-flags: -C debug-assertions
13 // Check that we do *not* overflow on a number of edge cases.
14 // (compare with test/run-fail/overflowing-{lsh,rsh}*.rs)
21 fn test_left_shift() {
22 // negative rhs can panic, but values in [0,N-1] are okay for iN
25 ($iN
:ty
, $uN
:ty
, $max_rhs
:expr
, $expect_i
:expr
, $expect_u
:expr
) => { {
26 let x
= (1 as $iN
) << 0;
28 let x
= (1 as $uN
) << 0;
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
);
47 assert_eq
!(x
, std
::i8::MIN
);
50 // high-order bits on LHS are silently discarded without panic.
52 assert_eq
!(x
, std
::i8::MIN
);
56 // above is (approximately) expanded from:
57 tests
!(i8, u8, 7, std
::i8::MIN
, 0x80_u8);
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);
64 fn test_right_shift() {
65 // negative rhs can panic, but values in [0,N-1] are okay for iN
68 ($iN
:ty
, $uN
:ty
, $max_rhs
:expr
,
69 $signbit_i
:expr
, $highbit_i
:expr
, $highbit_u
:expr
) =>
71 let x
= (1 as $iN
) >> 0;
73 let x
= (1 as $uN
) >> 0;
75 let x
= ($highbit_i
) >> $max_rhs
-1;
77 let x
= ($highbit_u
) >> $max_rhs
;
79 // sign-bit is carried by arithmetic right shift
80 let x
= ($signbit_i
) >> $max_rhs
;
82 // low-order bits on LHS are silently discarded without panic.
83 let x
= ($highbit_i
+ 1) >> $max_rhs
-1;
85 let x
= ($highbit_u
+ 1) >> $max_rhs
;
87 let x
= ($signbit_i
+ 1) >> $max_rhs
;
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);