1 module tests.decode;
2 
3 import unit_threaded;
4 import cerealed.decerealiser;
5 import core.exception;
6 
7 void testDecodeBool() {
8     auto cereal = Decerealiser([1, 0, 1, 0, 0, 1]);
9     bool val;
10     cereal.grain(val); shouldEqual(val, true);
11     cereal.grain(val); shouldEqual(val, false);
12     cereal.grain(val); shouldEqual(val, true);
13     cereal.grain(val); shouldEqual(val, false);
14     cereal.grain(val); shouldEqual(val, false);
15     cereal.grain(val); shouldEqual(val, true);
16     cereal.value!bool.shouldThrow!RangeError; //no more bytes
17 }
18 
19 
20 void testDecodeByte() {
21     auto cereal = Decerealiser([0x0, 0x2, 0xfc]);
22     cereal.value!byte.shouldEqual(0);
23     cereal.value!byte.shouldEqual(2);
24     cereal.value!byte.shouldEqual(-4);
25     cereal.value!ubyte.shouldThrow!RangeError; //no more bytes
26 }
27 
28 void testDecodeRefByte() {
29     auto cereal = Decerealiser([0xfc]);
30     byte val;
31     cereal.grain(val);
32     val.shouldEqual(-4);
33 }
34 
35 void testDecodeUByte() {
36     auto cereal = Decerealiser([0x0, 0x2, 0xfc]);
37     cereal.value!ubyte.shouldEqual(0);
38     cereal.value!ubyte.shouldEqual(2);
39     cereal.value!ubyte.shouldEqual(252);
40     cereal.value!ubyte.shouldThrow!RangeError; //no more bytes
41 }
42 
43 void testDecodeShort() {
44     auto cereal = Decerealiser([0xff, 0xfe, 0x0, 0x3]);
45     cereal.value!short.shouldEqual(-2);
46     cereal.value!short.shouldEqual(3);
47     shouldThrow!RangeError(cereal.value!short); //no more bytes
48 }
49 
50 void testDecodeRefShort() {
51     auto cereal = Decerealiser([0xff, 0xfe]);
52     short val;
53     cereal.grain(val);
54     val.shouldEqual(-2);
55 }
56 
57 void testDecodeInt() {
58     auto cereal = Decerealiser([ 0xff, 0xf0, 0xbd, 0xc0]);
59     cereal.value!int.shouldEqual(-1_000_000);
60     shouldThrow!RangeError(cereal.value!int); //no more bytes
61 }
62 
63 void testDecodeRefInt() {
64     auto cereal = Decerealiser([0xff, 0xf0, 0xbd, 0xc0]);
65     int val;
66     cereal.grain(val);
67     val.shouldEqual(-1_000_000);
68 }
69 
70 void testDecodeLong() {
71     auto cereal = Decerealiser([ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2]);
72     cereal.value!long.shouldEqual(1);
73     cereal.value!long.shouldEqual(2);
74     cereal.value!ubyte.shouldThrow!RangeError; //no more bytes
75 }
76 
77 void testDecodeRefLong() {
78     auto cereal = Decerealiser([ 0, 0, 0, 0, 0, 0, 0, 1]);
79     long val;
80     cereal.grain(val);
81     val.shouldEqual(1);
82 }
83 
84 
85 void testDecodeBigULong() {
86     auto dec = Decerealiser([0xd8, 0xbf, 0xc7, 0xcd, 0x2d, 0x9b, 0xa1, 0xb1]);
87     dec.value!ulong.shouldEqual(0xd8bfc7cd2d9ba1b1);
88     shouldThrow!RangeError(dec.value!ubyte); //no more bytes
89 }
90 
91 
92 void testDecodeDouble() {
93     auto cereal = Decerealiser([ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2]);
94     shouldNotThrow(cereal.value!double);
95     shouldNotThrow(cereal.value!double);
96     cereal.value!ubyte.shouldThrow!RangeError; //no more bytes
97 }
98 
99 void testDecodeChars() {
100     auto cereal = Decerealiser([ 0xff, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff ]);
101     cereal.value!char.shouldEqual(0xff);
102     cereal.value!wchar.shouldEqual(0xffff);
103     cereal.value!dchar.shouldEqual(0x0000ffff);
104     cereal.value!ubyte.shouldThrow!RangeError; //no more bytes
105 }
106 
107 void testDecodeRefChar() {
108     auto cereal = Decerealiser([0xff]);
109     char val;
110     cereal.grain(val);
111     val.shouldEqual(0xff);
112 }
113 
114 
115 void testDecodeArray() {
116     auto cereal = Decerealiser([ 0, 3, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 9 ]);
117     cereal.value!(int[]).shouldEqual([ 2, 6, 9 ]);
118     cereal.value!ubyte.shouldThrow!RangeError; //no more bytes
119 }
120 
121 void testDecodeRefArray() {
122     auto cereal = Decerealiser([0, 1, 0, 0, 0, 2]);
123     int[] val;
124     cereal.grain(val);
125     val.shouldEqual([2]);
126 }
127 
128 void testDecodeArrayLongLength() {
129     auto cereal = Decerealiser([ 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 9 ]);
130     cereal.value!(int[], long).shouldEqual([ 2, 6, 9 ]);
131     cereal.value!ubyte.shouldThrow!RangeError; //no more bytes
132 }
133 
134 void testDecodeAssocArray() {
135     auto cereal = Decerealiser([ 0, 2, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 6 ]);
136     cereal.value!(int[int]).shouldEqual([ 1:2, 3:6]);
137     cereal.value!ubyte.shouldThrow!RangeError; //no more bytes
138 }
139 
140 
141 void testDecodeRefAssocArray() {
142     auto cereal = Decerealiser([0, 1, 0, 0, 0, 2, 0, 0, 0, 3]);
143     int[int] val;
144     cereal.grain(val);
145     val.shouldEqual([2:3]);
146 }
147 
148 void testDecodeAssocArrayIntLength() {
149     auto cereal = Decerealiser([ 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 6 ]);
150     cereal.value!(int[int], int).shouldEqual([ 1:2, 3:6]);
151     cereal.value!ubyte.shouldThrow!RangeError; //no more bytes
152 }
153 
154 void testDecodeString() {
155     auto cereal = Decerealiser([0, 5, 'a', 't', 'o', 'y', 'n']);
156     cereal.value!(string).shouldEqual("atoyn");
157     cereal.value!ubyte.shouldThrow!RangeError; //no more bytes
158 }
159 
160 void testDecodeRefString() {
161     auto cereal = Decerealiser([0, 5, 'a', 't', 'o', 'y', 'n']);
162     string val;
163     cereal.grain(val);
164     val.shouldEqual("atoyn");
165     cereal.value!ubyte.shouldThrow!RangeError; //no more bytes
166 }
167 
168 void testDecodeBits() {
169     auto cereal = Decerealiser([ 0x9e, 0xea]);
170     //1001 1110 1110 1010 or
171     //100 111 10111 01 010
172     cereal.readBits(3).shouldEqual(4);
173     cereal.readBits(3).shouldEqual(7);
174     cereal.readBits(5).shouldEqual(23);
175     cereal.readBits(2).shouldEqual(1);
176     cereal.readBits(3).shouldEqual(2);
177 
178     cereal.reset();
179     cereal.readBits(3).shouldEqual(4);
180     cereal.readBits(3).shouldEqual(7);
181     cereal.readBits(5).shouldEqual(23);
182     cereal.readBits(2).shouldEqual(1);
183     cereal.readBits(3).shouldEqual(2);
184 }
185 
186 void testDecodeBitsMultiByte() {
187     auto cereal = Decerealiser([ 0x9e, 0xea]);
188     cereal.readBits(9).shouldEqual(317);
189     cereal.readBits(7).shouldEqual(0x6a);
190 }
191 
192 void testDecodeStringArray() {
193     auto dec = Decerealiser([ 0, 3,
194                               0, 3, 'f', 'o', 'o',
195                               0, 4, 'w', '0', '0', 't',
196                               0, 2, 'l', 'i']);
197     dec.value!(string[]).shouldEqual(["foo", "w00t", "li"]);
198 }