-
Notifications
You must be signed in to change notification settings - Fork 3
/
encode.d
177 lines (153 loc) · 4.63 KB
/
encode.d
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
module tests.encode;
import unit_threaded.check;
import cerealed.cerealiser;
void testEncodeBool() {
auto cereal = new Cerealiser();
cereal.write(false);
cereal.write(true);
cereal.write(false);
checkEqual(cereal.bytes, [ 0x0, 0x1, 0x0 ]);
cereal ~= true;
checkEqual(cereal.bytes, [ 0x0, 0x1, 0x0, 0x1 ]);
}
void testEncodeByte() {
auto cereal = new Cerealiser();
byte[] ins = [ 1, 3, -2, 5, -4];
foreach(i; ins) cereal.write(i);
checkEqual(cereal.bytes, [ 0x1, 0x3, 0xfe, 0x5, 0xfc ]);
}
void testEncodeUByte() {
auto cereal = new Cerealiser();
ubyte[] ins = [ 2, 3, 12, 10];
foreach(i; ins) cereal ~= i;
checkEqual(cereal.bytes, [ 0x2, 0x3, 0xc, 0xa ]);
}
void testEncodeShort() {
auto cereal = new Cerealiser();
short[] ins = [ -2, 3, -32767, 0];
foreach(i; ins) cereal ~= i;
checkEqual(cereal.bytes, [ 0xff, 0xfe, 0x0, 0x3, 0x80, 0x01, 0x0, 0x0 ]);
}
void testEncodeUShort() {
auto cereal = new Cerealiser();
ushort[] ins = [ 2, 3, cast(short)65535, 0];
foreach(i; ins) cereal ~= i;
checkEqual(cereal.bytes, [ 0x0, 0x2, 0x0, 0x3, 0xff, 0xff, 0x0, 0x0 ]);
}
void testEncodeInt() {
auto cereal = new Cerealiser();
int[] ins = [ 3, -1_000_000];
foreach(i; ins) cereal ~= i;
checkEqual(cereal.bytes, [ 0x0, 0x0, 0x0, 0x3, 0xff, 0xf0, 0xbd, 0xc0 ]);
}
void testEncodeUInt() {
auto cereal = new Cerealiser();
uint[] ins = [ 1_000_000, 0];
foreach(i; ins) cereal ~= i;
checkEqual(cereal.bytes, [ 0x0, 0x0f, 0x42, 0x40, 0x0, 0x0, 0x0, 0x0 ]);
}
void testEncodeLong() {
auto cereal = new Cerealiser();
long[] ins = [1, 2];
foreach(i; ins) cereal ~= i;
checkEqual(cereal.bytes, [ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2]);
}
void testEncodeULong() {
auto cereal = new Cerealiser();
cereal ~= 45L;
checkEqual(cereal.bytes, [ 0, 0, 0, 0, 0, 0, 0, 45 ]);
cereal = new Cerealiser();
cereal ~= 42L;
checkEqual(cereal.bytes, [ 0, 0, 0, 0, 0, 0, 0, 42 ]);
}
void testEncodeFloat() {
auto cereal = new Cerealiser();
cereal ~= 1.0f;
}
void testEncodeDouble() {
auto cereal = new Cerealiser();
cereal ~= 1.0;
}
void testEncodeChars() {
auto cereal = new Cerealiser();
char c; cereal ~= c;
wchar w; cereal ~= w;
dchar d; cereal ~= d;
checkEqual(cereal.bytes, [ 0xff, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff]);
}
void testEncodeArray() {
auto cereal = new Cerealiser();
const ints = [ 2, 6, 9];
cereal ~= ints;
//encoding should be a short with the length, plus payload
checkEqual(cereal.bytes, [ 0, 3, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 9]);
}
void testEncodeAssocArray() {
auto cereal = new Cerealiser();
const intToInt = [ 1:2, 3:6];
cereal ~= intToInt;
//short with length, then payload
checkEqual(cereal.bytes, [ 0, 2, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 6]);
}
void testEncodeString() {
auto cereal = new Cerealiser();
const str = "foobarbaz";
cereal ~= str;
//short with length, then payload
checkEqual(cereal.bytes, [ 0, 9, 'f', 'o', 'o', 'b', 'a', 'r', 'b', 'a', 'z' ]);
}
void testEncodeNibble() {
auto cereal = new Cerealiser();
cereal.writeBits(0x4, 4);
cereal.writeBits(0xf, 4);
checkEqual(cereal.bytes, [ 0x4f ]);
}
void testEncodeSubByte() {
auto cereal = new Cerealiser();
cereal.writeBits(1, 1);
cereal.writeBits(3, 2);
cereal.writeBits(0, 1);
cereal.writeBits(5, 3);
cereal.writeBits(1, 1);
checkEqual(cereal.bytes, [ 0xeb]);
}
void testEncodeSubWord() {
auto cereal = new Cerealiser();
cereal.writeBits(4, 3);
cereal.writeBits(7, 3);
cereal.writeBits(23, 5);
cereal.writeBits(1, 2);
cereal.writeBits(2, 3);
checkEqual(cereal.bytes, [ 0x9e, 0xea]);
}
void testEncodeMoreThan8Bits() {
{
auto cereal = new Cerealiser();
cereal.writeBits(1, 9);
cereal.writeBits(15, 7);
checkEqual(cereal.bytes, [ 0x00, 0x8f]);
}
{
auto cereal = new Cerealiser();
cereal.writeBits((0x9e << 1) | 1, 9);
cereal.writeBits(0xea & 0x7f, 7);
checkEqual(cereal.bytes, [ 0x9e, 0xea]);
}
}
void testEncodeFailsIfTooBigForBits() {
auto cereal = new Cerealiser();
checkNotThrown(cereal.writeBits(1, 1));
checkThrown(cereal.writeBits(2, 1));
checkNotThrown(cereal.writeBits(3, 2));
checkThrown(cereal.writeBits(5, 2));
}
void testEncodeTwoBytesBits() {
auto cereal = new Cerealiser();
immutable uint value = 5;
cereal.writeBits(3, 4);
cereal.writeBits(1, 1);
cereal.writeBits(2, 2);
cereal.writeBits(0, 1);
cereal.writeBits(5, 8);
checkEqual(cereal.bytes, [0x3c, 0x05]);
}