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