radix_common/math/bnum_integer/
test_macros.rs

1#[macro_export]
2macro_rules! test_from_builtin {
3    ($i:ident, ($($t:ident),*)) => {
4        paste! {
5            $(
6                #[test]
7                fn [<from_builtin_$i:lower _ $t:lower>]() {
8                    let b = <$i>::[<from_$t>](127).unwrap();
9                    assert_eq!(b.to_string(), "127");
10                }
11            )*
12        }
13    };
14}
15#[macro_export]
16#[cfg(test)]
17macro_rules! from_builtin {
18        ($a:expr, $i:ty, ($($t:ident),*)) => {
19            paste!{
20                $(
21                    $a = <$i>::[<from_$t>]( 8 as $t ).unwrap();
22                    assert_eq!($a.to_string(), "8");
23                )*
24            }
25        }
26}
27
28#[macro_export]
29#[cfg(test)]
30macro_rules! try_from_safe {
31        ($a:expr, $i:ty, ($($t:ident),*)) => {
32            paste!{
33                $(
34                    $a = <$i>::try_from(<$t>::try_from(19u8).unwrap()).unwrap();
35                    assert_eq!($a.to_string(), "19");
36                )*
37            }
38        }
39}
40
41#[macro_export]
42#[cfg(test)]
43macro_rules! to_builtin {
44        ($a:expr, $i:ty, ($($t:ident),*)) => {
45            paste!{
46                $(
47                    let builtin: $t = $a.[<to_$t>]().unwrap();
48                    assert_eq!(builtin.to_string(), "11");
49                )*
50            }
51        }
52}
53
54#[macro_export]
55macro_rules! test_impl {
56    ($($i:ident),*) => ($(
57
58            paste! {
59                #[test]
60                fn [<test_display_$i:lower>]() {
61                    #[cfg(feature = "alloc")]
62                    use sbor::prelude::format;
63
64                    let x = <$i>::MAX;
65                    let _f = format!("{:?}", x);
66                    let _f = format!("{}", x);
67
68                    #[cfg(not(feature = "alloc"))]
69                    let _f = format!("{}", [< Parse $i Error >] ::NegativeToUnsigned);
70
71                    #[cfg(not(feature = "alloc"))]
72                    let _f = format!("{:?}", [< Parse $i Error >] ::NegativeToUnsigned);
73                }
74
75                #[test]
76                #[should_panic]
77                fn [<test_add_overflow_$i:lower>]() {
78                    let _ = <$i>::MAX + <$i>::try_from(1u8).unwrap(); // panics on overflow
79                }
80
81                #[test]
82                #[should_panic]
83                fn  [<test_sub_overflow_$i:lower>]() {
84                    let _ = <$i>::MIN - <$i>::try_from(1u8).unwrap(); // panics on overflow
85                }
86
87                #[test]
88                #[should_panic]
89                fn  [<test_mul_overflow_$i:lower>]() {
90                    let _ = <$i>::MAX * <$i>::try_from(2u8).unwrap(); // panics because of overflow
91                }
92
93                #[test]
94                #[should_panic]
95                fn  [<test_div_overflow_$i:lower>]() {
96                    let _ = <$i>::MIN / <$i>::try_from(0u8).unwrap(); // panics because of division by zero
97                }
98
99                #[test]
100                #[should_panic]
101                fn  [<test_rem_overflow_$i:lower>]() {
102                    let _ = <$i>::MIN % $i::try_from(0u8).unwrap(); // panics because of division by zero
103                }
104
105                #[test]
106                #[should_panic]
107                fn  [<test_shl_overflow_$i:lower>]() {
108                    let _ = <$i>::MAX << (<$i>::BITS + 1);  // panics because of overflow
109                }
110
111                #[test]
112                #[should_panic]
113                fn  [<test_shr_overflow_$i:lower>]() {
114                    let _ = <$i>::MAX >> (<$i>::BITS + 1);  // panics because of overflow
115                }
116
117                #[test]
118                #[should_panic]
119                fn  [<test_shl_overflow_neg_$i:lower>]() {
120                    let _ = <$i>::MIN << (<$i>::BITS + 1);  // panics because of overflow
121                }
122
123                #[test]
124                #[should_panic]
125                fn  [<test_shr_overflow_neg_$i:lower>]() {
126                    let _ = <$i>::MIN >> (<$i>::BITS + 1);  // panics because of overflow
127                }
128
129                #[test]
130                #[should_panic]
131                fn  [<test_pow_overflow_$i:lower>]() {
132                    let _ = <$i>::MAX.pow(2u32);             // panics because of overflow
133                }
134
135                #[test]
136                fn  [<test_max_to_string_$i:lower>]() {
137                    let a = <$i>::MAX;
138                    let b = BigInt::from(a);
139                    assert_eq!(a.to_string(), b.to_string());
140                }
141
142                #[test]
143                fn  [<test_min_to_string_$i:lower>]() {
144                    let a = <$i>::MIN;
145                    let b = BigInt::from(a);
146                    assert_eq!(a.to_string(), b.to_string());
147                }
148
149//                #[test]
150//                fn [<test_binary_$i:lower>]() {
151//                    let bin = <$i>::try_from(0x0b).unwrap();
152//                    assert_eq!(format!("{:b}", bin), "1011");
153//                }
154//
155//                #[test]
156//                fn [<test_octal_$i:lower>]() {
157//                    let oct = <$i>::try_from(0x0b).unwrap();
158//                    assert_eq!(format!("{:o}", oct), "13");
159//                }
160//
161//                #[test]
162//                fn [<test_hex_lower_$i:lower>]() {
163//                    let hex_lower = <$i>::try_from(0x0b).unwrap();
164//                    assert_eq!(format!("{:x}", hex_lower), "b");
165//                }
166//
167//                #[test]
168//                fn [<test_hex_upper_$i:lower>]() {
169//                    let hex_upper = <$i>::try_from(0x0b).unwrap();
170//                    assert_eq!(format!("{:X}", hex_upper), "B");
171//                }
172//
173                #[test]
174                fn [<test_default_$i:lower>]() {
175                    let zero = <$i>::try_from(0u8).unwrap();
176                    assert_eq!(zero, <$i>::default());
177                }
178
179                #[test]
180                fn [<test_zero_$i:lower>]() {
181                    let zero = <$i>::try_from(0u8).unwrap();
182                    assert_eq!(zero, <$i>::zero());
183                }
184
185                #[test]
186                fn [<test_is_zero_$i:lower>]() {
187                    let mut zero = <$i>::try_from(0u8).unwrap();
188                    assert_eq!(zero.is_zero(), true);
189                    zero = <$i>::try_from(1u8).unwrap();
190                    assert_eq!(zero.is_zero(), false);
191                }
192
193                #[test]
194                fn [<test_set_zero_$i:lower>]() {
195                    let mut zero = <$i>::try_from(1u8).unwrap();
196                    zero.set_zero();
197                    assert_eq!(zero.is_zero(), true);
198                }
199
200                #[test]
201                fn [<test_set_one_$i:lower>]() {
202                    let mut zero = <$i>::try_from(0u8).unwrap();
203                    zero.set_one();
204                    assert_eq!(zero.is_one(), true);
205                }
206
207                #[test]
208                fn [<test_ord_ $i:lower>]() {
209                    let zero = <$i>::try_from(0u8).unwrap();
210                    let one = <$i>::try_from(1u8).unwrap();
211                    assert_eq!(zero.cmp(&one), Ordering::Less);
212                }
213
214                #[test]
215                fn [<test_ord_5_1_ $i:lower>]() {
216                    let five = <$i>::try_from(5u8).unwrap();
217                    let one = <$i>::try_from(1u8).unwrap();
218                    assert_eq!(five.cmp(&one), Ordering::Greater);
219                }
220
221                #[test]
222                fn [<test_ord_5_5_ $i:lower>]() {
223                    let five = <$i>::try_from(5u8).unwrap();
224                    assert_eq!(five.cmp(&five), Ordering::Equal);
225                }
226
227                #[test]
228                fn [<test_ord_min_min_ $i:lower>]() {
229                    let min = <$i>::MIN;
230                    assert_eq!(min.cmp(&min), Ordering::Equal);
231                }
232
233                #[test]
234                fn [<test_ord_max_max_ $i:lower>]() {
235                    let max = <$i>::MAX;
236                    assert_eq!(max.cmp(&max), Ordering::Equal);
237                }
238
239                #[test]
240                fn [<test_ord_max_min_ $i:lower>]() {
241                    let max = <$i>::MAX;
242                    let min = <$i>::MIN;
243                    assert_eq!(max.cmp(&min), Ordering::Greater);
244                }
245
246                #[test]
247                fn [<test_ord_min_max_ $i:lower>]() {
248                    let max = <$i>::MAX;
249                    let min = <$i>::MIN;
250                    assert_eq!(min.cmp(&max), Ordering::Less);
251                }
252
253                test_from_builtin!{$i, (i8, i16, i32, i64, i128, u8, u16, u32, u64, u128)}
254
255            }
256    )*)
257}
258
259#[macro_export]
260macro_rules! test_math {
261    ($i:ident, $t:ident, $tlst:tt) => {
262        paste! {
263            #[test]
264            fn [<test_2_add_2_ $i:lower _ $t:lower>]() {
265                const A_BYTES: usize = (<$i>::BITS / 8) as usize;
266                const B_BYTES: usize = (<$t>::BITS / 8) as usize;
267                const MAX_BYTES: usize = if A_BYTES > B_BYTES { A_BYTES } else { B_BYTES };
268                let a: $i = BigInt::from_bytes_le(Sign::Plus, &[2u8; A_BYTES]).try_into().unwrap();
269                let b: $t = BigInt::from_bytes_le(Sign::Plus, &[2u8; B_BYTES]).try_into().unwrap();
270                let expect: $i = BigInt::from_bytes_le(Sign::Plus, &[4u8; MAX_BYTES]).try_into().unwrap();
271                assert_eq!(a.add(b), expect);
272                assert_eq!(&a.add(b), &expect);
273                assert_eq!(a.add(&b), expect);
274                assert_eq!(&a.add(&b), &expect);
275                assert_eq!(a + b, expect);
276                assert_eq!(&a + b, expect);
277                assert_eq!(a + &b, expect);
278                assert_eq!(&a + &b, expect);
279
280                let mut a: $i = BigInt::from_bytes_le(Sign::Plus, &[2u8; A_BYTES]).try_into().unwrap();
281                a += b;
282                assert_eq!(a, expect);
283
284                let mut a: $i = BigInt::from_bytes_le(Sign::Plus, &[2u8; A_BYTES]).try_into().unwrap();
285                a += &b;
286                assert_eq!(a, expect);
287            }
288
289            #[test]
290            fn [<test_0x88_add_0x88_ $i:lower _ $t:lower>]() {
291                const A_BYTES: usize = (<$i>::BITS / 8) as usize;
292                const B_BYTES: usize = (<$t>::BITS / 8) as usize;
293                const MAX_BYTES: usize = if A_BYTES > B_BYTES { A_BYTES } else { B_BYTES };
294                let mut a_arr = [0x88u8; A_BYTES];
295                let mut b_arr = [0x88u8; B_BYTES];
296                let mut expect_arr = [0x11u8; MAX_BYTES];
297                if A_BYTES == 1 {
298                    a_arr[0] = 0x03;
299                    b_arr[0] = 0x02;
300                    expect_arr[0] = 0x05;
301                } else {
302                    a_arr[A_BYTES - 1] = 0x00;
303                    b_arr[B_BYTES - 1] = 0x00;
304                    expect_arr[MAX_BYTES - 1] = 0x01;
305                    expect_arr[0] = 0x10;
306                }
307                let a: $i = BigInt::from_bytes_le(Sign::Plus, &a_arr).try_into().unwrap();
308                let b: $t = BigInt::from_bytes_le(Sign::Plus, &b_arr).try_into().unwrap();
309                let expect = BigInt::from_bytes_le(Sign::Plus, &expect_arr);
310                assert_eq!(a.add(b), expect.try_into().unwrap());
311            }
312
313            #[test]
314            fn [<test_2_sub_2_ $i:lower _ $t:lower>]() {
315                const A_BYTES: usize = (<$i>::BITS / 8) as usize;
316                const B_BYTES: usize = (<$t>::BITS / 8) as usize;
317                const MAX_BYTES: usize = if A_BYTES > B_BYTES { A_BYTES } else { B_BYTES };
318                let a: $i = BigInt::from_bytes_le(Sign::Plus, &[2u8; A_BYTES]).try_into().unwrap();
319                let b: $t = BigInt::from_bytes_le(Sign::Plus, &[2u8; B_BYTES]).try_into().unwrap();
320                let expect: $i = BigInt::from_bytes_le(Sign::Plus, &[0u8; MAX_BYTES]).try_into().unwrap();
321                assert_eq!(a.sub(b), expect);
322                assert_eq!(&a.sub(b), &expect);
323                assert_eq!(a.sub(&b), expect);
324                assert_eq!(&a.sub(&b), &expect);
325                assert_eq!(a - b, expect);
326                assert_eq!(&a - b, expect);
327                assert_eq!(a - &b, expect);
328                assert_eq!(&a - &b, expect);
329
330                let mut a: $i = BigInt::from_bytes_le(Sign::Plus, &[2u8; A_BYTES]).try_into().unwrap();
331                a -= b;
332                assert_eq!(a, expect);
333
334                let mut a: $i = BigInt::from_bytes_le(Sign::Plus, &[2u8; A_BYTES]).try_into().unwrap();
335                a -= &b;
336                assert_eq!(a, expect);
337            }
338
339            #[test]
340            fn [<test_2_mul_2_ $i:lower _ $t:lower>]() {
341                const A_BYTES: usize = (<$i>::BITS / 8) as usize;
342                const B_BYTES: usize = (<$t>::BITS / 8) as usize;
343                const MAX_BYTES: usize = if A_BYTES > B_BYTES { A_BYTES } else { B_BYTES };
344                let a: $i = BigInt::from_bytes_le(Sign::Plus, &[2u8; A_BYTES]).try_into().unwrap();
345                let b: $t = 2u8.try_into().unwrap();
346                let expect: $i = BigInt::from_bytes_le(Sign::Plus, &[4u8; MAX_BYTES]).try_into().unwrap();
347                assert_eq!(a.mul(b), expect);
348                assert_eq!(&a.mul(b), &expect);
349                assert_eq!(a.mul(&b), expect);
350                assert_eq!(&a.mul(&b), &expect);
351                assert_eq!(a * b, expect);
352                assert_eq!(&a * b, expect);
353                assert_eq!(a * &b, expect);
354                assert_eq!(&a * &b, expect);
355
356                let mut a: $i = BigInt::from_bytes_le(Sign::Plus, &[2u8; A_BYTES]).try_into().unwrap();
357                a *= b;
358                assert_eq!(a, expect);
359
360                let mut a: $i = BigInt::from_bytes_le(Sign::Plus, &[2u8; A_BYTES]).try_into().unwrap();
361                a *= &b;
362                assert_eq!(a, expect);
363            }
364
365            #[test]
366            fn [<test_2_div_2_ $i:lower _ $t:lower>]() {
367                const A_BYTES: usize = (<$i>::BITS / 8) as usize;
368                let a: $i = BigInt::from_bytes_le(Sign::Plus, &[4u8; A_BYTES]).try_into().unwrap();
369                let b: $i = BigInt::from_bytes_le(Sign::Plus, &[4u8; A_BYTES]).try_into().unwrap();
370                let expect = $i::try_from(1).unwrap();
371                assert_eq!(a.div(b), expect);
372                assert_eq!(&a.div(b), &expect);
373                assert_eq!(a.div(&b), expect);
374                assert_eq!(&a.div(&b), &expect);
375                assert_eq!(a / b, expect);
376                assert_eq!(&a / b, expect);
377                assert_eq!(a / &b, expect);
378                assert_eq!(&a / &b, expect);
379
380                let mut a: $i = BigInt::from_bytes_le(Sign::Plus, &[4u8; A_BYTES]).try_into().unwrap();
381                a /= b;
382                assert_eq!(a, expect);
383
384                let mut a: $i = BigInt::from_bytes_le(Sign::Plus, &[4u8; A_BYTES]).try_into().unwrap();
385                a /= &b;
386                assert_eq!(a, expect);
387            }
388
389            #[test]
390            fn [<test_10_rem_8_ $i:lower _ $t:lower>]() {
391                const A_BYTES: usize = (<$i>::BITS / 8) as usize;
392                let a: $i = BigInt::from_bytes_le(Sign::Plus, &[10u8; A_BYTES]).try_into().unwrap();
393                let b: $i = 8u8.try_into().unwrap();
394                let expect = $i::try_from(2u8).unwrap();
395                assert_eq!(a.rem(b), expect);
396
397                let mut a: $i = BigInt::from_bytes_le(Sign::Plus, &[10u8; A_BYTES]).try_into().unwrap();
398                a %= b;
399                assert_eq!(a, expect);
400
401                let mut a: $i = BigInt::from_bytes_le(Sign::Plus, &[10u8; A_BYTES]).try_into().unwrap();
402                a %= &b;
403                assert_eq!(a, expect);
404            }
405
406            #[test]
407            fn [<test_10_pow_0_ $i:lower _ $t:lower>]() {
408                const A_BYTES: usize = (<$i>::BITS / 8) as usize;
409                let a: $i = BigInt::from_bytes_le(Sign::Plus, &[10u8; A_BYTES]).try_into().unwrap();
410                let b = 0u32;
411                let expect = 1u8;
412                assert_eq!(a.pow(b), expect.try_into().unwrap());
413            }
414
415            #[test]
416            fn [<test_0_pow_0_ $i:lower _ $t:lower>]() {
417                const A_BYTES: usize = (<$i>::BITS / 8) as usize;
418                let a: $i = BigInt::from_bytes_le(Sign::Plus, &[0u8; A_BYTES]).try_into().unwrap();
419                let b = 0u32;
420                let expect = 1u8;
421                assert_eq!(a.pow(b), expect.try_into().unwrap());
422            }
423
424            #[test]
425            fn [<test_0_pow_1000_ $i:lower _ $t:lower>]() {
426                const A_BYTES: usize = (<$i>::BITS / 8) as usize;
427                let a: $i = BigInt::from_bytes_le(Sign::Plus, &[0u8; A_BYTES]).try_into().unwrap();
428                let b = 1000u32;
429                let expect = 0u8;
430                assert_eq!(a.pow(b), expect.try_into().unwrap());
431            }
432
433            #[test]
434            fn [<test_10x_pow_1_ $i:lower _ $t:lower>]() {
435                const A_BYTES: usize = (<$i>::BITS / 8) as usize;
436                let a: $i = BigInt::from_bytes_le(Sign::Plus, &[10u8; A_BYTES]).try_into().unwrap();
437                let b = 1u32;
438                let expect = a;
439                assert_eq!(a.pow(b), expect.try_into().unwrap());
440            }
441
442            #[test]
443            fn [<test_10x_pow_2_ $i:lower _ $t:lower>]() {
444                const A_BYTES: usize = (<$i>::BITS / 8) as usize;
445                let a: $i = BigInt::from_bytes_le(Sign::Plus, &[107u8; A_BYTES / 2]).try_into().unwrap();
446                let b = 2u32;
447                let expect = a.mul(a);
448                assert_eq!(a.pow(b), expect.try_into().unwrap());
449            }
450
451            #[test]
452            fn [<test_5x_pow_3_ $i:lower _ u8>]() {
453                const A_BYTES: usize = (<$i>::BITS / 8) as usize;
454                let a: $i = BigInt::from_bytes_le(Sign::Plus, &[5u8; A_BYTES / 3]).try_into().unwrap();
455                let b = 3u32;
456                let expect = a.mul(a).mul(a);
457                assert_eq!(a.pow(b), expect.try_into().unwrap());
458            }
459
460            #[test]
461            fn [<test_bits_10_not_ $i:lower _ $t:lower>]() {
462                const A_BYTES: usize = (<$i>::BITS / 8) as usize;
463                let a: $i;
464                let expect: $i;
465                if <$i>::MIN == Zero::zero() {
466                    a = BigInt::from_bytes_le(Sign::Plus, &[0b10101010u8; A_BYTES]).try_into().unwrap();
467                    expect = BigInt::from_bytes_le(Sign::Plus, &[0b01010101u8; A_BYTES]).try_into().unwrap();
468                } else {
469                    a = BigInt::from_signed_bytes_le(&[0b10101010u8; A_BYTES]).try_into().unwrap();
470                    expect = BigInt::from_signed_bytes_le(&[0b01010101u8; A_BYTES]).try_into().unwrap();
471                }
472                assert_eq!(a.not(), expect);
473
474                assert_eq!(!a, expect);
475                assert_eq!(!&a, expect);
476            }
477
478            #[test]
479            fn [<test_bits_bits_signed_ $i:lower >]() {
480                let expect: String = String::from(stringify!($i));
481                let mut i = 0;
482                for (idx, c) in expect.chars().enumerate() {
483                    if c.is_numeric() {
484                        i = idx;
485                        break;
486                    }
487                }
488                assert_eq!(<$i>::BITS, expect[i..].parse::<u32>().unwrap());
489            }
490
491            #[test]
492            fn [<test_bits_0b10101010_count_ones_ $i:lower >]() {
493                let a: $i;
494                if <$i>::MIN == Zero::zero() {
495                    a = BigInt::from_bytes_le(Sign::Plus, &[0b01101010u8; (<$i>::BITS / 8) as usize]).try_into().unwrap();
496                } else {
497                    a = BigInt::from_signed_bytes_le(&[0b01101010u8; (<$i>::BITS / 8) as usize]).try_into().unwrap();
498                }
499
500                assert_eq!(a.count_ones(), (<$i>::BITS / 2) as u32);
501            }
502
503            #[test]
504            fn [<test_bits_0_count_ones_ $i:lower >]() {
505                let a: $i = BigInt::from_bytes_le(Sign::Plus, &[0b00000000u8; (<$i>::BITS / 8) as usize]).try_into().unwrap();
506                assert_eq!(a.count_ones(), 0u32);
507            }
508
509            #[test]
510            fn [<test_bits_1_count_ones_ $i:lower >]() {
511                let a: $i;
512                if <$i>::MIN == Zero::zero() {
513                a = BigInt::from_bytes_le(Sign::Plus, &[0b11111111u8; (<$i>::BITS / 8) as usize]).try_into().unwrap();
514                } else {
515                    a = BigInt::from_signed_bytes_le(&[0b11111111u8; (<$i>::BITS / 8) as usize]).try_into().unwrap();
516                }
517                assert_eq!(a.count_ones(), (<$i>::BITS) as u32);
518            }
519
520            #[test]
521            fn [<test_bits_0b10101010_count_zeros_ $i:lower >]() {
522                let a: $i;
523                if <$i>::MIN == Zero::zero() {
524                    a = BigInt::from_bytes_le(Sign::Plus, &[0b01101010u8; (<$i>::BITS / 8) as usize]).try_into().unwrap();
525                } else {
526                    a = BigInt::from_signed_bytes_le(&[0b01101010u8; (<$i>::BITS / 8) as usize]).try_into().unwrap();
527                }
528
529                assert_eq!(a.count_zeros(), (<$i>::BITS / 2) as u32);
530            }
531
532            #[test]
533            fn [<test_bits_0_count_zeros_ $i:lower >]() {
534                let a: $i = BigInt::from_bytes_le(Sign::Plus, &[0b00000000u8; (<$i>::BITS / 8) as usize]).try_into().unwrap();
535                assert_eq!(a.count_zeros(), (<$i>::BITS) as u32);
536            }
537
538            #[test]
539            fn [<test_bits_1_count_zeros_ $i:lower >]() {
540                let a: $i;
541                if <$i>::MIN == Zero::zero() {
542                    a = BigInt::from_bytes_le(Sign::Plus, &[0b11111111u8; (<$i>::BITS / 8) as usize]).try_into().unwrap();
543                } else {
544                    a = BigInt::from_signed_bytes_le(&[0b11111111u8; (<$i>::BITS / 8) as usize]).try_into().unwrap();
545                }
546                assert_eq!(a.count_zeros(), 0u32);
547            }
548
549            #[test]
550            fn [<test_bits_0_trailing_zeros_ $i:lower >]() {
551                let a: $i;
552                if <$i>::MIN == Zero::zero() {
553                    a = BigInt::from_bytes_le(Sign::Plus, &[0b00000000u8; (<$i>::BITS / 8) as usize]).try_into().unwrap();
554                } else {
555                    a = BigInt::from_signed_bytes_le(&[0b00000000u8; (<$i>::BITS / 8) as usize]).try_into().unwrap();
556                }
557                assert_eq!(a.trailing_zeros(), (<$i>::BITS) as u32);
558            }
559
560
561            #[test]
562            fn [<test_bits_1_trailing_zeros_ $i:lower >]() {
563                let a: $i;
564                if <$i>::MIN == Zero::zero() {
565                    a = BigInt::from_bytes_le(Sign::Plus, &[0b11111111u8; (<$i>::BITS / 8) as usize]).try_into().unwrap();
566                } else {
567                    a = BigInt::from_signed_bytes_le(&[0b11111111u8; (<$i>::BITS / 8) as usize]).try_into().unwrap();
568                }
569                assert_eq!(a.trailing_zeros(), 0u32);
570            }
571
572            #[test]
573            fn [<test_bits_all_trailing_zeros_ $i:lower >]() {
574                let mut a: $i;
575                let bytes: [u8; (<$i>::BITS / 8) as usize] = [0b01010101u8; (<$i>::BITS / 8) as usize];
576                for i in 0..<$i>::BITS {
577                    if <$i>::MIN == Zero::zero() {
578                        a = BigInt::from_bytes_le(Sign::Plus, &bytes).try_into().unwrap();
579                    } else {
580                        a = BigInt::from_signed_bytes_le(&bytes).try_into().unwrap();
581                    }
582                    a <<= i.try_into().unwrap();
583                    assert_eq!(a.trailing_zeros(), i as u32);
584                }
585            }
586
587            #[test]
588            fn [<test_bits_swap_bytes_ $i:lower >]() {
589                let a: $i;
590                let exp: $i;
591                let mut bytes: [u8; (<$i>::BITS / 8) as usize] = [0b00000000u8; (<$i>::BITS / 8) as usize];
592                let mut expect: [u8; (<$i>::BITS / 8) as usize] = [0b00000000u8; (<$i>::BITS / 8) as usize];
593                for i in 0..(<$i>::BITS / 8) as usize {
594                    bytes[i] = i as u8;
595                    expect[i] = (<$i>::BITS / 8 - i as u32 - 1) as u8;
596                }
597                if <$i>::MIN == Zero::zero() {
598                    a = BigInt::from_bytes_le(Sign::Plus, &bytes).try_into().unwrap();
599                    exp = BigInt::from_bytes_le(Sign::Plus, &expect).try_into().unwrap();
600                } else {
601                    a = BigInt::from_signed_bytes_le(&bytes).try_into().unwrap();
602                    exp = BigInt::from_signed_bytes_le(&expect).try_into().unwrap();
603                }
604                assert_eq!(a.swap_bytes(), exp);
605            }
606
607            #[test]
608            fn [<test_bits_reverse_bits_ $i:lower >]() {
609                let a: $i;
610                let exp: $i;
611                const LEN: usize = (<$i>::BITS / 8) as usize;
612                let mut bytes: [u8; LEN] = [0b00001111u8; LEN];
613                let mut expect: [u8; LEN] = [0b11110000u8; LEN];
614                for i in 0..LEN / 2 {
615                    bytes[i] = 0b01010101 as u8;
616                    expect[LEN - i - 1] = 0b10101010 as u8;
617                }
618                if <$i>::MIN == Zero::zero() {
619                    a = BigInt::from_bytes_le(Sign::Plus, &bytes).try_into().unwrap();
620                    exp = BigInt::from_bytes_le(Sign::Plus, &expect).try_into().unwrap();
621                } else {
622                    a = BigInt::from_signed_bytes_le(&bytes).try_into().unwrap();
623                    exp = BigInt::from_signed_bytes_le(&expect).try_into().unwrap();
624                }
625                assert_eq!(a.reverse_bits(), exp);
626            }
627
628            #[test]
629            fn [<test_bits_zero_leading_zeros_ $i:lower >]() {
630                let a: $i;
631                let bytes: [u8; (<$i>::BITS / 8) as usize] = [0b11010101u8; (<$i>::BITS / 8) as usize];
632                if <$i>::MIN == Zero::zero() {
633                    a = BigInt::from_bytes_le(Sign::Plus, &bytes).try_into().unwrap();
634                } else {
635                    a = BigInt::from_signed_bytes_le(&bytes).try_into().unwrap();
636                }
637                assert_eq!(a.leading_zeros(), 0);
638            }
639
640            #[test]
641            fn [<test_bits_all_leading_zeros_ $i:lower >]() {
642                let mut a: $i;
643                let bytes: [u8; (<$i>::BITS / 8) as usize] = [0b01010101u8; (<$i>::BITS / 8) as usize];
644                for i in 0..<$i>::BITS {
645                    if <$i>::MIN == Zero::zero() {
646                        a = BigInt::from_bytes_le(Sign::Plus, &bytes).try_into().unwrap();
647                    } else {
648                        a = BigInt::from_signed_bytes_le(&bytes).try_into().unwrap();
649                    }
650                    a >>= i.try_into().unwrap();
651                    assert_eq!(a.leading_zeros(), i as u32 + 1);
652                }
653            }
654
655            #[test]
656            fn [<test_bits_from_be_ $i:lower >]() {
657                let i = <$i>::ONE;
658                let bytes = i.to_le_bytes();
659
660                let x = <$i>::from_be(i);
661                let b : BigInt = BigInt::from_bytes_be(Sign::Plus, &bytes).try_into().unwrap();
662
663                assert_eq!(x, <$i>::try_from(b).unwrap());
664            }
665
666            #[test]
667            fn [<test_bits_from_le_ $i:lower >]() {
668                let i = <$i>::ONE;
669                let bytes = i.to_le_bytes();
670
671                let x = <$i>::from_le(i);
672                let b : BigInt = BigInt::from_bytes_le(Sign::Plus, &bytes).try_into().unwrap();
673
674                assert_eq!(x, <$i>::try_from(b).unwrap());
675            }
676
677            #[test]
678            fn [<test_bits_to_be_ $i:lower >]() {
679                let i = <$i>::ONE;
680                let bytes = i.to_le_bytes();
681
682                let x = i.to_be();
683                let b : BigInt = BigInt::from_bytes_be(Sign::Plus, &bytes).try_into().unwrap();
684
685                assert_eq!(x, <$i>::try_from(b).unwrap());
686            }
687
688            #[test]
689            fn [<test_bits_to_le_ $i:lower >]() {
690                let i = <$i>::ONE;
691                let bytes = i.to_le_bytes();
692
693                let x = i.to_le();
694                let b : BigInt = BigInt::from_bytes_le(Sign::Plus, &bytes).try_into().unwrap();
695
696                assert_eq!(x, <$i>::try_from(b).unwrap());
697            }
698
699            #[test]
700            fn [<test_bits_bitxor_ $i:lower >]() {
701                let mut a: $i;
702                let b: $i;
703                let exp: $i;
704                const LEN: usize = (<$i>::BITS / 8) as usize;
705                let bytes: [u8; LEN] =    [0b11001100u8; LEN];
706                let bytes_b: [u8; LEN] =  [0b10101010u8; LEN];
707                let expected: [u8; LEN] = [0b01100110u8; LEN];
708                if <$i>::MIN == Zero::zero() {
709                    a = BigInt::from_bytes_le(Sign::Plus, &bytes).try_into().unwrap();
710                    b = BigInt::from_bytes_le(Sign::Plus, &bytes_b).try_into().unwrap();
711                    exp = BigInt::from_bytes_le(Sign::Plus, &expected).try_into().unwrap();
712                } else {
713                    a = BigInt::from_signed_bytes_le(&bytes).try_into().unwrap();
714                    b = BigInt::from_signed_bytes_le(&bytes_b).try_into().unwrap();
715                    exp = BigInt::from_signed_bytes_le(&expected).try_into().unwrap();
716                }
717                assert_eq!(a.bitxor(b), exp);
718                a ^= b;
719                assert_eq!(a, exp);
720            }
721
722            #[test]
723            fn [<test_bits_bitor_ $i:lower >]() {
724                let mut a: $i;
725                let b: $i;
726                let exp: $i;
727                const LEN: usize = (<$i>::BITS / 8) as usize;
728                let bytes: [u8; LEN] =    [0b11001100u8; LEN];
729                let bytes_b: [u8; LEN] =  [0b10101010u8; LEN];
730                let expected: [u8; LEN] = [0b11101110u8; LEN];
731                if <$i>::MIN == Zero::zero() {
732                    a = BigInt::from_bytes_le(Sign::Plus, &bytes).try_into().unwrap();
733                    b = BigInt::from_bytes_le(Sign::Plus, &bytes_b).try_into().unwrap();
734                    exp = BigInt::from_bytes_le(Sign::Plus, &expected).try_into().unwrap();
735                } else {
736                    a = BigInt::from_signed_bytes_le(&bytes).try_into().unwrap();
737                    b = BigInt::from_signed_bytes_le(&bytes_b).try_into().unwrap();
738                    exp = BigInt::from_signed_bytes_le(&expected).try_into().unwrap();
739                }
740                assert_eq!(a.bitor(b), exp);
741                a |= b;
742                assert_eq!(a, exp);
743            }
744
745
746            #[test]
747            fn [<test_bits_bitand_ $i:lower >]() {
748                let mut a: $i;
749                let b: $i;
750                let exp: $i;
751                const LEN: usize = (<$i>::BITS / 8) as usize;
752                let bytes: [u8; LEN] =    [0b11001100u8; LEN];
753                let bytes_b: [u8; LEN] =  [0b10101010u8; LEN];
754                let expected: [u8; LEN] = [0b10001000u8; LEN];
755                if <$i>::MIN == Zero::zero() {
756                    a = BigInt::from_bytes_le(Sign::Plus, &bytes).try_into().unwrap();
757                    b = BigInt::from_bytes_le(Sign::Plus, &bytes_b).try_into().unwrap();
758                    exp = BigInt::from_bytes_le(Sign::Plus, &expected).try_into().unwrap();
759                } else {
760                    a = BigInt::from_signed_bytes_le(&bytes).try_into().unwrap();
761                    b = BigInt::from_signed_bytes_le(&bytes_b).try_into().unwrap();
762                    exp = BigInt::from_signed_bytes_le(&expected).try_into().unwrap();
763                }
764                assert_eq!(a.bitand(b), exp);
765                a &= b;
766                assert_eq!(a, exp);
767            }
768
769            #[test]
770            fn [<test_bits_shl_shr_combined_ $i:lower >]() {
771                let mut a: $i;
772                let mut b: $i;
773                let mut shift: $i;
774                let bytes: [u8; (<$i>::BITS / 8) as usize] = [0b01010101u8; (<$i>::BITS / 8) as usize];
775                let bits: u32 = if <$i>::MIN == Zero::zero() {
776                    <$i>::BITS
777                } else {
778                    <$i>::BITS - 1
779                };
780                for i in 0..bits {
781                    if <$i>::MIN == Zero::zero() {
782                        a = BigInt::from_bytes_le(Sign::Plus, &bytes).try_into().unwrap();
783                    } else {
784                        a = BigInt::from_signed_bytes_le(&bytes).try_into().unwrap();
785                    }
786                    b = a;
787                    a >>= i.try_into().unwrap();
788                    a <<= i.try_into().unwrap();
789                    shift = <$i>::try_from(2).unwrap().pow(i as u32);
790                    assert_eq!(a, b / shift * shift);
791                }
792            }
793
794
795            #[test]
796            fn [<test_bits_shl_ $i:lower >]() {
797                let mut a: $i;
798                let mut expect: BigInt;
799                let bytes: [u8; (<$i>::BITS / 8) as usize] = [0b01010101u8; (<$i>::BITS / 8) as usize];
800                let bits: u32 = if <$i>::MIN == Zero::zero() {
801                    <$i>::BITS
802                } else {
803                    <$i>::BITS - 1
804                };
805                for i in 0..bits {
806                    if <$i>::MIN == Zero::zero() {
807                        a = BigInt::from_bytes_le(Sign::Plus, &bytes).try_into().unwrap();
808                    } else {
809                        a = BigInt::from_signed_bytes_le(&bytes).try_into().unwrap();
810                    }
811                    expect = a.into();
812                    expect <<= i;
813                    a <<= i.try_into().unwrap();
814                    let mut expect_bytes = expect.to_signed_bytes_le();
815                    expect_bytes.resize((<$i>::BITS / 8) as usize, 0);
816                    #[cfg(not(feature = "alloc"))]
817                    println!("expect_bytes= {:?} len={}", expect_bytes, expect_bytes.len());
818                    assert_eq!(a, <$i>::from_le_bytes(expect_bytes.as_slice().try_into().unwrap()));
819                }
820            }
821
822            #[test]
823            fn [<test_bits_shr_ $i:lower >]() {
824                let mut a: $i;
825                let mut expect: BigInt;
826                let bytes: [u8; (<$i>::BITS / 8) as usize] = [0b01010101u8; (<$i>::BITS / 8) as usize];
827                let bits: u32 = if <$i>::MIN == Zero::zero() {
828                    <$i>::BITS
829                } else {
830                    <$i>::BITS - 1
831                };
832                for i in 0..bits {
833                    if <$i>::MIN == Zero::zero() {
834                        a = BigInt::from_bytes_le(Sign::Plus, &bytes).try_into().unwrap();
835                    } else {
836                        a = BigInt::from_signed_bytes_le(&bytes).try_into().unwrap();
837                    }
838                    expect = a.into();
839                    expect >>= i;
840                    a >>= i.try_into().unwrap();
841                    assert_eq!(a, expect.try_into().unwrap());
842                }
843            }
844
845            #[test]
846            fn [<test_try_from_builtin_ $i:lower>]() {
847               let mut a: $i;
848               from_builtin!{a, $i, (i8, u8, i16, u16, i32, u32, i64, u64, i128, u128)}
849            }
850
851            #[test]
852            fn [<test_try_to_builtin_ $i:lower>]() {
853               let a: $i = <$i>::try_from(11u8).unwrap();
854               to_builtin!{a, $i, (i8, u8, i16, u16, i32, u32, i64, u64, i128, u128)}
855            }
856
857            #[test]
858            fn [<test_try_from_safe_ $i:lower>]() {
859               let mut a: $i;
860               try_from_safe!{a, $i, $tlst}
861            }
862
863            #[test]
864            #[should_panic]
865            fn [<test_try_from_panic_ $i:lower>]() {
866                let mut expect: BigInt = 1.into();
867                expect = expect.shl(<$i>::BITS as u32);
868                let _:$i = expect.try_into().unwrap();
869            }
870
871            #[test]
872            fn [<test_try_from_bigint_ $i:lower>]() {
873                let mut a: $i;
874                const LEN: usize = (<$i>::BITS / 8) as usize;
875                let mut expect: BigInt = BigInt::from_signed_bytes_le(&[78u8; LEN]);
876                let bits: u32 = <$i>::BITS as u32;
877                for _ in 0..bits {
878                    a = expect.clone().try_into().unwrap();
879                    assert_eq!(a.to_string(), expect.clone().to_string());
880                    expect >>= 1;
881                }
882            }
883
884            #[test]
885            fn [<test_try_from_bigint_negative_ $i:lower>]() {
886                let mut a: $i;
887                const LEN: usize = (<$i>::BITS / 8) as usize;
888                let mut expect: BigInt;
889                if <$i>::MIN < Zero::zero() {
890                    expect = BigInt::from_signed_bytes_le(&[0x81u8; LEN]);
891                } else {
892                    // we do not test negative numbers on unsigned types
893                    expect = BigInt::from_bytes_le(Sign::Plus, &[0x81u8; LEN]);
894                }
895                let bits: u32 = <$i>::BITS as u32;
896                for _ in 0..bits {
897                    if expect.clone().to_signed_bytes_le().len() > LEN {
898                        expect = expect.clone().shr((expect.clone().to_signed_bytes_le().len() - LEN) * 8);
899                    }
900                    a = <$i>::try_from(expect.clone()).unwrap();
901                    assert_eq!(a.to_string(), expect.clone().to_string());
902                    expect >>= 1;
903                }
904            }
905
906            #[test]
907            fn [<test_try_from_vector_ $i:lower>]() {
908                #[cfg(feature = "alloc")]
909                use sbor::prelude::vec;
910
911                let mut a: $i;
912                let mut expect: BigInt;
913                let bits: usize = <$i>::BITS as usize;
914                for bytes in 0..bits/8 {
915                    expect = BigInt::from_signed_bytes_le(&vec![78u8; bytes]);
916                    a = <$i>::try_from(vec![78u8; bytes]).unwrap();
917                    assert_eq!(a.to_string(), expect.clone().to_string());
918                }
919            }
920
921            #[test]
922            fn [<test_try_from_slice_ $i:lower>]() {
923                #[cfg(feature = "alloc")]
924                use sbor::prelude::vec;
925
926                let mut a: $i;
927                let mut expect: BigInt;
928                let bits: usize = <$i>::BITS as usize;
929                let mut slice: &[u8];
930                let mut vec: Vec<u8>;
931                for bytes in 0..bits/8 {
932                    vec = vec![78u8; bytes];
933                    slice = &vec[..];
934                    expect = BigInt::from_signed_bytes_le(&slice);
935                    a = <$i>::try_from(slice).unwrap();
936                    assert_eq!(a.to_string(), expect.clone().to_string());
937                }
938            }
939
940            #[test]
941            fn [<test_from_string_ $i:lower>]() {
942                let mut a: $i = <$i>::from_str("118").unwrap();
943                assert_eq!(a.to_string(), "118");
944                let b: String = a.to_string();
945                a = <$i>::from_str(&b).unwrap();
946                assert_eq!(a.to_string(), "118");
947                let c: &str = &a.to_string();
948                a = <$i>::from_str(c).unwrap();
949                assert_eq!(a.to_string(), "118");
950            }
951
952            #[test]
953            fn [<test_bigint_from_ $i:lower>]() {
954                let a: $i = 119u8.try_into().unwrap();
955                let expect: BigInt = BigInt::from(a);
956                assert_eq!(a.to_string(), expect.to_string());
957            }
958        }
959    };
960}
961
962#[macro_export]
963macro_rules! test_add_all {
964    (($($i:ident),*), $tlst:tt) => {
965        $(
966            test_math!{$i, $i, $tlst}
967        )*
968    };
969}
970
971#[macro_export]
972macro_rules! test_signed {
973    ($($i:ident),*) => {
974        paste! {
975            $(
976                #[test]
977                fn [<test_neg_ $i:lower>]() {
978                    let a: $i = BigInt::from_bytes_le(Sign::Plus, &[10u8; (<$i>::BITS / 8) as usize]).try_into().unwrap();
979                    let expect: $i = BigInt::from_bytes_le(Sign::Minus, &[10u8; (<$i>::BITS / 8) as usize]).try_into().unwrap();
980                    assert_eq!(a.neg(), expect.try_into().unwrap());
981                }
982
983                #[test]
984                fn [<test_8_abs_pos_ $i:lower>]() {
985                    let a: $i = BigInt::from_bytes_le(Sign::Plus, &[8u8; (<$i>::BITS / 8) as usize]).try_into().unwrap();
986                    let expect: $i = BigInt::from_bytes_le(Sign::Plus, &[8u8; (<$i>::BITS / 8) as usize]).try_into().unwrap();
987                    assert_eq!(a.abs(), expect.try_into().unwrap());
988                }
989
990                #[test]
991                fn [<test_8_abs_neg_ $i:lower>]() {
992                    let a: $i = BigInt::from_bytes_le(Sign::NoSign, &[0u8; (<$i>::BITS / 8) as usize]).try_into().unwrap();
993                    let expect: $i = BigInt::from_bytes_le(Sign::NoSign, &[0u8; (<$i>::BITS / 8) as usize]).try_into().unwrap();
994                    assert_eq!(a.abs(), expect.try_into().unwrap());
995                }
996
997                #[test]
998                fn [<test_minus1_signum_neg_ $i:lower>]() {
999                    let a: $i = BigInt::from_bytes_le(Sign::Minus, &[13u8; (<$i>::BITS / 8) as usize]).try_into().unwrap();
1000                    let expect: $i = <$i>::from(-1i8);
1001                    assert_eq!(a.signum(), expect);
1002                }
1003
1004                #[test]
1005                fn [<test_8_signum_0_ $i:lower>]() {
1006                    let a: $i = Zero::zero();
1007                    let expect: $i = Zero::zero();
1008                    assert_eq!(a.signum(), expect);
1009                }
1010
1011                #[test]
1012                fn [<test_1_signum_0_ $i:lower>]() {
1013                    let a: $i = One::one();
1014                    let expect: $i = One::one();
1015                    assert_eq!(a.signum(), expect);
1016                }
1017
1018                #[test]
1019                fn [<test_1_is_positive_ $i:lower>]() {
1020                    let a: $i = One::one();
1021                    let expect = true;
1022                    assert_eq!(a.is_positive(), expect);
1023                }
1024
1025                #[test]
1026                fn [<test_0_is_positive_ $i:lower>]() {
1027                    let a: $i = Zero::zero();
1028                    let expect = false;
1029                    assert_eq!(a.is_positive(), expect);
1030                }
1031
1032                #[test]
1033                fn [<test_minus1_is_positive_ $i:lower>]() {
1034                    let a: $i = <$i>::from(-1i8);
1035                    let expect = false;
1036                    assert_eq!(a.is_positive(), expect);
1037                }
1038
1039                #[test]
1040                fn [<test_minus13_is_positive_ $i:lower>]() {
1041                    let a: $i = BigInt::from_bytes_le(Sign::Minus, &[13u8; (<$i>::BITS / 8) as usize]).try_into().unwrap();
1042                    let expect = false;
1043                    assert_eq!(a.is_positive(), expect);
1044                }
1045
1046                #[test]
1047                fn [<test_1_is_negative_ $i:lower>]() {
1048                    let a: $i = One::one();
1049                    let expect = false;
1050                    assert_eq!(a.is_negative(), expect);
1051                }
1052
1053                #[test]
1054                fn [<test_0_is_negative_ $i:lower>]() {
1055                    let a: $i = Zero::zero();
1056                    let expect = false;
1057                    assert_eq!(a.is_negative(), expect);
1058                }
1059
1060                #[test]
1061                fn [<test_minus1_is_negative_ $i:lower>]() {
1062                    let a: $i = <$i>::from(-1i8);
1063                    let expect = true;
1064                    assert_eq!(a.is_negative(), expect);
1065                }
1066
1067                #[test]
1068                fn [<test_minus13_is_negative_ $i:lower>]() {
1069                    let a: $i = BigInt::from_bytes_le(Sign::Minus, &[13u8; (<$i>::BITS / 8) as usize]).try_into().unwrap();
1070                    let expect = true;
1071                    assert_eq!(a.is_negative(), expect);
1072                }
1073
1074                #[test]
1075                fn [<test_min_signed_ $i:lower >]() {
1076                    let mut bytes = [0x00u8; (<$i>::BITS / 8) as usize];
1077                    bytes[bytes.len() - 1] = 0x80;
1078                    let expect: $i = BigInt::from_signed_bytes_le(&bytes).try_into().unwrap();
1079                    assert_eq!(<$i>::MIN, expect);
1080                }
1081
1082                #[test]
1083                fn [<test_max_signed_ $i:lower >]() {
1084                    let mut bytes = [0xffu8; (<$i>::BITS / 8) as usize];
1085                    bytes[bytes.len() - 1] = 0x7f;
1086                    let expect: $i = BigInt::from_signed_bytes_le(&bytes).try_into().unwrap();
1087                    assert_eq!(<$i>::MAX, expect);
1088                }
1089
1090                #[test]
1091                fn [<test_ord_neg_ $i:lower>]() {
1092                    let zero = <$i>::try_from(0i8).unwrap();
1093                    let minus_one = <$i>::try_from(-1i8).unwrap();
1094                    assert_eq!(zero.cmp(&minus_one), Ordering::Greater);
1095                }
1096            )*
1097        }
1098    };
1099}
1100
1101#[macro_export]
1102macro_rules! test_unsigned {
1103    ($($i:ident),*) => {
1104        paste! {
1105            $(
1106                #[test]
1107                fn [<test_8_is_power_of_two_ $i:lower>]() {
1108                    let mut bytes = [0u8; (<$i>::BITS / 8) as usize];
1109                    bytes[bytes.len() - 1] = 8u8;
1110                    let a: $i = BigInt::from_bytes_le(Sign::Plus, &bytes).try_into().unwrap();
1111                    let expect = true;
1112                    assert_eq!(a.is_power_of_two(), expect);
1113                }
1114
1115                #[test]
1116                fn [<test_3_is_power_of_two_3_ $i:lower>]() {
1117                    let mut bytes = [0u8; (<$i>::BITS / 8) as usize];
1118                    bytes[bytes.len() - 1] = 3;
1119                    let a: $i = BigInt::from_bytes_le(Sign::Plus, &bytes).try_into().unwrap();
1120                    let expect = false;
1121                    assert_eq!(a.is_power_of_two(), expect);
1122                }
1123
1124                #[test]
1125                fn [<test_0xff_is_power_of_two_ $i:lower>]() {
1126                    let bytes = [0xffu8; (<$i>::BITS / 8) as usize];
1127                    let a: $i = BigInt::from_bytes_le(Sign::Plus, &bytes).try_into().unwrap();
1128                    let expect = false;
1129                    assert_eq!(a.is_power_of_two(), expect);
1130                }
1131
1132                #[test]
1133                fn [<test_0_is_power_of_two_ $i:lower>]() {
1134                    let bytes = [0u8; (<$i>::BITS / 8) as usize];
1135                    let a: $i = BigInt::from_bytes_le(Sign::Plus, &bytes).try_into().unwrap();
1136                    let expect = false;
1137                    assert_eq!(a.is_power_of_two(), expect);
1138                }
1139
1140                #[test]
1141                fn [<test_8_next_power_of_two_ $i:lower>]() {
1142                    let mut bytes = [0u8; (<$i>::BITS / 8) as usize];
1143                    bytes[bytes.len() - 1] = 0b100u8;
1144                    let a: $i = BigInt::from_bytes_le(Sign::Plus, &bytes).try_into().unwrap();
1145                    bytes[bytes.len() - 1] = 0b100u8;
1146                    let expect: $i = BigInt::from_bytes_le(Sign::Plus, &bytes).try_into().unwrap();
1147                    assert_eq!(a.next_power_of_two(), expect);
1148                }
1149
1150                #[test]
1151                fn [<test_0b01011111_next_power_of_two_ $i:lower>]() {
1152                    let mut bytes = [0b01011111u8; (<$i>::BITS / 8) as usize];
1153                    let a: $i = BigInt::from_bytes_le(Sign::Plus, &bytes).try_into().unwrap();
1154                    bytes = [0u8; (<$i>::BITS / 8) as usize];
1155                    bytes[bytes.len() - 1] = 0b10000000u8;
1156                    let expect: $i = BigInt::from_bytes_le(Sign::Plus, &bytes).try_into().unwrap();
1157                    assert_eq!(a.next_power_of_two(), expect);
1158                }
1159
1160                #[test]
1161                fn [<test_0_next_power_of_two_ $i:lower>]() {
1162                    let bytes = [0u8; (<$i>::BITS / 8) as usize];
1163                    let a: $i = BigInt::from_bytes_le(Sign::Plus, &bytes).try_into().unwrap();
1164                    let expect = <$i>::one();
1165                    assert_eq!(a.next_power_of_two(), expect);
1166                }
1167
1168                #[test]
1169                #[should_panic]
1170                fn [<test_0b10000001_next_power_of_two_ $i:lower>]() {
1171                    let mut bytes = [0b10000000u8; (<$i>::BITS / 8) as usize];
1172                    if <$i>::BITS > 8 {
1173                        bytes[0] = 0b0000000001u8;
1174                    } else {
1175                        bytes[0] = 0b10000001u8;
1176                    }
1177                    let a: $i = BigInt::from_bytes_le(Sign::Plus, &bytes).try_into().unwrap();
1178                    let _ = a.next_power_of_two();
1179                }
1180
1181                #[test]
1182                fn [<test_min_unsigned_ $i:lower >]() {
1183                    let expect = <$i>::from(0u8);
1184                    assert_eq!(<$i>::MIN, expect);
1185                }
1186
1187                #[test]
1188                fn [<test_max_unsigned_ $i:lower >]() {
1189                    let bytes = [0xffu8; (<$i>::BITS / 8) as usize];
1190                    let expect: $i = BigInt::from_bytes_le(Sign::Plus, &bytes).try_into().unwrap();
1191                    assert_eq!(<$i>::MAX, expect);
1192                }
1193            )*
1194        }
1195    };
1196}
1197
1198#[macro_export]
1199macro_rules! test_from_all_types_builtin_safe {
1200    ($i:ty, ($($from:ty),*)) => {
1201        paste!{
1202        $(
1203            #[test]
1204            fn [<test_from_builtin_ $i:lower _from_safe_ $from:lower>]() {
1205                let a: $i = <$i>::from(<$from>::try_from(112u8).unwrap());
1206                let expect: $i = <$i>::try_from(112u8).unwrap();
1207                assert_eq!(a, expect);
1208            }
1209        )*
1210        }
1211    };
1212}
1213
1214#[macro_export]
1215macro_rules! test_from_all_types_safe_builtin {
1216    ($i:ty, ($($from:ty),*)) => {
1217        paste!{
1218        $(
1219            #[test]
1220            fn [<test_from_safe_ $i:lower _from _builtin_ $from:lower>]() {
1221                let a: $i = <$i>::from(<$from>::try_from(112u8).unwrap());
1222                let expect: $i = <$i>::try_from(112u8).unwrap();
1223                assert_eq!(a, expect);
1224            }
1225        )*
1226        }
1227    };
1228}
1229
1230#[macro_export]
1231macro_rules! test_from_all_types_safe_safe {
1232    ($i:ty, ($($from:ty),*)) => {
1233        paste!{
1234        $(
1235            #[test]
1236            fn [<test_from_safe_ $i:lower _from_safe_ $from:lower>]() {
1237                let a: $i = <$i>::try_from(
1238                        <$from>::try_from(112u8).unwrap()
1239                    ).unwrap();
1240                let expect: $i = <$i>::try_from(112u8).unwrap();
1241                assert_eq!(a, expect);
1242            }
1243        )*
1244        }
1245    };
1246}