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