1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#[cfg(test)]
#[cfg(feature = "nightly")]
macro_rules! test_from_endian_slice {
    ($int: ty, $endian: ident) => {
        paste::paste! {
            quickcheck::quickcheck! {
                fn [<quickcheck_ $int _from_ $endian _slice>](int: $int, pad_length: u8) -> quickcheck::TestResult {
                    type Big = [<$int:upper>];
                    type Primitive = crate::test::types::$int;

                    use crate::test::TestConvert;
                    use crate::int::endian;

                    // pad_length is greater than the size of the integer in bytes
                    if pad_length >= Primitive::BITS as u8 / 8 {
                        return quickcheck::TestResult::discard();
                    }
                    let pad_length = pad_length as usize;

                    #[allow(unused_comparisons)]
                    let mut pad_bits = if int < 0 {
                        u8::MAX // 1111...
                    } else {
                        u8::MIN // 0000...
                    };

                    let mut bytes = int.[<to_ $endian _bytes>](); // random input bytes
                    // first, test that the original bytes as slice is converted back to the same integer
                    let mut passed = TestConvert::into(Big::[<from_ $endian _slice>](&bytes[..])) == Some(int);

                    let bytes_vec = endian::[<$endian _bytes_vec>](&bytes[..], pad_bits, pad_length); // random vector padded with a random amount of bytes
                    // test that the padded bytes are still converted back to the same integer
                    passed &= TestConvert::into(Big::[<from_ $endian _slice>](&bytes_vec[..])) == Some(int);

                    // most significant byte position, range of bytes indices to change to padding bits, range of bytes indices that will result in the same integer without the padding bits
                    let (msb, pad_range, slice_range) = endian::[<$endian _pad>](pad_length, Primitive::BITS);

                    pad_bits = {
                        #[allow(unused_comparisons)]
                        if Primitive::MIN < 0 && (bytes[msb] as i8).is_negative() {
                            u8::MAX
                        } else {
                            u8::MIN
                        }
                    };

                    for item in &mut bytes[pad_range] {
                        *item = pad_bits;
                    }
                    let correct = Some(Big::[<from_ $endian _bytes>](bytes));
                    // test that a shortened slice of bytes is converted to the same integer as the shortened slice that is padded to be the same number of bytes as the size of the integer
                    passed &= Big::[<from_ $endian _slice>](&bytes[slice_range]) == correct;

                    let bytes_vec = endian::[<$endian _bytes_vec>](&bytes[..], pad_bits, pad_length);
                    passed &= Big::[<from_ $endian _slice>](&bytes_vec[..]) == correct;

                    quickcheck::TestResult::from_bool(passed)
                }
            }
        }
    };
}

#[cfg(test)]
#[cfg(feature = "nightly")]
pub(crate) use test_from_endian_slice;

#[cfg(test)]
#[cfg(feature = "nightly")]
use alloc::vec::Vec;

#[cfg(test)]
#[cfg(feature = "nightly")]
use core::ops::{Range, RangeFrom};

#[cfg(feature = "nightly")]
#[cfg(test)]
/// Pad a slice of bytes with leading pad bits so that the resulting vector of bytes represents the same integer as the original slice
pub fn be_bytes_vec(bytes: &[u8], pad_bits: u8, pad_length: usize) -> Vec<u8> {
    let mut bytes_vec = vec![pad_bits; pad_length];
    bytes_vec.append(&mut bytes.to_vec());
    bytes_vec
}

#[cfg(feature = "nightly")]
#[cfg(test)]
pub fn be_pad(pad_length: usize, _bits: u32) -> (usize, Range<usize>, RangeFrom<usize>) {
    (pad_length, 0..pad_length, pad_length..)
}

#[cfg(feature = "nightly")]
#[cfg(test)]
/// Pad a slice of bytes with trailing pad bits so that the resulting vector of bytes represents the same integer as the original slice
pub fn le_bytes_vec(bytes: &[u8], pad_bits: u8, pad_length: usize) -> Vec<u8> {
    let mut bytes_vec = bytes.to_vec();
    bytes_vec.append(&mut vec![pad_bits; pad_length]);
    bytes_vec
}

#[cfg(feature = "nightly")]
#[cfg(test)]
pub fn le_pad(pad_length: usize, bits: u32) -> (usize, Range<usize>, Range<usize>) {
    let bytes = bits as usize / 8;
    (
        bytes - 1 - pad_length,
        (bytes - pad_length)..bytes,
        0..(bytes - pad_length),
    )
}

#[cfg(test)]
macro_rules! tests {
    ($Digit: ident; $int: ty) => {
        #[cfg(feature = "nightly")]
        use crate::test::U8ArrayWrapper;

        test_bignum! {
            function: <$int>::from_be(a: $int)
        }
        test_bignum! {
            function: <$int>::from_le(a: $int)
        }
        test_bignum! {
            function: <$int>::to_be(a: $int)
        }
        test_bignum! {
            function: <$int>::to_le(a: $int)
        }

        #[cfg(feature = "nightly")]
        test_bignum! {
            function: <$int>::to_be_bytes(a: $int)
        }

        #[cfg(feature = "nightly")]
        test_bignum! {
            function: <$int>::to_le_bytes(a: $int)
        }

        #[cfg(feature = "nightly")]
        test_bignum! {
            function: <$int>::to_ne_bytes(a: $int)
        }

        #[cfg(feature = "nightly")]
        test_bignum! {
            function: <$int>::from_be_bytes(a: U8ArrayWrapper<{<$int>::BITS as usize / 8}>)
        }

        #[cfg(feature = "nightly")]
        test_bignum! {
            function: <$int>::from_le_bytes(a: U8ArrayWrapper<{<$int>::BITS as usize / 8}>)
        }

        #[cfg(feature = "nightly")]
        test_bignum! {
            function: <$int>::from_ne_bytes(a: U8ArrayWrapper<{<$int>::BITS as usize / 8}>)
        }

        #[cfg(feature = "nightly")]
        crate::int::endian::test_from_endian_slice!($int, be);

        #[cfg(feature = "nightly")]
        crate::int::endian::test_from_endian_slice!($int, le);
    };
}

#[cfg(test)]
pub(crate) use tests;