1 extern crate num_bigint;
2 extern crate num_integer;
3 extern crate num_traits;
4 
5 use num_bigint::Sign::Plus;
6 use num_bigint::{BigInt, ToBigInt};
7 use num_bigint::{BigUint, ToBigUint};
8 use num_integer::Integer;
9 
10 use std::cmp::Ordering::{Equal, Greater, Less};
11 use std::collections::hash_map::RandomState;
12 use std::hash::{BuildHasher, Hash, Hasher};
13 use std::i64;
14 use std::iter::repeat;
15 use std::str::FromStr;
16 use std::{f32, f64};
17 #[cfg(has_i128)]
18 use std::{i128, u128};
19 use std::{u16, u32, u64, u8, usize};
20 
21 use num_traits::{
22     CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, Float, FromPrimitive, Num, One, Pow,
23     ToPrimitive, Zero,
24 };
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             BigUint::from_bytes_be(s.as_bytes()),
37             BigUint::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!(BigUint::from_bytes_be(&[]), 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 = BigUint::parse_bytes(result.as_bytes(), 10).unwrap();
51         assert_eq!(b.to_bytes_be(), s.as_bytes());
52     }
53     check("A", "65");
54     check("AA", "16705");
55     check("AB", "16706");
56     check("Hello world!", "22405534230753963835153736737");
57     let b: BigUint = Zero::zero();
58     assert_eq!(b.to_bytes_be(), [0]);
59 
60     // Test with leading/trailing zero bytes and a full BigDigit of value 0
61     let b = BigUint::from_str_radix("00010000000000000200", 16).unwrap();
62     assert_eq!(b.to_bytes_be(), [1, 0, 0, 0, 0, 0, 0, 2, 0]);
63 }
64 
65 #[test]
test_from_bytes_le()66 fn test_from_bytes_le() {
67     fn check(s: &str, result: &str) {
68         assert_eq!(
69             BigUint::from_bytes_le(s.as_bytes()),
70             BigUint::parse_bytes(result.as_bytes(), 10).unwrap()
71         );
72     }
73     check("A", "65");
74     check("AA", "16705");
75     check("BA", "16706");
76     check("!dlrow olleH", "22405534230753963835153736737");
77     assert_eq!(BigUint::from_bytes_le(&[]), Zero::zero());
78 }
79 
80 #[test]
test_to_bytes_le()81 fn test_to_bytes_le() {
82     fn check(s: &str, result: &str) {
83         let b = BigUint::parse_bytes(result.as_bytes(), 10).unwrap();
84         assert_eq!(b.to_bytes_le(), s.as_bytes());
85     }
86     check("A", "65");
87     check("AA", "16705");
88     check("BA", "16706");
89     check("!dlrow olleH", "22405534230753963835153736737");
90     let b: BigUint = Zero::zero();
91     assert_eq!(b.to_bytes_le(), [0]);
92 
93     // Test with leading/trailing zero bytes and a full BigDigit of value 0
94     let b = BigUint::from_str_radix("00010000000000000200", 16).unwrap();
95     assert_eq!(b.to_bytes_le(), [0, 2, 0, 0, 0, 0, 0, 0, 1]);
96 }
97 
98 #[test]
test_cmp()99 fn test_cmp() {
100     let data: [&[_]; 7] = [&[], &[1], &[2], &[!0], &[0, 1], &[2, 1], &[1, 1, 1]];
101     let data: Vec<BigUint> = data.iter().map(|v| BigUint::from_slice(*v)).collect();
102     for (i, ni) in data.iter().enumerate() {
103         for (j0, nj) in data[i..].iter().enumerate() {
104             let j = j0 + i;
105             if i == j {
106                 assert_eq!(ni.cmp(nj), Equal);
107                 assert_eq!(nj.cmp(ni), Equal);
108                 assert_eq!(ni, nj);
109                 assert!(!(ni != nj));
110                 assert!(ni <= nj);
111                 assert!(ni >= nj);
112                 assert!(!(ni < nj));
113                 assert!(!(ni > nj));
114             } else {
115                 assert_eq!(ni.cmp(nj), Less);
116                 assert_eq!(nj.cmp(ni), Greater);
117 
118                 assert!(!(ni == nj));
119                 assert!(ni != nj);
120 
121                 assert!(ni <= nj);
122                 assert!(!(ni >= nj));
123                 assert!(ni < nj);
124                 assert!(!(ni > nj));
125 
126                 assert!(!(nj <= ni));
127                 assert!(nj >= ni);
128                 assert!(!(nj < ni));
129                 assert!(nj > ni);
130             }
131         }
132     }
133 }
134 
hash<T: Hash>(x: &T) -> u64135 fn hash<T: Hash>(x: &T) -> u64 {
136     let mut hasher = <RandomState as BuildHasher>::Hasher::new();
137     x.hash(&mut hasher);
138     hasher.finish()
139 }
140 
141 #[test]
test_hash()142 fn test_hash() {
143     use hash;
144 
145     let a = BigUint::new(vec![]);
146     let b = BigUint::new(vec![0]);
147     let c = BigUint::new(vec![1]);
148     let d = BigUint::new(vec![1, 0, 0, 0, 0, 0]);
149     let e = BigUint::new(vec![0, 0, 0, 0, 0, 1]);
150     assert!(hash(&a) == hash(&b));
151     assert!(hash(&b) != hash(&c));
152     assert!(hash(&c) == hash(&d));
153     assert!(hash(&d) != hash(&e));
154 }
155 
156 // LEFT, RIGHT, AND, OR, XOR
157 const BIT_TESTS: &'static [(
158     &'static [u32],
159     &'static [u32],
160     &'static [u32],
161     &'static [u32],
162     &'static [u32],
163 )] = &[
164     (&[], &[], &[], &[], &[]),
165     (&[1, 0, 1], &[1, 1], &[1], &[1, 1, 1], &[0, 1, 1]),
166     (&[1, 0, 1], &[0, 1, 1], &[0, 0, 1], &[1, 1, 1], &[1, 1]),
167     (
168         &[268, 482, 17],
169         &[964, 54],
170         &[260, 34],
171         &[972, 502, 17],
172         &[712, 468, 17],
173     ),
174 ];
175 
176 #[test]
test_bitand()177 fn test_bitand() {
178     for elm in BIT_TESTS {
179         let (a_vec, b_vec, c_vec, _, _) = *elm;
180         let a = BigUint::from_slice(a_vec);
181         let b = BigUint::from_slice(b_vec);
182         let c = BigUint::from_slice(c_vec);
183 
184         assert_op!(a & b == c);
185         assert_op!(b & a == c);
186         assert_assign_op!(a &= b == c);
187         assert_assign_op!(b &= a == c);
188     }
189 }
190 
191 #[test]
test_bitor()192 fn test_bitor() {
193     for elm in BIT_TESTS {
194         let (a_vec, b_vec, _, c_vec, _) = *elm;
195         let a = BigUint::from_slice(a_vec);
196         let b = BigUint::from_slice(b_vec);
197         let c = BigUint::from_slice(c_vec);
198 
199         assert_op!(a | b == c);
200         assert_op!(b | a == c);
201         assert_assign_op!(a |= b == c);
202         assert_assign_op!(b |= a == c);
203     }
204 }
205 
206 #[test]
test_bitxor()207 fn test_bitxor() {
208     for elm in BIT_TESTS {
209         let (a_vec, b_vec, _, _, c_vec) = *elm;
210         let a = BigUint::from_slice(a_vec);
211         let b = BigUint::from_slice(b_vec);
212         let c = BigUint::from_slice(c_vec);
213 
214         assert_op!(a ^ b == c);
215         assert_op!(b ^ a == c);
216         assert_op!(a ^ c == b);
217         assert_op!(c ^ a == b);
218         assert_op!(b ^ c == a);
219         assert_op!(c ^ b == a);
220         assert_assign_op!(a ^= b == c);
221         assert_assign_op!(b ^= a == c);
222         assert_assign_op!(a ^= c == b);
223         assert_assign_op!(c ^= a == b);
224         assert_assign_op!(b ^= c == a);
225         assert_assign_op!(c ^= b == a);
226     }
227 }
228 
229 #[test]
test_shl()230 fn test_shl() {
231     fn check(s: &str, shift: usize, ans: &str) {
232         let opt_biguint = BigUint::from_str_radix(s, 16).ok();
233         let mut bu_assign = opt_biguint.unwrap();
234         let bu = (bu_assign.clone() << shift).to_str_radix(16);
235         assert_eq!(bu, ans);
236         bu_assign <<= shift;
237         assert_eq!(bu_assign.to_str_radix(16), ans);
238     }
239 
240     check("0", 3, "0");
241     check("1", 3, "8");
242 
243     check(
244         "1\
245          0000\
246          0000\
247          0000\
248          0001\
249          0000\
250          0000\
251          0000\
252          0001",
253         3,
254         "8\
255          0000\
256          0000\
257          0000\
258          0008\
259          0000\
260          0000\
261          0000\
262          0008",
263     );
264     check(
265         "1\
266          0000\
267          0001\
268          0000\
269          0001",
270         2,
271         "4\
272          0000\
273          0004\
274          0000\
275          0004",
276     );
277     check(
278         "1\
279          0001\
280          0001",
281         1,
282         "2\
283          0002\
284          0002",
285     );
286 
287     check(
288         "\
289          4000\
290          0000\
291          0000\
292          0000",
293         3,
294         "2\
295          0000\
296          0000\
297          0000\
298          0000",
299     );
300     check(
301         "4000\
302          0000",
303         2,
304         "1\
305          0000\
306          0000",
307     );
308     check(
309         "4000",
310         2,
311         "1\
312          0000",
313     );
314 
315     check(
316         "4000\
317          0000\
318          0000\
319          0000",
320         67,
321         "2\
322          0000\
323          0000\
324          0000\
325          0000\
326          0000\
327          0000\
328          0000\
329          0000",
330     );
331     check(
332         "4000\
333          0000",
334         35,
335         "2\
336          0000\
337          0000\
338          0000\
339          0000",
340     );
341     check(
342         "4000",
343         19,
344         "2\
345          0000\
346          0000",
347     );
348 
349     check(
350         "fedc\
351          ba98\
352          7654\
353          3210\
354          fedc\
355          ba98\
356          7654\
357          3210",
358         4,
359         "f\
360          edcb\
361          a987\
362          6543\
363          210f\
364          edcb\
365          a987\
366          6543\
367          2100",
368     );
369     check(
370         "88887777666655554444333322221111",
371         16,
372         "888877776666555544443333222211110000",
373     );
374 }
375 
376 #[test]
test_shr()377 fn test_shr() {
378     fn check(s: &str, shift: usize, ans: &str) {
379         let opt_biguint = BigUint::from_str_radix(s, 16).ok();
380         let mut bu_assign = opt_biguint.unwrap();
381         let bu = (bu_assign.clone() >> shift).to_str_radix(16);
382         assert_eq!(bu, ans);
383         bu_assign >>= shift;
384         assert_eq!(bu_assign.to_str_radix(16), ans);
385     }
386 
387     check("0", 3, "0");
388     check("f", 3, "1");
389 
390     check(
391         "1\
392          0000\
393          0000\
394          0000\
395          0001\
396          0000\
397          0000\
398          0000\
399          0001",
400         3,
401         "2000\
402          0000\
403          0000\
404          0000\
405          2000\
406          0000\
407          0000\
408          0000",
409     );
410     check(
411         "1\
412          0000\
413          0001\
414          0000\
415          0001",
416         2,
417         "4000\
418          0000\
419          4000\
420          0000",
421     );
422     check(
423         "1\
424          0001\
425          0001",
426         1,
427         "8000\
428          8000",
429     );
430 
431     check(
432         "2\
433          0000\
434          0000\
435          0000\
436          0001\
437          0000\
438          0000\
439          0000\
440          0001",
441         67,
442         "4000\
443          0000\
444          0000\
445          0000",
446     );
447     check(
448         "2\
449          0000\
450          0001\
451          0000\
452          0001",
453         35,
454         "4000\
455          0000",
456     );
457     check(
458         "2\
459          0001\
460          0001",
461         19,
462         "4000",
463     );
464 
465     check(
466         "1\
467          0000\
468          0000\
469          0000\
470          0000",
471         1,
472         "8000\
473          0000\
474          0000\
475          0000",
476     );
477     check(
478         "1\
479          0000\
480          0000",
481         1,
482         "8000\
483          0000",
484     );
485     check(
486         "1\
487          0000",
488         1,
489         "8000",
490     );
491     check(
492         "f\
493          edcb\
494          a987\
495          6543\
496          210f\
497          edcb\
498          a987\
499          6543\
500          2100",
501         4,
502         "fedc\
503          ba98\
504          7654\
505          3210\
506          fedc\
507          ba98\
508          7654\
509          3210",
510     );
511 
512     check(
513         "888877776666555544443333222211110000",
514         16,
515         "88887777666655554444333322221111",
516     );
517 }
518 
519 // `DoubleBigDigit` size dependent
520 #[test]
test_convert_i64()521 fn test_convert_i64() {
522     fn check(b1: BigUint, i: i64) {
523         let b2: BigUint = FromPrimitive::from_i64(i).unwrap();
524         assert_eq!(b1, b2);
525         assert_eq!(b1.to_i64().unwrap(), i);
526     }
527 
528     check(Zero::zero(), 0);
529     check(One::one(), 1);
530     check(i64::MAX.to_biguint().unwrap(), i64::MAX);
531 
532     check(BigUint::new(vec![]), 0);
533     check(BigUint::new(vec![1]), 1);
534     check(BigUint::new(vec![N1]), (1 << 32) - 1);
535     check(BigUint::new(vec![0, 1]), 1 << 32);
536     check(BigUint::new(vec![N1, N1 >> 1]), i64::MAX);
537 
538     assert_eq!(i64::MIN.to_biguint(), None);
539     assert_eq!(BigUint::new(vec![N1, N1]).to_i64(), None);
540     assert_eq!(BigUint::new(vec![0, 0, 1]).to_i64(), None);
541     assert_eq!(BigUint::new(vec![N1, N1, N1]).to_i64(), None);
542 }
543 
544 #[test]
545 #[cfg(has_i128)]
test_convert_i128()546 fn test_convert_i128() {
547     fn check(b1: BigUint, i: i128) {
548         let b2: BigUint = FromPrimitive::from_i128(i).unwrap();
549         assert_eq!(b1, b2);
550         assert_eq!(b1.to_i128().unwrap(), i);
551     }
552 
553     check(Zero::zero(), 0);
554     check(One::one(), 1);
555     check(i128::MAX.to_biguint().unwrap(), i128::MAX);
556 
557     check(BigUint::new(vec![]), 0);
558     check(BigUint::new(vec![1]), 1);
559     check(BigUint::new(vec![N1]), (1 << 32) - 1);
560     check(BigUint::new(vec![0, 1]), 1 << 32);
561     check(BigUint::new(vec![N1, N1, N1, N1 >> 1]), i128::MAX);
562 
563     assert_eq!(i128::MIN.to_biguint(), None);
564     assert_eq!(BigUint::new(vec![N1, N1, N1, N1]).to_i128(), None);
565     assert_eq!(BigUint::new(vec![0, 0, 0, 0, 1]).to_i128(), None);
566     assert_eq!(BigUint::new(vec![N1, N1, N1, N1, N1]).to_i128(), None);
567 }
568 
569 // `DoubleBigDigit` size dependent
570 #[test]
test_convert_u64()571 fn test_convert_u64() {
572     fn check(b1: BigUint, u: u64) {
573         let b2: BigUint = FromPrimitive::from_u64(u).unwrap();
574         assert_eq!(b1, b2);
575         assert_eq!(b1.to_u64().unwrap(), u);
576     }
577 
578     check(Zero::zero(), 0);
579     check(One::one(), 1);
580     check(u64::MIN.to_biguint().unwrap(), u64::MIN);
581     check(u64::MAX.to_biguint().unwrap(), u64::MAX);
582 
583     check(BigUint::new(vec![]), 0);
584     check(BigUint::new(vec![1]), 1);
585     check(BigUint::new(vec![N1]), (1 << 32) - 1);
586     check(BigUint::new(vec![0, 1]), 1 << 32);
587     check(BigUint::new(vec![N1, N1]), u64::MAX);
588 
589     assert_eq!(BigUint::new(vec![0, 0, 1]).to_u64(), None);
590     assert_eq!(BigUint::new(vec![N1, N1, N1]).to_u64(), None);
591 }
592 
593 #[test]
594 #[cfg(has_i128)]
test_convert_u128()595 fn test_convert_u128() {
596     fn check(b1: BigUint, u: u128) {
597         let b2: BigUint = FromPrimitive::from_u128(u).unwrap();
598         assert_eq!(b1, b2);
599         assert_eq!(b1.to_u128().unwrap(), u);
600     }
601 
602     check(Zero::zero(), 0);
603     check(One::one(), 1);
604     check(u128::MIN.to_biguint().unwrap(), u128::MIN);
605     check(u128::MAX.to_biguint().unwrap(), u128::MAX);
606 
607     check(BigUint::new(vec![]), 0);
608     check(BigUint::new(vec![1]), 1);
609     check(BigUint::new(vec![N1]), (1 << 32) - 1);
610     check(BigUint::new(vec![0, 1]), 1 << 32);
611     check(BigUint::new(vec![N1, N1, N1, N1]), u128::MAX);
612 
613     assert_eq!(BigUint::new(vec![0, 0, 0, 0, 1]).to_u128(), None);
614     assert_eq!(BigUint::new(vec![N1, N1, N1, N1, N1]).to_u128(), None);
615 }
616 
617 #[test]
test_convert_f32()618 fn test_convert_f32() {
619     fn check(b1: &BigUint, f: f32) {
620         let b2 = BigUint::from_f32(f).unwrap();
621         assert_eq!(b1, &b2);
622         assert_eq!(b1.to_f32().unwrap(), f);
623     }
624 
625     check(&BigUint::zero(), 0.0);
626     check(&BigUint::one(), 1.0);
627     check(&BigUint::from(u16::MAX), 2.0.powi(16) - 1.0);
628     check(&BigUint::from(1u64 << 32), 2.0.powi(32));
629     check(&BigUint::from_slice(&[0, 0, 1]), 2.0.powi(64));
630     check(
631         &((BigUint::one() << 100) + (BigUint::one() << 123)),
632         2.0.powi(100) + 2.0.powi(123),
633     );
634     check(&(BigUint::one() << 127), 2.0.powi(127));
635     check(&(BigUint::from((1u64 << 24) - 1) << (128 - 24)), f32::MAX);
636 
637     // keeping all 24 digits with the bits at different offsets to the BigDigits
638     let x: u32 = 0b00000000101111011111011011011101;
639     let mut f = x as f32;
640     let mut b = BigUint::from(x);
641     for _ in 0..64 {
642         check(&b, f);
643         f *= 2.0;
644         b = b << 1;
645     }
646 
647     // this number when rounded to f64 then f32 isn't the same as when rounded straight to f32
648     let n: u64 = 0b0000000000111111111111111111111111011111111111111111111111111111;
649     assert!((n as f64) as f32 != n as f32);
650     assert_eq!(BigUint::from(n).to_f32(), Some(n as f32));
651 
652     // test rounding up with the bits at different offsets to the BigDigits
653     let mut f = ((1u64 << 25) - 1) as f32;
654     let mut b = BigUint::from(1u64 << 25);
655     for _ in 0..64 {
656         assert_eq!(b.to_f32(), Some(f));
657         f *= 2.0;
658         b = b << 1;
659     }
660 
661     // rounding
662     assert_eq!(BigUint::from_f32(-1.0), None);
663     assert_eq!(BigUint::from_f32(-0.99999), Some(BigUint::zero()));
664     assert_eq!(BigUint::from_f32(-0.5), Some(BigUint::zero()));
665     assert_eq!(BigUint::from_f32(-0.0), Some(BigUint::zero()));
666     assert_eq!(
667         BigUint::from_f32(f32::MIN_POSITIVE / 2.0),
668         Some(BigUint::zero())
669     );
670     assert_eq!(BigUint::from_f32(f32::MIN_POSITIVE), Some(BigUint::zero()));
671     assert_eq!(BigUint::from_f32(0.5), Some(BigUint::zero()));
672     assert_eq!(BigUint::from_f32(0.99999), Some(BigUint::zero()));
673     assert_eq!(BigUint::from_f32(f32::consts::E), Some(BigUint::from(2u32)));
674     assert_eq!(
675         BigUint::from_f32(f32::consts::PI),
676         Some(BigUint::from(3u32))
677     );
678 
679     // special float values
680     assert_eq!(BigUint::from_f32(f32::NAN), None);
681     assert_eq!(BigUint::from_f32(f32::INFINITY), None);
682     assert_eq!(BigUint::from_f32(f32::NEG_INFINITY), None);
683     assert_eq!(BigUint::from_f32(f32::MIN), None);
684 
685     // largest BigUint that will round to a finite f32 value
686     let big_num = (BigUint::one() << 128) - BigUint::one() - (BigUint::one() << (128 - 25));
687     assert_eq!(big_num.to_f32(), Some(f32::MAX));
688     assert_eq!((big_num + BigUint::one()).to_f32(), None);
689 
690     assert_eq!(((BigUint::one() << 128) - BigUint::one()).to_f32(), None);
691     assert_eq!((BigUint::one() << 128).to_f32(), None);
692 }
693 
694 #[test]
test_convert_f64()695 fn test_convert_f64() {
696     fn check(b1: &BigUint, f: f64) {
697         let b2 = BigUint::from_f64(f).unwrap();
698         assert_eq!(b1, &b2);
699         assert_eq!(b1.to_f64().unwrap(), f);
700     }
701 
702     check(&BigUint::zero(), 0.0);
703     check(&BigUint::one(), 1.0);
704     check(&BigUint::from(u32::MAX), 2.0.powi(32) - 1.0);
705     check(&BigUint::from(1u64 << 32), 2.0.powi(32));
706     check(&BigUint::from_slice(&[0, 0, 1]), 2.0.powi(64));
707     check(
708         &((BigUint::one() << 100) + (BigUint::one() << 152)),
709         2.0.powi(100) + 2.0.powi(152),
710     );
711     check(&(BigUint::one() << 1023), 2.0.powi(1023));
712     check(&(BigUint::from((1u64 << 53) - 1) << (1024 - 53)), f64::MAX);
713 
714     // keeping all 53 digits with the bits at different offsets to the BigDigits
715     let x: u64 = 0b0000000000011110111110110111111101110111101111011111011011011101;
716     let mut f = x as f64;
717     let mut b = BigUint::from(x);
718     for _ in 0..128 {
719         check(&b, f);
720         f *= 2.0;
721         b = b << 1;
722     }
723 
724     // test rounding up with the bits at different offsets to the BigDigits
725     let mut f = ((1u64 << 54) - 1) as f64;
726     let mut b = BigUint::from(1u64 << 54);
727     for _ in 0..128 {
728         assert_eq!(b.to_f64(), Some(f));
729         f *= 2.0;
730         b = b << 1;
731     }
732 
733     // rounding
734     assert_eq!(BigUint::from_f64(-1.0), None);
735     assert_eq!(BigUint::from_f64(-0.99999), Some(BigUint::zero()));
736     assert_eq!(BigUint::from_f64(-0.5), Some(BigUint::zero()));
737     assert_eq!(BigUint::from_f64(-0.0), Some(BigUint::zero()));
738     assert_eq!(
739         BigUint::from_f64(f64::MIN_POSITIVE / 2.0),
740         Some(BigUint::zero())
741     );
742     assert_eq!(BigUint::from_f64(f64::MIN_POSITIVE), Some(BigUint::zero()));
743     assert_eq!(BigUint::from_f64(0.5), Some(BigUint::zero()));
744     assert_eq!(BigUint::from_f64(0.99999), Some(BigUint::zero()));
745     assert_eq!(BigUint::from_f64(f64::consts::E), Some(BigUint::from(2u32)));
746     assert_eq!(
747         BigUint::from_f64(f64::consts::PI),
748         Some(BigUint::from(3u32))
749     );
750 
751     // special float values
752     assert_eq!(BigUint::from_f64(f64::NAN), None);
753     assert_eq!(BigUint::from_f64(f64::INFINITY), None);
754     assert_eq!(BigUint::from_f64(f64::NEG_INFINITY), None);
755     assert_eq!(BigUint::from_f64(f64::MIN), None);
756 
757     // largest BigUint that will round to a finite f64 value
758     let big_num = (BigUint::one() << 1024) - BigUint::one() - (BigUint::one() << (1024 - 54));
759     assert_eq!(big_num.to_f64(), Some(f64::MAX));
760     assert_eq!((big_num + BigUint::one()).to_f64(), None);
761 
762     assert_eq!(((BigInt::one() << 1024) - BigInt::one()).to_f64(), None);
763     assert_eq!((BigUint::one() << 1024).to_f64(), None);
764 }
765 
766 #[test]
test_convert_to_bigint()767 fn test_convert_to_bigint() {
768     fn check(n: BigUint, ans: BigInt) {
769         assert_eq!(n.to_bigint().unwrap(), ans);
770         assert_eq!(n.to_bigint().unwrap().to_biguint().unwrap(), n);
771     }
772     check(Zero::zero(), Zero::zero());
773     check(
774         BigUint::new(vec![1, 2, 3]),
775         BigInt::from_biguint(Plus, BigUint::new(vec![1, 2, 3])),
776     );
777 }
778 
779 #[test]
test_convert_from_uint()780 fn test_convert_from_uint() {
781     macro_rules! check {
782         ($ty:ident, $max:expr) => {
783             assert_eq!(BigUint::from($ty::zero()), BigUint::zero());
784             assert_eq!(BigUint::from($ty::one()), BigUint::one());
785             assert_eq!(BigUint::from($ty::MAX - $ty::one()), $max - BigUint::one());
786             assert_eq!(BigUint::from($ty::MAX), $max);
787         };
788     }
789 
790     check!(u8, BigUint::from_slice(&[u8::MAX as u32]));
791     check!(u16, BigUint::from_slice(&[u16::MAX as u32]));
792     check!(u32, BigUint::from_slice(&[u32::MAX]));
793     check!(u64, BigUint::from_slice(&[u32::MAX, u32::MAX]));
794     #[cfg(has_i128)]
795     check!(
796         u128,
797         BigUint::from_slice(&[u32::MAX, u32::MAX, u32::MAX, u32::MAX])
798     );
799     check!(usize, BigUint::from(usize::MAX as u64));
800 }
801 
802 #[test]
test_add()803 fn test_add() {
804     for elm in SUM_TRIPLES.iter() {
805         let (a_vec, b_vec, c_vec) = *elm;
806         let a = BigUint::from_slice(a_vec);
807         let b = BigUint::from_slice(b_vec);
808         let c = BigUint::from_slice(c_vec);
809 
810         assert_op!(a + b == c);
811         assert_op!(b + a == c);
812         assert_assign_op!(a += b == c);
813         assert_assign_op!(b += a == c);
814     }
815 }
816 
817 #[test]
test_sub()818 fn test_sub() {
819     for elm in SUM_TRIPLES.iter() {
820         let (a_vec, b_vec, c_vec) = *elm;
821         let a = BigUint::from_slice(a_vec);
822         let b = BigUint::from_slice(b_vec);
823         let c = BigUint::from_slice(c_vec);
824 
825         assert_op!(c - a == b);
826         assert_op!(c - b == a);
827         assert_assign_op!(c -= a == b);
828         assert_assign_op!(c -= b == a);
829     }
830 }
831 
832 #[test]
833 #[should_panic]
test_sub_fail_on_underflow()834 fn test_sub_fail_on_underflow() {
835     let (a, b): (BigUint, BigUint) = (Zero::zero(), One::one());
836     let _ = a - b;
837 }
838 
839 #[test]
test_mul()840 fn test_mul() {
841     for elm in MUL_TRIPLES.iter() {
842         let (a_vec, b_vec, c_vec) = *elm;
843         let a = BigUint::from_slice(a_vec);
844         let b = BigUint::from_slice(b_vec);
845         let c = BigUint::from_slice(c_vec);
846 
847         assert_op!(a * b == c);
848         assert_op!(b * a == c);
849         assert_assign_op!(a *= b == c);
850         assert_assign_op!(b *= a == c);
851     }
852 
853     for elm in DIV_REM_QUADRUPLES.iter() {
854         let (a_vec, b_vec, c_vec, d_vec) = *elm;
855         let a = BigUint::from_slice(a_vec);
856         let b = BigUint::from_slice(b_vec);
857         let c = BigUint::from_slice(c_vec);
858         let d = BigUint::from_slice(d_vec);
859 
860         assert!(a == &b * &c + &d);
861         assert!(a == &c * &b + &d);
862     }
863 }
864 
865 #[test]
test_div_rem()866 fn test_div_rem() {
867     for elm in MUL_TRIPLES.iter() {
868         let (a_vec, b_vec, c_vec) = *elm;
869         let a = BigUint::from_slice(a_vec);
870         let b = BigUint::from_slice(b_vec);
871         let c = BigUint::from_slice(c_vec);
872 
873         if !a.is_zero() {
874             assert_op!(c / a == b);
875             assert_op!(c % a == Zero::zero());
876             assert_assign_op!(c /= a == b);
877             assert_assign_op!(c %= a == Zero::zero());
878             assert_eq!(c.div_rem(&a), (b.clone(), Zero::zero()));
879         }
880         if !b.is_zero() {
881             assert_op!(c / b == a);
882             assert_op!(c % b == Zero::zero());
883             assert_assign_op!(c /= b == a);
884             assert_assign_op!(c %= b == Zero::zero());
885             assert_eq!(c.div_rem(&b), (a.clone(), Zero::zero()));
886         }
887     }
888 
889     for elm in DIV_REM_QUADRUPLES.iter() {
890         let (a_vec, b_vec, c_vec, d_vec) = *elm;
891         let a = BigUint::from_slice(a_vec);
892         let b = BigUint::from_slice(b_vec);
893         let c = BigUint::from_slice(c_vec);
894         let d = BigUint::from_slice(d_vec);
895 
896         if !b.is_zero() {
897             assert_op!(a / b == c);
898             assert_op!(a % b == d);
899             assert_assign_op!(a /= b == c);
900             assert_assign_op!(a %= b == d);
901             assert!(a.div_rem(&b) == (c, d));
902         }
903     }
904 }
905 
906 #[test]
test_checked_add()907 fn test_checked_add() {
908     for elm in SUM_TRIPLES.iter() {
909         let (a_vec, b_vec, c_vec) = *elm;
910         let a = BigUint::from_slice(a_vec);
911         let b = BigUint::from_slice(b_vec);
912         let c = BigUint::from_slice(c_vec);
913 
914         assert!(a.checked_add(&b).unwrap() == c);
915         assert!(b.checked_add(&a).unwrap() == c);
916     }
917 }
918 
919 #[test]
test_checked_sub()920 fn test_checked_sub() {
921     for elm in SUM_TRIPLES.iter() {
922         let (a_vec, b_vec, c_vec) = *elm;
923         let a = BigUint::from_slice(a_vec);
924         let b = BigUint::from_slice(b_vec);
925         let c = BigUint::from_slice(c_vec);
926 
927         assert!(c.checked_sub(&a).unwrap() == b);
928         assert!(c.checked_sub(&b).unwrap() == a);
929 
930         if a > c {
931             assert!(a.checked_sub(&c).is_none());
932         }
933         if b > c {
934             assert!(b.checked_sub(&c).is_none());
935         }
936     }
937 }
938 
939 #[test]
test_checked_mul()940 fn test_checked_mul() {
941     for elm in MUL_TRIPLES.iter() {
942         let (a_vec, b_vec, c_vec) = *elm;
943         let a = BigUint::from_slice(a_vec);
944         let b = BigUint::from_slice(b_vec);
945         let c = BigUint::from_slice(c_vec);
946 
947         assert!(a.checked_mul(&b).unwrap() == c);
948         assert!(b.checked_mul(&a).unwrap() == c);
949     }
950 
951     for elm in DIV_REM_QUADRUPLES.iter() {
952         let (a_vec, b_vec, c_vec, d_vec) = *elm;
953         let a = BigUint::from_slice(a_vec);
954         let b = BigUint::from_slice(b_vec);
955         let c = BigUint::from_slice(c_vec);
956         let d = BigUint::from_slice(d_vec);
957 
958         assert!(a == b.checked_mul(&c).unwrap() + &d);
959         assert!(a == c.checked_mul(&b).unwrap() + &d);
960     }
961 }
962 
963 #[test]
test_mul_overflow()964 fn test_mul_overflow() {
965     /* Test for issue #187 - overflow due to mac3 incorrectly sizing temporary */
966     let s = "531137992816767098689588206552468627329593117727031923199444138200403559860852242739162502232636710047537552105951370000796528760829212940754539968588340162273730474622005920097370111";
967     let a: BigUint = s.parse().unwrap();
968     let b = a.clone();
969     let _ = a.checked_mul(&b);
970 }
971 
972 #[test]
test_checked_div()973 fn test_checked_div() {
974     for elm in MUL_TRIPLES.iter() {
975         let (a_vec, b_vec, c_vec) = *elm;
976         let a = BigUint::from_slice(a_vec);
977         let b = BigUint::from_slice(b_vec);
978         let c = BigUint::from_slice(c_vec);
979 
980         if !a.is_zero() {
981             assert!(c.checked_div(&a).unwrap() == b);
982         }
983         if !b.is_zero() {
984             assert!(c.checked_div(&b).unwrap() == a);
985         }
986 
987         assert!(c.checked_div(&Zero::zero()).is_none());
988     }
989 }
990 
991 #[test]
test_gcd()992 fn test_gcd() {
993     fn check(a: usize, b: usize, c: usize) {
994         let big_a: BigUint = FromPrimitive::from_usize(a).unwrap();
995         let big_b: BigUint = FromPrimitive::from_usize(b).unwrap();
996         let big_c: BigUint = FromPrimitive::from_usize(c).unwrap();
997 
998         assert_eq!(big_a.gcd(&big_b), big_c);
999     }
1000 
1001     check(10, 2, 2);
1002     check(10, 3, 1);
1003     check(0, 3, 3);
1004     check(3, 3, 3);
1005     check(56, 42, 14);
1006 }
1007 
1008 #[test]
test_lcm()1009 fn test_lcm() {
1010     fn check(a: usize, b: usize, c: usize) {
1011         let big_a: BigUint = FromPrimitive::from_usize(a).unwrap();
1012         let big_b: BigUint = FromPrimitive::from_usize(b).unwrap();
1013         let big_c: BigUint = FromPrimitive::from_usize(c).unwrap();
1014 
1015         assert_eq!(big_a.lcm(&big_b), big_c);
1016     }
1017 
1018     check(0, 0, 0);
1019     check(1, 0, 0);
1020     check(0, 1, 0);
1021     check(1, 1, 1);
1022     check(8, 9, 72);
1023     check(11, 5, 55);
1024     check(99, 17, 1683);
1025 }
1026 
1027 #[test]
test_is_even()1028 fn test_is_even() {
1029     let one: BigUint = FromStr::from_str("1").unwrap();
1030     let two: BigUint = FromStr::from_str("2").unwrap();
1031     let thousand: BigUint = FromStr::from_str("1000").unwrap();
1032     let big: BigUint = FromStr::from_str("1000000000000000000000").unwrap();
1033     let bigger: BigUint = FromStr::from_str("1000000000000000000001").unwrap();
1034     assert!(one.is_odd());
1035     assert!(two.is_even());
1036     assert!(thousand.is_even());
1037     assert!(big.is_even());
1038     assert!(bigger.is_odd());
1039     assert!((&one << 64).is_even());
1040     assert!(((&one << 64) + one).is_odd());
1041 }
1042 
to_str_pairs() -> Vec<(BigUint, Vec<(u32, String)>)>1043 fn to_str_pairs() -> Vec<(BigUint, Vec<(u32, String)>)> {
1044     let bits = 32;
1045     vec![
1046         (
1047             Zero::zero(),
1048             vec![(2, "0".to_string()), (3, "0".to_string())],
1049         ),
1050         (
1051             BigUint::from_slice(&[0xff]),
1052             vec![
1053                 (2, "11111111".to_string()),
1054                 (3, "100110".to_string()),
1055                 (4, "3333".to_string()),
1056                 (5, "2010".to_string()),
1057                 (6, "1103".to_string()),
1058                 (7, "513".to_string()),
1059                 (8, "377".to_string()),
1060                 (9, "313".to_string()),
1061                 (10, "255".to_string()),
1062                 (11, "212".to_string()),
1063                 (12, "193".to_string()),
1064                 (13, "168".to_string()),
1065                 (14, "143".to_string()),
1066                 (15, "120".to_string()),
1067                 (16, "ff".to_string()),
1068             ],
1069         ),
1070         (
1071             BigUint::from_slice(&[0xfff]),
1072             vec![
1073                 (2, "111111111111".to_string()),
1074                 (4, "333333".to_string()),
1075                 (16, "fff".to_string()),
1076             ],
1077         ),
1078         (
1079             BigUint::from_slice(&[1, 2]),
1080             vec![
1081                 (
1082                     2,
1083                     format!("10{}1", repeat("0").take(bits - 1).collect::<String>()),
1084                 ),
1085                 (
1086                     4,
1087                     format!("2{}1", repeat("0").take(bits / 2 - 1).collect::<String>()),
1088                 ),
1089                 (
1090                     10,
1091                     match bits {
1092                         64 => "36893488147419103233".to_string(),
1093                         32 => "8589934593".to_string(),
1094                         16 => "131073".to_string(),
1095                         _ => panic!(),
1096                     },
1097                 ),
1098                 (
1099                     16,
1100                     format!("2{}1", repeat("0").take(bits / 4 - 1).collect::<String>()),
1101                 ),
1102             ],
1103         ),
1104         (
1105             BigUint::from_slice(&[1, 2, 3]),
1106             vec![
1107                 (
1108                     2,
1109                     format!(
1110                         "11{}10{}1",
1111                         repeat("0").take(bits - 2).collect::<String>(),
1112                         repeat("0").take(bits - 1).collect::<String>()
1113                     ),
1114                 ),
1115                 (
1116                     4,
1117                     format!(
1118                         "3{}2{}1",
1119                         repeat("0").take(bits / 2 - 1).collect::<String>(),
1120                         repeat("0").take(bits / 2 - 1).collect::<String>()
1121                     ),
1122                 ),
1123                 (
1124                     8,
1125                     match bits {
1126                         64 => "14000000000000000000004000000000000000000001".to_string(),
1127                         32 => "6000000000100000000001".to_string(),
1128                         16 => "140000400001".to_string(),
1129                         _ => panic!(),
1130                     },
1131                 ),
1132                 (
1133                     10,
1134                     match bits {
1135                         64 => "1020847100762815390427017310442723737601".to_string(),
1136                         32 => "55340232229718589441".to_string(),
1137                         16 => "12885032961".to_string(),
1138                         _ => panic!(),
1139                     },
1140                 ),
1141                 (
1142                     16,
1143                     format!(
1144                         "3{}2{}1",
1145                         repeat("0").take(bits / 4 - 1).collect::<String>(),
1146                         repeat("0").take(bits / 4 - 1).collect::<String>()
1147                     ),
1148                 ),
1149             ],
1150         ),
1151     ]
1152 }
1153 
1154 #[test]
test_to_str_radix()1155 fn test_to_str_radix() {
1156     let r = to_str_pairs();
1157     for num_pair in r.iter() {
1158         let &(ref n, ref rs) = num_pair;
1159         for str_pair in rs.iter() {
1160             let &(ref radix, ref str) = str_pair;
1161             assert_eq!(n.to_str_radix(*radix), *str);
1162         }
1163     }
1164 }
1165 
1166 #[test]
test_from_and_to_radix()1167 fn test_from_and_to_radix() {
1168     const GROUND_TRUTH: &'static [(&'static [u8], u32, &'static [u8])] = &[
1169         (b"0", 42, &[0]),
1170         (
1171             b"ffffeeffbb",
1172             2,
1173             &[
1174                 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,
1175                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1176             ],
1177         ),
1178         (
1179             b"ffffeeffbb",
1180             3,
1181             &[
1182                 2, 2, 1, 1, 2, 1, 1, 2, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 1, 0, 0, 2, 2, 0, 1,
1183             ],
1184         ),
1185         (
1186             b"ffffeeffbb",
1187             4,
1188             &[3, 2, 3, 2, 3, 3, 3, 3, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3],
1189         ),
1190         (
1191             b"ffffeeffbb",
1192             5,
1193             &[0, 4, 3, 3, 1, 4, 2, 4, 1, 4, 4, 2, 3, 0, 0, 1, 2, 1],
1194         ),
1195         (
1196             b"ffffeeffbb",
1197             6,
1198             &[5, 5, 4, 5, 5, 0, 0, 1, 2, 5, 3, 0, 1, 0, 2, 2],
1199         ),
1200         (
1201             b"ffffeeffbb",
1202             7,
1203             &[4, 2, 3, 6, 0, 1, 6, 1, 6, 2, 0, 3, 2, 4, 1],
1204         ),
1205         (
1206             b"ffffeeffbb",
1207             8,
1208             &[3, 7, 6, 7, 7, 5, 3, 7, 7, 7, 7, 7, 7, 1],
1209         ),
1210         (b"ffffeeffbb", 9, &[8, 4, 5, 7, 0, 0, 3, 2, 0, 3, 0, 8, 3]),
1211         (b"ffffeeffbb", 10, &[5, 9, 5, 3, 1, 5, 0, 1, 5, 9, 9, 0, 1]),
1212         (b"ffffeeffbb", 11, &[10, 7, 6, 5, 2, 0, 3, 3, 3, 4, 9, 3]),
1213         (b"ffffeeffbb", 12, &[11, 8, 5, 10, 1, 10, 3, 1, 1, 9, 5, 1]),
1214         (b"ffffeeffbb", 13, &[0, 5, 7, 4, 6, 5, 6, 11, 8, 12, 7]),
1215         (b"ffffeeffbb", 14, &[11, 4, 4, 11, 8, 4, 6, 0, 3, 11, 3]),
1216         (b"ffffeeffbb", 15, &[5, 11, 13, 2, 1, 10, 2, 0, 9, 13, 1]),
1217         (b"ffffeeffbb", 16, &[11, 11, 15, 15, 14, 14, 15, 15, 15, 15]),
1218         (b"ffffeeffbb", 17, &[0, 2, 14, 12, 2, 14, 8, 10, 4, 9]),
1219         (b"ffffeeffbb", 18, &[17, 15, 5, 13, 10, 16, 16, 13, 9, 5]),
1220         (b"ffffeeffbb", 19, &[14, 13, 2, 8, 9, 0, 1, 14, 7, 3]),
1221         (b"ffffeeffbb", 20, &[15, 19, 3, 14, 0, 17, 19, 18, 2, 2]),
1222         (b"ffffeeffbb", 21, &[11, 5, 4, 13, 5, 18, 9, 1, 8, 1]),
1223         (b"ffffeeffbb", 22, &[21, 3, 7, 21, 15, 12, 17, 0, 20]),
1224         (b"ffffeeffbb", 23, &[21, 21, 6, 9, 10, 7, 21, 0, 14]),
1225         (b"ffffeeffbb", 24, &[11, 10, 19, 14, 22, 11, 17, 23, 9]),
1226         (b"ffffeeffbb", 25, &[20, 18, 21, 22, 21, 14, 3, 5, 7]),
1227         (b"ffffeeffbb", 26, &[13, 15, 24, 11, 17, 6, 23, 6, 5]),
1228         (b"ffffeeffbb", 27, &[17, 16, 7, 0, 21, 0, 3, 24, 3]),
1229         (b"ffffeeffbb", 28, &[11, 16, 11, 15, 14, 18, 13, 25, 2]),
1230         (b"ffffeeffbb", 29, &[6, 8, 7, 19, 14, 13, 21, 5, 2]),
1231         (b"ffffeeffbb", 30, &[5, 13, 18, 11, 10, 7, 8, 20, 1]),
1232         (b"ffffeeffbb", 31, &[22, 26, 15, 19, 8, 27, 29, 8, 1]),
1233         (b"ffffeeffbb", 32, &[27, 29, 31, 29, 30, 31, 31, 31]),
1234         (b"ffffeeffbb", 33, &[32, 20, 27, 12, 1, 12, 26, 25]),
1235         (b"ffffeeffbb", 34, &[17, 9, 16, 33, 13, 25, 31, 20]),
1236         (b"ffffeeffbb", 35, &[25, 32, 2, 25, 11, 4, 3, 17]),
1237         (b"ffffeeffbb", 36, &[35, 34, 5, 6, 32, 3, 1, 14]),
1238         (b"ffffeeffbb", 37, &[16, 21, 18, 4, 33, 19, 21, 11]),
1239         (b"ffffeeffbb", 38, &[33, 25, 19, 29, 20, 6, 23, 9]),
1240         (b"ffffeeffbb", 39, &[26, 27, 29, 23, 16, 18, 0, 8]),
1241         (b"ffffeeffbb", 40, &[35, 39, 30, 11, 16, 17, 28, 6]),
1242         (b"ffffeeffbb", 41, &[36, 30, 9, 18, 12, 19, 26, 5]),
1243         (b"ffffeeffbb", 42, &[11, 34, 37, 27, 1, 13, 32, 4]),
1244         (b"ffffeeffbb", 43, &[3, 24, 11, 2, 10, 40, 1, 4]),
1245         (b"ffffeeffbb", 44, &[43, 12, 40, 32, 3, 23, 19, 3]),
1246         (b"ffffeeffbb", 45, &[35, 38, 44, 18, 22, 18, 42, 2]),
1247         (b"ffffeeffbb", 46, &[21, 45, 18, 41, 17, 2, 24, 2]),
1248         (b"ffffeeffbb", 47, &[37, 37, 11, 12, 6, 0, 8, 2]),
1249         (b"ffffeeffbb", 48, &[11, 41, 40, 43, 5, 43, 41, 1]),
1250         (b"ffffeeffbb", 49, &[18, 45, 7, 13, 20, 21, 30, 1]),
1251         (b"ffffeeffbb", 50, &[45, 21, 5, 34, 21, 18, 20, 1]),
1252         (b"ffffeeffbb", 51, &[17, 6, 26, 22, 38, 24, 11, 1]),
1253         (b"ffffeeffbb", 52, &[39, 33, 38, 30, 46, 31, 3, 1]),
1254         (b"ffffeeffbb", 53, &[31, 7, 44, 23, 9, 32, 49]),
1255         (b"ffffeeffbb", 54, &[17, 35, 8, 37, 31, 18, 44]),
1256         (b"ffffeeffbb", 55, &[10, 52, 9, 48, 36, 39, 39]),
1257         (b"ffffeeffbb", 56, &[11, 50, 51, 22, 25, 36, 35]),
1258         (b"ffffeeffbb", 57, &[14, 55, 12, 43, 20, 3, 32]),
1259         (b"ffffeeffbb", 58, &[35, 18, 45, 56, 9, 51, 28]),
1260         (b"ffffeeffbb", 59, &[51, 28, 20, 26, 55, 3, 26]),
1261         (b"ffffeeffbb", 60, &[35, 6, 27, 46, 58, 33, 23]),
1262         (b"ffffeeffbb", 61, &[58, 7, 6, 54, 49, 20, 21]),
1263         (b"ffffeeffbb", 62, &[53, 59, 3, 14, 10, 22, 19]),
1264         (b"ffffeeffbb", 63, &[53, 50, 23, 4, 56, 36, 17]),
1265         (b"ffffeeffbb", 64, &[59, 62, 47, 59, 63, 63, 15]),
1266         (b"ffffeeffbb", 65, &[0, 53, 39, 4, 40, 37, 14]),
1267         (b"ffffeeffbb", 66, &[65, 59, 39, 1, 64, 19, 13]),
1268         (b"ffffeeffbb", 67, &[35, 14, 19, 16, 25, 10, 12]),
1269         (b"ffffeeffbb", 68, &[51, 38, 63, 50, 15, 8, 11]),
1270         (b"ffffeeffbb", 69, &[44, 45, 18, 58, 68, 12, 10]),
1271         (b"ffffeeffbb", 70, &[25, 51, 0, 60, 13, 24, 9]),
1272         (b"ffffeeffbb", 71, &[54, 30, 9, 65, 28, 41, 8]),
1273         (b"ffffeeffbb", 72, &[35, 35, 55, 54, 17, 64, 7]),
1274         (b"ffffeeffbb", 73, &[34, 4, 48, 40, 27, 19, 7]),
1275         (b"ffffeeffbb", 74, &[53, 47, 4, 56, 36, 51, 6]),
1276         (b"ffffeeffbb", 75, &[20, 56, 10, 72, 24, 13, 6]),
1277         (b"ffffeeffbb", 76, &[71, 31, 52, 60, 48, 53, 5]),
1278         (b"ffffeeffbb", 77, &[32, 73, 14, 63, 15, 21, 5]),
1279         (b"ffffeeffbb", 78, &[65, 13, 17, 32, 64, 68, 4]),
1280         (b"ffffeeffbb", 79, &[37, 56, 2, 56, 25, 41, 4]),
1281         (b"ffffeeffbb", 80, &[75, 59, 37, 41, 43, 15, 4]),
1282         (b"ffffeeffbb", 81, &[44, 68, 0, 21, 27, 72, 3]),
1283         (b"ffffeeffbb", 82, &[77, 35, 2, 74, 46, 50, 3]),
1284         (b"ffffeeffbb", 83, &[52, 51, 19, 76, 10, 30, 3]),
1285         (b"ffffeeffbb", 84, &[11, 80, 19, 19, 76, 10, 3]),
1286         (b"ffffeeffbb", 85, &[0, 82, 20, 14, 68, 77, 2]),
1287         (b"ffffeeffbb", 86, &[3, 12, 78, 37, 62, 61, 2]),
1288         (b"ffffeeffbb", 87, &[35, 12, 20, 8, 52, 46, 2]),
1289         (b"ffffeeffbb", 88, &[43, 6, 54, 42, 30, 32, 2]),
1290         (b"ffffeeffbb", 89, &[49, 52, 85, 21, 80, 18, 2]),
1291         (b"ffffeeffbb", 90, &[35, 64, 78, 24, 18, 6, 2]),
1292         (b"ffffeeffbb", 91, &[39, 17, 83, 63, 17, 85, 1]),
1293         (b"ffffeeffbb", 92, &[67, 22, 85, 79, 75, 74, 1]),
1294         (b"ffffeeffbb", 93, &[53, 60, 39, 29, 4, 65, 1]),
1295         (b"ffffeeffbb", 94, &[37, 89, 2, 72, 76, 55, 1]),
1296         (b"ffffeeffbb", 95, &[90, 74, 89, 9, 9, 47, 1]),
1297         (b"ffffeeffbb", 96, &[59, 20, 46, 35, 81, 38, 1]),
1298         (b"ffffeeffbb", 97, &[94, 87, 60, 71, 3, 31, 1]),
1299         (b"ffffeeffbb", 98, &[67, 22, 63, 50, 62, 23, 1]),
1300         (b"ffffeeffbb", 99, &[98, 6, 69, 12, 61, 16, 1]),
1301         (b"ffffeeffbb", 100, &[95, 35, 51, 10, 95, 9, 1]),
1302         (b"ffffeeffbb", 101, &[87, 27, 7, 8, 62, 3, 1]),
1303         (b"ffffeeffbb", 102, &[17, 3, 32, 79, 59, 99]),
1304         (b"ffffeeffbb", 103, &[30, 22, 90, 0, 87, 94]),
1305         (b"ffffeeffbb", 104, &[91, 68, 87, 68, 38, 90]),
1306         (b"ffffeeffbb", 105, &[95, 80, 54, 73, 15, 86]),
1307         (b"ffffeeffbb", 106, &[31, 30, 24, 16, 17, 82]),
1308         (b"ffffeeffbb", 107, &[51, 50, 10, 12, 42, 78]),
1309         (b"ffffeeffbb", 108, &[71, 71, 96, 78, 89, 74]),
1310         (b"ffffeeffbb", 109, &[33, 18, 93, 22, 50, 71]),
1311         (b"ffffeeffbb", 110, &[65, 53, 57, 88, 29, 68]),
1312         (b"ffffeeffbb", 111, &[53, 93, 67, 90, 27, 65]),
1313         (b"ffffeeffbb", 112, &[11, 109, 96, 65, 43, 62]),
1314         (b"ffffeeffbb", 113, &[27, 23, 106, 56, 76, 59]),
1315         (b"ffffeeffbb", 114, &[71, 84, 31, 112, 11, 57]),
1316         (b"ffffeeffbb", 115, &[90, 22, 1, 56, 76, 54]),
1317         (b"ffffeeffbb", 116, &[35, 38, 98, 57, 40, 52]),
1318         (b"ffffeeffbb", 117, &[26, 113, 115, 62, 17, 50]),
1319         (b"ffffeeffbb", 118, &[51, 14, 5, 18, 7, 48]),
1320         (b"ffffeeffbb", 119, &[102, 31, 110, 108, 8, 46]),
1321         (b"ffffeeffbb", 120, &[35, 93, 96, 50, 22, 44]),
1322         (b"ffffeeffbb", 121, &[87, 61, 2, 36, 47, 42]),
1323         (b"ffffeeffbb", 122, &[119, 64, 1, 22, 83, 40]),
1324         (b"ffffeeffbb", 123, &[77, 119, 32, 90, 6, 39]),
1325         (b"ffffeeffbb", 124, &[115, 122, 31, 79, 62, 37]),
1326         (b"ffffeeffbb", 125, &[95, 108, 47, 74, 3, 36]),
1327         (b"ffffeeffbb", 126, &[53, 25, 116, 39, 78, 34]),
1328         (b"ffffeeffbb", 127, &[22, 23, 125, 67, 35, 33]),
1329         (b"ffffeeffbb", 128, &[59, 127, 59, 127, 127, 31]),
1330         (b"ffffeeffbb", 129, &[89, 36, 1, 59, 100, 30]),
1331         (b"ffffeeffbb", 130, &[65, 91, 123, 89, 79, 29]),
1332         (b"ffffeeffbb", 131, &[58, 72, 39, 63, 65, 28]),
1333         (b"ffffeeffbb", 132, &[131, 62, 92, 82, 57, 27]),
1334         (b"ffffeeffbb", 133, &[109, 31, 51, 123, 55, 26]),
1335         (b"ffffeeffbb", 134, &[35, 74, 21, 27, 60, 25]),
1336         (b"ffffeeffbb", 135, &[125, 132, 49, 37, 70, 24]),
1337         (b"ffffeeffbb", 136, &[51, 121, 117, 133, 85, 23]),
1338         (b"ffffeeffbb", 137, &[113, 60, 135, 22, 107, 22]),
1339         (b"ffffeeffbb", 138, &[113, 91, 73, 93, 133, 21]),
1340         (b"ffffeeffbb", 139, &[114, 75, 102, 51, 26, 21]),
1341         (b"ffffeeffbb", 140, &[95, 25, 35, 16, 62, 20]),
1342         (b"ffffeeffbb", 141, &[131, 137, 16, 110, 102, 19]),
1343         (b"ffffeeffbb", 142, &[125, 121, 108, 34, 6, 19]),
1344         (b"ffffeeffbb", 143, &[65, 78, 138, 55, 55, 18]),
1345         (b"ffffeeffbb", 144, &[107, 125, 121, 15, 109, 17]),
1346         (b"ffffeeffbb", 145, &[35, 13, 122, 42, 22, 17]),
1347         (b"ffffeeffbb", 146, &[107, 38, 103, 123, 83, 16]),
1348         (b"ffffeeffbb", 147, &[116, 96, 71, 98, 2, 16]),
1349         (b"ffffeeffbb", 148, &[127, 23, 75, 99, 71, 15]),
1350         (b"ffffeeffbb", 149, &[136, 110, 53, 114, 144, 14]),
1351         (b"ffffeeffbb", 150, &[95, 140, 133, 130, 71, 14]),
1352         (b"ffffeeffbb", 151, &[15, 50, 29, 137, 0, 14]),
1353         (b"ffffeeffbb", 152, &[147, 15, 89, 121, 83, 13]),
1354         (b"ffffeeffbb", 153, &[17, 87, 93, 72, 17, 13]),
1355         (b"ffffeeffbb", 154, &[109, 113, 3, 133, 106, 12]),
1356         (b"ffffeeffbb", 155, &[115, 141, 120, 139, 44, 12]),
1357         (b"ffffeeffbb", 156, &[143, 45, 4, 82, 140, 11]),
1358         (b"ffffeeffbb", 157, &[149, 92, 15, 106, 82, 11]),
1359         (b"ffffeeffbb", 158, &[37, 107, 79, 46, 26, 11]),
1360         (b"ffffeeffbb", 159, &[137, 37, 146, 51, 130, 10]),
1361         (b"ffffeeffbb", 160, &[155, 69, 29, 115, 77, 10]),
1362         (b"ffffeeffbb", 161, &[67, 98, 46, 68, 26, 10]),
1363         (b"ffffeeffbb", 162, &[125, 155, 60, 63, 138, 9]),
1364         (b"ffffeeffbb", 163, &[96, 43, 118, 93, 90, 9]),
1365         (b"ffffeeffbb", 164, &[159, 99, 123, 152, 43, 9]),
1366         (b"ffffeeffbb", 165, &[65, 17, 1, 69, 163, 8]),
1367         (b"ffffeeffbb", 166, &[135, 108, 25, 165, 119, 8]),
1368         (b"ffffeeffbb", 167, &[165, 116, 164, 103, 77, 8]),
1369         (b"ffffeeffbb", 168, &[11, 166, 67, 44, 36, 8]),
1370         (b"ffffeeffbb", 169, &[65, 59, 71, 149, 164, 7]),
1371         (b"ffffeeffbb", 170, &[85, 83, 26, 76, 126, 7]),
1372         (b"ffffeeffbb", 171, &[71, 132, 140, 157, 88, 7]),
1373         (b"ffffeeffbb", 172, &[3, 6, 127, 47, 52, 7]),
1374         (b"ffffeeffbb", 173, &[122, 66, 53, 83, 16, 7]),
1375         (b"ffffeeffbb", 174, &[35, 6, 5, 88, 155, 6]),
1376         (b"ffffeeffbb", 175, &[95, 20, 84, 56, 122, 6]),
1377         (b"ffffeeffbb", 176, &[43, 91, 57, 159, 89, 6]),
1378         (b"ffffeeffbb", 177, &[110, 127, 54, 40, 58, 6]),
1379         (b"ffffeeffbb", 178, &[49, 115, 43, 47, 27, 6]),
1380         (b"ffffeeffbb", 179, &[130, 91, 4, 178, 175, 5]),
1381         (b"ffffeeffbb", 180, &[35, 122, 109, 70, 147, 5]),
1382         (b"ffffeeffbb", 181, &[94, 94, 4, 79, 119, 5]),
1383         (b"ffffeeffbb", 182, &[39, 54, 66, 19, 92, 5]),
1384         (b"ffffeeffbb", 183, &[119, 2, 143, 69, 65, 5]),
1385         (b"ffffeeffbb", 184, &[67, 57, 90, 44, 39, 5]),
1386         (b"ffffeeffbb", 185, &[90, 63, 141, 123, 13, 5]),
1387         (b"ffffeeffbb", 186, &[53, 123, 172, 119, 174, 4]),
1388         (b"ffffeeffbb", 187, &[153, 21, 68, 28, 151, 4]),
1389         (b"ffffeeffbb", 188, &[131, 138, 94, 32, 128, 4]),
1390         (b"ffffeeffbb", 189, &[179, 121, 156, 130, 105, 4]),
1391         (b"ffffeeffbb", 190, &[185, 179, 164, 131, 83, 4]),
1392         (b"ffffeeffbb", 191, &[118, 123, 37, 31, 62, 4]),
1393         (b"ffffeeffbb", 192, &[59, 106, 83, 16, 41, 4]),
1394         (b"ffffeeffbb", 193, &[57, 37, 47, 86, 20, 4]),
1395         (b"ffffeeffbb", 194, &[191, 140, 63, 45, 0, 4]),
1396         (b"ffffeeffbb", 195, &[65, 169, 83, 84, 175, 3]),
1397         (b"ffffeeffbb", 196, &[67, 158, 64, 6, 157, 3]),
1398         (b"ffffeeffbb", 197, &[121, 26, 167, 3, 139, 3]),
1399         (b"ffffeeffbb", 198, &[197, 151, 165, 75, 121, 3]),
1400         (b"ffffeeffbb", 199, &[55, 175, 36, 22, 104, 3]),
1401         (b"ffffeeffbb", 200, &[195, 167, 162, 38, 87, 3]),
1402         (b"ffffeeffbb", 201, &[35, 27, 136, 124, 70, 3]),
1403         (b"ffffeeffbb", 202, &[87, 64, 153, 76, 54, 3]),
1404         (b"ffffeeffbb", 203, &[151, 191, 14, 94, 38, 3]),
1405         (b"ffffeeffbb", 204, &[119, 103, 135, 175, 22, 3]),
1406         (b"ffffeeffbb", 205, &[200, 79, 123, 115, 7, 3]),
1407         (b"ffffeeffbb", 206, &[133, 165, 202, 115, 198, 2]),
1408         (b"ffffeeffbb", 207, &[44, 153, 193, 175, 184, 2]),
1409         (b"ffffeeffbb", 208, &[91, 190, 125, 86, 171, 2]),
1410         (b"ffffeeffbb", 209, &[109, 151, 34, 53, 158, 2]),
1411         (b"ffffeeffbb", 210, &[95, 40, 171, 74, 145, 2]),
1412         (b"ffffeeffbb", 211, &[84, 195, 162, 150, 132, 2]),
1413         (b"ffffeeffbb", 212, &[31, 15, 59, 68, 120, 2]),
1414         (b"ffffeeffbb", 213, &[125, 57, 127, 36, 108, 2]),
1415         (b"ffffeeffbb", 214, &[51, 132, 2, 55, 96, 2]),
1416         (b"ffffeeffbb", 215, &[175, 133, 177, 122, 84, 2]),
1417         (b"ffffeeffbb", 216, &[179, 35, 78, 23, 73, 2]),
1418         (b"ffffeeffbb", 217, &[53, 101, 208, 186, 61, 2]),
1419         (b"ffffeeffbb", 218, &[33, 9, 214, 179, 50, 2]),
1420         (b"ffffeeffbb", 219, &[107, 147, 175, 217, 39, 2]),
1421         (b"ffffeeffbb", 220, &[175, 81, 179, 79, 29, 2]),
1422         (b"ffffeeffbb", 221, &[0, 76, 95, 204, 18, 2]),
1423         (b"ffffeeffbb", 222, &[53, 213, 16, 150, 8, 2]),
1424         (b"ffffeeffbb", 223, &[158, 161, 42, 136, 221, 1]),
1425         (b"ffffeeffbb", 224, &[123, 54, 52, 162, 212, 1]),
1426         (b"ffffeeffbb", 225, &[170, 43, 151, 2, 204, 1]),
1427         (b"ffffeeffbb", 226, &[27, 68, 224, 105, 195, 1]),
1428         (b"ffffeeffbb", 227, &[45, 69, 157, 20, 187, 1]),
1429         (b"ffffeeffbb", 228, &[71, 213, 64, 199, 178, 1]),
1430         (b"ffffeeffbb", 229, &[129, 203, 66, 186, 170, 1]),
1431         (b"ffffeeffbb", 230, &[205, 183, 57, 208, 162, 1]),
1432         (b"ffffeeffbb", 231, &[32, 50, 164, 33, 155, 1]),
1433         (b"ffffeeffbb", 232, &[35, 135, 53, 123, 147, 1]),
1434         (b"ffffeeffbb", 233, &[209, 47, 89, 13, 140, 1]),
1435         (b"ffffeeffbb", 234, &[143, 56, 175, 168, 132, 1]),
1436         (b"ffffeeffbb", 235, &[225, 157, 216, 121, 125, 1]),
1437         (b"ffffeeffbb", 236, &[51, 66, 119, 105, 118, 1]),
1438         (b"ffffeeffbb", 237, &[116, 150, 26, 119, 111, 1]),
1439         (b"ffffeeffbb", 238, &[221, 15, 87, 162, 104, 1]),
1440         (b"ffffeeffbb", 239, &[234, 155, 214, 234, 97, 1]),
1441         (b"ffffeeffbb", 240, &[155, 46, 84, 96, 91, 1]),
1442         (b"ffffeeffbb", 241, &[187, 48, 90, 225, 84, 1]),
1443         (b"ffffeeffbb", 242, &[87, 212, 151, 140, 78, 1]),
1444         (b"ffffeeffbb", 243, &[206, 22, 189, 81, 72, 1]),
1445         (b"ffffeeffbb", 244, &[119, 93, 122, 48, 66, 1]),
1446         (b"ffffeeffbb", 245, &[165, 224, 117, 40, 60, 1]),
1447         (b"ffffeeffbb", 246, &[77, 121, 100, 57, 54, 1]),
1448         (b"ffffeeffbb", 247, &[52, 128, 242, 98, 48, 1]),
1449         (b"ffffeeffbb", 248, &[115, 247, 224, 164, 42, 1]),
1450         (b"ffffeeffbb", 249, &[218, 127, 223, 5, 37, 1]),
1451         (b"ffffeeffbb", 250, &[95, 54, 168, 118, 31, 1]),
1452         (b"ffffeeffbb", 251, &[121, 204, 240, 3, 26, 1]),
1453         (b"ffffeeffbb", 252, &[179, 138, 123, 162, 20, 1]),
1454         (b"ffffeeffbb", 253, &[21, 50, 1, 91, 15, 1]),
1455         (b"ffffeeffbb", 254, &[149, 11, 63, 40, 10, 1]),
1456         (b"ffffeeffbb", 255, &[170, 225, 247, 9, 5, 1]),
1457         (b"ffffeeffbb", 256, &[187, 255, 238, 255, 255]),
1458     ];
1459 
1460     for &(bigint, radix, inbaseradix_le) in GROUND_TRUTH.iter() {
1461         let bigint = BigUint::parse_bytes(bigint, 16).unwrap();
1462         // to_radix_le
1463         assert_eq!(bigint.to_radix_le(radix), inbaseradix_le);
1464         // to_radix_be
1465         let mut inbase_be = bigint.to_radix_be(radix);
1466         inbase_be.reverse(); // now le
1467         assert_eq!(inbase_be, inbaseradix_le);
1468         // from_radix_le
1469         assert_eq!(
1470             BigUint::from_radix_le(inbaseradix_le, radix).unwrap(),
1471             bigint
1472         );
1473         // from_radix_be
1474         let mut inbaseradix_be = Vec::from(inbaseradix_le);
1475         inbaseradix_be.reverse();
1476         assert_eq!(
1477             BigUint::from_radix_be(&inbaseradix_be, radix).unwrap(),
1478             bigint
1479         );
1480     }
1481 
1482     assert!(BigUint::from_radix_le(&[10, 100, 10], 50).is_none());
1483 }
1484 
1485 #[test]
test_from_str_radix()1486 fn test_from_str_radix() {
1487     let r = to_str_pairs();
1488     for num_pair in r.iter() {
1489         let &(ref n, ref rs) = num_pair;
1490         for str_pair in rs.iter() {
1491             let &(ref radix, ref str) = str_pair;
1492             assert_eq!(n, &BigUint::from_str_radix(str, *radix).unwrap());
1493         }
1494     }
1495 
1496     let zed = BigUint::from_str_radix("Z", 10).ok();
1497     assert_eq!(zed, None);
1498     let blank = BigUint::from_str_radix("_", 2).ok();
1499     assert_eq!(blank, None);
1500     let blank_one = BigUint::from_str_radix("_1", 2).ok();
1501     assert_eq!(blank_one, None);
1502     let plus_one = BigUint::from_str_radix("+1", 10).ok();
1503     assert_eq!(plus_one, Some(BigUint::from_slice(&[1])));
1504     let plus_plus_one = BigUint::from_str_radix("++1", 10).ok();
1505     assert_eq!(plus_plus_one, None);
1506     let minus_one = BigUint::from_str_radix("-1", 10).ok();
1507     assert_eq!(minus_one, None);
1508     let zero_plus_two = BigUint::from_str_radix("0+2", 10).ok();
1509     assert_eq!(zero_plus_two, None);
1510     let three = BigUint::from_str_radix("1_1", 2).ok();
1511     assert_eq!(three, Some(BigUint::from_slice(&[3])));
1512     let ff = BigUint::from_str_radix("1111_1111", 2).ok();
1513     assert_eq!(ff, Some(BigUint::from_slice(&[0xff])));
1514 }
1515 
1516 #[test]
test_all_str_radix()1517 fn test_all_str_radix() {
1518     #[allow(deprecated, unused_imports)]
1519     use std::ascii::AsciiExt;
1520 
1521     let n = BigUint::new((0..10).collect());
1522     for radix in 2..37 {
1523         let s = n.to_str_radix(radix);
1524         let x = BigUint::from_str_radix(&s, radix);
1525         assert_eq!(x.unwrap(), n);
1526 
1527         let s = s.to_ascii_uppercase();
1528         let x = BigUint::from_str_radix(&s, radix);
1529         assert_eq!(x.unwrap(), n);
1530     }
1531 }
1532 
1533 #[test]
test_lower_hex()1534 fn test_lower_hex() {
1535     let a = BigUint::parse_bytes(b"A", 16).unwrap();
1536     let hello = BigUint::parse_bytes("22405534230753963835153736737".as_bytes(), 10).unwrap();
1537 
1538     assert_eq!(format!("{:x}", a), "a");
1539     assert_eq!(format!("{:x}", hello), "48656c6c6f20776f726c6421");
1540     assert_eq!(format!("{:♥>+#8x}", a), "♥♥♥♥+0xa");
1541 }
1542 
1543 #[test]
test_upper_hex()1544 fn test_upper_hex() {
1545     let a = BigUint::parse_bytes(b"A", 16).unwrap();
1546     let hello = BigUint::parse_bytes("22405534230753963835153736737".as_bytes(), 10).unwrap();
1547 
1548     assert_eq!(format!("{:X}", a), "A");
1549     assert_eq!(format!("{:X}", hello), "48656C6C6F20776F726C6421");
1550     assert_eq!(format!("{:♥>+#8X}", a), "♥♥♥♥+0xA");
1551 }
1552 
1553 #[test]
test_binary()1554 fn test_binary() {
1555     let a = BigUint::parse_bytes(b"A", 16).unwrap();
1556     let hello = BigUint::parse_bytes("224055342307539".as_bytes(), 10).unwrap();
1557 
1558     assert_eq!(format!("{:b}", a), "1010");
1559     assert_eq!(
1560         format!("{:b}", hello),
1561         "110010111100011011110011000101101001100011010011"
1562     );
1563     assert_eq!(format!("{:♥>+#8b}", a), "♥+0b1010");
1564 }
1565 
1566 #[test]
test_octal()1567 fn test_octal() {
1568     let a = BigUint::parse_bytes(b"A", 16).unwrap();
1569     let hello = BigUint::parse_bytes("22405534230753963835153736737".as_bytes(), 10).unwrap();
1570 
1571     assert_eq!(format!("{:o}", a), "12");
1572     assert_eq!(format!("{:o}", hello), "22062554330674403566756233062041");
1573     assert_eq!(format!("{:♥>+#8o}", a), "♥♥♥+0o12");
1574 }
1575 
1576 #[test]
test_display()1577 fn test_display() {
1578     let a = BigUint::parse_bytes(b"A", 16).unwrap();
1579     let hello = BigUint::parse_bytes("22405534230753963835153736737".as_bytes(), 10).unwrap();
1580 
1581     assert_eq!(format!("{}", a), "10");
1582     assert_eq!(format!("{}", hello), "22405534230753963835153736737");
1583     assert_eq!(format!("{:♥>+#8}", a), "♥♥♥♥♥+10");
1584 }
1585 
1586 #[test]
test_factor()1587 fn test_factor() {
1588     fn factor(n: usize) -> BigUint {
1589         let mut f: BigUint = One::one();
1590         for i in 2..n + 1 {
1591             // FIXME(#5992): assignment operator overloads
1592             // f *= FromPrimitive::from_usize(i);
1593             let bu: BigUint = FromPrimitive::from_usize(i).unwrap();
1594             f = f * bu;
1595         }
1596         return f;
1597     }
1598 
1599     fn check(n: usize, s: &str) {
1600         let n = factor(n);
1601         let ans = match BigUint::from_str_radix(s, 10) {
1602             Ok(x) => x,
1603             Err(_) => panic!(),
1604         };
1605         assert_eq!(n, ans);
1606     }
1607 
1608     check(3, "6");
1609     check(10, "3628800");
1610     check(20, "2432902008176640000");
1611     check(30, "265252859812191058636308480000000");
1612 }
1613 
1614 #[test]
test_bits()1615 fn test_bits() {
1616     assert_eq!(BigUint::new(vec![0, 0, 0, 0]).bits(), 0);
1617     let n: BigUint = FromPrimitive::from_usize(0).unwrap();
1618     assert_eq!(n.bits(), 0);
1619     let n: BigUint = FromPrimitive::from_usize(1).unwrap();
1620     assert_eq!(n.bits(), 1);
1621     let n: BigUint = FromPrimitive::from_usize(3).unwrap();
1622     assert_eq!(n.bits(), 2);
1623     let n: BigUint = BigUint::from_str_radix("4000000000", 16).unwrap();
1624     assert_eq!(n.bits(), 39);
1625     let one: BigUint = One::one();
1626     assert_eq!((one << 426).bits(), 427);
1627 }
1628 
1629 #[test]
test_iter_sum()1630 fn test_iter_sum() {
1631     let result: BigUint = FromPrimitive::from_isize(1234567).unwrap();
1632     let data: Vec<BigUint> = vec![
1633         FromPrimitive::from_u32(1000000).unwrap(),
1634         FromPrimitive::from_u32(200000).unwrap(),
1635         FromPrimitive::from_u32(30000).unwrap(),
1636         FromPrimitive::from_u32(4000).unwrap(),
1637         FromPrimitive::from_u32(500).unwrap(),
1638         FromPrimitive::from_u32(60).unwrap(),
1639         FromPrimitive::from_u32(7).unwrap(),
1640     ];
1641 
1642     assert_eq!(result, data.iter().sum());
1643     assert_eq!(result, data.into_iter().sum());
1644 }
1645 
1646 #[test]
test_iter_product()1647 fn test_iter_product() {
1648     let data: Vec<BigUint> = vec![
1649         FromPrimitive::from_u32(1001).unwrap(),
1650         FromPrimitive::from_u32(1002).unwrap(),
1651         FromPrimitive::from_u32(1003).unwrap(),
1652         FromPrimitive::from_u32(1004).unwrap(),
1653         FromPrimitive::from_u32(1005).unwrap(),
1654     ];
1655     let result = data.get(0).unwrap()
1656         * data.get(1).unwrap()
1657         * data.get(2).unwrap()
1658         * data.get(3).unwrap()
1659         * data.get(4).unwrap();
1660 
1661     assert_eq!(result, data.iter().product());
1662     assert_eq!(result, data.into_iter().product());
1663 }
1664 
1665 #[test]
test_iter_sum_generic()1666 fn test_iter_sum_generic() {
1667     let result: BigUint = FromPrimitive::from_isize(1234567).unwrap();
1668     let data = vec![1000000_u32, 200000, 30000, 4000, 500, 60, 7];
1669 
1670     assert_eq!(result, data.iter().sum());
1671     assert_eq!(result, data.into_iter().sum());
1672 }
1673 
1674 #[test]
test_iter_product_generic()1675 fn test_iter_product_generic() {
1676     let data = vec![1001_u32, 1002, 1003, 1004, 1005];
1677     let result = data[0].to_biguint().unwrap()
1678         * data[1].to_biguint().unwrap()
1679         * data[2].to_biguint().unwrap()
1680         * data[3].to_biguint().unwrap()
1681         * data[4].to_biguint().unwrap();
1682 
1683     assert_eq!(result, data.iter().product());
1684     assert_eq!(result, data.into_iter().product());
1685 }
1686 
1687 #[test]
test_pow()1688 fn test_pow() {
1689     let one = BigUint::from(1u32);
1690     let two = BigUint::from(2u32);
1691     let four = BigUint::from(4u32);
1692     let eight = BigUint::from(8u32);
1693     let tentwentyfour = BigUint::from(1024u32);
1694     let twentyfourtyeight = BigUint::from(2048u32);
1695     macro_rules! check {
1696         ($t:ty) => {
1697             assert_eq!(two.pow(0 as $t), one);
1698             assert_eq!(two.pow(1 as $t), two);
1699             assert_eq!(two.pow(2 as $t), four);
1700             assert_eq!(two.pow(3 as $t), eight);
1701             assert_eq!(two.pow(10 as $t), tentwentyfour);
1702             assert_eq!(two.pow(11 as $t), twentyfourtyeight);
1703             assert_eq!(two.pow(&(11 as $t)), twentyfourtyeight);
1704         };
1705     }
1706     check!(u8);
1707     check!(u16);
1708     check!(u32);
1709     check!(u64);
1710     check!(usize);
1711     #[cfg(has_i128)]
1712     check!(u128);
1713 }
1714