1
use std::borrow::Cow;
2

            
3
use bonsaidb::core::key::{Key, KeyEncoding};
4

            
5
1
#[test]
6
1
fn tuple_struct() {
7
1
    #[derive(Clone, Debug, Key)]
8
1
    struct Test(i32, i32, String);
9
1

            
10
1
    assert_eq!(
11
1
        &[0, 0, 0, 1, 0, 0, 0, 2, 116, 101, 115, 116, 0, 4],
12
1
        Test(1, 2, "test".into()).as_ord_bytes().unwrap().as_ref()
13
1
    )
14
1
}
15

            
16
1
#[test]
17
1
fn transparent_structs() {
18
1
    #[derive(Clone, Debug, Key)]
19
1
    struct Test(i32);
20
1
    #[derive(Clone, Debug, Key)]
21
1
    struct TestNamed {
22
1
        named: i32,
23
1
    }
24
1

            
25
1
    assert_eq!(&[0, 0, 0, 1], Test(1).as_ord_bytes().unwrap().as_ref());
26
1
    assert_eq!(
27
1
        &[0, 0, 0, 1],
28
1
        TestNamed { named: 1 }.as_ord_bytes().unwrap().as_ref()
29
1
    )
30
1
}
31

            
32
1
#[test]
33
1
fn struct_struct() {
34
1
    #[derive(Clone, Debug, Key)]
35
1
    struct Test {
36
1
        a: i32,
37
1
        b: String,
38
1
    }
39
1
    assert_eq!(
40
1
        &[255, 255, 255, 214, 109, 101, 97, 110, 105, 110, 103, 0, 7],
41
1
        Test {
42
1
            a: -42,
43
1
            b: "meaning".into()
44
1
        }
45
1
        .as_ord_bytes()
46
1
        .unwrap()
47
1
        .as_ref()
48
1
    )
49
1
}
50

            
51
1
#[test]
52
1
fn unit_struct() {
53
1
    #[derive(Clone, Debug, Key)]
54
1
    struct Test;
55
1

            
56
1
    assert_eq!(b"", Test.as_ord_bytes().unwrap().as_ref())
57
1
}
58

            
59
1
#[test]
60
1
fn r#enum() {
61
3
    #[derive(Clone, Debug, Key)]
62
1
    enum Test {
63
1
        A,
64
1
        B(i32, String),
65
1
        C { a: String, b: i32 },
66
1
    }
67
1

            
68
1
    assert_eq!(&[128, 0, 1], Test::A.as_ord_bytes().unwrap().as_ref());
69

            
70
1
    assert_eq!(
71
1
        &[129, 0, 0, 0, 0, 2, 97, 0, 1, 1],
72
1
        Test::B(2, "a".into()).as_ord_bytes().unwrap().as_ref()
73
1
    );
74

            
75
1
    assert_eq!(
76
1
        &[130, 0, 98, 0, 0, 0, 0, 3, 1, 1],
77
1
        Test::C {
78
1
            a: "b".into(),
79
1
            b: 3
80
1
        }
81
1
        .as_ord_bytes()
82
1
        .unwrap()
83
1
        .as_ref()
84
1
    )
85
1
}
86

            
87
1
#[test]
88
1
fn enum_repr() {
89
1
    #[repr(u8)]
90
4
    #[derive(Clone, Debug, Key)]
91
1
    enum Test1 {
92
1
        A = 1,
93
1
        B = 2,
94
1
    }
95
1

            
96
2
    #[derive(Clone, Debug, Key)]
97
1
    #[key(enum_repr = u8)]
98
1
    enum Test2 {
99
1
        A = 2,
100
1
        B = 1,
101
1
    }
102
1

            
103
1
    assert_eq!(
104
1
        Test1::A.as_ord_bytes().unwrap(),
105
1
        Test2::B.as_ord_bytes().unwrap()
106
1
    );
107
1
    assert_eq!(Test1::A.as_ord_bytes().unwrap().as_ref(), &[1]);
108

            
109
1
    assert_eq!(
110
1
        Test1::B.as_ord_bytes().unwrap(),
111
1
        Test2::A.as_ord_bytes().unwrap()
112
1
    );
113
1
    assert_eq!(Test1::B.as_ord_bytes().unwrap().as_ref(), &[2]);
114
1
}
115

            
116
1
#[test]
117
1
fn enum_u64() {
118
1
    #[repr(u64)]
119
1
    #[derive(Clone, Debug, Key)]
120
1
    enum Test {
121
1
        A = 0,
122
1
        B = u64::MAX - 1,
123
1
        C,
124
1
    }
125
1
    assert_eq!(
126
1
        Test::C.as_ord_bytes().unwrap().as_ref(),
127
1
        &[255, 255, 255, 255, 255, 255, 255, 255]
128
1
    );
129
1
}
130

            
131
1
#[test]
132
1
fn lifetime() {
133
1
    #[derive(Clone, Debug, Key)]
134
1
    struct Test<'a, 'b>(Cow<'a, str>, Cow<'b, str>);
135
1

            
136
1
    assert_eq!(
137
1
        &[97, 0, 98, 0, 1, 1],
138
1
        Test("a".into(), "b".into())
139
1
            .as_ord_bytes()
140
1
            .unwrap()
141
1
            .as_ref()
142
1
    )
143
1
}