test_marshalls.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. #ifndef TEST_MARSHALLS_H
  31. #define TEST_MARSHALLS_H
  32. #include "core/io/marshalls.h"
  33. #include "tests/test_macros.h"
  34. namespace TestMarshalls {
  35. TEST_CASE("[Marshalls] Unsigned 16 bit integer encoding") {
  36. uint8_t arr[2];
  37. unsigned int actual_size = encode_uint16(0x1234, arr);
  38. CHECK(actual_size == sizeof(uint16_t));
  39. CHECK_MESSAGE(arr[0] == 0x34, "First encoded byte value should be equal to low order byte value.");
  40. CHECK_MESSAGE(arr[1] == 0x12, "Last encoded byte value should be equal to high order byte value.");
  41. }
  42. TEST_CASE("[Marshalls] Unsigned 32 bit integer encoding") {
  43. uint8_t arr[4];
  44. unsigned int actual_size = encode_uint32(0x12345678, arr);
  45. CHECK(actual_size == sizeof(uint32_t));
  46. CHECK_MESSAGE(arr[0] == 0x78, "First encoded byte value should be equal to low order byte value.");
  47. CHECK(arr[1] == 0x56);
  48. CHECK(arr[2] == 0x34);
  49. CHECK_MESSAGE(arr[3] == 0x12, "Last encoded byte value should be equal to high order byte value.");
  50. }
  51. TEST_CASE("[Marshalls] Unsigned 64 bit integer encoding") {
  52. uint8_t arr[8];
  53. unsigned int actual_size = encode_uint64(0x0f123456789abcdef, arr);
  54. CHECK(actual_size == sizeof(uint64_t));
  55. CHECK_MESSAGE(arr[0] == 0xef, "First encoded byte value should be equal to low order byte value.");
  56. CHECK(arr[1] == 0xcd);
  57. CHECK(arr[2] == 0xab);
  58. CHECK(arr[3] == 0x89);
  59. CHECK(arr[4] == 0x67);
  60. CHECK(arr[5] == 0x45);
  61. CHECK(arr[6] == 0x23);
  62. CHECK_MESSAGE(arr[7] == 0xf1, "Last encoded byte value should be equal to high order byte value.");
  63. }
  64. TEST_CASE("[Marshalls] Unsigned 16 bit integer decoding") {
  65. uint8_t arr[] = { 0x34, 0x12 };
  66. CHECK(decode_uint16(arr) == 0x1234);
  67. }
  68. TEST_CASE("[Marshalls] Unsigned 32 bit integer decoding") {
  69. uint8_t arr[] = { 0x78, 0x56, 0x34, 0x12 };
  70. CHECK(decode_uint32(arr) == 0x12345678);
  71. }
  72. TEST_CASE("[Marshalls] Unsigned 64 bit integer decoding") {
  73. uint8_t arr[] = { 0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0xf1 };
  74. CHECK(decode_uint64(arr) == 0x0f123456789abcdef);
  75. }
  76. TEST_CASE("[Marshalls] Floating point single precision encoding") {
  77. uint8_t arr[4];
  78. // Decimal: 0.15625
  79. // IEEE 754 single-precision binary floating-point format:
  80. // sign exponent (8 bits) fraction (23 bits)
  81. // 0 01111100 01000000000000000000000
  82. // Hexadecimal: 0x3E200000
  83. unsigned int actual_size = encode_float(0.15625f, arr);
  84. CHECK(actual_size == sizeof(uint32_t));
  85. CHECK(arr[0] == 0x00);
  86. CHECK(arr[1] == 0x00);
  87. CHECK(arr[2] == 0x20);
  88. CHECK(arr[3] == 0x3e);
  89. }
  90. TEST_CASE("[Marshalls] Floating point double precision encoding") {
  91. uint8_t arr[8];
  92. // Decimal: 0.333333333333333314829616256247390992939472198486328125
  93. // IEEE 754 double-precision binary floating-point format:
  94. // sign exponent (11 bits) fraction (52 bits)
  95. // 0 01111111101 0101010101010101010101010101010101010101010101010101
  96. // Hexadecimal: 0x3FD5555555555555
  97. unsigned int actual_size = encode_double(0.33333333333333333, arr);
  98. CHECK(actual_size == sizeof(uint64_t));
  99. CHECK(arr[0] == 0x55);
  100. CHECK(arr[1] == 0x55);
  101. CHECK(arr[2] == 0x55);
  102. CHECK(arr[3] == 0x55);
  103. CHECK(arr[4] == 0x55);
  104. CHECK(arr[5] == 0x55);
  105. CHECK(arr[6] == 0xd5);
  106. CHECK(arr[7] == 0x3f);
  107. }
  108. TEST_CASE("[Marshalls] Floating point single precision decoding") {
  109. uint8_t arr[] = { 0x00, 0x00, 0x20, 0x3e };
  110. // See floating point encoding test case for details behind expected values
  111. CHECK(decode_float(arr) == 0.15625f);
  112. }
  113. TEST_CASE("[Marshalls] Floating point double precision decoding") {
  114. uint8_t arr[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xd5, 0x3f };
  115. // See floating point encoding test case for details behind expected values
  116. CHECK(decode_double(arr) == 0.33333333333333333);
  117. }
  118. TEST_CASE("[Marshalls] C string encoding") {
  119. char cstring[] = "Godot"; // 5 characters
  120. uint8_t data[6];
  121. int actual_size = encode_cstring(cstring, data);
  122. CHECK(actual_size == 6);
  123. CHECK(data[0] == 'G');
  124. CHECK(data[1] == 'o');
  125. CHECK(data[2] == 'd');
  126. CHECK(data[3] == 'o');
  127. CHECK(data[4] == 't');
  128. CHECK(data[5] == '\0');
  129. }
  130. TEST_CASE("[Marshalls] NIL Variant encoding") {
  131. int r_len;
  132. Variant variant;
  133. uint8_t buffer[4];
  134. CHECK(encode_variant(variant, buffer, r_len) == OK);
  135. CHECK_MESSAGE(r_len == 4, "Length == 4 bytes for Variant::Type");
  136. CHECK_MESSAGE(buffer[0] == 0x00, "Variant::NIL");
  137. CHECK(buffer[1] == 0x00);
  138. CHECK(buffer[2] == 0x00);
  139. CHECK(buffer[3] == 0x00);
  140. // No value
  141. }
  142. TEST_CASE("[Marshalls] INT 32 bit Variant encoding") {
  143. int r_len;
  144. Variant variant(0x12345678);
  145. uint8_t buffer[8];
  146. CHECK(encode_variant(variant, buffer, r_len) == OK);
  147. CHECK_MESSAGE(r_len == 8, "Length == 4 bytes for Variant::Type + 4 bytes for int32_t");
  148. CHECK_MESSAGE(buffer[0] == 0x02, "Variant::INT");
  149. CHECK(buffer[1] == 0x00);
  150. CHECK(buffer[2] == 0x00);
  151. CHECK(buffer[3] == 0x00);
  152. // Check value
  153. CHECK(buffer[4] == 0x78);
  154. CHECK(buffer[5] == 0x56);
  155. CHECK(buffer[6] == 0x34);
  156. CHECK(buffer[7] == 0x12);
  157. }
  158. TEST_CASE("[Marshalls] INT 64 bit Variant encoding") {
  159. int r_len;
  160. Variant variant(uint64_t(0x0f123456789abcdef));
  161. uint8_t buffer[12];
  162. CHECK(encode_variant(variant, buffer, r_len) == OK);
  163. CHECK_MESSAGE(r_len == 12, "Length == 4 bytes for Variant::Type + 8 bytes for int64_t");
  164. CHECK_MESSAGE(buffer[0] == 0x02, "Variant::INT");
  165. CHECK(buffer[1] == 0x00);
  166. CHECK_MESSAGE(buffer[2] == 0x01, "ENCODE_FLAG_64");
  167. CHECK(buffer[3] == 0x00);
  168. // Check value
  169. CHECK(buffer[4] == 0xef);
  170. CHECK(buffer[5] == 0xcd);
  171. CHECK(buffer[6] == 0xab);
  172. CHECK(buffer[7] == 0x89);
  173. CHECK(buffer[8] == 0x67);
  174. CHECK(buffer[9] == 0x45);
  175. CHECK(buffer[10] == 0x23);
  176. CHECK(buffer[11] == 0xf1);
  177. }
  178. TEST_CASE("[Marshalls] FLOAT single precision Variant encoding") {
  179. int r_len;
  180. Variant variant(0.15625f);
  181. uint8_t buffer[8];
  182. CHECK(encode_variant(variant, buffer, r_len) == OK);
  183. CHECK_MESSAGE(r_len == 8, "Length == 4 bytes for Variant::Type + 4 bytes for float");
  184. CHECK_MESSAGE(buffer[0] == 0x03, "Variant::FLOAT");
  185. CHECK(buffer[1] == 0x00);
  186. CHECK(buffer[2] == 0x00);
  187. CHECK(buffer[3] == 0x00);
  188. // Check value
  189. CHECK(buffer[4] == 0x00);
  190. CHECK(buffer[5] == 0x00);
  191. CHECK(buffer[6] == 0x20);
  192. CHECK(buffer[7] == 0x3e);
  193. }
  194. TEST_CASE("[Marshalls] FLOAT double precision Variant encoding") {
  195. int r_len;
  196. Variant variant(0.33333333333333333);
  197. uint8_t buffer[12];
  198. CHECK(encode_variant(variant, buffer, r_len) == OK);
  199. CHECK_MESSAGE(r_len == 12, "Length == 4 bytes for Variant::Type + 8 bytes for double");
  200. CHECK_MESSAGE(buffer[0] == 0x03, "Variant::FLOAT");
  201. CHECK(buffer[1] == 0x00);
  202. CHECK_MESSAGE(buffer[2] == 0x01, "ENCODE_FLAG_64");
  203. CHECK(buffer[3] == 0x00);
  204. // Check value
  205. CHECK(buffer[4] == 0x55);
  206. CHECK(buffer[5] == 0x55);
  207. CHECK(buffer[6] == 0x55);
  208. CHECK(buffer[7] == 0x55);
  209. CHECK(buffer[8] == 0x55);
  210. CHECK(buffer[9] == 0x55);
  211. CHECK(buffer[10] == 0xd5);
  212. CHECK(buffer[11] == 0x3f);
  213. }
  214. TEST_CASE("[Marshalls] Invalid data Variant decoding") {
  215. Variant variant;
  216. int r_len = 0;
  217. uint8_t some_buffer[1] = { 0x00 };
  218. uint8_t out_of_range_type_buffer[4] = { 0xff }; // Greater than Variant::VARIANT_MAX
  219. ERR_PRINT_OFF;
  220. CHECK(decode_variant(variant, some_buffer, /* less than 4 */ 1, &r_len) == ERR_INVALID_DATA);
  221. CHECK(r_len == 0);
  222. CHECK(decode_variant(variant, out_of_range_type_buffer, 4, &r_len) == ERR_INVALID_DATA);
  223. CHECK(r_len == 0);
  224. ERR_PRINT_ON;
  225. }
  226. TEST_CASE("[Marshalls] NIL Variant decoding") {
  227. Variant variant;
  228. int r_len;
  229. uint8_t buffer[] = {
  230. 0x00, 0x00, 0x00, 0x00 // Variant::NIL
  231. };
  232. CHECK(decode_variant(variant, buffer, 4, &r_len) == OK);
  233. CHECK(r_len == 4);
  234. CHECK(variant == Variant());
  235. }
  236. TEST_CASE("[Marshalls] INT 32 bit Variant decoding") {
  237. Variant variant;
  238. int r_len;
  239. uint8_t buffer[] = {
  240. 0x02, 0x00, 0x00, 0x00, // Variant::INT
  241. 0x78, 0x56, 0x34, 0x12 // value
  242. };
  243. CHECK(decode_variant(variant, buffer, 8, &r_len) == OK);
  244. CHECK(r_len == 8);
  245. CHECK(variant == Variant(0x12345678));
  246. }
  247. TEST_CASE("[Marshalls] INT 64 bit Variant decoding") {
  248. Variant variant;
  249. int r_len;
  250. uint8_t buffer[] = {
  251. 0x02, 0x00, 0x01, 0x00, // Variant::INT & ENCODE_FLAG_64
  252. 0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0xf1 // value
  253. };
  254. CHECK(decode_variant(variant, buffer, 12, &r_len) == OK);
  255. CHECK(r_len == 12);
  256. CHECK(variant == Variant(uint64_t(0x0f123456789abcdef)));
  257. }
  258. TEST_CASE("[Marshalls] FLOAT single precision Variant decoding") {
  259. Variant variant;
  260. int r_len;
  261. uint8_t buffer[] = {
  262. 0x03, 0x00, 0x00, 0x00, // Variant::FLOAT
  263. 0x00, 0x00, 0x20, 0x3e // value
  264. };
  265. CHECK(decode_variant(variant, buffer, 8, &r_len) == OK);
  266. CHECK(r_len == 8);
  267. CHECK(variant == Variant(0.15625f));
  268. }
  269. TEST_CASE("[Marshalls] FLOAT double precision Variant decoding") {
  270. Variant variant;
  271. int r_len;
  272. uint8_t buffer[] = {
  273. 0x03, 0x00, 0x01, 0x00, // Variant::FLOAT & ENCODE_FLAG_64
  274. 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xd5, 0x3f // value
  275. };
  276. CHECK(decode_variant(variant, buffer, 12, &r_len) == OK);
  277. CHECK(r_len == 12);
  278. CHECK(variant == Variant(0.33333333333333333));
  279. }
  280. } // namespace TestMarshalls
  281. #endif // TEST_MARSHALLS_H