1 extern crate num_bigint;
2 extern crate num_integer;
3 extern crate num_traits;
4 #[cfg(feature = "rand")]
5 extern crate rand;
6
7 use num_bigint::BigUint;
8 use num_bigint::Sign::{Minus, NoSign, Plus};
9 use num_bigint::{BigInt, ToBigInt};
10
11 use std::cmp::Ordering::{Equal, Greater, Less};
12 use std::collections::hash_map::RandomState;
13 use std::hash::{BuildHasher, Hash, Hasher};
14 use std::iter::repeat;
15 use std::ops::Neg;
16 use std::{f32, f64};
17 #[cfg(has_i128)]
18 use std::{i128, u128};
19 use std::{i16, i32, i64, i8, isize};
20 use std::{u16, u32, u64, u8, usize};
21
22 use num_integer::Integer;
23 use num_traits::{Float, FromPrimitive, Num, One, Pow, Signed, ToPrimitive, Zero};
24
25 mod consts;
26 use consts::*;
27
28 #[macro_use]
29 mod macros;
30
31 #[test]
test_from_bytes_be()32 fn test_from_bytes_be() {
33 fn check(s: &str, result: &str) {
34 assert_eq!(
35 BigInt::from_bytes_be(Plus, s.as_bytes()),
36 BigInt::parse_bytes(result.as_bytes(), 10).unwrap()
37 );
38 }
39 check("A", "65");
40 check("AA", "16705");
41 check("AB", "16706");
42 check("Hello world!", "22405534230753963835153736737");
43 assert_eq!(BigInt::from_bytes_be(Plus, &[]), Zero::zero());
44 assert_eq!(BigInt::from_bytes_be(Minus, &[]), Zero::zero());
45 }
46
47 #[test]
test_to_bytes_be()48 fn test_to_bytes_be() {
49 fn check(s: &str, result: &str) {
50 let b = BigInt::parse_bytes(result.as_bytes(), 10).unwrap();
51 let (sign, v) = b.to_bytes_be();
52 assert_eq!((Plus, s.as_bytes()), (sign, &*v));
53 }
54 check("A", "65");
55 check("AA", "16705");
56 check("AB", "16706");
57 check("Hello world!", "22405534230753963835153736737");
58 let b: BigInt = Zero::zero();
59 assert_eq!(b.to_bytes_be(), (NoSign, vec![0]));
60
61 // Test with leading/trailing zero bytes and a full BigDigit of value 0
62 let b = BigInt::from_str_radix("00010000000000000200", 16).unwrap();
63 assert_eq!(b.to_bytes_be(), (Plus, vec![1, 0, 0, 0, 0, 0, 0, 2, 0]));
64 }
65
66 #[test]
test_from_bytes_le()67 fn test_from_bytes_le() {
68 fn check(s: &str, result: &str) {
69 assert_eq!(
70 BigInt::from_bytes_le(Plus, s.as_bytes()),
71 BigInt::parse_bytes(result.as_bytes(), 10).unwrap()
72 );
73 }
74 check("A", "65");
75 check("AA", "16705");
76 check("BA", "16706");
77 check("!dlrow olleH", "22405534230753963835153736737");
78 assert_eq!(BigInt::from_bytes_le(Plus, &[]), Zero::zero());
79 assert_eq!(BigInt::from_bytes_le(Minus, &[]), Zero::zero());
80 }
81
82 #[test]
test_to_bytes_le()83 fn test_to_bytes_le() {
84 fn check(s: &str, result: &str) {
85 let b = BigInt::parse_bytes(result.as_bytes(), 10).unwrap();
86 let (sign, v) = b.to_bytes_le();
87 assert_eq!((Plus, s.as_bytes()), (sign, &*v));
88 }
89 check("A", "65");
90 check("AA", "16705");
91 check("BA", "16706");
92 check("!dlrow olleH", "22405534230753963835153736737");
93 let b: BigInt = Zero::zero();
94 assert_eq!(b.to_bytes_le(), (NoSign, vec![0]));
95
96 // Test with leading/trailing zero bytes and a full BigDigit of value 0
97 let b = BigInt::from_str_radix("00010000000000000200", 16).unwrap();
98 assert_eq!(b.to_bytes_le(), (Plus, vec![0, 2, 0, 0, 0, 0, 0, 0, 1]));
99 }
100
101 #[test]
test_to_signed_bytes_le()102 fn test_to_signed_bytes_le() {
103 fn check(s: &str, result: Vec<u8>) {
104 assert_eq!(
105 BigInt::parse_bytes(s.as_bytes(), 10)
106 .unwrap()
107 .to_signed_bytes_le(),
108 result
109 );
110 }
111
112 check("0", vec![0]);
113 check("32767", vec![0xff, 0x7f]);
114 check("-1", vec![0xff]);
115 check("16777216", vec![0, 0, 0, 1]);
116 check("-100", vec![156]);
117 check("-8388608", vec![0, 0, 0x80]);
118 check("-192", vec![0x40, 0xff]);
119 check("128", vec![0x80, 0])
120 }
121
122 #[test]
test_from_signed_bytes_le()123 fn test_from_signed_bytes_le() {
124 fn check(s: &[u8], result: &str) {
125 assert_eq!(
126 BigInt::from_signed_bytes_le(s),
127 BigInt::parse_bytes(result.as_bytes(), 10).unwrap()
128 );
129 }
130
131 check(&[], "0");
132 check(&[0], "0");
133 check(&[0; 10], "0");
134 check(&[0xff, 0x7f], "32767");
135 check(&[0xff], "-1");
136 check(&[0, 0, 0, 1], "16777216");
137 check(&[156], "-100");
138 check(&[0, 0, 0x80], "-8388608");
139 check(&[0xff; 10], "-1");
140 check(&[0x40, 0xff], "-192");
141 }
142
143 #[test]
test_to_signed_bytes_be()144 fn test_to_signed_bytes_be() {
145 fn check(s: &str, result: Vec<u8>) {
146 assert_eq!(
147 BigInt::parse_bytes(s.as_bytes(), 10)
148 .unwrap()
149 .to_signed_bytes_be(),
150 result
151 );
152 }
153
154 check("0", vec![0]);
155 check("32767", vec![0x7f, 0xff]);
156 check("-1", vec![255]);
157 check("16777216", vec![1, 0, 0, 0]);
158 check("-100", vec![156]);
159 check("-8388608", vec![128, 0, 0]);
160 check("-192", vec![0xff, 0x40]);
161 check("128", vec![0, 0x80]);
162 }
163
164 #[test]
test_from_signed_bytes_be()165 fn test_from_signed_bytes_be() {
166 fn check(s: &[u8], result: &str) {
167 assert_eq!(
168 BigInt::from_signed_bytes_be(s),
169 BigInt::parse_bytes(result.as_bytes(), 10).unwrap()
170 );
171 }
172
173 check(&[], "0");
174 check(&[0], "0");
175 check(&[0; 10], "0");
176 check(&[127, 255], "32767");
177 check(&[255], "-1");
178 check(&[1, 0, 0, 0], "16777216");
179 check(&[156], "-100");
180 check(&[128, 0, 0], "-8388608");
181 check(&[255; 10], "-1");
182 check(&[0xff, 0x40], "-192");
183 }
184
185 #[test]
test_signed_bytes_be_round_trip()186 fn test_signed_bytes_be_round_trip() {
187 for i in -0x1FFFF..0x20000 {
188 let n = BigInt::from(i);
189 assert_eq!(n, BigInt::from_signed_bytes_be(&n.to_signed_bytes_be()));
190 }
191 }
192
193 #[test]
test_signed_bytes_le_round_trip()194 fn test_signed_bytes_le_round_trip() {
195 for i in -0x1FFFF..0x20000 {
196 let n = BigInt::from(i);
197 assert_eq!(n, BigInt::from_signed_bytes_le(&n.to_signed_bytes_le()));
198 }
199 }
200
201 #[test]
test_cmp()202 fn test_cmp() {
203 let vs: [&[u32]; 4] = [&[2 as u32], &[1, 1], &[2, 1], &[1, 1, 1]];
204 let mut nums = Vec::new();
205 for s in vs.iter().rev() {
206 nums.push(BigInt::from_slice(Minus, *s));
207 }
208 nums.push(Zero::zero());
209 nums.extend(vs.iter().map(|s| BigInt::from_slice(Plus, *s)));
210
211 for (i, ni) in nums.iter().enumerate() {
212 for (j0, nj) in nums[i..].iter().enumerate() {
213 let j = i + j0;
214 if i == j {
215 assert_eq!(ni.cmp(nj), Equal);
216 assert_eq!(nj.cmp(ni), Equal);
217 assert_eq!(ni, nj);
218 assert!(!(ni != nj));
219 assert!(ni <= nj);
220 assert!(ni >= nj);
221 assert!(!(ni < nj));
222 assert!(!(ni > nj));
223 } else {
224 assert_eq!(ni.cmp(nj), Less);
225 assert_eq!(nj.cmp(ni), Greater);
226
227 assert!(!(ni == nj));
228 assert!(ni != nj);
229
230 assert!(ni <= nj);
231 assert!(!(ni >= nj));
232 assert!(ni < nj);
233 assert!(!(ni > nj));
234
235 assert!(!(nj <= ni));
236 assert!(nj >= ni);
237 assert!(!(nj < ni));
238 assert!(nj > ni);
239 }
240 }
241 }
242 }
243
hash<T: Hash>(x: &T) -> u64244 fn hash<T: Hash>(x: &T) -> u64 {
245 let mut hasher = <RandomState as BuildHasher>::Hasher::new();
246 x.hash(&mut hasher);
247 hasher.finish()
248 }
249
250 #[test]
test_hash()251 fn test_hash() {
252 let a = BigInt::new(NoSign, vec![]);
253 let b = BigInt::new(NoSign, vec![0]);
254 let c = BigInt::new(Plus, vec![1]);
255 let d = BigInt::new(Plus, vec![1, 0, 0, 0, 0, 0]);
256 let e = BigInt::new(Plus, vec![0, 0, 0, 0, 0, 1]);
257 let f = BigInt::new(Minus, vec![1]);
258 assert!(hash(&a) == hash(&b));
259 assert!(hash(&b) != hash(&c));
260 assert!(hash(&c) == hash(&d));
261 assert!(hash(&d) != hash(&e));
262 assert!(hash(&c) != hash(&f));
263 }
264
265 #[test]
test_convert_i64()266 fn test_convert_i64() {
267 fn check(b1: BigInt, i: i64) {
268 let b2: BigInt = FromPrimitive::from_i64(i).unwrap();
269 assert!(b1 == b2);
270 assert!(b1.to_i64().unwrap() == i);
271 }
272
273 check(Zero::zero(), 0);
274 check(One::one(), 1);
275 check(i64::MIN.to_bigint().unwrap(), i64::MIN);
276 check(i64::MAX.to_bigint().unwrap(), i64::MAX);
277
278 assert_eq!((i64::MAX as u64 + 1).to_bigint().unwrap().to_i64(), None);
279
280 assert_eq!(
281 BigInt::from_biguint(Plus, BigUint::new(vec![1, 2, 3, 4, 5])).to_i64(),
282 None
283 );
284
285 assert_eq!(
286 BigInt::from_biguint(Minus, BigUint::new(vec![1, 0, 0, 1 << 31])).to_i64(),
287 None
288 );
289
290 assert_eq!(
291 BigInt::from_biguint(Minus, BigUint::new(vec![1, 2, 3, 4, 5])).to_i64(),
292 None
293 );
294 }
295
296 #[test]
297 #[cfg(has_i128)]
test_convert_i128()298 fn test_convert_i128() {
299 fn check(b1: BigInt, i: i128) {
300 let b2: BigInt = FromPrimitive::from_i128(i).unwrap();
301 assert!(b1 == b2);
302 assert!(b1.to_i128().unwrap() == i);
303 }
304
305 check(Zero::zero(), 0);
306 check(One::one(), 1);
307 check(i128::MIN.to_bigint().unwrap(), i128::MIN);
308 check(i128::MAX.to_bigint().unwrap(), i128::MAX);
309
310 assert_eq!((i128::MAX as u128 + 1).to_bigint().unwrap().to_i128(), None);
311
312 assert_eq!(
313 BigInt::from_biguint(Plus, BigUint::new(vec![1, 2, 3, 4, 5])).to_i128(),
314 None
315 );
316
317 assert_eq!(
318 BigInt::from_biguint(Minus, BigUint::new(vec![1, 0, 0, 1 << 31])).to_i128(),
319 None
320 );
321
322 assert_eq!(
323 BigInt::from_biguint(Minus, BigUint::new(vec![1, 2, 3, 4, 5])).to_i128(),
324 None
325 );
326 }
327
328 #[test]
test_convert_u64()329 fn test_convert_u64() {
330 fn check(b1: BigInt, u: u64) {
331 let b2: BigInt = FromPrimitive::from_u64(u).unwrap();
332 assert!(b1 == b2);
333 assert!(b1.to_u64().unwrap() == u);
334 }
335
336 check(Zero::zero(), 0);
337 check(One::one(), 1);
338 check(u64::MIN.to_bigint().unwrap(), u64::MIN);
339 check(u64::MAX.to_bigint().unwrap(), u64::MAX);
340
341 assert_eq!(
342 BigInt::from_biguint(Plus, BigUint::new(vec![1, 2, 3, 4, 5])).to_u64(),
343 None
344 );
345
346 let max_value: BigUint = FromPrimitive::from_u64(u64::MAX).unwrap();
347 assert_eq!(BigInt::from_biguint(Minus, max_value).to_u64(), None);
348 assert_eq!(
349 BigInt::from_biguint(Minus, BigUint::new(vec![1, 2, 3, 4, 5])).to_u64(),
350 None
351 );
352 }
353
354 #[test]
355 #[cfg(has_i128)]
test_convert_u128()356 fn test_convert_u128() {
357 fn check(b1: BigInt, u: u128) {
358 let b2: BigInt = FromPrimitive::from_u128(u).unwrap();
359 assert!(b1 == b2);
360 assert!(b1.to_u128().unwrap() == u);
361 }
362
363 check(Zero::zero(), 0);
364 check(One::one(), 1);
365 check(u128::MIN.to_bigint().unwrap(), u128::MIN);
366 check(u128::MAX.to_bigint().unwrap(), u128::MAX);
367
368 assert_eq!(
369 BigInt::from_biguint(Plus, BigUint::new(vec![1, 2, 3, 4, 5])).to_u128(),
370 None
371 );
372
373 let max_value: BigUint = FromPrimitive::from_u128(u128::MAX).unwrap();
374 assert_eq!(BigInt::from_biguint(Minus, max_value).to_u128(), None);
375 assert_eq!(
376 BigInt::from_biguint(Minus, BigUint::new(vec![1, 2, 3, 4, 5])).to_u128(),
377 None
378 );
379 }
380
381 #[test]
test_convert_f32()382 fn test_convert_f32() {
383 fn check(b1: &BigInt, f: f32) {
384 let b2 = BigInt::from_f32(f).unwrap();
385 assert_eq!(b1, &b2);
386 assert_eq!(b1.to_f32().unwrap(), f);
387 let neg_b1 = -b1;
388 let neg_b2 = BigInt::from_f32(-f).unwrap();
389 assert_eq!(neg_b1, neg_b2);
390 assert_eq!(neg_b1.to_f32().unwrap(), -f);
391 }
392
393 check(&BigInt::zero(), 0.0);
394 check(&BigInt::one(), 1.0);
395 check(&BigInt::from(u16::MAX), 2.0.powi(16) - 1.0);
396 check(&BigInt::from(1u64 << 32), 2.0.powi(32));
397 check(&BigInt::from_slice(Plus, &[0, 0, 1]), 2.0.powi(64));
398 check(
399 &((BigInt::one() << 100) + (BigInt::one() << 123)),
400 2.0.powi(100) + 2.0.powi(123),
401 );
402 check(&(BigInt::one() << 127), 2.0.powi(127));
403 check(&(BigInt::from((1u64 << 24) - 1) << (128 - 24)), f32::MAX);
404
405 // keeping all 24 digits with the bits at different offsets to the BigDigits
406 let x: u32 = 0b00000000101111011111011011011101;
407 let mut f = x as f32;
408 let mut b = BigInt::from(x);
409 for _ in 0..64 {
410 check(&b, f);
411 f *= 2.0;
412 b = b << 1;
413 }
414
415 // this number when rounded to f64 then f32 isn't the same as when rounded straight to f32
416 let mut n: i64 = 0b0000000000111111111111111111111111011111111111111111111111111111;
417 assert!((n as f64) as f32 != n as f32);
418 assert_eq!(BigInt::from(n).to_f32(), Some(n as f32));
419 n = -n;
420 assert!((n as f64) as f32 != n as f32);
421 assert_eq!(BigInt::from(n).to_f32(), Some(n as f32));
422
423 // test rounding up with the bits at different offsets to the BigDigits
424 let mut f = ((1u64 << 25) - 1) as f32;
425 let mut b = BigInt::from(1u64 << 25);
426 for _ in 0..64 {
427 assert_eq!(b.to_f32(), Some(f));
428 f *= 2.0;
429 b = b << 1;
430 }
431
432 // rounding
433 assert_eq!(
434 BigInt::from_f32(-f32::consts::PI),
435 Some(BigInt::from(-3i32))
436 );
437 assert_eq!(BigInt::from_f32(-f32::consts::E), Some(BigInt::from(-2i32)));
438 assert_eq!(BigInt::from_f32(-0.99999), Some(BigInt::zero()));
439 assert_eq!(BigInt::from_f32(-0.5), Some(BigInt::zero()));
440 assert_eq!(BigInt::from_f32(-0.0), Some(BigInt::zero()));
441 assert_eq!(
442 BigInt::from_f32(f32::MIN_POSITIVE / 2.0),
443 Some(BigInt::zero())
444 );
445 assert_eq!(BigInt::from_f32(f32::MIN_POSITIVE), Some(BigInt::zero()));
446 assert_eq!(BigInt::from_f32(0.5), Some(BigInt::zero()));
447 assert_eq!(BigInt::from_f32(0.99999), Some(BigInt::zero()));
448 assert_eq!(BigInt::from_f32(f32::consts::E), Some(BigInt::from(2u32)));
449 assert_eq!(BigInt::from_f32(f32::consts::PI), Some(BigInt::from(3u32)));
450
451 // special float values
452 assert_eq!(BigInt::from_f32(f32::NAN), None);
453 assert_eq!(BigInt::from_f32(f32::INFINITY), None);
454 assert_eq!(BigInt::from_f32(f32::NEG_INFINITY), None);
455
456 // largest BigInt that will round to a finite f32 value
457 let big_num = (BigInt::one() << 128) - BigInt::one() - (BigInt::one() << (128 - 25));
458 assert_eq!(big_num.to_f32(), Some(f32::MAX));
459 assert_eq!((&big_num + BigInt::one()).to_f32(), None);
460 assert_eq!((-&big_num).to_f32(), Some(f32::MIN));
461 assert_eq!(((-&big_num) - BigInt::one()).to_f32(), None);
462
463 assert_eq!(((BigInt::one() << 128) - BigInt::one()).to_f32(), None);
464 assert_eq!((BigInt::one() << 128).to_f32(), None);
465 assert_eq!((-((BigInt::one() << 128) - BigInt::one())).to_f32(), None);
466 assert_eq!((-(BigInt::one() << 128)).to_f32(), None);
467 }
468
469 #[test]
test_convert_f64()470 fn test_convert_f64() {
471 fn check(b1: &BigInt, f: f64) {
472 let b2 = BigInt::from_f64(f).unwrap();
473 assert_eq!(b1, &b2);
474 assert_eq!(b1.to_f64().unwrap(), f);
475 let neg_b1 = -b1;
476 let neg_b2 = BigInt::from_f64(-f).unwrap();
477 assert_eq!(neg_b1, neg_b2);
478 assert_eq!(neg_b1.to_f64().unwrap(), -f);
479 }
480
481 check(&BigInt::zero(), 0.0);
482 check(&BigInt::one(), 1.0);
483 check(&BigInt::from(u32::MAX), 2.0.powi(32) - 1.0);
484 check(&BigInt::from(1u64 << 32), 2.0.powi(32));
485 check(&BigInt::from_slice(Plus, &[0, 0, 1]), 2.0.powi(64));
486 check(
487 &((BigInt::one() << 100) + (BigInt::one() << 152)),
488 2.0.powi(100) + 2.0.powi(152),
489 );
490 check(&(BigInt::one() << 1023), 2.0.powi(1023));
491 check(&(BigInt::from((1u64 << 53) - 1) << (1024 - 53)), f64::MAX);
492
493 // keeping all 53 digits with the bits at different offsets to the BigDigits
494 let x: u64 = 0b0000000000011110111110110111111101110111101111011111011011011101;
495 let mut f = x as f64;
496 let mut b = BigInt::from(x);
497 for _ in 0..128 {
498 check(&b, f);
499 f *= 2.0;
500 b = b << 1;
501 }
502
503 // test rounding up with the bits at different offsets to the BigDigits
504 let mut f = ((1u64 << 54) - 1) as f64;
505 let mut b = BigInt::from(1u64 << 54);
506 for _ in 0..128 {
507 assert_eq!(b.to_f64(), Some(f));
508 f *= 2.0;
509 b = b << 1;
510 }
511
512 // rounding
513 assert_eq!(
514 BigInt::from_f64(-f64::consts::PI),
515 Some(BigInt::from(-3i32))
516 );
517 assert_eq!(BigInt::from_f64(-f64::consts::E), Some(BigInt::from(-2i32)));
518 assert_eq!(BigInt::from_f64(-0.99999), Some(BigInt::zero()));
519 assert_eq!(BigInt::from_f64(-0.5), Some(BigInt::zero()));
520 assert_eq!(BigInt::from_f64(-0.0), Some(BigInt::zero()));
521 assert_eq!(
522 BigInt::from_f64(f64::MIN_POSITIVE / 2.0),
523 Some(BigInt::zero())
524 );
525 assert_eq!(BigInt::from_f64(f64::MIN_POSITIVE), Some(BigInt::zero()));
526 assert_eq!(BigInt::from_f64(0.5), Some(BigInt::zero()));
527 assert_eq!(BigInt::from_f64(0.99999), Some(BigInt::zero()));
528 assert_eq!(BigInt::from_f64(f64::consts::E), Some(BigInt::from(2u32)));
529 assert_eq!(BigInt::from_f64(f64::consts::PI), Some(BigInt::from(3u32)));
530
531 // special float values
532 assert_eq!(BigInt::from_f64(f64::NAN), None);
533 assert_eq!(BigInt::from_f64(f64::INFINITY), None);
534 assert_eq!(BigInt::from_f64(f64::NEG_INFINITY), None);
535
536 // largest BigInt that will round to a finite f64 value
537 let big_num = (BigInt::one() << 1024) - BigInt::one() - (BigInt::one() << (1024 - 54));
538 assert_eq!(big_num.to_f64(), Some(f64::MAX));
539 assert_eq!((&big_num + BigInt::one()).to_f64(), None);
540 assert_eq!((-&big_num).to_f64(), Some(f64::MIN));
541 assert_eq!(((-&big_num) - BigInt::one()).to_f64(), None);
542
543 assert_eq!(((BigInt::one() << 1024) - BigInt::one()).to_f64(), None);
544 assert_eq!((BigInt::one() << 1024).to_f64(), None);
545 assert_eq!((-((BigInt::one() << 1024) - BigInt::one())).to_f64(), None);
546 assert_eq!((-(BigInt::one() << 1024)).to_f64(), None);
547 }
548
549 #[test]
test_convert_to_biguint()550 fn test_convert_to_biguint() {
551 fn check(n: BigInt, ans_1: BigUint) {
552 assert_eq!(n.to_biguint().unwrap(), ans_1);
553 assert_eq!(n.to_biguint().unwrap().to_bigint().unwrap(), n);
554 }
555 let zero: BigInt = Zero::zero();
556 let unsigned_zero: BigUint = Zero::zero();
557 let positive = BigInt::from_biguint(Plus, BigUint::new(vec![1, 2, 3]));
558 let negative = -&positive;
559
560 check(zero, unsigned_zero);
561 check(positive, BigUint::new(vec![1, 2, 3]));
562
563 assert_eq!(negative.to_biguint(), None);
564 }
565
566 #[test]
test_convert_from_uint()567 fn test_convert_from_uint() {
568 macro_rules! check {
569 ($ty:ident, $max:expr) => {
570 assert_eq!(BigInt::from($ty::zero()), BigInt::zero());
571 assert_eq!(BigInt::from($ty::one()), BigInt::one());
572 assert_eq!(BigInt::from($ty::MAX - $ty::one()), $max - BigInt::one());
573 assert_eq!(BigInt::from($ty::MAX), $max);
574 };
575 }
576
577 check!(u8, BigInt::from_slice(Plus, &[u8::MAX as u32]));
578 check!(u16, BigInt::from_slice(Plus, &[u16::MAX as u32]));
579 check!(u32, BigInt::from_slice(Plus, &[u32::MAX]));
580 check!(u64, BigInt::from_slice(Plus, &[u32::MAX, u32::MAX]));
581 #[cfg(has_i128)]
582 check!(
583 u128,
584 BigInt::from_slice(Plus, &[u32::MAX, u32::MAX, u32::MAX, u32::MAX])
585 );
586 check!(usize, BigInt::from(usize::MAX as u64));
587 }
588
589 #[test]
test_convert_from_int()590 fn test_convert_from_int() {
591 macro_rules! check {
592 ($ty:ident, $min:expr, $max:expr) => {
593 assert_eq!(BigInt::from($ty::MIN), $min);
594 assert_eq!(BigInt::from($ty::MIN + $ty::one()), $min + BigInt::one());
595 assert_eq!(BigInt::from(-$ty::one()), -BigInt::one());
596 assert_eq!(BigInt::from($ty::zero()), BigInt::zero());
597 assert_eq!(BigInt::from($ty::one()), BigInt::one());
598 assert_eq!(BigInt::from($ty::MAX - $ty::one()), $max - BigInt::one());
599 assert_eq!(BigInt::from($ty::MAX), $max);
600 };
601 }
602
603 check!(
604 i8,
605 BigInt::from_slice(Minus, &[1 << 7]),
606 BigInt::from_slice(Plus, &[i8::MAX as u32])
607 );
608 check!(
609 i16,
610 BigInt::from_slice(Minus, &[1 << 15]),
611 BigInt::from_slice(Plus, &[i16::MAX as u32])
612 );
613 check!(
614 i32,
615 BigInt::from_slice(Minus, &[1 << 31]),
616 BigInt::from_slice(Plus, &[i32::MAX as u32])
617 );
618 check!(
619 i64,
620 BigInt::from_slice(Minus, &[0, 1 << 31]),
621 BigInt::from_slice(Plus, &[u32::MAX, i32::MAX as u32])
622 );
623 #[cfg(has_i128)]
624 check!(
625 i128,
626 BigInt::from_slice(Minus, &[0, 0, 0, 1 << 31]),
627 BigInt::from_slice(Plus, &[u32::MAX, u32::MAX, u32::MAX, i32::MAX as u32])
628 );
629 check!(
630 isize,
631 BigInt::from(isize::MIN as i64),
632 BigInt::from(isize::MAX as i64)
633 );
634 }
635
636 #[test]
test_convert_from_biguint()637 fn test_convert_from_biguint() {
638 assert_eq!(BigInt::from(BigUint::zero()), BigInt::zero());
639 assert_eq!(BigInt::from(BigUint::one()), BigInt::one());
640 assert_eq!(
641 BigInt::from(BigUint::from_slice(&[1, 2, 3])),
642 BigInt::from_slice(Plus, &[1, 2, 3])
643 );
644 }
645
646 #[test]
test_add()647 fn test_add() {
648 for elm in SUM_TRIPLES.iter() {
649 let (a_vec, b_vec, c_vec) = *elm;
650 let a = BigInt::from_slice(Plus, a_vec);
651 let b = BigInt::from_slice(Plus, b_vec);
652 let c = BigInt::from_slice(Plus, c_vec);
653 let (na, nb, nc) = (-&a, -&b, -&c);
654
655 assert_op!(a + b == c);
656 assert_op!(b + a == c);
657 assert_op!(c + na == b);
658 assert_op!(c + nb == a);
659 assert_op!(a + nc == nb);
660 assert_op!(b + nc == na);
661 assert_op!(na + nb == nc);
662 assert_op!(a + na == Zero::zero());
663
664 assert_assign_op!(a += b == c);
665 assert_assign_op!(b += a == c);
666 assert_assign_op!(c += na == b);
667 assert_assign_op!(c += nb == a);
668 assert_assign_op!(a += nc == nb);
669 assert_assign_op!(b += nc == na);
670 assert_assign_op!(na += nb == nc);
671 assert_assign_op!(a += na == Zero::zero());
672 }
673 }
674
675 #[test]
test_sub()676 fn test_sub() {
677 for elm in SUM_TRIPLES.iter() {
678 let (a_vec, b_vec, c_vec) = *elm;
679 let a = BigInt::from_slice(Plus, a_vec);
680 let b = BigInt::from_slice(Plus, b_vec);
681 let c = BigInt::from_slice(Plus, c_vec);
682 let (na, nb, nc) = (-&a, -&b, -&c);
683
684 assert_op!(c - a == b);
685 assert_op!(c - b == a);
686 assert_op!(nb - a == nc);
687 assert_op!(na - b == nc);
688 assert_op!(b - na == c);
689 assert_op!(a - nb == c);
690 assert_op!(nc - na == nb);
691 assert_op!(a - a == Zero::zero());
692
693 assert_assign_op!(c -= a == b);
694 assert_assign_op!(c -= b == a);
695 assert_assign_op!(nb -= a == nc);
696 assert_assign_op!(na -= b == nc);
697 assert_assign_op!(b -= na == c);
698 assert_assign_op!(a -= nb == c);
699 assert_assign_op!(nc -= na == nb);
700 assert_assign_op!(a -= a == Zero::zero());
701 }
702 }
703
704 #[test]
test_mul()705 fn test_mul() {
706 for elm in MUL_TRIPLES.iter() {
707 let (a_vec, b_vec, c_vec) = *elm;
708 let a = BigInt::from_slice(Plus, a_vec);
709 let b = BigInt::from_slice(Plus, b_vec);
710 let c = BigInt::from_slice(Plus, c_vec);
711 let (na, nb, nc) = (-&a, -&b, -&c);
712
713 assert_op!(a * b == c);
714 assert_op!(b * a == c);
715 assert_op!(na * nb == c);
716
717 assert_op!(na * b == nc);
718 assert_op!(nb * a == nc);
719
720 assert_assign_op!(a *= b == c);
721 assert_assign_op!(b *= a == c);
722 assert_assign_op!(na *= nb == c);
723
724 assert_assign_op!(na *= b == nc);
725 assert_assign_op!(nb *= a == nc);
726 }
727
728 for elm in DIV_REM_QUADRUPLES.iter() {
729 let (a_vec, b_vec, c_vec, d_vec) = *elm;
730 let a = BigInt::from_slice(Plus, a_vec);
731 let b = BigInt::from_slice(Plus, b_vec);
732 let c = BigInt::from_slice(Plus, c_vec);
733 let d = BigInt::from_slice(Plus, d_vec);
734
735 assert!(a == &b * &c + &d);
736 assert!(a == &c * &b + &d);
737 }
738 }
739
740 #[test]
test_div_mod_floor()741 fn test_div_mod_floor() {
742 fn check_sub(a: &BigInt, b: &BigInt, ans_d: &BigInt, ans_m: &BigInt) {
743 let (d, m) = a.div_mod_floor(b);
744 if !m.is_zero() {
745 assert_eq!(m.sign(), b.sign());
746 }
747 assert!(m.abs() <= b.abs());
748 assert!(*a == b * &d + &m);
749 assert!(d == *ans_d);
750 assert!(m == *ans_m);
751 }
752
753 fn check(a: &BigInt, b: &BigInt, d: &BigInt, m: &BigInt) {
754 if m.is_zero() {
755 check_sub(a, b, d, m);
756 check_sub(a, &b.neg(), &d.neg(), m);
757 check_sub(&a.neg(), b, &d.neg(), m);
758 check_sub(&a.neg(), &b.neg(), d, m);
759 } else {
760 let one: BigInt = One::one();
761 check_sub(a, b, d, m);
762 check_sub(a, &b.neg(), &(d.neg() - &one), &(m - b));
763 check_sub(&a.neg(), b, &(d.neg() - &one), &(b - m));
764 check_sub(&a.neg(), &b.neg(), d, &m.neg());
765 }
766 }
767
768 for elm in MUL_TRIPLES.iter() {
769 let (a_vec, b_vec, c_vec) = *elm;
770 let a = BigInt::from_slice(Plus, a_vec);
771 let b = BigInt::from_slice(Plus, b_vec);
772 let c = BigInt::from_slice(Plus, c_vec);
773
774 if !a.is_zero() {
775 check(&c, &a, &b, &Zero::zero());
776 }
777 if !b.is_zero() {
778 check(&c, &b, &a, &Zero::zero());
779 }
780 }
781
782 for elm in DIV_REM_QUADRUPLES.iter() {
783 let (a_vec, b_vec, c_vec, d_vec) = *elm;
784 let a = BigInt::from_slice(Plus, a_vec);
785 let b = BigInt::from_slice(Plus, b_vec);
786 let c = BigInt::from_slice(Plus, c_vec);
787 let d = BigInt::from_slice(Plus, d_vec);
788
789 if !b.is_zero() {
790 check(&a, &b, &c, &d);
791 }
792 }
793 }
794
795 #[test]
test_div_rem()796 fn test_div_rem() {
797 fn check_sub(a: &BigInt, b: &BigInt, ans_q: &BigInt, ans_r: &BigInt) {
798 let (q, r) = a.div_rem(b);
799 if !r.is_zero() {
800 assert_eq!(r.sign(), a.sign());
801 }
802 assert!(r.abs() <= b.abs());
803 assert!(*a == b * &q + &r);
804 assert!(q == *ans_q);
805 assert!(r == *ans_r);
806
807 let (a, b, ans_q, ans_r) = (a.clone(), b.clone(), ans_q.clone(), ans_r.clone());
808 assert_op!(a / b == ans_q);
809 assert_op!(a % b == ans_r);
810 assert_assign_op!(a /= b == ans_q);
811 assert_assign_op!(a %= b == ans_r);
812 }
813
814 fn check(a: &BigInt, b: &BigInt, q: &BigInt, r: &BigInt) {
815 check_sub(a, b, q, r);
816 check_sub(a, &b.neg(), &q.neg(), r);
817 check_sub(&a.neg(), b, &q.neg(), &r.neg());
818 check_sub(&a.neg(), &b.neg(), q, &r.neg());
819 }
820 for elm in MUL_TRIPLES.iter() {
821 let (a_vec, b_vec, c_vec) = *elm;
822 let a = BigInt::from_slice(Plus, a_vec);
823 let b = BigInt::from_slice(Plus, b_vec);
824 let c = BigInt::from_slice(Plus, c_vec);
825
826 if !a.is_zero() {
827 check(&c, &a, &b, &Zero::zero());
828 }
829 if !b.is_zero() {
830 check(&c, &b, &a, &Zero::zero());
831 }
832 }
833
834 for elm in DIV_REM_QUADRUPLES.iter() {
835 let (a_vec, b_vec, c_vec, d_vec) = *elm;
836 let a = BigInt::from_slice(Plus, a_vec);
837 let b = BigInt::from_slice(Plus, b_vec);
838 let c = BigInt::from_slice(Plus, c_vec);
839 let d = BigInt::from_slice(Plus, d_vec);
840
841 if !b.is_zero() {
842 check(&a, &b, &c, &d);
843 }
844 }
845 }
846
847 #[test]
test_checked_add()848 fn test_checked_add() {
849 for elm in SUM_TRIPLES.iter() {
850 let (a_vec, b_vec, c_vec) = *elm;
851 let a = BigInt::from_slice(Plus, a_vec);
852 let b = BigInt::from_slice(Plus, b_vec);
853 let c = BigInt::from_slice(Plus, c_vec);
854
855 assert!(a.checked_add(&b).unwrap() == c);
856 assert!(b.checked_add(&a).unwrap() == c);
857 assert!(c.checked_add(&(-&a)).unwrap() == b);
858 assert!(c.checked_add(&(-&b)).unwrap() == a);
859 assert!(a.checked_add(&(-&c)).unwrap() == (-&b));
860 assert!(b.checked_add(&(-&c)).unwrap() == (-&a));
861 assert!((-&a).checked_add(&(-&b)).unwrap() == (-&c));
862 assert!(a.checked_add(&(-&a)).unwrap() == Zero::zero());
863 }
864 }
865
866 #[test]
test_checked_sub()867 fn test_checked_sub() {
868 for elm in SUM_TRIPLES.iter() {
869 let (a_vec, b_vec, c_vec) = *elm;
870 let a = BigInt::from_slice(Plus, a_vec);
871 let b = BigInt::from_slice(Plus, b_vec);
872 let c = BigInt::from_slice(Plus, c_vec);
873
874 assert!(c.checked_sub(&a).unwrap() == b);
875 assert!(c.checked_sub(&b).unwrap() == a);
876 assert!((-&b).checked_sub(&a).unwrap() == (-&c));
877 assert!((-&a).checked_sub(&b).unwrap() == (-&c));
878 assert!(b.checked_sub(&(-&a)).unwrap() == c);
879 assert!(a.checked_sub(&(-&b)).unwrap() == c);
880 assert!((-&c).checked_sub(&(-&a)).unwrap() == (-&b));
881 assert!(a.checked_sub(&a).unwrap() == Zero::zero());
882 }
883 }
884
885 #[test]
test_checked_mul()886 fn test_checked_mul() {
887 for elm in MUL_TRIPLES.iter() {
888 let (a_vec, b_vec, c_vec) = *elm;
889 let a = BigInt::from_slice(Plus, a_vec);
890 let b = BigInt::from_slice(Plus, b_vec);
891 let c = BigInt::from_slice(Plus, c_vec);
892
893 assert!(a.checked_mul(&b).unwrap() == c);
894 assert!(b.checked_mul(&a).unwrap() == c);
895
896 assert!((-&a).checked_mul(&b).unwrap() == -&c);
897 assert!((-&b).checked_mul(&a).unwrap() == -&c);
898 }
899
900 for elm in DIV_REM_QUADRUPLES.iter() {
901 let (a_vec, b_vec, c_vec, d_vec) = *elm;
902 let a = BigInt::from_slice(Plus, a_vec);
903 let b = BigInt::from_slice(Plus, b_vec);
904 let c = BigInt::from_slice(Plus, c_vec);
905 let d = BigInt::from_slice(Plus, d_vec);
906
907 assert!(a == b.checked_mul(&c).unwrap() + &d);
908 assert!(a == c.checked_mul(&b).unwrap() + &d);
909 }
910 }
911 #[test]
test_checked_div()912 fn test_checked_div() {
913 for elm in MUL_TRIPLES.iter() {
914 let (a_vec, b_vec, c_vec) = *elm;
915 let a = BigInt::from_slice(Plus, a_vec);
916 let b = BigInt::from_slice(Plus, b_vec);
917 let c = BigInt::from_slice(Plus, c_vec);
918
919 if !a.is_zero() {
920 assert!(c.checked_div(&a).unwrap() == b);
921 assert!((-&c).checked_div(&(-&a)).unwrap() == b);
922 assert!((-&c).checked_div(&a).unwrap() == -&b);
923 }
924 if !b.is_zero() {
925 assert!(c.checked_div(&b).unwrap() == a);
926 assert!((-&c).checked_div(&(-&b)).unwrap() == a);
927 assert!((-&c).checked_div(&b).unwrap() == -&a);
928 }
929
930 assert!(c.checked_div(&Zero::zero()).is_none());
931 assert!((-&c).checked_div(&Zero::zero()).is_none());
932 }
933 }
934
935 #[test]
test_gcd()936 fn test_gcd() {
937 fn check(a: isize, b: isize, c: isize) {
938 let big_a: BigInt = FromPrimitive::from_isize(a).unwrap();
939 let big_b: BigInt = FromPrimitive::from_isize(b).unwrap();
940 let big_c: BigInt = FromPrimitive::from_isize(c).unwrap();
941
942 assert_eq!(big_a.gcd(&big_b), big_c);
943 }
944
945 check(10, 2, 2);
946 check(10, 3, 1);
947 check(0, 3, 3);
948 check(3, 3, 3);
949 check(56, 42, 14);
950 check(3, -3, 3);
951 check(-6, 3, 3);
952 check(-4, -2, 2);
953 }
954
955 #[test]
test_lcm()956 fn test_lcm() {
957 fn check(a: isize, b: isize, c: isize) {
958 let big_a: BigInt = FromPrimitive::from_isize(a).unwrap();
959 let big_b: BigInt = FromPrimitive::from_isize(b).unwrap();
960 let big_c: BigInt = FromPrimitive::from_isize(c).unwrap();
961
962 assert_eq!(big_a.lcm(&big_b), big_c);
963 }
964
965 check(0, 0, 0);
966 check(1, 0, 0);
967 check(0, 1, 0);
968 check(1, 1, 1);
969 check(-1, 1, 1);
970 check(1, -1, 1);
971 check(-1, -1, 1);
972 check(8, 9, 72);
973 check(11, 5, 55);
974 }
975
976 #[test]
test_abs_sub()977 fn test_abs_sub() {
978 let zero: BigInt = Zero::zero();
979 let one: BigInt = One::one();
980 assert_eq!((-&one).abs_sub(&one), zero);
981 let one: BigInt = One::one();
982 let zero: BigInt = Zero::zero();
983 assert_eq!(one.abs_sub(&one), zero);
984 let one: BigInt = One::one();
985 let zero: BigInt = Zero::zero();
986 assert_eq!(one.abs_sub(&zero), one);
987 let one: BigInt = One::one();
988 let two: BigInt = FromPrimitive::from_isize(2).unwrap();
989 assert_eq!(one.abs_sub(&-&one), two);
990 }
991
992 #[test]
test_from_str_radix()993 fn test_from_str_radix() {
994 fn check(s: &str, ans: Option<isize>) {
995 let ans = ans.map(|n| {
996 let x: BigInt = FromPrimitive::from_isize(n).unwrap();
997 x
998 });
999 assert_eq!(BigInt::from_str_radix(s, 10).ok(), ans);
1000 }
1001 check("10", Some(10));
1002 check("1", Some(1));
1003 check("0", Some(0));
1004 check("-1", Some(-1));
1005 check("-10", Some(-10));
1006 check("+10", Some(10));
1007 check("--7", None);
1008 check("++5", None);
1009 check("+-9", None);
1010 check("-+3", None);
1011 check("Z", None);
1012 check("_", None);
1013
1014 // issue 10522, this hit an edge case that caused it to
1015 // attempt to allocate a vector of size (-1u) == huge.
1016 let x: BigInt = format!("1{}", repeat("0").take(36).collect::<String>())
1017 .parse()
1018 .unwrap();
1019 let _y = x.to_string();
1020 }
1021
1022 #[test]
test_lower_hex()1023 fn test_lower_hex() {
1024 let a = BigInt::parse_bytes(b"A", 16).unwrap();
1025 let hello = BigInt::parse_bytes("-22405534230753963835153736737".as_bytes(), 10).unwrap();
1026
1027 assert_eq!(format!("{:x}", a), "a");
1028 assert_eq!(format!("{:x}", hello), "-48656c6c6f20776f726c6421");
1029 assert_eq!(format!("{:♥>+#8x}", a), "♥♥♥♥+0xa");
1030 }
1031
1032 #[test]
test_upper_hex()1033 fn test_upper_hex() {
1034 let a = BigInt::parse_bytes(b"A", 16).unwrap();
1035 let hello = BigInt::parse_bytes("-22405534230753963835153736737".as_bytes(), 10).unwrap();
1036
1037 assert_eq!(format!("{:X}", a), "A");
1038 assert_eq!(format!("{:X}", hello), "-48656C6C6F20776F726C6421");
1039 assert_eq!(format!("{:♥>+#8X}", a), "♥♥♥♥+0xA");
1040 }
1041
1042 #[test]
test_binary()1043 fn test_binary() {
1044 let a = BigInt::parse_bytes(b"A", 16).unwrap();
1045 let hello = BigInt::parse_bytes("-224055342307539".as_bytes(), 10).unwrap();
1046
1047 assert_eq!(format!("{:b}", a), "1010");
1048 assert_eq!(
1049 format!("{:b}", hello),
1050 "-110010111100011011110011000101101001100011010011"
1051 );
1052 assert_eq!(format!("{:♥>+#8b}", a), "♥+0b1010");
1053 }
1054
1055 #[test]
test_octal()1056 fn test_octal() {
1057 let a = BigInt::parse_bytes(b"A", 16).unwrap();
1058 let hello = BigInt::parse_bytes("-22405534230753963835153736737".as_bytes(), 10).unwrap();
1059
1060 assert_eq!(format!("{:o}", a), "12");
1061 assert_eq!(format!("{:o}", hello), "-22062554330674403566756233062041");
1062 assert_eq!(format!("{:♥>+#8o}", a), "♥♥♥+0o12");
1063 }
1064
1065 #[test]
test_display()1066 fn test_display() {
1067 let a = BigInt::parse_bytes(b"A", 16).unwrap();
1068 let hello = BigInt::parse_bytes("-22405534230753963835153736737".as_bytes(), 10).unwrap();
1069
1070 assert_eq!(format!("{}", a), "10");
1071 assert_eq!(format!("{}", hello), "-22405534230753963835153736737");
1072 assert_eq!(format!("{:♥>+#8}", a), "♥♥♥♥♥+10");
1073 }
1074
1075 #[test]
test_neg()1076 fn test_neg() {
1077 assert!(-BigInt::new(Plus, vec![1, 1, 1]) == BigInt::new(Minus, vec![1, 1, 1]));
1078 assert!(-BigInt::new(Minus, vec![1, 1, 1]) == BigInt::new(Plus, vec![1, 1, 1]));
1079 let zero: BigInt = Zero::zero();
1080 assert_eq!(-&zero, zero);
1081 }
1082
1083 #[test]
test_negative_shr()1084 fn test_negative_shr() {
1085 assert_eq!(BigInt::from(-1) >> 1, BigInt::from(-1));
1086 assert_eq!(BigInt::from(-2) >> 1, BigInt::from(-1));
1087 assert_eq!(BigInt::from(-3) >> 1, BigInt::from(-2));
1088 assert_eq!(BigInt::from(-3) >> 2, BigInt::from(-1));
1089 }
1090
1091 #[test]
1092 #[cfg(feature = "rand")]
test_random_shr()1093 fn test_random_shr() {
1094 use rand::distributions::Standard;
1095 use rand::Rng;
1096 let mut rng = rand::thread_rng();
1097
1098 for p in rng.sample_iter::<i64, _>(&Standard).take(1000) {
1099 let big = BigInt::from(p);
1100 let bigger = &big << 1000;
1101 assert_eq!(&bigger >> 1000, big);
1102 for i in 0..64 {
1103 let answer = BigInt::from(p >> i);
1104 assert_eq!(&big >> i, answer);
1105 assert_eq!(&bigger >> (1000 + i), answer);
1106 }
1107 }
1108 }
1109
1110 #[test]
test_iter_sum()1111 fn test_iter_sum() {
1112 let result: BigInt = FromPrimitive::from_isize(-1234567).unwrap();
1113 let data: Vec<BigInt> = vec![
1114 FromPrimitive::from_i32(-1000000).unwrap(),
1115 FromPrimitive::from_i32(-200000).unwrap(),
1116 FromPrimitive::from_i32(-30000).unwrap(),
1117 FromPrimitive::from_i32(-4000).unwrap(),
1118 FromPrimitive::from_i32(-500).unwrap(),
1119 FromPrimitive::from_i32(-60).unwrap(),
1120 FromPrimitive::from_i32(-7).unwrap(),
1121 ];
1122
1123 assert_eq!(result, data.iter().sum());
1124 assert_eq!(result, data.into_iter().sum());
1125 }
1126
1127 #[test]
test_iter_product()1128 fn test_iter_product() {
1129 let data: Vec<BigInt> = vec![
1130 FromPrimitive::from_i32(1001).unwrap(),
1131 FromPrimitive::from_i32(-1002).unwrap(),
1132 FromPrimitive::from_i32(1003).unwrap(),
1133 FromPrimitive::from_i32(-1004).unwrap(),
1134 FromPrimitive::from_i32(1005).unwrap(),
1135 ];
1136 let result = data.get(0).unwrap()
1137 * data.get(1).unwrap()
1138 * data.get(2).unwrap()
1139 * data.get(3).unwrap()
1140 * data.get(4).unwrap();
1141
1142 assert_eq!(result, data.iter().product());
1143 assert_eq!(result, data.into_iter().product());
1144 }
1145
1146 #[test]
test_iter_sum_generic()1147 fn test_iter_sum_generic() {
1148 let result: BigInt = FromPrimitive::from_isize(-1234567).unwrap();
1149 let data = vec![-1000000, -200000, -30000, -4000, -500, -60, -7];
1150
1151 assert_eq!(result, data.iter().sum());
1152 assert_eq!(result, data.into_iter().sum());
1153 }
1154
1155 #[test]
test_iter_product_generic()1156 fn test_iter_product_generic() {
1157 let data = vec![1001, -1002, 1003, -1004, 1005];
1158 let result = data[0].to_bigint().unwrap()
1159 * data[1].to_bigint().unwrap()
1160 * data[2].to_bigint().unwrap()
1161 * data[3].to_bigint().unwrap()
1162 * data[4].to_bigint().unwrap();
1163
1164 assert_eq!(result, data.iter().product());
1165 assert_eq!(result, data.into_iter().product());
1166 }
1167
1168 #[test]
test_pow()1169 fn test_pow() {
1170 let one = BigInt::from(1i32);
1171 let two = BigInt::from(2i32);
1172 let four = BigInt::from(4i32);
1173 let eight = BigInt::from(8i32);
1174 let minus_two = BigInt::from(-2i32);
1175 macro_rules! check {
1176 ($t:ty) => {
1177 assert_eq!(two.pow(0 as $t), one);
1178 assert_eq!(two.pow(1 as $t), two);
1179 assert_eq!(two.pow(2 as $t), four);
1180 assert_eq!(two.pow(3 as $t), eight);
1181 assert_eq!(two.pow(&(3 as $t)), eight);
1182 assert_eq!(minus_two.pow(0 as $t), one, "-2^0");
1183 assert_eq!(minus_two.pow(1 as $t), minus_two, "-2^1");
1184 assert_eq!(minus_two.pow(2 as $t), four, "-2^2");
1185 assert_eq!(minus_two.pow(3 as $t), -&eight, "-2^3");
1186 };
1187 }
1188 check!(u8);
1189 check!(u16);
1190 check!(u32);
1191 check!(u64);
1192 check!(usize);
1193 }
1194