]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/wrapping-int-api.rs
Auto merge of #28816 - petrochenkov:unistruct, r=nrc
[rust.git] / src / test / run-pass / wrapping-int-api.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 #![feature(num_wrapping)]
12
13 // Test inherent wrapping_* methods for {i,u}{size,8,16,32,64}.
14
15 use std::{i8, i16, i32, i64, isize};
16 use std::{u8, u16, u32, u64, usize};
17
18 fn main() {
19     assert_eq!(   i8::MAX.wrapping_add(1),    i8::MIN);
20     assert_eq!(  i16::MAX.wrapping_add(1),   i16::MIN);
21     assert_eq!(  i32::MAX.wrapping_add(1),   i32::MIN);
22     assert_eq!(  i64::MAX.wrapping_add(1),   i64::MIN);
23     assert_eq!(isize::MAX.wrapping_add(1), isize::MIN);
24
25     assert_eq!(   i8::MIN.wrapping_sub(1),    i8::MAX);
26     assert_eq!(  i16::MIN.wrapping_sub(1),   i16::MAX);
27     assert_eq!(  i32::MIN.wrapping_sub(1),   i32::MAX);
28     assert_eq!(  i64::MIN.wrapping_sub(1),   i64::MAX);
29     assert_eq!(isize::MIN.wrapping_sub(1), isize::MAX);
30
31     assert_eq!(   u8::MAX.wrapping_add(1),    u8::MIN);
32     assert_eq!(  u16::MAX.wrapping_add(1),   u16::MIN);
33     assert_eq!(  u32::MAX.wrapping_add(1),   u32::MIN);
34     assert_eq!(  u64::MAX.wrapping_add(1),   u64::MIN);
35     assert_eq!(usize::MAX.wrapping_add(1), usize::MIN);
36
37     assert_eq!(   u8::MIN.wrapping_sub(1),    u8::MAX);
38     assert_eq!(  u16::MIN.wrapping_sub(1),   u16::MAX);
39     assert_eq!(  u32::MIN.wrapping_sub(1),   u32::MAX);
40     assert_eq!(  u64::MIN.wrapping_sub(1),   u64::MAX);
41     assert_eq!(usize::MIN.wrapping_sub(1), usize::MAX);
42
43     assert_eq!((0xfe_u8 as i8).wrapping_mul(16),
44                (0xe0_u8 as i8));
45     assert_eq!((0xfedc_u16 as i16).wrapping_mul(16),
46                (0xedc0_u16 as i16));
47     assert_eq!((0xfedc_ba98_u32 as i32).wrapping_mul(16),
48                (0xedcb_a980_u32 as i32));
49     assert_eq!((0xfedc_ba98_7654_3217_u64 as i64).wrapping_mul(16),
50                (0xedcb_a987_6543_2170_u64 as i64));
51
52     match () {
53         #[cfg(target_pointer_width = "32")]
54         () => {
55             assert_eq!((0xfedc_ba98_u32 as isize).wrapping_mul(16),
56                        (0xedcb_a980_u32 as isize));
57         }
58         #[cfg(target_pointer_width = "64")]
59         () => {
60             assert_eq!((0xfedc_ba98_7654_3217_u64 as isize).wrapping_mul(16),
61                        (0xedcb_a987_6543_2170_u64 as isize));
62         }
63     }
64
65     assert_eq!((0xfe as u8).wrapping_mul(16),
66                (0xe0 as u8));
67     assert_eq!((0xfedc as u16).wrapping_mul(16),
68                (0xedc0 as u16));
69     assert_eq!((0xfedc_ba98 as u32).wrapping_mul(16),
70                (0xedcb_a980 as u32));
71     assert_eq!((0xfedc_ba98_7654_3217 as u64).wrapping_mul(16),
72                (0xedcb_a987_6543_2170 as u64));
73
74     match () {
75         #[cfg(target_pointer_width = "32")]
76         () => {
77             assert_eq!((0xfedc_ba98 as usize).wrapping_mul(16),
78                        (0xedcb_a980 as usize));
79         }
80         #[cfg(target_pointer_width = "64")]
81         () => {
82             assert_eq!((0xfedc_ba98_7654_3217 as usize).wrapping_mul(16),
83                        (0xedcb_a987_6543_2170 as usize));
84         }
85     }
86
87     macro_rules! check_mul_no_wrap {
88         ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_mul($f), ($e) * $f); }
89     }
90     macro_rules! check_mul_wraps {
91         ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_mul($f), $e); }
92     }
93
94     check_mul_no_wrap!(0xfe_u8 as i8, -1);
95     check_mul_no_wrap!(0xfedc_u16 as i16, -1);
96     check_mul_no_wrap!(0xfedc_ba98_u32 as i32, -1);
97     check_mul_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -1);
98     check_mul_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -1);
99
100     check_mul_no_wrap!(0xfe_u8 as i8, -2);
101     check_mul_no_wrap!(0xfedc_u16 as i16, -2);
102     check_mul_no_wrap!(0xfedc_ba98_u32 as i32, -2);
103     check_mul_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -2);
104     check_mul_no_wrap!(0xfedc_ba98_fedc_ba98_u64 as u64 as isize, -2);
105
106     check_mul_no_wrap!(0xfe_u8 as i8, 2);
107     check_mul_no_wrap!(0xfedc_u16 as i16, 2);
108     check_mul_no_wrap!(0xfedc_ba98_u32 as i32, 2);
109     check_mul_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, 2);
110     check_mul_no_wrap!(0xfedc_ba98_fedc_ba98_u64 as u64 as isize, 2);
111
112     check_mul_wraps!(0x80_u8 as i8, -1);
113     check_mul_wraps!(0x8000_u16 as i16, -1);
114     check_mul_wraps!(0x8000_0000_u32 as i32, -1);
115     check_mul_wraps!(0x8000_0000_0000_0000_u64 as i64, -1);
116     match () {
117         #[cfg(target_pointer_width = "32")]
118         () => {
119             check_mul_wraps!(0x8000_0000_u32 as isize, -1);
120         }
121         #[cfg(target_pointer_width = "64")]
122         () => {
123             check_mul_wraps!(0x8000_0000_0000_0000_u64 as isize, -1);
124         }
125     }
126
127     macro_rules! check_div_no_wrap {
128         ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_div($f), ($e) / $f); }
129     }
130     macro_rules! check_div_wraps {
131         ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_div($f), $e); }
132     }
133
134     check_div_no_wrap!(0xfe_u8 as i8, -1);
135     check_div_no_wrap!(0xfedc_u16 as i16, -1);
136     check_div_no_wrap!(0xfedc_ba98_u32 as i32, -1);
137     check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -1);
138     check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -1);
139
140     check_div_no_wrap!(0xfe_u8 as i8, -2);
141     check_div_no_wrap!(0xfedc_u16 as i16, -2);
142     check_div_no_wrap!(0xfedc_ba98_u32 as i32, -2);
143     check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -2);
144     check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -2);
145
146     check_div_no_wrap!(0xfe_u8 as i8, 2);
147     check_div_no_wrap!(0xfedc_u16 as i16, 2);
148     check_div_no_wrap!(0xfedc_ba98_u32 as i32, 2);
149     check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, 2);
150     check_div_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, 2);
151
152     check_div_wraps!(-128 as i8, -1);
153     check_div_wraps!(0x8000_u16 as i16, -1);
154     check_div_wraps!(0x8000_0000_u32 as i32, -1);
155     check_div_wraps!(0x8000_0000_0000_0000_u64 as i64, -1);
156     match () {
157         #[cfg(target_pointer_width = "32")]
158         () => {
159             check_div_wraps!(0x8000_0000_u32 as isize, -1);
160         }
161         #[cfg(target_pointer_width = "64")]
162         () => {
163             check_div_wraps!(0x8000_0000_0000_0000_u64 as isize, -1);
164         }
165     }
166
167
168     macro_rules! check_rem_no_wrap {
169         ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_rem($f), ($e) % $f); }
170     }
171     macro_rules! check_rem_wraps {
172         ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_rem($f), 0); }
173     }
174
175     check_rem_no_wrap!(0xfe_u8 as i8, -1);
176     check_rem_no_wrap!(0xfedc_u16 as i16, -1);
177     check_rem_no_wrap!(0xfedc_ba98_u32 as i32, -1);
178     check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -1);
179     check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -1);
180
181     check_rem_no_wrap!(0xfe_u8 as i8, -2);
182     check_rem_no_wrap!(0xfedc_u16 as i16, -2);
183     check_rem_no_wrap!(0xfedc_ba98_u32 as i32, -2);
184     check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, -2);
185     check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, -2);
186
187     check_rem_no_wrap!(0xfe_u8 as i8, 2);
188     check_rem_no_wrap!(0xfedc_u16 as i16, 2);
189     check_rem_no_wrap!(0xfedc_ba98_u32 as i32, 2);
190     check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64, 2);
191     check_rem_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize, 2);
192
193     check_rem_wraps!(0x80_u8 as i8, -1);
194     check_rem_wraps!(0x8000_u16 as i16, -1);
195     check_rem_wraps!(0x8000_0000_u32 as i32, -1);
196     check_rem_wraps!(0x8000_0000_0000_0000_u64 as i64, -1);
197     match () {
198         #[cfg(target_pointer_width = "32")]
199         () => {
200             check_rem_wraps!(0x8000_0000_u32 as isize, -1);
201         }
202         #[cfg(target_pointer_width = "64")]
203         () => {
204             check_rem_wraps!(0x8000_0000_0000_0000_u64 as isize, -1);
205         }
206     }
207
208     macro_rules! check_neg_no_wrap {
209         ($e:expr) => { assert_eq!(($e).wrapping_neg(), -($e)); }
210     }
211     macro_rules! check_neg_wraps {
212         ($e:expr) => { assert_eq!(($e).wrapping_neg(),  ($e)); }
213     }
214
215     check_neg_no_wrap!(0xfe_u8 as i8);
216     check_neg_no_wrap!(0xfedc_u16 as i16);
217     check_neg_no_wrap!(0xfedc_ba98_u32 as i32);
218     check_neg_no_wrap!(0xfedc_ba98_7654_3217_u64 as i64);
219     check_neg_no_wrap!(0xfedc_ba98_7654_3217_u64 as u64 as isize);
220
221     check_neg_wraps!(0x80_u8 as i8);
222     check_neg_wraps!(0x8000_u16 as i16);
223     check_neg_wraps!(0x8000_0000_u32 as i32);
224     check_neg_wraps!(0x8000_0000_0000_0000_u64 as i64);
225     match () {
226         #[cfg(target_pointer_width = "32")]
227         () => {
228             check_neg_wraps!(0x8000_0000_u32 as isize);
229         }
230         #[cfg(target_pointer_width = "64")]
231         () => {
232             check_neg_wraps!(0x8000_0000_0000_0000_u64 as isize);
233         }
234     }
235
236 }