compact_thrift_runtime/
lib.rs

1mod error;
2pub mod macros;
3mod protocol;
4mod types;
5mod uleb;
6
7pub use error::*;
8pub use protocol::*;
9
10#[cfg(test)]
11mod tests {
12    use crate::{CompactThriftInput, CompactThriftOutput, CompactThriftProtocol, FieldName, skip_field, CompactThriftInputSlice, ThriftError};
13    use crate::uleb::decode_uleb;
14
15    #[test]
16    fn test_size_of_error() {
17        assert_eq!(std::mem::size_of::<ThriftError>(), 16);
18        assert_eq!(std::mem::size_of::<Result<(), ThriftError>>(), 16);
19    }
20
21    #[test]
22    fn test_field_name() {
23        assert_eq!(std::mem::size_of::<FieldName>(), std::mem::size_of::<usize>());
24        assert_eq!(&FieldName::from(c"foobar").to_string(), "foobar");
25    }
26
27    #[test]
28    fn test_slice_input_read_byte() {
29        let mut input = CompactThriftInputSlice::new(&[1]);
30        assert_eq!(input.read_byte().unwrap(), 1);
31    }
32
33    #[test]
34    fn test_read_uleb() {
35        assert_eq!(decode_uleb(&mut CompactThriftInputSlice::new(&[1])).unwrap(), 1);
36        assert_eq!(decode_uleb(&mut CompactThriftInputSlice::new(&[0b0111_1111])).unwrap(), 0b0111_1111);
37        assert_eq!(decode_uleb(&mut CompactThriftInputSlice::new(&[0b1000_1111, 0b0111_0101])).unwrap(), 0b0011_1010_1000_1111);
38    }
39
40    #[test]
41    fn test_read_uleb_overlong() {
42        decode_uleb(&mut CompactThriftInputSlice::new(&[0b1000_0001, 0b1000_0001, 0b1000_0001, 0b1000_0001, 0b1000_0001, 0b1000_0001, 0b1000_0001, 0b1000_0001, 0b1000_0001, 0b1000_0001, 0b1000_0001, 0])).unwrap();
43    }
44
45    #[test]
46    fn test_slice_input_read_i32() {
47        assert_eq!(CompactThriftInputSlice::new(&[0]).read_i32().unwrap(), 0);
48        assert_eq!(CompactThriftInputSlice::new(&[1]).read_i32().unwrap(), -1);
49        assert_eq!(CompactThriftInputSlice::new(&[2]).read_i32().unwrap(), 1);
50
51        assert_eq!(CompactThriftInputSlice::new(&[0b0111_1111]).read_i32().unwrap(), -64);
52        assert_eq!(CompactThriftInputSlice::new(&[0b1000_1111, 0b0111_0101]).read_i32().unwrap(), -7496);
53        assert_eq!(CompactThriftInputSlice::new(&[0b1000_1111, 0b0111_0101, 0, 0]).read_i32().unwrap(), -7496);
54        assert_eq!(CompactThriftInputSlice::new(&[0b1000_1111, 0b0111_0101, 0, 0, 0]).read_i32().unwrap(), -7496);
55    }
56
57    #[test]
58    fn test_uleb_roundtrip() {
59        let mut w = vec![];
60        w.write_i64(1234567890).unwrap();
61        let mut r = CompactThriftInputSlice::new(&w);
62        assert_eq!(r.read_i64().unwrap(), 1234567890);
63    }
64
65    #[test]
66    fn test_read_vec_bool() {
67        let mut data = CompactThriftInputSlice::new(&[0x42, 0, 1, 1, 0]);
68        let actual = Vec::<bool>::read_thrift(&mut data).unwrap();
69        let expected = vec![false, true, true, false];
70        assert_eq!(&actual, &expected);
71
72        // also allow element type 1 for boolean
73        let mut data = CompactThriftInputSlice::new(&[0x41, 0, 1, 1, 0]);
74        let actual = Vec::<bool>::read_thrift(&mut data).unwrap();
75        let expected = vec![false, true, true, false];
76        assert_eq!(&actual, &expected);
77    }
78
79    #[test]
80    fn test_read_box() {
81        let mut data = CompactThriftInputSlice::new(&[0x2]);
82        let actual = Box::<i32>::read_thrift(&mut data).unwrap();
83        let expected = Box::new(1);
84        assert_eq!(&actual, &expected);
85    }
86
87    #[test]
88    fn test_read_option_box() {
89        let mut data = CompactThriftInputSlice::new(&[0x2]);
90        let actual = Option::<Box::<i32>>::read_thrift(&mut data).unwrap();
91        let expected = Some(Box::new(1));
92        assert_eq!(&actual, &expected);
93    }
94
95    #[test]
96    fn test_empty_vec_roundtrip() {
97        let input = Vec::<i64>::default();
98        let mut buffer = vec![];
99        input.write_thrift(&mut buffer).unwrap();
100        let result = Vec::<i64>::read_thrift(&mut CompactThriftInputSlice::new(&buffer)).unwrap();
101        assert_eq!(&result, &input);
102    }
103
104    #[test]
105    fn test_vec_bool_roundtrip() {
106        let input = vec![true, false, false, true];
107        let mut buffer = vec![];
108        input.write_thrift(&mut buffer).unwrap();
109        let result = Vec::<bool>::read_thrift(&mut CompactThriftInputSlice::new(&buffer)).unwrap();
110        assert_eq!(&result, &input);
111    }
112
113    #[test]
114    fn test_vec_integer_roundtrip() {
115        let input = vec![1_i64, i64::MIN, i64::MAX, 9999999];
116        let mut buffer = vec![];
117        input.write_thrift(&mut buffer).unwrap();
118        let result = Vec::<i64>::read_thrift(&mut CompactThriftInputSlice::new(&buffer)).unwrap();
119        assert_eq!(&result, &input);
120    }
121
122    #[test]
123    fn test_skip_vec_bool() {
124        let input = vec![true, false, false, true];
125        let mut buffer = vec![];
126        input.write_thrift(&mut buffer).unwrap();
127        let mut slice = CompactThriftInputSlice::new(&buffer);
128        skip_field(&mut slice, Vec::<bool>::FIELD_TYPE, true).unwrap();
129        assert_eq!(&slice.as_slice(), &[]);
130    }
131
132    #[test]
133    fn test_skip_vec_integer() {
134        let input = vec![1_i64, 999999999999, -1, i64::MAX];
135        let mut buffer = vec![];
136        input.write_thrift(&mut buffer).unwrap();
137        let mut slice = CompactThriftInputSlice::new(&buffer);
138        skip_field(&mut slice, Vec::<i64>::FIELD_TYPE, true).unwrap();
139        assert_eq!(&slice.as_slice(), &[]);
140    }
141}