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