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