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