summaryrefslogtreecommitdiffstats
path: root/third_party/rust/serde_cbor/tests/canonical.rs
blob: 438e29e7d474546cbe6dfb690e37a251fb6abf85 (plain)
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
#[cfg(feature = "std")]
mod std_tests {
    use serde_cbor::value::Value;

    #[test]
    fn integer_canonical_sort_order() {
        let expected = [
            0,
            23,
            24,
            255,
            256,
            65535,
            65536,
            4294967295,
            -1,
            -24,
            -25,
            -256,
            -257,
            -65536,
            -65537,
            -4294967296,
        ]
        .iter()
        .map(|i| Value::Integer(*i))
        .collect::<Vec<_>>();

        let mut sorted = expected.clone();
        sorted.sort();

        assert_eq!(expected, sorted);
    }

    #[test]
    fn string_canonical_sort_order() {
        let expected = ["", "a", "b", "aa"]
            .iter()
            .map(|s| Value::Text(s.to_string()))
            .collect::<Vec<_>>();

        let mut sorted = expected.clone();
        sorted.sort();

        assert_eq!(expected, sorted);
    }

    #[test]
    fn bytes_canonical_sort_order() {
        let expected = vec![vec![], vec![0u8], vec![1u8], vec![0u8, 0u8]]
            .into_iter()
            .map(|v| Value::Bytes(v))
            .collect::<Vec<_>>();

        let mut sorted = expected.clone();
        sorted.sort();

        assert_eq!(expected, sorted);
    }

    #[test]
    fn simple_data_canonical_sort_order() {
        let expected = vec![Value::Bool(false), Value::Bool(true), Value::Null];

        let mut sorted = expected.clone();
        sorted.sort();

        assert_eq!(expected, sorted);
    }

    #[test]
    fn major_type_canonical_sort_order() {
        let expected = vec![
            Value::Integer(0),
            Value::Integer(-1),
            Value::Bytes(vec![]),
            Value::Text("".to_string()),
            Value::Null,
        ];

        let mut sorted = expected.clone();
        sorted.sort();

        assert_eq!(expected, sorted);
    }

    #[test]
    fn test_rfc_example() {
        // See: https://tools.ietf.org/html/draft-ietf-cbor-7049bis-04#section-4.10
        let expected = vec![
            Value::Integer(10),
            Value::Integer(100),
            Value::Integer(-1),
            Value::Text("z".to_owned()),
            Value::Text("aa".to_owned()),
            Value::Array(vec![Value::Integer(100)]),
            Value::Array(vec![Value::Integer(-1)]),
            Value::Bool(false),
        ];
        let mut sorted = expected.clone();
        sorted.sort();
        assert_eq!(expected, sorted);
    }
}