summaryrefslogtreecommitdiff
path: root/bzipper/src/deserialise/test.rs
blob: f5939780c5ffab14efe684165b1eaeab097c3a3d (plain) (blame)
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
// Copyright 2024 Gabriel Bjørnager Jensen.
//
// This file is part of bzipper.
//
// bzipper is free software: you can redistribute
// it and/or modify it under the terms of the GNU
// Lesser General Public License as published by
// the Free Software Foundation, either version 3
// of the License, or (at your option) any later
// version.
//
// bzipper is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without
// even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Less-
// er General Public License along with bzipper. If
// not, see <https://www.gnu.org/licenses/>.

use crate::{Deserialise, Serialise};

#[test]
fn test() {
	#[derive(Debug, Deserialise, PartialEq, Serialise)]
	struct ProcExit {
		exit_code: i32,
		timestmap: u64,
	}

	#[derive(Debug, Deserialise, PartialEq, Serialise)]
	struct NewByte(u8);

	#[derive(Debug, Deserialise, PartialEq, Serialise)]
	struct Unit;

	#[derive(Debug, Deserialise, PartialEq, Serialise)]
	enum UnitOrFields {
		Unit(Unit),
		Unnamed(i32),
		Named { timestamp: u64 },
	}

	macro_rules! test {
		($ty:ty: $data:expr => $value:expr) => {{
			use ::bzipper::{Deserialise, Serialise};

			let buf: [u8; <$ty as Serialise>::SERIALISED_SIZE] = $data;

			let left  = <$ty as Deserialise>::deserialise(&buf).unwrap();
			let right = $value;

			assert_eq!(left, right);
		}};
	}

	test!(i8: [0x00] =>  0x00);
	test!(i8: [0x7F] =>  0x7F);
	test!(i8: [0x80] => -0x80);
	test!(i8: [0xFF] => -0x01);

	test!(i16: [0x00, 0x00] =>  0x0000);
	test!(i16: [0x7F, 0xFF] =>  0x7FFF);
	test!(i16: [0x80, 0x00] => -0x8000);
	test!(i16: [0xFF, 0xFF] => -0x0001);

	test!(i32: [0x00, 0x00, 0x00, 0x00] =>  0x00000000);
	test!(i32: [0x7F, 0xFF, 0xFF, 0xFF] =>  0x7FFFFFFF);
	test!(i32: [0x80, 0x00, 0x00, 0x00] => -0x80000000);
	test!(i32: [0xFF, 0xFF, 0xFF, 0xFF] => -0x00000001);

	test!(i64: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] =>  0x0000000000000000);
	test!(i64: [0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF] =>  0x7FFFFFFFFFFFFFFF);
	test!(i64: [0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] => -0x8000000000000000);
	test!(i64: [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF] => -0x0000000000000001);

	test!(u128: [
		0xFF, 0x0F, 0xEF, 0x1F, 0xDF, 0x2F, 0xCF, 0x3F,
		0xBF, 0x4F, 0xAF, 0x5F, 0x9F, 0x6F, 0x8F, 0x7F,
	] => 0xFF_0F_EF_1F_DF_2F_CF_3F_BF_4F_AF_5F_9F_6F_8F_7F);

	test!([char; 0x5]: [
		0x00, 0x00, 0x03, 0xBB, 0x00, 0x00, 0x03, 0x91,
		0x00, 0x00, 0x03, 0xBC, 0x00, 0x00, 0x03, 0x94,
		0x00, 0x00, 0x03, 0xB1
	] => ['\u{03BB}', '\u{0391}', '\u{03BC}', '\u{0394}', '\u{03B1}']);

	test!(Option<()>: [0x00] => None);
	test!(Option<()>: [0x01] => Some(()));

	test!(Result<(), i8>: [0x00, 0x00] => Ok(()));
	test!(Result<(), i8>: [0x01, 0x7F] => Err(i8::MAX));

	test!(ProcExit: [
		0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
		0x5E, 0x0B, 0xE1, 0x00,
	] => ProcExit { exit_code: 0x1, timestmap: 1577836800 });

	test!(NewByte: [0x80] => NewByte(0x80));

	test!(Unit: [] => Unit);

	test!(UnitOrFields: [
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00,
	] => UnitOrFields::Unit(Unit));

	test!(UnitOrFields: [
		0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF,
		0x00, 0x00, 0x00, 0x00,
	] => UnitOrFields::Unnamed(-0x1));

	test!(UnitOrFields: [
		0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
		0x66, 0xC5, 0xC8, 0x4C,
	] => UnitOrFields::Named { timestamp: 1724237900 });
}