-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstatic_array.d
57 lines (46 loc) · 1.34 KB
/
static_array.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
module tests.static_array;
import unit_threaded;
import cerealed;
void testStaticArray() {
int[2] original, restored;
original[0] = 34;
original[1] = 45;
auto enc = Cerealiser();
enc ~= original;
assert(enc.bytes == [
// no length because it's already known
0, 0, 0, 34,
0, 0, 0, 45,
]);
auto dec = Decerealiser(enc.bytes);
restored = dec.value!(int[2]);
assert(original == restored);
}
void testArrayOfArrays() {
static struct Unit {
ubyte thingie;
ushort length;
@NoCereal ubyte[] data;
void postBlit(C)(auto ref C cereal) if(isCereal!C) {
static if(isDecerealiser!C) {
writelnUt("Decerealiser bytesLeft ", cereal.bytesLeft);
writelnUt("length: ", length);
}
cereal.grainLengthedArray(data, length);
}
}
static struct Packet {
ubyte vrsion;
@RawArray Unit[] units;
}
ubyte[] bytes = [7, //vrsion
1, 0, 3, 0xa, 0xb, 0xc, //1st unit
2, 0, 5, 1, 2, 3, 4, 5 //2nd unit
];
auto dec = Decerealiser(bytes);
auto pkt = dec.value!Packet;
pkt.shouldEqual(Packet(7, [Unit(1, 3, [0xa, 0xb, 0xc]), Unit(2, 5, [1, 2, 3, 4, 5])]));
auto enc = Cerealiser();
enc ~= pkt;
enc.bytes.shouldEqual(bytes);
}