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
#[cfg(test)]
mod tests {
    use crate::fixed::FixedInt;

    #[cfg(any(feature = "tokio_async", feature = "futures_async"))]
    use crate::reader::FixedIntAsyncReader;
    #[cfg(any(feature = "tokio_async", feature = "futures_async"))]
    use crate::writer::FixedIntAsyncWriter;

    use crate::reader::FixedIntReader;
    use crate::writer::FixedIntWriter;

    #[test]
    fn test_u32_enc() {
        let result = (32 as u32).encode_fixed_vec();
        assert_eq!(result, vec![32, 0, 0, 0]);
    }
    #[test]
    fn test_u16_enc() {
        let result = (256 as u16).encode_fixed_vec();
        assert_eq!(result, vec![0, 1]);
    }
    #[test]
    fn test_u8_enc() {
        let result = (255 as u8).encode_fixed_vec();
        assert_eq!(result, vec![255]);
    }
    #[test]
    fn test_i8_enc() {
        let result = (-1 as i8).encode_fixed_vec();
        assert_eq!(result, vec![255]);
    }
    #[test]
    fn test_i16_enc() {
        let result = (-32768 as i16).encode_fixed_vec();
        assert_eq!(result, vec![0, 128]);
    }
    #[test]
    fn test_i32_enc() {
        let result = (-32767 as i32).encode_fixed_vec();
        assert_eq!(result, vec![1, 128, 255, 255]);
    }

    // This must fail to compile:
    /*
    fn test() -> &'static [u8] {
        let int = -32767 as i32;
        let result = int.encode_fixed_light();
        assert_eq!(result, &[1, 128, 255, 255]);
        result
    }
    */

    #[test]
    fn test_i32_enc_light() {
        let int = -32767 as i32;
        let result = int.encode_fixed_light();
        assert_eq!(result, &[1, 128, 255, 255]);
    }
    #[test]
    fn test_all_identity() {
        let a: u8 = 17;
        let b: u16 = 17;
        let c: u32 = 17;
        let d: u64 = 17;
        let e: i8 = -17;
        let f: i16 = -17;
        let g: i32 = -17;
        let h: i64 = -17;

        assert_eq!(a, FixedInt::decode_fixed_vec(&a.encode_fixed_vec()));
        assert_eq!(b, FixedInt::decode_fixed_vec(&b.encode_fixed_vec()));
        assert_eq!(c, FixedInt::decode_fixed_vec(&c.encode_fixed_vec()));
        assert_eq!(d, FixedInt::decode_fixed_vec(&d.encode_fixed_vec()));
        assert_eq!(e, FixedInt::decode_fixed_vec(&e.encode_fixed_vec()));
        assert_eq!(f, FixedInt::decode_fixed_vec(&f.encode_fixed_vec()));
        assert_eq!(g, FixedInt::decode_fixed_vec(&g.encode_fixed_vec()));
        assert_eq!(h, FixedInt::decode_fixed_vec(&h.encode_fixed_vec()));

        assert_eq!(a, FixedInt::decode_fixed(&a.encode_fixed_light()));
        assert_eq!(b, FixedInt::decode_fixed(&b.encode_fixed_light()));
        assert_eq!(c, FixedInt::decode_fixed(&c.encode_fixed_light()));
        assert_eq!(d, FixedInt::decode_fixed(&d.encode_fixed_light()));
        assert_eq!(e, FixedInt::decode_fixed(&e.encode_fixed_light()));
        assert_eq!(f, FixedInt::decode_fixed(&f.encode_fixed_light()));
        assert_eq!(g, FixedInt::decode_fixed(&g.encode_fixed_light()));
        assert_eq!(h, FixedInt::decode_fixed(&h.encode_fixed_light()));
    }

    #[test]
    fn test_reader_writer() {
        let mut buf = Vec::with_capacity(128);

        let i1: u32 = 123;
        let i2: u32 = 124;
        let i3: u32 = 125;

        assert!(buf.write_fixedint(i1).is_ok());
        assert!(buf.write_fixedint(i2).is_ok());
        assert!(buf.write_fixedint(i3).is_ok());

        assert_eq!(3 * 4, buf.len());

        let mut reader: &[u8] = buf.as_ref();

        let i1_res = reader.read_fixedint().unwrap();
        let i2_res = reader.read_fixedint().unwrap();
        let i3_res = reader.read_fixedint().unwrap();

        assert_eq!(i1, i1_res);
        assert_eq!(i2, i2_res);
        assert_eq!(i3, i3_res);

        assert!(reader.read_fixedint::<u32>().is_err());
    }

    #[should_panic]
    #[test]
    fn test_invalid_decode_size() {
        assert_eq!(33, u64::decode_fixed(&[1, 0, 0, 0, 0, 1]));
    }
    #[should_panic]
    #[test]
    fn test_invalid_encode_size() {
        let mut buf = [0 as u8; 4];
        (11 as u64).encode_fixed(&mut buf);
    }

    #[cfg(any(feature = "tokio_async", feature = "futures_async"))]
    #[tokio::test]
    async fn test_async_reader() {
        let mut buf = Vec::with_capacity(128);

        let i1: u32 = 1;
        let i2: u32 = 65532;
        let i3: u32 = 4200123456;
        let i4: i64 = i3 as i64 * 1000;
        let i5: i32 = -32456;
        let i6: i8 = -128;
        let i7: u8 = 255;

        buf.write_fixedint_async(i1).await.unwrap();
        buf.write_fixedint_async(i2).await.unwrap();
        buf.write_fixedint_async(i3).await.unwrap();
        buf.write_fixedint_async(i4).await.unwrap();
        buf.write_fixedint_async(i5).await.unwrap();
        buf.write_fixedint_async(i6).await.unwrap();
        buf.write_fixedint_async(i7).await.unwrap();

        let mut reader: &[u8] = buf.as_ref();

        assert_eq!(i1, reader.read_fixedint_async().await.unwrap());
        assert_eq!(i2, reader.read_fixedint_async().await.unwrap());
        assert_eq!(i3, reader.read_fixedint_async().await.unwrap());
        assert_eq!(i4, reader.read_fixedint_async().await.unwrap());
        assert_eq!(i5, reader.read_fixedint_async().await.unwrap());
        assert_eq!(i6, reader.read_fixedint_async().await.unwrap());
        assert_eq!(i7, reader.read_fixedint_async().await.unwrap());
        assert!(reader.read_fixedint_async::<u32>().await.is_err());
    }
}