Debian Codegraph @

debian/rust-vendor/num-bigint/tests/biguint.rs

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