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