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.
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 #![feature(num_wrapping)]
13 // Test inherent wrapping_* methods for {i,u}{size,8,16,32,64}.
15 use std::{i8, i16, i32, i64, isize};
16 use std::{u8, u16, u32, u64, usize};
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);
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);
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);
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);
43 assert_eq!((0xfe_u8 as i8).wrapping_mul(16),
45 assert_eq!((0xfedc_u16 as i16).wrapping_mul(16),
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));
53 #[cfg(target_pointer_width = "32")]
55 assert_eq!((0xfedc_ba98_u32 as isize).wrapping_mul(16),
56 (0xedcb_a980_u32 as isize));
58 #[cfg(target_pointer_width = "64")]
60 assert_eq!((0xfedc_ba98_7654_3217_u64 as isize).wrapping_mul(16),
61 (0xedcb_a987_6543_2170_u64 as isize));
65 assert_eq!((0xfe as u8).wrapping_mul(16),
67 assert_eq!((0xfedc as u16).wrapping_mul(16),
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));
75 #[cfg(target_pointer_width = "32")]
77 assert_eq!((0xfedc_ba98 as usize).wrapping_mul(16),
78 (0xedcb_a980 as usize));
80 #[cfg(target_pointer_width = "64")]
82 assert_eq!((0xfedc_ba98_7654_3217 as usize).wrapping_mul(16),
83 (0xedcb_a987_6543_2170 as usize));
87 macro_rules! check_mul_no_wrap {
88 ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_mul($f), ($e) * $f); }
90 macro_rules! check_mul_wraps {
91 ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_mul($f), $e); }
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);
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);
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);
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);
117 #[cfg(target_pointer_width = "32")]
119 check_mul_wraps!(0x8000_0000_u32 as isize, -1);
121 #[cfg(target_pointer_width = "64")]
123 check_mul_wraps!(0x8000_0000_0000_0000_u64 as isize, -1);
127 macro_rules! check_div_no_wrap {
128 ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_div($f), ($e) / $f); }
130 macro_rules! check_div_wraps {
131 ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_div($f), $e); }
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);
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);
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);
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);
157 #[cfg(target_pointer_width = "32")]
159 check_div_wraps!(0x8000_0000_u32 as isize, -1);
161 #[cfg(target_pointer_width = "64")]
163 check_div_wraps!(0x8000_0000_0000_0000_u64 as isize, -1);
168 macro_rules! check_rem_no_wrap {
169 ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_rem($f), ($e) % $f); }
171 macro_rules! check_rem_wraps {
172 ($e:expr, $f:expr) => { assert_eq!(($e).wrapping_rem($f), 0); }
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);
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);
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);
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);
198 #[cfg(target_pointer_width = "32")]
200 check_rem_wraps!(0x8000_0000_u32 as isize, -1);
202 #[cfg(target_pointer_width = "64")]
204 check_rem_wraps!(0x8000_0000_0000_0000_u64 as isize, -1);
208 macro_rules! check_neg_no_wrap {
209 ($e:expr) => { assert_eq!(($e).wrapping_neg(), -($e)); }
211 macro_rules! check_neg_wraps {
212 ($e:expr) => { assert_eq!(($e).wrapping_neg(), ($e)); }
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);
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);
226 #[cfg(target_pointer_width = "32")]
228 check_neg_wraps!(0x8000_0000_u32 as isize);
230 #[cfg(target_pointer_width = "64")]
232 check_neg_wraps!(0x8000_0000_0000_0000_u64 as isize);