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