|
| 1 | +/// Write the proptests for an integer type. |
| 2 | +macro_rules! int_proptests { |
| 3 | + ( $($int:ident),+ ) => { |
| 4 | + $( |
| 5 | + mod $int { |
| 6 | + use ctutils::{CtSelect, CtEq, Choice}; |
| 7 | + use proptest::prelude::*; |
| 8 | + |
| 9 | + proptest! { |
| 10 | + #[test] |
| 11 | + fn ct_assign(a in any::<$int>(), b in any::<$int>(), byte in any::<u8>()) { |
| 12 | + let choice = Choice::from_u8_lsb(byte); |
| 13 | + let mut actual = a; |
| 14 | + actual.ct_assign(&b, choice); |
| 15 | + |
| 16 | + let expected = if byte & 1 == 1 { |
| 17 | + b |
| 18 | + } else { |
| 19 | + a |
| 20 | + }; |
| 21 | + |
| 22 | + prop_assert_eq!(expected, actual); |
| 23 | + } |
| 24 | + |
| 25 | + #[test] |
| 26 | + fn ct_eq(a in any::<$int>(), b in any::<$int>()) { |
| 27 | + let actual = a.ct_eq(&b); |
| 28 | + prop_assert_eq!(a == b, actual.to_bool()); |
| 29 | + } |
| 30 | + |
| 31 | + #[test] |
| 32 | + fn ct_ne(a in any::<$int>(), b in any::<$int>()) { |
| 33 | + let actual = a.ct_ne(&b); |
| 34 | + prop_assert_eq!(a != b, actual.to_bool()); |
| 35 | + } |
| 36 | + |
| 37 | + #[test] |
| 38 | + fn ct_select(a in any::<$int>(), b in any::<$int>(), byte in any::<u8>()) { |
| 39 | + let choice = Choice::from_u8_lsb(byte); |
| 40 | + let actual = a.ct_select(&b, choice); |
| 41 | + |
| 42 | + let expected = if byte & 1 == 1 { |
| 43 | + b |
| 44 | + } else { |
| 45 | + a |
| 46 | + }; |
| 47 | + |
| 48 | + prop_assert_eq!(expected, actual); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + )+ |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +int_proptests!(i8, i16, i32, i64, i128, u8, u16, u32, u64, u128); |
0 commit comments