1 module tests.static_array;
2 
3 import unit_threaded;
4 import cerealed;
5 
6 void testStaticArray() {
7     int[2] original, restored;
8 
9     original[0] = 34;
10     original[1] = 45;
11     auto enc = Cerealiser();
12     enc ~= original;
13     assert(enc.bytes == [
14     // no length because it's already known
15         0, 0, 0, 34,
16         0, 0, 0, 45,
17     ]);
18     auto dec = Decerealiser(enc.bytes);
19     restored = dec.value!(int[2]);
20 
21     assert(original == restored);
22 }
23 
24 
25 void testArrayOfArrays() {
26     static struct Unit {
27         ubyte thingie;
28         ushort length;
29         @NoCereal ubyte[] data;
30 
31         void postBlit(C)(auto ref C cereal) if(isCereal!C) {
32             static if(isDecerealiser!C) {
33                 writelnUt("Decerealiser bytesLeft ", cereal.bytesLeft);
34                 writelnUt("length: ", length);
35             }
36             cereal.grainLengthedArray(data, length);
37         }
38     }
39 
40     static struct Packet {
41         ubyte vrsion;
42         @RawArray Unit[] units;
43     }
44 
45     ubyte[] bytes = [7, //vrsion
46                      1, 0, 3, 0xa, 0xb, 0xc, //1st unit
47                      2, 0, 5, 1, 2, 3, 4, 5 //2nd unit
48         ];
49 
50     auto dec = Decerealiser(bytes);
51     auto pkt = dec.value!Packet;
52     pkt.shouldEqual(Packet(7, [Unit(1, 3, [0xa, 0xb, 0xc]), Unit(2, 5, [1, 2, 3, 4, 5])]));
53 
54     auto enc = Cerealiser();
55     enc ~= pkt;
56     enc.bytes.shouldEqual(bytes);
57 }