Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/io/test_marshalls.h
10278 views
1
/**************************************************************************/
2
/* test_marshalls.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/io/marshalls.h"
34
35
#include "tests/test_macros.h"
36
37
namespace TestMarshalls {
38
39
TEST_CASE("[Marshalls] Unsigned 16 bit integer encoding") {
40
uint8_t arr[2];
41
42
unsigned int actual_size = encode_uint16(0x1234, arr);
43
CHECK(actual_size == sizeof(uint16_t));
44
CHECK_MESSAGE(arr[0] == 0x34, "First encoded byte value should be equal to low order byte value.");
45
CHECK_MESSAGE(arr[1] == 0x12, "Last encoded byte value should be equal to high order byte value.");
46
}
47
48
TEST_CASE("[Marshalls] Unsigned 32 bit integer encoding") {
49
uint8_t arr[4];
50
51
unsigned int actual_size = encode_uint32(0x12345678, arr);
52
CHECK(actual_size == sizeof(uint32_t));
53
CHECK_MESSAGE(arr[0] == 0x78, "First encoded byte value should be equal to low order byte value.");
54
CHECK(arr[1] == 0x56);
55
CHECK(arr[2] == 0x34);
56
CHECK_MESSAGE(arr[3] == 0x12, "Last encoded byte value should be equal to high order byte value.");
57
}
58
59
TEST_CASE("[Marshalls] Unsigned 64 bit integer encoding") {
60
uint8_t arr[8];
61
62
unsigned int actual_size = encode_uint64(0x0f123456789abcdef, arr);
63
CHECK(actual_size == sizeof(uint64_t));
64
CHECK_MESSAGE(arr[0] == 0xef, "First encoded byte value should be equal to low order byte value.");
65
CHECK(arr[1] == 0xcd);
66
CHECK(arr[2] == 0xab);
67
CHECK(arr[3] == 0x89);
68
CHECK(arr[4] == 0x67);
69
CHECK(arr[5] == 0x45);
70
CHECK(arr[6] == 0x23);
71
CHECK_MESSAGE(arr[7] == 0xf1, "Last encoded byte value should be equal to high order byte value.");
72
}
73
74
TEST_CASE("[Marshalls] Unsigned 16 bit integer decoding") {
75
uint8_t arr[] = { 0x34, 0x12 };
76
77
CHECK(decode_uint16(arr) == 0x1234);
78
}
79
80
TEST_CASE("[Marshalls] Unsigned 32 bit integer decoding") {
81
uint8_t arr[] = { 0x78, 0x56, 0x34, 0x12 };
82
83
CHECK(decode_uint32(arr) == 0x12345678);
84
}
85
86
TEST_CASE("[Marshalls] Unsigned 64 bit integer decoding") {
87
uint8_t arr[] = { 0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0xf1 };
88
89
CHECK(decode_uint64(arr) == 0x0f123456789abcdef);
90
}
91
92
TEST_CASE("[Marshalls] Floating point half precision encoding") {
93
uint8_t arr[2];
94
95
// Decimal: 0.33325195
96
// IEEE 754 half-precision binary floating-point format:
97
// sign exponent (5 bits) fraction (10 bits)
98
// 0 01101 0101010101
99
// Hexadecimal: 0x3555
100
unsigned int actual_size = encode_half(0.33325195f, arr);
101
CHECK(actual_size == sizeof(uint16_t));
102
CHECK(arr[0] == 0x55);
103
CHECK(arr[1] == 0x35);
104
}
105
106
TEST_CASE("[Marshalls] Floating point single precision encoding") {
107
uint8_t arr[4];
108
109
// Decimal: 0.15625
110
// IEEE 754 single-precision binary floating-point format:
111
// sign exponent (8 bits) fraction (23 bits)
112
// 0 01111100 01000000000000000000000
113
// Hexadecimal: 0x3E200000
114
unsigned int actual_size = encode_float(0.15625f, arr);
115
CHECK(actual_size == sizeof(uint32_t));
116
CHECK(arr[0] == 0x00);
117
CHECK(arr[1] == 0x00);
118
CHECK(arr[2] == 0x20);
119
CHECK(arr[3] == 0x3e);
120
}
121
122
TEST_CASE("[Marshalls] Floating point double precision encoding") {
123
uint8_t arr[8];
124
125
// Decimal: 0.333333333333333314829616256247390992939472198486328125
126
// IEEE 754 double-precision binary floating-point format:
127
// sign exponent (11 bits) fraction (52 bits)
128
// 0 01111111101 0101010101010101010101010101010101010101010101010101
129
// Hexadecimal: 0x3FD5555555555555
130
unsigned int actual_size = encode_double(0.33333333333333333, arr);
131
CHECK(actual_size == sizeof(uint64_t));
132
CHECK(arr[0] == 0x55);
133
CHECK(arr[1] == 0x55);
134
CHECK(arr[2] == 0x55);
135
CHECK(arr[3] == 0x55);
136
CHECK(arr[4] == 0x55);
137
CHECK(arr[5] == 0x55);
138
CHECK(arr[6] == 0xd5);
139
CHECK(arr[7] == 0x3f);
140
}
141
142
TEST_CASE("[Marshalls] Floating point half precision decoding") {
143
uint8_t arr[] = { 0x55, 0x35 };
144
145
// See floating point half precision encoding test case for details behind expected values.
146
CHECK(decode_half(arr) == 0.33325195f);
147
}
148
149
TEST_CASE("[Marshalls] Floating point single precision decoding") {
150
uint8_t arr[] = { 0x00, 0x00, 0x20, 0x3e };
151
152
// See floating point encoding test case for details behind expected values
153
CHECK(decode_float(arr) == 0.15625f);
154
}
155
156
TEST_CASE("[Marshalls] Floating point double precision decoding") {
157
uint8_t arr[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xd5, 0x3f };
158
159
// See floating point encoding test case for details behind expected values
160
CHECK(decode_double(arr) == 0.33333333333333333);
161
}
162
163
TEST_CASE("[Marshalls] C string encoding") {
164
char cstring[] = "Godot"; // 5 characters
165
uint8_t data[6];
166
167
int actual_size = encode_cstring(cstring, data);
168
CHECK(actual_size == 6);
169
CHECK(data[0] == 'G');
170
CHECK(data[1] == 'o');
171
CHECK(data[2] == 'd');
172
CHECK(data[3] == 'o');
173
CHECK(data[4] == 't');
174
CHECK(data[5] == '\0');
175
}
176
177
TEST_CASE("[Marshalls] NIL Variant encoding") {
178
int r_len;
179
Variant variant;
180
uint8_t buffer[4];
181
182
CHECK(encode_variant(variant, buffer, r_len) == OK);
183
CHECK_MESSAGE(r_len == 4, "Length == 4 bytes for header.");
184
CHECK_MESSAGE(buffer[0] == 0x00, "Variant::NIL");
185
CHECK(buffer[1] == 0x00);
186
CHECK(buffer[2] == 0x00);
187
CHECK(buffer[3] == 0x00);
188
// No value
189
}
190
191
TEST_CASE("[Marshalls] INT 32 bit Variant encoding") {
192
int r_len;
193
Variant variant(0x12345678);
194
uint8_t buffer[8];
195
196
CHECK(encode_variant(variant, buffer, r_len) == OK);
197
CHECK_MESSAGE(r_len == 8, "Length == 4 bytes for header + 4 bytes for `int32_t`.");
198
CHECK_MESSAGE(buffer[0] == 0x02, "Variant::INT");
199
CHECK(buffer[1] == 0x00);
200
CHECK(buffer[2] == 0x00);
201
CHECK(buffer[3] == 0x00);
202
// Check value
203
CHECK(buffer[4] == 0x78);
204
CHECK(buffer[5] == 0x56);
205
CHECK(buffer[6] == 0x34);
206
CHECK(buffer[7] == 0x12);
207
}
208
209
TEST_CASE("[Marshalls] INT 64 bit Variant encoding") {
210
int r_len;
211
Variant variant(uint64_t(0x0f123456789abcdef));
212
uint8_t buffer[12];
213
214
CHECK(encode_variant(variant, buffer, r_len) == OK);
215
CHECK_MESSAGE(r_len == 12, "Length == 4 bytes for header + 8 bytes for `int64_t`.");
216
CHECK_MESSAGE(buffer[0] == 0x02, "Variant::INT");
217
CHECK(buffer[1] == 0x00);
218
CHECK_MESSAGE(buffer[2] == 0x01, "HEADER_DATA_FLAG_64");
219
CHECK(buffer[3] == 0x00);
220
// Check value
221
CHECK(buffer[4] == 0xef);
222
CHECK(buffer[5] == 0xcd);
223
CHECK(buffer[6] == 0xab);
224
CHECK(buffer[7] == 0x89);
225
CHECK(buffer[8] == 0x67);
226
CHECK(buffer[9] == 0x45);
227
CHECK(buffer[10] == 0x23);
228
CHECK(buffer[11] == 0xf1);
229
}
230
231
TEST_CASE("[Marshalls] FLOAT single precision Variant encoding") {
232
int r_len;
233
Variant variant(0.15625f);
234
uint8_t buffer[8];
235
236
CHECK(encode_variant(variant, buffer, r_len) == OK);
237
CHECK_MESSAGE(r_len == 8, "Length == 4 bytes for header + 4 bytes for `float`.");
238
CHECK_MESSAGE(buffer[0] == 0x03, "Variant::FLOAT");
239
CHECK(buffer[1] == 0x00);
240
CHECK(buffer[2] == 0x00);
241
CHECK(buffer[3] == 0x00);
242
// Check value
243
CHECK(buffer[4] == 0x00);
244
CHECK(buffer[5] == 0x00);
245
CHECK(buffer[6] == 0x20);
246
CHECK(buffer[7] == 0x3e);
247
}
248
249
TEST_CASE("[Marshalls] FLOAT double precision Variant encoding") {
250
int r_len;
251
Variant variant(0.33333333333333333);
252
uint8_t buffer[12];
253
254
CHECK(encode_variant(variant, buffer, r_len) == OK);
255
CHECK_MESSAGE(r_len == 12, "Length == 4 bytes for header + 8 bytes for `double`.");
256
CHECK_MESSAGE(buffer[0] == 0x03, "Variant::FLOAT");
257
CHECK(buffer[1] == 0x00);
258
CHECK_MESSAGE(buffer[2] == 0x01, "HEADER_DATA_FLAG_64");
259
CHECK(buffer[3] == 0x00);
260
// Check value
261
CHECK(buffer[4] == 0x55);
262
CHECK(buffer[5] == 0x55);
263
CHECK(buffer[6] == 0x55);
264
CHECK(buffer[7] == 0x55);
265
CHECK(buffer[8] == 0x55);
266
CHECK(buffer[9] == 0x55);
267
CHECK(buffer[10] == 0xd5);
268
CHECK(buffer[11] == 0x3f);
269
}
270
271
TEST_CASE("[Marshalls] Invalid data Variant decoding") {
272
Variant variant;
273
int r_len = 0;
274
uint8_t some_buffer[1] = { 0x00 };
275
uint8_t out_of_range_type_buffer[4] = { 0xff }; // Greater than Variant::VARIANT_MAX
276
277
ERR_PRINT_OFF;
278
CHECK(decode_variant(variant, some_buffer, /* less than 4 */ 1, &r_len) == ERR_INVALID_DATA);
279
CHECK(r_len == 0);
280
281
CHECK(decode_variant(variant, out_of_range_type_buffer, 4, &r_len) == ERR_INVALID_DATA);
282
CHECK(r_len == 0);
283
ERR_PRINT_ON;
284
}
285
286
TEST_CASE("[Marshalls] NIL Variant decoding") {
287
Variant variant;
288
int r_len;
289
uint8_t buffer[] = {
290
0x00, 0x00, 0x00, 0x00 // Variant::NIL
291
};
292
293
CHECK(decode_variant(variant, buffer, 4, &r_len) == OK);
294
CHECK(r_len == 4);
295
CHECK(variant == Variant());
296
}
297
298
TEST_CASE("[Marshalls] INT 32 bit Variant decoding") {
299
Variant variant;
300
int r_len;
301
uint8_t buffer[] = {
302
0x02, 0x00, 0x00, 0x00, // Variant::INT
303
0x78, 0x56, 0x34, 0x12 // value
304
};
305
306
CHECK(decode_variant(variant, buffer, 8, &r_len) == OK);
307
CHECK(r_len == 8);
308
CHECK(variant == Variant(0x12345678));
309
}
310
311
TEST_CASE("[Marshalls] INT 64 bit Variant decoding") {
312
Variant variant;
313
int r_len;
314
uint8_t buffer[] = {
315
0x02, 0x00, 0x01, 0x00, // Variant::INT, HEADER_DATA_FLAG_64
316
0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0xf1 // value
317
};
318
319
CHECK(decode_variant(variant, buffer, 12, &r_len) == OK);
320
CHECK(r_len == 12);
321
CHECK(variant == Variant(uint64_t(0x0f123456789abcdef)));
322
}
323
324
TEST_CASE("[Marshalls] FLOAT single precision Variant decoding") {
325
Variant variant;
326
int r_len;
327
uint8_t buffer[] = {
328
0x03, 0x00, 0x00, 0x00, // Variant::FLOAT
329
0x00, 0x00, 0x20, 0x3e // value
330
};
331
332
CHECK(decode_variant(variant, buffer, 8, &r_len) == OK);
333
CHECK(r_len == 8);
334
CHECK(variant == Variant(0.15625f));
335
}
336
337
TEST_CASE("[Marshalls] FLOAT double precision Variant decoding") {
338
Variant variant;
339
int r_len;
340
uint8_t buffer[] = {
341
0x03, 0x00, 0x01, 0x00, // Variant::FLOAT, HEADER_DATA_FLAG_64
342
0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xd5, 0x3f // value
343
};
344
345
CHECK(decode_variant(variant, buffer, 12, &r_len) == OK);
346
CHECK(r_len == 12);
347
CHECK(variant == Variant(0.33333333333333333));
348
}
349
350
TEST_CASE("[Marshalls] Typed array encoding") {
351
int r_len;
352
Array array;
353
array.set_typed(Variant::INT, StringName(), Ref<Script>());
354
array.push_back(Variant(uint64_t(0x0f123456789abcdef)));
355
uint8_t buffer[24];
356
357
CHECK(encode_variant(array, buffer, r_len) == OK);
358
CHECK_MESSAGE(r_len == 24, "Length == 4 bytes for header + 4 bytes for array type + 4 bytes for array size + 12 bytes for element.");
359
CHECK_MESSAGE(buffer[0] == 0x1c, "Variant::ARRAY");
360
CHECK(buffer[1] == 0x00);
361
CHECK_MESSAGE(buffer[2] == 0x01, "CONTAINER_TYPE_KIND_BUILTIN");
362
CHECK(buffer[3] == 0x00);
363
// Check array type.
364
CHECK_MESSAGE(buffer[4] == 0x02, "Variant::INT");
365
CHECK(buffer[5] == 0x00);
366
CHECK(buffer[6] == 0x00);
367
CHECK(buffer[7] == 0x00);
368
// Check array size.
369
CHECK(buffer[8] == 0x01);
370
CHECK(buffer[9] == 0x00);
371
CHECK(buffer[10] == 0x00);
372
CHECK(buffer[11] == 0x00);
373
// Check element type.
374
CHECK_MESSAGE(buffer[12] == 0x02, "Variant::INT");
375
CHECK(buffer[13] == 0x00);
376
CHECK_MESSAGE(buffer[14] == 0x01, "HEADER_DATA_FLAG_64");
377
CHECK(buffer[15] == 0x00);
378
// Check element value.
379
CHECK(buffer[16] == 0xef);
380
CHECK(buffer[17] == 0xcd);
381
CHECK(buffer[18] == 0xab);
382
CHECK(buffer[19] == 0x89);
383
CHECK(buffer[20] == 0x67);
384
CHECK(buffer[21] == 0x45);
385
CHECK(buffer[22] == 0x23);
386
CHECK(buffer[23] == 0xf1);
387
}
388
389
TEST_CASE("[Marshalls] Typed array decoding") {
390
Variant variant;
391
int r_len;
392
uint8_t buffer[] = {
393
0x1c, 0x00, 0x01, 0x00, // Variant::ARRAY, CONTAINER_TYPE_KIND_BUILTIN
394
0x02, 0x00, 0x00, 0x00, // Array type (Variant::INT).
395
0x01, 0x00, 0x00, 0x00, // Array size.
396
0x02, 0x00, 0x01, 0x00, // Element type (Variant::INT, HEADER_DATA_FLAG_64).
397
0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0xf1, // Element value.
398
};
399
400
CHECK(decode_variant(variant, buffer, 24, &r_len) == OK);
401
CHECK(r_len == 24);
402
CHECK(variant.get_type() == Variant::ARRAY);
403
Array array = variant;
404
CHECK(array.get_typed_builtin() == Variant::INT);
405
CHECK(array.size() == 1);
406
CHECK(array[0] == Variant(uint64_t(0x0f123456789abcdef)));
407
}
408
409
TEST_CASE("[Marshalls] Typed dicttionary encoding") {
410
int r_len;
411
Dictionary dictionary;
412
dictionary.set_typed(Variant::INT, StringName(), Ref<Script>(), Variant::INT, StringName(), Ref<Script>());
413
dictionary[Variant(uint64_t(0x0f123456789abcdef))] = Variant(uint64_t(0x0f123456789abcdef));
414
uint8_t buffer[40];
415
416
CHECK(encode_variant(dictionary, buffer, r_len) == OK);
417
CHECK_MESSAGE(r_len == 40, "Length == 4 bytes for header + 8 bytes for dictionary type + 4 bytes for dictionary size + 24 bytes for key-value pair.");
418
CHECK_MESSAGE(buffer[0] == 0x1b, "Variant::DICTIONARY");
419
CHECK(buffer[1] == 0x00);
420
CHECK_MESSAGE(buffer[2] == 0x05, "key: CONTAINER_TYPE_KIND_BUILTIN | value: CONTAINER_TYPE_KIND_BUILTIN");
421
CHECK(buffer[3] == 0x00);
422
// Check dictionary key type.
423
CHECK_MESSAGE(buffer[4] == 0x02, "Variant::INT");
424
CHECK(buffer[5] == 0x00);
425
CHECK(buffer[6] == 0x00);
426
CHECK(buffer[7] == 0x00);
427
// Check dictionary value type.
428
CHECK_MESSAGE(buffer[8] == 0x02, "Variant::INT");
429
CHECK(buffer[9] == 0x00);
430
CHECK(buffer[10] == 0x00);
431
CHECK(buffer[11] == 0x00);
432
// Check dictionary size.
433
CHECK(buffer[12] == 0x01);
434
CHECK(buffer[13] == 0x00);
435
CHECK(buffer[14] == 0x00);
436
CHECK(buffer[15] == 0x00);
437
// Check key type.
438
CHECK_MESSAGE(buffer[16] == 0x02, "Variant::INT");
439
CHECK(buffer[17] == 0x00);
440
CHECK_MESSAGE(buffer[18] == 0x01, "HEADER_DATA_FLAG_64");
441
CHECK(buffer[19] == 0x00);
442
// Check key value.
443
CHECK(buffer[20] == 0xef);
444
CHECK(buffer[21] == 0xcd);
445
CHECK(buffer[22] == 0xab);
446
CHECK(buffer[23] == 0x89);
447
CHECK(buffer[24] == 0x67);
448
CHECK(buffer[25] == 0x45);
449
CHECK(buffer[26] == 0x23);
450
CHECK(buffer[27] == 0xf1);
451
// Check value type.
452
CHECK_MESSAGE(buffer[28] == 0x02, "Variant::INT");
453
CHECK(buffer[29] == 0x00);
454
CHECK_MESSAGE(buffer[30] == 0x01, "HEADER_DATA_FLAG_64");
455
CHECK(buffer[31] == 0x00);
456
// Check value value.
457
CHECK(buffer[32] == 0xef);
458
CHECK(buffer[33] == 0xcd);
459
CHECK(buffer[34] == 0xab);
460
CHECK(buffer[35] == 0x89);
461
CHECK(buffer[36] == 0x67);
462
CHECK(buffer[37] == 0x45);
463
CHECK(buffer[38] == 0x23);
464
CHECK(buffer[39] == 0xf1);
465
}
466
467
TEST_CASE("[Marshalls] Typed dictionary decoding") {
468
Variant variant;
469
int r_len;
470
uint8_t buffer[] = {
471
0x1b, 0x00, 0x05, 0x00, // Variant::DICTIONARY, key: CONTAINER_TYPE_KIND_BUILTIN | value: CONTAINER_TYPE_KIND_BUILTIN
472
0x02, 0x00, 0x00, 0x00, // Dictionary key type (Variant::INT).
473
0x02, 0x00, 0x00, 0x00, // Dictionary value type (Variant::INT).
474
0x01, 0x00, 0x00, 0x00, // Dictionary size.
475
0x02, 0x00, 0x01, 0x00, // Key type (Variant::INT, HEADER_DATA_FLAG_64).
476
0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0xf1, // Key value.
477
0x02, 0x00, 0x01, 0x00, // Value type (Variant::INT, HEADER_DATA_FLAG_64).
478
0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0xf1, // Value value.
479
};
480
481
CHECK(decode_variant(variant, buffer, 40, &r_len) == OK);
482
CHECK(r_len == 40);
483
CHECK(variant.get_type() == Variant::DICTIONARY);
484
Dictionary dictionary = variant;
485
CHECK(dictionary.get_typed_key_builtin() == Variant::INT);
486
CHECK(dictionary.get_typed_value_builtin() == Variant::INT);
487
CHECK(dictionary.size() == 1);
488
CHECK(dictionary.has(Variant(uint64_t(0x0f123456789abcdef))));
489
CHECK(dictionary[Variant(uint64_t(0x0f123456789abcdef))] == Variant(uint64_t(0x0f123456789abcdef)));
490
}
491
492
} // namespace TestMarshalls
493
494