1 #[cfg(feature = "std")]
2 mod std_tests {
3     use serde_cbor::value::Value;
4 
5     #[test]
integer_canonical_sort_order()6     fn integer_canonical_sort_order() {
7         let expected = [
8             0,
9             23,
10             24,
11             255,
12             256,
13             65535,
14             65536,
15             4294967295,
16             -1,
17             -24,
18             -25,
19             -256,
20             -257,
21             -65536,
22             -65537,
23             -4294967296,
24         ]
25         .iter()
26         .map(|i| Value::Integer(*i))
27         .collect::<Vec<_>>();
28 
29         let mut sorted = expected.clone();
30         sorted.sort();
31 
32         assert_eq!(expected, sorted);
33     }
34 
35     #[test]
string_canonical_sort_order()36     fn string_canonical_sort_order() {
37         let expected = ["", "a", "b", "aa"]
38             .iter()
39             .map(|s| Value::Text(s.to_string()))
40             .collect::<Vec<_>>();
41 
42         let mut sorted = expected.clone();
43         sorted.sort();
44 
45         assert_eq!(expected, sorted);
46     }
47 
48     #[test]
bytes_canonical_sort_order()49     fn bytes_canonical_sort_order() {
50         let expected = vec![vec![], vec![0u8], vec![1u8], vec![0u8, 0u8]]
51             .into_iter()
52             .map(|v| Value::Bytes(v))
53             .collect::<Vec<_>>();
54 
55         let mut sorted = expected.clone();
56         sorted.sort();
57 
58         assert_eq!(expected, sorted);
59     }
60 
61     #[test]
simple_data_canonical_sort_order()62     fn simple_data_canonical_sort_order() {
63         let expected = vec![Value::Bool(false), Value::Bool(true), Value::Null];
64 
65         let mut sorted = expected.clone();
66         sorted.sort();
67 
68         assert_eq!(expected, sorted);
69     }
70 
71     #[test]
major_type_canonical_sort_order()72     fn major_type_canonical_sort_order() {
73         let expected = vec![
74             Value::Integer(0),
75             Value::Integer(-1),
76             Value::Bytes(vec![]),
77             Value::Text("".to_string()),
78             Value::Null,
79         ];
80 
81         let mut sorted = expected.clone();
82         sorted.sort();
83 
84         assert_eq!(expected, sorted);
85     }
86 
87     #[test]
test_rfc_example()88     fn test_rfc_example() {
89         // See: https://tools.ietf.org/html/draft-ietf-cbor-7049bis-04#section-4.10
90         let expected = vec![
91             Value::Integer(10),
92             Value::Integer(100),
93             Value::Integer(-1),
94             Value::Text("z".to_owned()),
95             Value::Text("aa".to_owned()),
96             Value::Array(vec![Value::Integer(100)]),
97             Value::Array(vec![Value::Integer(-1)]),
98             Value::Bool(false),
99         ];
100         let mut sorted = expected.clone();
101         sorted.sort();
102         assert_eq!(expected, sorted);
103     }
104 }
105