PageRenderTime 31ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/src/test/test-generated-code2.c

http://protobuf-c.googlecode.com/
C | 1601 lines | 1352 code | 238 blank | 11 comment | 86 complexity | 8fe95daa5a70c898dc959e3e7c2a9882 MD5 | raw file
Possible License(s): BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "generated-code/test-full.pb-c.h"
  5. #include "generated-code/test-full-cxx-output.inc"
  6. #define TEST_ENUM_SMALL_TYPE_NAME Foo__TestEnumSmall
  7. #define TEST_ENUM_SMALL(shortname) FOO__TEST_ENUM_SMALL__##shortname
  8. #define TEST_ENUM_TYPE_NAME Foo__TestEnum
  9. #include "common-test-arrays.h"
  10. #define N_ELEMENTS(arr) (sizeof(arr)/sizeof((arr)[0]))
  11. /* ==== helper functions ==== */
  12. static protobuf_c_boolean
  13. are_bytes_equal (unsigned actual_len,
  14. const uint8_t *actual_data,
  15. unsigned expected_len,
  16. const uint8_t *expected_data)
  17. {
  18. if (actual_len != expected_len)
  19. return 0;
  20. return memcmp (actual_data, expected_data, actual_len) == 0;
  21. }
  22. static void
  23. dump_bytes_stderr (size_t len, const uint8_t *data)
  24. {
  25. size_t i;
  26. for (i = 0; i < len; i++)
  27. fprintf(stderr," %02x", data[i]);
  28. fprintf(stderr,"\n");
  29. }
  30. static void
  31. test_versus_static_array(unsigned actual_len,
  32. const uint8_t *actual_data,
  33. unsigned expected_len,
  34. const uint8_t *expected_data,
  35. const char *static_buf_name,
  36. const char *filename,
  37. unsigned lineno)
  38. {
  39. if (are_bytes_equal (actual_len, actual_data, expected_len, expected_data))
  40. return;
  41. fprintf (stderr, "test_versus_static_array failed:\n"
  42. "actual: [length=%u]\n", actual_len);
  43. dump_bytes_stderr (actual_len, actual_data);
  44. fprintf (stderr, "expected: [length=%u] [buffer %s]\n",
  45. expected_len, static_buf_name);
  46. dump_bytes_stderr (expected_len, expected_data);
  47. fprintf (stderr, "at line %u of %s.\n", lineno, filename);
  48. abort();
  49. }
  50. #define TEST_VERSUS_STATIC_ARRAY(actual_len, actual_data, buf) \
  51. test_versus_static_array(actual_len,actual_data, \
  52. sizeof(buf), buf, #buf, __FILE__, __LINE__)
  53. /* rv is unpacked message */
  54. static void *
  55. test_compare_pack_methods (ProtobufCMessage *message,
  56. size_t *packed_len_out,
  57. uint8_t **packed_out)
  58. {
  59. unsigned char scratch[16];
  60. ProtobufCBufferSimple bs = PROTOBUF_C_BUFFER_SIMPLE_INIT (scratch);
  61. size_t siz1 = protobuf_c_message_get_packed_size (message);
  62. size_t siz2;
  63. size_t siz3 = protobuf_c_message_pack_to_buffer (message, &bs.base);
  64. void *packed1 = malloc (siz1);
  65. void *rv;
  66. assert (packed1 != NULL);
  67. assert (siz1 == siz3);
  68. siz2 = protobuf_c_message_pack (message, packed1);
  69. assert (siz1 == siz2);
  70. assert (bs.len == siz1);
  71. assert (memcmp (bs.data, packed1, siz1) == 0);
  72. rv = protobuf_c_message_unpack (message->descriptor, NULL, siz1, packed1);
  73. assert (rv != NULL);
  74. PROTOBUF_C_BUFFER_SIMPLE_CLEAR (&bs);
  75. *packed_len_out = siz1;
  76. *packed_out = packed1;
  77. return rv;
  78. }
  79. #define NUMERIC_EQUALS(a,b) ((a) == (b))
  80. #define STRING_EQUALS(a,b) (strcmp((a),(b))==0)
  81. static protobuf_c_boolean
  82. binary_data_equals (ProtobufCBinaryData a, ProtobufCBinaryData b)
  83. {
  84. if (a.len != b.len)
  85. return 0;
  86. return memcmp (a.data, b.data, a.len) == 0;
  87. }
  88. static protobuf_c_boolean
  89. submesses_equals (Foo__SubMess *a, Foo__SubMess *b)
  90. {
  91. assert(a->base.descriptor == &foo__sub_mess__descriptor);
  92. assert(b->base.descriptor == &foo__sub_mess__descriptor);
  93. return a->test == b->test;
  94. }
  95. /* === the actual tests === */
  96. /* === misc fencepost tests === */
  97. static void test_enum_small (void)
  98. {
  99. #define DO_TEST(UC_VALUE) \
  100. do{ \
  101. Foo__TestMessRequiredEnumSmall small = FOO__TEST_MESS_REQUIRED_ENUM_SMALL__INIT; \
  102. size_t len; \
  103. uint8_t *data; \
  104. Foo__TestMessRequiredEnumSmall *unpacked; \
  105. small.test = FOO__TEST_ENUM_SMALL__##UC_VALUE; \
  106. unpacked = test_compare_pack_methods ((ProtobufCMessage*)&small, &len, &data); \
  107. assert (unpacked->test == FOO__TEST_ENUM_SMALL__##UC_VALUE); \
  108. foo__test_mess_required_enum_small__free_unpacked (unpacked, NULL); \
  109. TEST_VERSUS_STATIC_ARRAY (len, data, test_enum_small_##UC_VALUE); \
  110. free (data); \
  111. }while(0)
  112. assert (sizeof (Foo__TestEnumSmall) == 4);
  113. DO_TEST(VALUE);
  114. DO_TEST(OTHER_VALUE);
  115. #undef DO_TEST
  116. }
  117. static void test_enum_big (void)
  118. {
  119. Foo__TestMessRequiredEnum big = FOO__TEST_MESS_REQUIRED_ENUM__INIT;
  120. size_t len;
  121. uint8_t *data;
  122. Foo__TestMessRequiredEnum *unpacked;
  123. assert (sizeof (Foo__TestEnum) == 4);
  124. #define DO_ONE_TEST(shortname, numeric_value, encoded_len) \
  125. do{ \
  126. big.test = FOO__TEST_ENUM__##shortname; \
  127. unpacked = test_compare_pack_methods ((ProtobufCMessage*)&big, &len, &data); \
  128. assert (unpacked->test == FOO__TEST_ENUM__##shortname); \
  129. foo__test_mess_required_enum__free_unpacked (unpacked, NULL); \
  130. TEST_VERSUS_STATIC_ARRAY (len, data, test_enum_big_##shortname); \
  131. assert (encoded_len + 1 == len); \
  132. assert (big.test == numeric_value); \
  133. free (data); \
  134. }while(0)
  135. DO_ONE_TEST(VALUE0, 0, 1);
  136. DO_ONE_TEST(VALUE127, 127, 1);
  137. DO_ONE_TEST(VALUE128, 128, 2);
  138. DO_ONE_TEST(VALUE16383, 16383, 2);
  139. DO_ONE_TEST(VALUE16384, 16384, 3);
  140. DO_ONE_TEST(VALUE2097151, 2097151, 3);
  141. DO_ONE_TEST(VALUE2097152, 2097152, 4);
  142. DO_ONE_TEST(VALUE268435455, 268435455, 4);
  143. DO_ONE_TEST(VALUE268435456, 268435456, 5);
  144. #undef DO_ONE_TEST
  145. }
  146. static void test_field_numbers (void)
  147. {
  148. size_t len;
  149. uint8_t *data;
  150. #define DO_ONE_TEST(num, exp_len) \
  151. { \
  152. Foo__TestFieldNo##num t = FOO__TEST_FIELD_NO##num##__INIT; \
  153. Foo__TestFieldNo##num *t2; \
  154. t.test = "tst"; \
  155. t2 = test_compare_pack_methods ((ProtobufCMessage*)(&t), &len, &data); \
  156. assert (strcmp (t2->test, "tst") == 0); \
  157. TEST_VERSUS_STATIC_ARRAY (len, data, test_field_number_##num); \
  158. assert (len == exp_len); \
  159. free (data); \
  160. foo__test_field_no##num##__free_unpacked (t2, NULL); \
  161. }
  162. DO_ONE_TEST (15, 1 + 1 + 3);
  163. DO_ONE_TEST (16, 2 + 1 + 3);
  164. DO_ONE_TEST (2047, 2 + 1 + 3);
  165. DO_ONE_TEST (2048, 3 + 1 + 3);
  166. DO_ONE_TEST (262143, 3 + 1 + 3);
  167. DO_ONE_TEST (262144, 4 + 1 + 3);
  168. DO_ONE_TEST (33554431, 4 + 1 + 3);
  169. DO_ONE_TEST (33554432, 5 + 1 + 3);
  170. #undef DO_ONE_TEST
  171. }
  172. /* === Required type fields === */
  173. #define DO_TEST_REQUIRED(Type, TYPE, type, value, example_packed_data, equal_func) \
  174. do{ \
  175. Foo__TestMessRequired##Type opt = FOO__TEST_MESS_REQUIRED_##TYPE##__INIT; \
  176. Foo__TestMessRequired##Type *mess; \
  177. size_t len; uint8_t *data; \
  178. opt.test = value; \
  179. mess = test_compare_pack_methods (&opt.base, &len, &data); \
  180. TEST_VERSUS_STATIC_ARRAY (len, data, example_packed_data); \
  181. assert (equal_func (mess->test, value)); \
  182. foo__test_mess_required_##type##__free_unpacked (mess, NULL); \
  183. free (data); \
  184. }while(0)
  185. static void test_required_int32 (void)
  186. {
  187. #define DO_TEST(value, example_packed_data) \
  188. DO_TEST_REQUIRED(Int32, INT32, int32, value, example_packed_data, NUMERIC_EQUALS)
  189. DO_TEST(INT32_MIN, test_required_int32_min);
  190. DO_TEST(-1000, test_required_int32_m1000);
  191. DO_TEST(0, test_required_int32_0);
  192. DO_TEST(INT32_MAX, test_required_int32_max);
  193. #undef DO_TEST
  194. }
  195. static void test_required_sint32 (void)
  196. {
  197. #define DO_TEST(value, example_packed_data) \
  198. DO_TEST_REQUIRED(SInt32, SINT32, sint32, value, example_packed_data, NUMERIC_EQUALS)
  199. DO_TEST(INT32_MIN, test_required_sint32_min);
  200. DO_TEST(-1000, test_required_sint32_m1000);
  201. DO_TEST(0, test_required_sint32_0);
  202. DO_TEST(INT32_MAX, test_required_sint32_max);
  203. #undef DO_TEST
  204. }
  205. static void test_required_sfixed32 (void)
  206. {
  207. #define DO_TEST(value, example_packed_data) \
  208. DO_TEST_REQUIRED(SFixed32, SFIXED32, sfixed32, value, example_packed_data, NUMERIC_EQUALS)
  209. DO_TEST(INT32_MIN, test_required_sfixed32_min);
  210. DO_TEST(-1000, test_required_sfixed32_m1000);
  211. DO_TEST(0, test_required_sfixed32_0);
  212. DO_TEST(INT32_MAX, test_required_sfixed32_max);
  213. #undef DO_TEST
  214. }
  215. static void test_required_uint32 (void)
  216. {
  217. #define DO_TEST(value, example_packed_data) \
  218. DO_TEST_REQUIRED(UInt32, UINT32, uint32, value, example_packed_data, NUMERIC_EQUALS)
  219. DO_TEST(0, test_required_uint32_0);
  220. DO_TEST(MILLION, test_required_uint32_million);
  221. DO_TEST(UINT32_MAX, test_required_uint32_max);
  222. #undef DO_TEST
  223. }
  224. static void test_required_fixed32 (void)
  225. {
  226. #define DO_TEST(value, example_packed_data) \
  227. DO_TEST_REQUIRED(Fixed32, FIXED32, fixed32, value, example_packed_data, NUMERIC_EQUALS)
  228. DO_TEST(0, test_required_fixed32_0);
  229. DO_TEST(MILLION, test_required_fixed32_million);
  230. DO_TEST(UINT32_MAX, test_required_fixed32_max);
  231. #undef DO_TEST
  232. }
  233. static void test_required_int64 (void)
  234. {
  235. #define DO_TEST(value, example_packed_data) \
  236. DO_TEST_REQUIRED(Int64, INT64, int64, value, example_packed_data, NUMERIC_EQUALS)
  237. DO_TEST(INT64_MIN, test_required_int64_min);
  238. DO_TEST(-TRILLION, test_required_int64_mtril);
  239. DO_TEST(0, test_required_int64_0);
  240. DO_TEST(QUADRILLION, test_required_int64_quad);
  241. DO_TEST(INT64_MAX, test_required_int64_max);
  242. #undef DO_TEST
  243. }
  244. static void test_required_sint64 (void)
  245. {
  246. #define DO_TEST(value, example_packed_data) \
  247. DO_TEST_REQUIRED(SInt64, SINT64, sint64, value, example_packed_data, NUMERIC_EQUALS)
  248. DO_TEST(INT64_MIN, test_required_sint64_min);
  249. DO_TEST(-TRILLION, test_required_sint64_mtril);
  250. DO_TEST(0, test_required_sint64_0);
  251. DO_TEST(QUADRILLION, test_required_sint64_quad);
  252. DO_TEST(INT64_MAX, test_required_sint64_max);
  253. #undef DO_TEST
  254. }
  255. static void test_required_sfixed64 (void)
  256. {
  257. #define DO_TEST(value, example_packed_data) \
  258. DO_TEST_REQUIRED(SFixed64, SFIXED64, sfixed64, value, example_packed_data, NUMERIC_EQUALS)
  259. DO_TEST(INT64_MIN, test_required_sfixed64_min);
  260. DO_TEST(-TRILLION, test_required_sfixed64_mtril);
  261. DO_TEST(0, test_required_sfixed64_0);
  262. DO_TEST(QUADRILLION, test_required_sfixed64_quad);
  263. DO_TEST(INT64_MAX, test_required_sfixed64_max);
  264. #undef DO_TEST
  265. }
  266. static void test_required_uint64 (void)
  267. {
  268. #define DO_TEST(value, example_packed_data) \
  269. DO_TEST_REQUIRED(UInt64, UINT64, uint64, value, example_packed_data, NUMERIC_EQUALS)
  270. DO_TEST(0, test_required_uint64_0);
  271. DO_TEST(THOUSAND, test_required_uint64_thou);
  272. DO_TEST(MILLION, test_required_uint64_mill);
  273. DO_TEST(BILLION, test_required_uint64_bill);
  274. DO_TEST(TRILLION, test_required_uint64_tril);
  275. DO_TEST(QUADRILLION, test_required_uint64_quad);
  276. DO_TEST(QUINTILLION, test_required_uint64_quint);
  277. DO_TEST(UINT64_MAX, test_required_uint64_max);
  278. #undef DO_TEST
  279. }
  280. static void test_required_fixed64 (void)
  281. {
  282. #define DO_TEST(value, example_packed_data) \
  283. DO_TEST_REQUIRED(Fixed64, FIXED64, fixed64, value, example_packed_data, NUMERIC_EQUALS)
  284. DO_TEST(0, test_required_fixed64_0);
  285. DO_TEST(THOUSAND, test_required_fixed64_thou);
  286. DO_TEST(MILLION, test_required_fixed64_mill);
  287. DO_TEST(BILLION, test_required_fixed64_bill);
  288. DO_TEST(TRILLION, test_required_fixed64_tril);
  289. DO_TEST(QUADRILLION, test_required_fixed64_quad);
  290. DO_TEST(QUINTILLION, test_required_fixed64_quint);
  291. DO_TEST(UINT64_MAX, test_required_fixed64_max);
  292. #undef DO_TEST
  293. }
  294. static void test_required_float (void)
  295. {
  296. #define DO_TEST(value, example_packed_data) \
  297. DO_TEST_REQUIRED(Float, FLOAT, float, value, example_packed_data, NUMERIC_EQUALS)
  298. DO_TEST(-THOUSAND, test_required_float_mthou);
  299. DO_TEST(0, test_required_float_0);
  300. DO_TEST(420, test_required_float_420);
  301. #undef DO_TEST
  302. }
  303. static void test_required_double (void)
  304. {
  305. #define DO_TEST(value, example_packed_data) \
  306. DO_TEST_REQUIRED(Double, DOUBLE, double, value, example_packed_data, NUMERIC_EQUALS)
  307. DO_TEST(-THOUSAND, test_required_double_mthou);
  308. DO_TEST(0, test_required_double_0);
  309. DO_TEST(420, test_required_double_420);
  310. #undef DO_TEST
  311. }
  312. static void test_required_bool (void)
  313. {
  314. #define DO_TEST(value, example_packed_data) \
  315. DO_TEST_REQUIRED(Bool, BOOL, bool, value, example_packed_data, NUMERIC_EQUALS)
  316. DO_TEST(0, test_required_bool_0);
  317. DO_TEST(1, test_required_bool_1);
  318. #undef DO_TEST
  319. }
  320. static void test_required_TestEnumSmall (void)
  321. {
  322. #define DO_TEST(value, example_packed_data) \
  323. DO_TEST_REQUIRED(EnumSmall, ENUM_SMALL, enum_small, value, example_packed_data, NUMERIC_EQUALS)
  324. DO_TEST(FOO__TEST_ENUM_SMALL__VALUE, test_required_enum_small_VALUE);
  325. DO_TEST(FOO__TEST_ENUM_SMALL__OTHER_VALUE, test_required_enum_small_OTHER_VALUE);
  326. #undef DO_TEST
  327. }
  328. static void test_required_TestEnum (void)
  329. {
  330. #define DO_TEST(value, example_packed_data) \
  331. DO_TEST_REQUIRED(Enum, ENUM, enum, value, example_packed_data, NUMERIC_EQUALS)
  332. DO_TEST (FOO__TEST_ENUM__VALUE0, test_required_enum_0);
  333. DO_TEST (FOO__TEST_ENUM__VALUE1, test_required_enum_1);
  334. DO_TEST (FOO__TEST_ENUM__VALUE127, test_required_enum_127);
  335. DO_TEST (FOO__TEST_ENUM__VALUE128, test_required_enum_128);
  336. DO_TEST (FOO__TEST_ENUM__VALUE16383, test_required_enum_16383);
  337. DO_TEST (FOO__TEST_ENUM__VALUE16384, test_required_enum_16384);
  338. DO_TEST (FOO__TEST_ENUM__VALUE2097151, test_required_enum_2097151);
  339. DO_TEST (FOO__TEST_ENUM__VALUE2097152, test_required_enum_2097152);
  340. DO_TEST (FOO__TEST_ENUM__VALUE268435455, test_required_enum_268435455);
  341. DO_TEST (FOO__TEST_ENUM__VALUE268435456, test_required_enum_268435456);
  342. #undef DO_TEST
  343. }
  344. static void test_required_string (void)
  345. {
  346. #define DO_TEST(value, example_packed_data) \
  347. DO_TEST_REQUIRED(String, STRING, string, value, example_packed_data, STRING_EQUALS)
  348. DO_TEST("", test_required_string_empty);
  349. DO_TEST("hello", test_required_string_hello);
  350. DO_TEST("two hundred xs follow: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", test_required_string_long);
  351. #undef DO_TEST
  352. }
  353. static void test_required_bytes (void)
  354. {
  355. static ProtobufCBinaryData bd_empty = { 0, (uint8_t*)"" };
  356. static ProtobufCBinaryData bd_hello = { 5, (uint8_t*)"hello" };
  357. static ProtobufCBinaryData bd_random = { 5, (uint8_t*)"\1\0\375\2\4" };
  358. #define DO_TEST(value, example_packed_data) \
  359. DO_TEST_REQUIRED (Bytes, BYTES, bytes, value, example_packed_data, binary_data_equals)
  360. DO_TEST (bd_empty, test_required_bytes_empty);
  361. DO_TEST (bd_hello, test_required_bytes_hello);
  362. DO_TEST (bd_random, test_required_bytes_random);
  363. #undef DO_TEST
  364. }
  365. static void test_required_SubMess (void)
  366. {
  367. Foo__SubMess submess = FOO__SUB_MESS__INIT;
  368. #define DO_TEST(value, example_packed_data) \
  369. DO_TEST_REQUIRED (Message, MESSAGE, message, value, example_packed_data, submesses_equals)
  370. submess.test = 0;
  371. DO_TEST (&submess, test_required_submess_0);
  372. submess.test = 42;
  373. DO_TEST (&submess, test_required_submess_42);
  374. #undef DO_TEST
  375. }
  376. /* === Optional type fields === */
  377. static void test_empty_optional (void)
  378. {
  379. Foo__TestMessOptional mess = FOO__TEST_MESS_OPTIONAL__INIT;
  380. size_t len;
  381. uint8_t *data;
  382. Foo__TestMessOptional *mess2 = test_compare_pack_methods (&mess.base, &len, &data);
  383. assert (len == 0);
  384. free (data);
  385. foo__test_mess_optional__free_unpacked (mess2, NULL);
  386. }
  387. #define DO_TEST_OPTIONAL(base_member, value, example_packed_data, equal_func) \
  388. do{ \
  389. Foo__TestMessOptional opt = FOO__TEST_MESS_OPTIONAL__INIT; \
  390. Foo__TestMessOptional *mess; \
  391. size_t len; uint8_t *data; \
  392. opt.has_##base_member = 1; \
  393. opt.base_member = value; \
  394. mess = test_compare_pack_methods (&opt.base, &len, &data); \
  395. TEST_VERSUS_STATIC_ARRAY (len, data, example_packed_data); \
  396. assert (mess->has_##base_member); \
  397. assert (equal_func (mess->base_member, value)); \
  398. foo__test_mess_optional__free_unpacked (mess, NULL); \
  399. free (data); \
  400. }while(0)
  401. static void test_optional_int32 (void)
  402. {
  403. #define DO_TEST(value, example_packed_data) \
  404. DO_TEST_OPTIONAL(test_int32, value, example_packed_data, NUMERIC_EQUALS)
  405. DO_TEST (INT32_MIN, test_optional_int32_min);
  406. DO_TEST (-1, test_optional_int32_m1);
  407. DO_TEST (0, test_optional_int32_0);
  408. DO_TEST (666, test_optional_int32_666);
  409. DO_TEST (INT32_MAX, test_optional_int32_max);
  410. #undef DO_TEST
  411. }
  412. static void test_optional_sint32 (void)
  413. {
  414. #define DO_TEST(value, example_packed_data) \
  415. DO_TEST_OPTIONAL(test_sint32, value, example_packed_data, NUMERIC_EQUALS)
  416. DO_TEST (INT32_MIN, test_optional_sint32_min);
  417. DO_TEST (-1, test_optional_sint32_m1);
  418. DO_TEST (0, test_optional_sint32_0);
  419. DO_TEST (666, test_optional_sint32_666);
  420. DO_TEST (INT32_MAX, test_optional_sint32_max);
  421. #undef DO_TEST
  422. }
  423. static void test_optional_sfixed32 (void)
  424. {
  425. #define DO_TEST(value, example_packed_data) \
  426. DO_TEST_OPTIONAL(test_sfixed32, value, example_packed_data, NUMERIC_EQUALS)
  427. DO_TEST (INT32_MIN, test_optional_sfixed32_min);
  428. DO_TEST (-1, test_optional_sfixed32_m1);
  429. DO_TEST (0, test_optional_sfixed32_0);
  430. DO_TEST (666, test_optional_sfixed32_666);
  431. DO_TEST (INT32_MAX, test_optional_sfixed32_max);
  432. #undef DO_TEST
  433. }
  434. static void test_optional_int64 (void)
  435. {
  436. #define DO_TEST(value, example_packed_data) \
  437. DO_TEST_OPTIONAL(test_int64, value, example_packed_data, NUMERIC_EQUALS)
  438. DO_TEST (INT64_MIN, test_optional_int64_min);
  439. DO_TEST (-1111111111LL, test_optional_int64_m1111111111LL);
  440. DO_TEST (0, test_optional_int64_0);
  441. DO_TEST (QUINTILLION, test_optional_int64_quintillion);
  442. DO_TEST (INT64_MAX, test_optional_int64_max);
  443. #undef DO_TEST
  444. }
  445. static void test_optional_sint64 (void)
  446. {
  447. #define DO_TEST(value, example_packed_data) \
  448. DO_TEST_OPTIONAL(test_sint64, value, example_packed_data, NUMERIC_EQUALS)
  449. DO_TEST (INT64_MIN, test_optional_sint64_min);
  450. DO_TEST (-1111111111LL, test_optional_sint64_m1111111111LL);
  451. DO_TEST (0, test_optional_sint64_0);
  452. DO_TEST (QUINTILLION, test_optional_sint64_quintillion);
  453. DO_TEST (INT64_MAX, test_optional_sint64_max);
  454. #undef DO_TEST
  455. }
  456. static void test_optional_sfixed64 (void)
  457. {
  458. #define DO_TEST(value, example_packed_data) \
  459. DO_TEST_OPTIONAL(test_sfixed64, value, example_packed_data, NUMERIC_EQUALS)
  460. DO_TEST (INT64_MIN, test_optional_sfixed64_min);
  461. DO_TEST (-1111111111LL, test_optional_sfixed64_m1111111111LL);
  462. DO_TEST (0, test_optional_sfixed64_0);
  463. DO_TEST (QUINTILLION, test_optional_sfixed64_quintillion);
  464. DO_TEST (INT64_MAX, test_optional_sfixed64_max);
  465. #undef DO_TEST
  466. }
  467. static void test_optional_uint32 (void)
  468. {
  469. #define DO_TEST(value, example_packed_data) \
  470. DO_TEST_OPTIONAL(test_uint32, value, example_packed_data, NUMERIC_EQUALS)
  471. DO_TEST (0, test_optional_uint32_0);
  472. DO_TEST (669, test_optional_uint32_669);
  473. DO_TEST (UINT32_MAX, test_optional_uint32_max);
  474. #undef DO_TEST
  475. }
  476. static void test_optional_fixed32 (void)
  477. {
  478. #define DO_TEST(value, example_packed_data) \
  479. DO_TEST_OPTIONAL(test_fixed32, value, example_packed_data, NUMERIC_EQUALS)
  480. DO_TEST (0, test_optional_fixed32_0);
  481. DO_TEST (669, test_optional_fixed32_669);
  482. DO_TEST (UINT32_MAX, test_optional_fixed32_max);
  483. #undef DO_TEST
  484. }
  485. static void test_optional_uint64 (void)
  486. {
  487. #define DO_TEST(value, example_packed_data) \
  488. DO_TEST_OPTIONAL(test_uint64, value, example_packed_data, NUMERIC_EQUALS)
  489. DO_TEST (0, test_optional_uint64_0);
  490. DO_TEST (669669669669669ULL, test_optional_uint64_669669669669669);
  491. DO_TEST (UINT64_MAX, test_optional_uint64_max);
  492. #undef DO_TEST
  493. }
  494. static void test_optional_fixed64 (void)
  495. {
  496. #define DO_TEST(value, example_packed_data) \
  497. DO_TEST_OPTIONAL(test_fixed64, value, example_packed_data, NUMERIC_EQUALS)
  498. DO_TEST (0, test_optional_fixed64_0);
  499. DO_TEST (669669669669669ULL, test_optional_fixed64_669669669669669);
  500. DO_TEST (UINT64_MAX, test_optional_fixed64_max);
  501. #undef DO_TEST
  502. }
  503. static void test_optional_float (void)
  504. {
  505. #define DO_TEST(value, example_packed_data) \
  506. DO_TEST_OPTIONAL(test_float, value, example_packed_data, NUMERIC_EQUALS)
  507. DO_TEST (-100, test_optional_float_m100);
  508. DO_TEST (0, test_optional_float_0);
  509. DO_TEST (141243, test_optional_float_141243);
  510. #undef DO_TEST
  511. }
  512. static void test_optional_double (void)
  513. {
  514. #define DO_TEST(value, example_packed_data) \
  515. DO_TEST_OPTIONAL(test_double, value, example_packed_data, NUMERIC_EQUALS)
  516. DO_TEST (-100, test_optional_double_m100);
  517. DO_TEST (0, test_optional_double_0);
  518. DO_TEST (141243, test_optional_double_141243);
  519. #undef DO_TEST
  520. }
  521. static void test_optional_bool (void)
  522. {
  523. #define DO_TEST(value, example_packed_data) \
  524. DO_TEST_OPTIONAL(test_boolean, value, example_packed_data, NUMERIC_EQUALS)
  525. DO_TEST (0, test_optional_bool_0);
  526. DO_TEST (1, test_optional_bool_1);
  527. #undef DO_TEST
  528. }
  529. static void test_optional_TestEnumSmall (void)
  530. {
  531. #define DO_TEST(value, example_packed_data) \
  532. DO_TEST_OPTIONAL(test_enum_small, value, example_packed_data, NUMERIC_EQUALS)
  533. DO_TEST (0, test_optional_enum_small_0);
  534. DO_TEST (1, test_optional_enum_small_1);
  535. #undef DO_TEST
  536. }
  537. static void test_optional_TestEnum (void)
  538. {
  539. #define DO_TEST(value, example_packed_data) \
  540. DO_TEST_OPTIONAL(test_enum, value, example_packed_data, NUMERIC_EQUALS)
  541. DO_TEST (FOO__TEST_ENUM__VALUE0, test_optional_enum_0);
  542. DO_TEST (FOO__TEST_ENUM__VALUE1, test_optional_enum_1);
  543. DO_TEST (FOO__TEST_ENUM__VALUE127, test_optional_enum_127);
  544. DO_TEST (FOO__TEST_ENUM__VALUE128, test_optional_enum_128);
  545. DO_TEST (FOO__TEST_ENUM__VALUE16383, test_optional_enum_16383);
  546. DO_TEST (FOO__TEST_ENUM__VALUE16384, test_optional_enum_16384);
  547. DO_TEST (FOO__TEST_ENUM__VALUE2097151, test_optional_enum_2097151);
  548. DO_TEST (FOO__TEST_ENUM__VALUE2097152, test_optional_enum_2097152);
  549. DO_TEST (FOO__TEST_ENUM__VALUE268435455, test_optional_enum_268435455);
  550. DO_TEST (FOO__TEST_ENUM__VALUE268435456, test_optional_enum_268435456);
  551. #undef DO_TEST
  552. }
  553. #define DO_TEST_OPTIONAL__NO_HAS(base_member, value, example_packed_data, equal_func) \
  554. do{ \
  555. Foo__TestMessOptional opt = FOO__TEST_MESS_OPTIONAL__INIT; \
  556. Foo__TestMessOptional *mess; \
  557. size_t len; uint8_t *data; \
  558. opt.base_member = value; \
  559. mess = test_compare_pack_methods (&opt.base, &len, &data); \
  560. TEST_VERSUS_STATIC_ARRAY (len, data, example_packed_data); \
  561. assert (mess->base_member != NULL); \
  562. assert (equal_func (mess->base_member, value)); \
  563. foo__test_mess_optional__free_unpacked (mess, NULL); \
  564. free (data); \
  565. }while(0)
  566. static void test_optional_string (void)
  567. {
  568. #define DO_TEST(value, example_packed_data) \
  569. DO_TEST_OPTIONAL__NO_HAS (test_string, value, example_packed_data, STRING_EQUALS)
  570. DO_TEST ("", test_optional_string_empty);
  571. DO_TEST ("hello", test_optional_string_hello);
  572. #undef DO_TEST
  573. }
  574. static void test_optional_bytes (void)
  575. {
  576. static ProtobufCBinaryData bd_empty = { 0, (uint8_t*)"" };
  577. static ProtobufCBinaryData bd_hello = { 5, (uint8_t*)"hello" };
  578. static ProtobufCBinaryData bd_random = { 5, (uint8_t*)"\1\0\375\2\4" };
  579. #define DO_TEST(value, example_packed_data) \
  580. DO_TEST_OPTIONAL (test_bytes, value, example_packed_data, binary_data_equals)
  581. DO_TEST (bd_empty, test_optional_bytes_empty);
  582. DO_TEST (bd_hello, test_optional_bytes_hello);
  583. DO_TEST (bd_random, test_optional_bytes_random);
  584. #undef DO_TEST
  585. }
  586. static void test_optional_SubMess (void)
  587. {
  588. Foo__SubMess submess = FOO__SUB_MESS__INIT;
  589. #define DO_TEST(value, example_packed_data) \
  590. DO_TEST_OPTIONAL__NO_HAS (test_message, value, example_packed_data, submesses_equals)
  591. submess.test = 0;
  592. DO_TEST (&submess, test_optional_submess_0);
  593. submess.test = 42;
  594. DO_TEST (&submess, test_optional_submess_42);
  595. #undef DO_TEST
  596. }
  597. /* === repeated type fields === */
  598. #define DO_TEST_REPEATED(lc_member_name, cast, \
  599. static_array, example_packed_data, \
  600. equals_macro) \
  601. do{ \
  602. Foo__TestMess mess = FOO__TEST_MESS__INIT; \
  603. Foo__TestMess *mess2; \
  604. size_t len; \
  605. uint8_t *data; \
  606. unsigned i; \
  607. mess.n_##lc_member_name = N_ELEMENTS (static_array); \
  608. mess.lc_member_name = cast static_array; \
  609. mess2 = test_compare_pack_methods ((ProtobufCMessage*)(&mess), &len, &data); \
  610. assert(mess2->n_##lc_member_name == N_ELEMENTS (static_array)); \
  611. for (i = 0; i < N_ELEMENTS (static_array); i++) \
  612. assert(equals_macro(mess2->lc_member_name[i], static_array[i])); \
  613. TEST_VERSUS_STATIC_ARRAY (len, data, example_packed_data); \
  614. free (data); \
  615. foo__test_mess__free_unpacked (mess2, NULL); \
  616. }while(0)
  617. static void test_empty_repeated (void)
  618. {
  619. Foo__TestMess mess = FOO__TEST_MESS__INIT;
  620. size_t len;
  621. uint8_t *data;
  622. Foo__TestMess *mess2 = test_compare_pack_methods (&mess.base, &len, &data);
  623. assert (len == 0);
  624. free (data);
  625. foo__test_mess__free_unpacked (mess2, NULL);
  626. }
  627. static void test_repeated_int32 (void)
  628. {
  629. #define DO_TEST(static_array, example_packed_data) \
  630. DO_TEST_REPEATED(test_int32, , \
  631. static_array, example_packed_data, \
  632. NUMERIC_EQUALS)
  633. DO_TEST (int32_arr0, test_repeated_int32_arr0);
  634. DO_TEST (int32_arr1, test_repeated_int32_arr1);
  635. DO_TEST (int32_arr_min_max, test_repeated_int32_arr_min_max);
  636. #undef DO_TEST
  637. }
  638. static void test_repeated_sint32 (void)
  639. {
  640. #define DO_TEST(static_array, example_packed_data) \
  641. DO_TEST_REPEATED(test_sint32, , \
  642. static_array, example_packed_data, \
  643. NUMERIC_EQUALS)
  644. DO_TEST (int32_arr0, test_repeated_sint32_arr0);
  645. DO_TEST (int32_arr1, test_repeated_sint32_arr1);
  646. DO_TEST (int32_arr_min_max, test_repeated_sint32_arr_min_max);
  647. #undef DO_TEST
  648. }
  649. static void test_repeated_sfixed32 (void)
  650. {
  651. #define DO_TEST(static_array, example_packed_data) \
  652. DO_TEST_REPEATED(test_sfixed32, , \
  653. static_array, example_packed_data, \
  654. NUMERIC_EQUALS)
  655. DO_TEST (int32_arr0, test_repeated_sfixed32_arr0);
  656. DO_TEST (int32_arr1, test_repeated_sfixed32_arr1);
  657. DO_TEST (int32_arr_min_max, test_repeated_sfixed32_arr_min_max);
  658. #undef DO_TEST
  659. }
  660. static void test_repeated_uint32 (void)
  661. {
  662. #define DO_TEST(static_array, example_packed_data) \
  663. DO_TEST_REPEATED(test_uint32, , \
  664. static_array, example_packed_data, \
  665. NUMERIC_EQUALS)
  666. DO_TEST (uint32_roundnumbers, test_repeated_uint32_roundnumbers);
  667. DO_TEST (uint32_0_max, test_repeated_uint32_0_max);
  668. #undef DO_TEST
  669. }
  670. static void test_repeated_fixed32 (void)
  671. {
  672. #define DO_TEST(static_array, example_packed_data) \
  673. DO_TEST_REPEATED(test_fixed32, , \
  674. static_array, example_packed_data, \
  675. NUMERIC_EQUALS)
  676. DO_TEST (uint32_roundnumbers, test_repeated_fixed32_roundnumbers);
  677. DO_TEST (uint32_0_max, test_repeated_fixed32_0_max);
  678. #undef DO_TEST
  679. }
  680. static void test_repeated_int64 (void)
  681. {
  682. #define DO_TEST(static_array, example_packed_data) \
  683. DO_TEST_REPEATED(test_int64, , \
  684. static_array, example_packed_data, \
  685. NUMERIC_EQUALS)
  686. DO_TEST (int64_roundnumbers, test_repeated_int64_roundnumbers);
  687. DO_TEST (int64_min_max, test_repeated_int64_min_max);
  688. #undef DO_TEST
  689. }
  690. static void test_repeated_sint64 (void)
  691. {
  692. #define DO_TEST(static_array, example_packed_data) \
  693. DO_TEST_REPEATED(test_sint64, , \
  694. static_array, example_packed_data, \
  695. NUMERIC_EQUALS)
  696. DO_TEST (int64_roundnumbers, test_repeated_sint64_roundnumbers);
  697. DO_TEST (int64_min_max, test_repeated_sint64_min_max);
  698. #undef DO_TEST
  699. }
  700. static void test_repeated_sfixed64 (void)
  701. {
  702. #define DO_TEST(static_array, example_packed_data) \
  703. DO_TEST_REPEATED(test_sfixed64, , \
  704. static_array, example_packed_data, \
  705. NUMERIC_EQUALS)
  706. DO_TEST (int64_roundnumbers, test_repeated_sfixed64_roundnumbers);
  707. DO_TEST (int64_min_max, test_repeated_sfixed64_min_max);
  708. #undef DO_TEST
  709. }
  710. static void test_repeated_uint64 (void)
  711. {
  712. #define DO_TEST(static_array, example_packed_data) \
  713. DO_TEST_REPEATED(test_uint64, , \
  714. static_array, example_packed_data, \
  715. NUMERIC_EQUALS)
  716. DO_TEST(uint64_roundnumbers, test_repeated_uint64_roundnumbers);
  717. DO_TEST(uint64_0_1_max, test_repeated_uint64_0_1_max);
  718. DO_TEST(uint64_random, test_repeated_uint64_random);
  719. #undef DO_TEST
  720. }
  721. static void test_repeated_fixed64 (void)
  722. {
  723. #define DO_TEST(static_array, example_packed_data) \
  724. DO_TEST_REPEATED(test_fixed64, , \
  725. static_array, example_packed_data, \
  726. NUMERIC_EQUALS)
  727. DO_TEST(uint64_roundnumbers, test_repeated_fixed64_roundnumbers);
  728. DO_TEST(uint64_0_1_max, test_repeated_fixed64_0_1_max);
  729. DO_TEST(uint64_random, test_repeated_fixed64_random);
  730. #undef DO_TEST
  731. }
  732. static void test_repeated_float (void)
  733. {
  734. #define DO_TEST(static_array, example_packed_data) \
  735. DO_TEST_REPEATED(test_float, , \
  736. static_array, example_packed_data, \
  737. NUMERIC_EQUALS)
  738. DO_TEST(float_random, test_repeated_float_random);
  739. #undef DO_TEST
  740. }
  741. static void test_repeated_double (void)
  742. {
  743. #define DO_TEST(static_array, example_packed_data) \
  744. DO_TEST_REPEATED(test_double, , \
  745. static_array, example_packed_data, \
  746. NUMERIC_EQUALS)
  747. DO_TEST(double_random, test_repeated_double_random);
  748. #undef DO_TEST
  749. }
  750. static void test_repeated_boolean (void)
  751. {
  752. #define DO_TEST(static_array, example_packed_data) \
  753. DO_TEST_REPEATED(test_boolean, , \
  754. static_array, example_packed_data, \
  755. NUMERIC_EQUALS)
  756. DO_TEST(boolean_0, test_repeated_boolean_0);
  757. DO_TEST(boolean_1, test_repeated_boolean_1);
  758. DO_TEST(boolean_random, test_repeated_boolean_random);
  759. #undef DO_TEST
  760. }
  761. static void test_repeated_TestEnumSmall (void)
  762. {
  763. #define DO_TEST(static_array, example_packed_data) \
  764. DO_TEST_REPEATED(test_enum_small, , \
  765. static_array, example_packed_data, \
  766. NUMERIC_EQUALS)
  767. DO_TEST(enum_small_0, test_repeated_enum_small_0);
  768. DO_TEST(enum_small_1, test_repeated_enum_small_1);
  769. DO_TEST(enum_small_random, test_repeated_enum_small_random);
  770. #undef DO_TEST
  771. }
  772. static void test_repeated_TestEnum (void)
  773. {
  774. #define DO_TEST(static_array, example_packed_data) \
  775. DO_TEST_REPEATED(test_enum, , \
  776. static_array, example_packed_data, \
  777. NUMERIC_EQUALS)
  778. DO_TEST(enum_0, test_repeated_enum_0);
  779. DO_TEST(enum_1, test_repeated_enum_1);
  780. DO_TEST(enum_random, test_repeated_enum_random);
  781. #undef DO_TEST
  782. }
  783. static void test_repeated_string (void)
  784. {
  785. #define DO_TEST(static_array, example_packed_data) \
  786. DO_TEST_REPEATED(test_string, (char **), \
  787. static_array, example_packed_data, \
  788. STRING_EQUALS)
  789. DO_TEST(repeated_strings_0, test_repeated_strings_0);
  790. DO_TEST(repeated_strings_1, test_repeated_strings_1);
  791. DO_TEST(repeated_strings_2, test_repeated_strings_2);
  792. DO_TEST(repeated_strings_3, test_repeated_strings_3);
  793. #undef DO_TEST
  794. }
  795. static void test_repeated_bytes (void)
  796. {
  797. static ProtobufCBinaryData test_binary_data_0[] = {
  798. { 4, (uint8_t *) "text" },
  799. { 9, (uint8_t *) "str\1\2\3\4\5\0" },
  800. { 10, (uint8_t *) "gobble\0foo" }
  801. };
  802. #define DO_TEST(static_array, example_packed_data) \
  803. DO_TEST_REPEATED(test_bytes, , \
  804. static_array, example_packed_data, \
  805. binary_data_equals)
  806. DO_TEST (test_binary_data_0, test_repeated_bytes_0);
  807. #undef DO_TEST
  808. }
  809. static void test_repeated_SubMess (void)
  810. {
  811. static Foo__SubMess submess0 = FOO__SUB_MESS__INIT;
  812. static Foo__SubMess submess1 = FOO__SUB_MESS__INIT;
  813. static Foo__SubMess submess2 = FOO__SUB_MESS__INIT;
  814. static Foo__SubMess *submesses[3] = { &submess0, &submess1, &submess2 };
  815. #define DO_TEST(static_array, example_packed_data) \
  816. DO_TEST_REPEATED(test_message, , \
  817. static_array, example_packed_data, \
  818. submesses_equals)
  819. DO_TEST (submesses, test_repeated_submess_0);
  820. submess0.test = 42;
  821. submess1.test = -10000;
  822. submess2.test = 667;
  823. DO_TEST (submesses, test_repeated_submess_1);
  824. #undef DO_TEST
  825. }
  826. #define DO_TEST_PACKED_REPEATED(lc_member_name, cast, \
  827. static_array, example_packed_data, \
  828. equals_macro) \
  829. do{ \
  830. Foo__TestMessPacked mess = FOO__TEST_MESS_PACKED__INIT; \
  831. Foo__TestMessPacked *mess2; \
  832. size_t len; \
  833. uint8_t *data; \
  834. unsigned i; \
  835. mess.n_##lc_member_name = N_ELEMENTS (static_array); \
  836. mess.lc_member_name = cast static_array; \
  837. mess2 = test_compare_pack_methods ((ProtobufCMessage*)(&mess), &len, &data); \
  838. TEST_VERSUS_STATIC_ARRAY (len, data, example_packed_data); \
  839. assert(mess2->n_##lc_member_name == N_ELEMENTS (static_array)); \
  840. for (i = 0; i < N_ELEMENTS (static_array); i++) \
  841. assert(equals_macro(mess2->lc_member_name[i], static_array[i])); \
  842. free (data); \
  843. foo__test_mess_packed__free_unpacked (mess2, NULL); \
  844. }while(0)
  845. static void test_packed_repeated_int32 (void)
  846. {
  847. #define DO_TEST(static_array, example_packed_data) \
  848. DO_TEST_PACKED_REPEATED(test_int32, , \
  849. static_array, example_packed_data, \
  850. NUMERIC_EQUALS)
  851. DO_TEST (int32_arr0, test_packed_repeated_int32_arr0);
  852. DO_TEST (int32_arr1, test_packed_repeated_int32_arr1);
  853. DO_TEST (int32_arr_min_max, test_packed_repeated_int32_arr_min_max);
  854. #undef DO_TEST
  855. }
  856. static void test_packed_repeated_sint32 (void)
  857. {
  858. #define DO_TEST(static_array, example_packed_data) \
  859. DO_TEST_PACKED_REPEATED(test_sint32, , \
  860. static_array, example_packed_data, \
  861. NUMERIC_EQUALS)
  862. DO_TEST (int32_arr0, test_packed_repeated_sint32_arr0);
  863. DO_TEST (int32_arr1, test_packed_repeated_sint32_arr1);
  864. DO_TEST (int32_arr_min_max, test_packed_repeated_sint32_arr_min_max);
  865. #undef DO_TEST
  866. }
  867. static void test_packed_repeated_sfixed32 (void)
  868. {
  869. #define DO_TEST(static_array, example_packed_data) \
  870. DO_TEST_PACKED_REPEATED(test_sfixed32, , \
  871. static_array, example_packed_data, \
  872. NUMERIC_EQUALS)
  873. DO_TEST (int32_arr0, test_packed_repeated_sfixed32_arr0);
  874. DO_TEST (int32_arr1, test_packed_repeated_sfixed32_arr1);
  875. DO_TEST (int32_arr_min_max, test_packed_repeated_sfixed32_arr_min_max);
  876. #undef DO_TEST
  877. }
  878. static void test_packed_repeated_uint32 (void)
  879. {
  880. #define DO_TEST(static_array, example_packed_data) \
  881. DO_TEST_PACKED_REPEATED(test_uint32, , \
  882. static_array, example_packed_data, \
  883. NUMERIC_EQUALS)
  884. DO_TEST (uint32_roundnumbers, test_packed_repeated_uint32_roundnumbers);
  885. DO_TEST (uint32_0_max, test_packed_repeated_uint32_0_max);
  886. #undef DO_TEST
  887. }
  888. static void test_packed_repeated_fixed32 (void)
  889. {
  890. #define DO_TEST(static_array, example_packed_data) \
  891. DO_TEST_PACKED_REPEATED(test_fixed32, , \
  892. static_array, example_packed_data, \
  893. NUMERIC_EQUALS)
  894. DO_TEST (uint32_roundnumbers, test_packed_repeated_fixed32_roundnumbers);
  895. DO_TEST (uint32_0_max, test_packed_repeated_fixed32_0_max);
  896. #undef DO_TEST
  897. }
  898. static void test_packed_repeated_int64 (void)
  899. {
  900. #define DO_TEST(static_array, example_packed_data) \
  901. DO_TEST_PACKED_REPEATED(test_int64, , \
  902. static_array, example_packed_data, \
  903. NUMERIC_EQUALS)
  904. DO_TEST (int64_roundnumbers, test_packed_repeated_int64_roundnumbers);
  905. DO_TEST (int64_min_max, test_packed_repeated_int64_min_max);
  906. #undef DO_TEST
  907. }
  908. static void test_packed_repeated_sint64 (void)
  909. {
  910. #define DO_TEST(static_array, example_packed_data) \
  911. DO_TEST_PACKED_REPEATED(test_sint64, , \
  912. static_array, example_packed_data, \
  913. NUMERIC_EQUALS)
  914. DO_TEST (int64_roundnumbers, test_packed_repeated_sint64_roundnumbers);
  915. DO_TEST (int64_min_max, test_packed_repeated_sint64_min_max);
  916. #undef DO_TEST
  917. }
  918. static void test_packed_repeated_sfixed64 (void)
  919. {
  920. #define DO_TEST(static_array, example_packed_data) \
  921. DO_TEST_PACKED_REPEATED(test_sfixed64, , \
  922. static_array, example_packed_data, \
  923. NUMERIC_EQUALS)
  924. DO_TEST (int64_roundnumbers, test_packed_repeated_sfixed64_roundnumbers);
  925. DO_TEST (int64_min_max, test_packed_repeated_sfixed64_min_max);
  926. #undef DO_TEST
  927. }
  928. static void test_packed_repeated_uint64 (void)
  929. {
  930. #define DO_TEST(static_array, example_packed_data) \
  931. DO_TEST_PACKED_REPEATED(test_uint64, , \
  932. static_array, example_packed_data, \
  933. NUMERIC_EQUALS)
  934. DO_TEST(uint64_roundnumbers, test_packed_repeated_uint64_roundnumbers);
  935. DO_TEST(uint64_0_1_max, test_packed_repeated_uint64_0_1_max);
  936. DO_TEST(uint64_random, test_packed_repeated_uint64_random);
  937. #undef DO_TEST
  938. }
  939. static void test_packed_repeated_fixed64 (void)
  940. {
  941. #define DO_TEST(static_array, example_packed_data) \
  942. DO_TEST_PACKED_REPEATED(test_fixed64, , \
  943. static_array, example_packed_data, \
  944. NUMERIC_EQUALS)
  945. DO_TEST(uint64_roundnumbers, test_packed_repeated_fixed64_roundnumbers);
  946. DO_TEST(uint64_0_1_max, test_packed_repeated_fixed64_0_1_max);
  947. DO_TEST(uint64_random, test_packed_repeated_fixed64_random);
  948. #undef DO_TEST
  949. }
  950. static void test_packed_repeated_float (void)
  951. {
  952. #define DO_TEST(static_array, example_packed_data) \
  953. DO_TEST_PACKED_REPEATED(test_float, , \
  954. static_array, example_packed_data, \
  955. NUMERIC_EQUALS)
  956. DO_TEST(float_random, test_packed_repeated_float_random);
  957. #undef DO_TEST
  958. }
  959. static void test_packed_repeated_double (void)
  960. {
  961. #define DO_TEST(static_array, example_packed_data) \
  962. DO_TEST_PACKED_REPEATED(test_double, , \
  963. static_array, example_packed_data, \
  964. NUMERIC_EQUALS)
  965. DO_TEST(double_random, test_packed_repeated_double_random);
  966. #undef DO_TEST
  967. }
  968. static void test_packed_repeated_boolean (void)
  969. {
  970. #define DO_TEST(static_array, example_packed_data) \
  971. DO_TEST_PACKED_REPEATED(test_boolean, , \
  972. static_array, example_packed_data, \
  973. NUMERIC_EQUALS)
  974. DO_TEST(boolean_0, test_packed_repeated_boolean_0);
  975. DO_TEST(boolean_1, test_packed_repeated_boolean_1);
  976. DO_TEST(boolean_random, test_packed_repeated_boolean_random);
  977. #undef DO_TEST
  978. }
  979. static void test_packed_repeated_TestEnumSmall (void)
  980. {
  981. #define DO_TEST(static_array, example_packed_data) \
  982. DO_TEST_PACKED_REPEATED(test_enum_small, , \
  983. static_array, example_packed_data, \
  984. NUMERIC_EQUALS)
  985. DO_TEST(enum_small_0, test_packed_repeated_enum_small_0);
  986. DO_TEST(enum_small_1, test_packed_repeated_enum_small_1);
  987. DO_TEST(enum_small_random, test_packed_repeated_enum_small_random);
  988. #undef DO_TEST
  989. }
  990. static void test_packed_repeated_TestEnum (void)
  991. {
  992. #define DO_TEST(static_array, example_packed_data) \
  993. DO_TEST_PACKED_REPEATED(test_enum, , \
  994. static_array, example_packed_data, \
  995. NUMERIC_EQUALS)
  996. DO_TEST(enum_0, test_packed_repeated_enum_0);
  997. DO_TEST(enum_1, test_packed_repeated_enum_1);
  998. DO_TEST(enum_random, test_packed_repeated_enum_random);
  999. #undef DO_TEST
  1000. }
  1001. static void test_unknown_fields (void)
  1002. {
  1003. static Foo__EmptyMess mess = FOO__EMPTY_MESS__INIT;
  1004. static Foo__EmptyMess *mess2;
  1005. ProtobufCMessageUnknownField fields[2];
  1006. size_t len; uint8_t *data;
  1007. mess.base.n_unknown_fields = 2;
  1008. mess.base.unknown_fields = fields;
  1009. fields[0].tag = 5454;
  1010. fields[0].wire_type = PROTOBUF_C_WIRE_TYPE_VARINT;
  1011. fields[0].len = 2;
  1012. fields[0].data = (uint8_t*)"\377\1";
  1013. fields[1].tag = 5555;
  1014. fields[1].wire_type = PROTOBUF_C_WIRE_TYPE_32BIT;
  1015. fields[1].len = 4;
  1016. fields[1].data = (uint8_t*)"\4\1\0\0";
  1017. mess2 = test_compare_pack_methods (&mess.base, &len, &data);
  1018. assert (mess2->base.n_unknown_fields == 2);
  1019. assert (mess2->base.unknown_fields[0].tag == 5454);
  1020. assert (mess2->base.unknown_fields[0].wire_type == PROTOBUF_C_WIRE_TYPE_VARINT);
  1021. assert (mess2->base.unknown_fields[0].len == 2);
  1022. assert (memcmp (mess2->base.unknown_fields[0].data, fields[0].data, 2) == 0);
  1023. assert (mess2->base.unknown_fields[1].tag == 5555);
  1024. assert (mess2->base.unknown_fields[1].wire_type == PROTOBUF_C_WIRE_TYPE_32BIT);
  1025. assert (mess2->base.unknown_fields[1].len == 4);
  1026. assert (memcmp (mess2->base.unknown_fields[1].data, fields[1].data, 4) == 0);
  1027. TEST_VERSUS_STATIC_ARRAY (len, data, test_unknown_fields_0);
  1028. free (data);
  1029. foo__empty_mess__free_unpacked (mess2, NULL);
  1030. fields[0].tag = 6666;
  1031. fields[0].wire_type = PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED;
  1032. fields[0].len = 9;
  1033. fields[0].data = (uint8_t*)"\10xxxxxxxx";
  1034. fields[1].tag = 7777;
  1035. fields[1].wire_type = PROTOBUF_C_WIRE_TYPE_64BIT;
  1036. fields[1].len = 8;
  1037. fields[1].data = (uint8_t*)"\1\1\1\0\0\0\0\0";
  1038. mess2 = test_compare_pack_methods (&mess.base, &len, &data);
  1039. assert (mess2->base.n_unknown_fields == 2);
  1040. assert (mess2->base.unknown_fields[0].tag == 6666);
  1041. assert (mess2->base.unknown_fields[0].wire_type == PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED);
  1042. assert (mess2->base.unknown_fields[0].len == 9);
  1043. assert (memcmp (mess2->base.unknown_fields[0].data, fields[0].data, 9) == 0);
  1044. assert (mess2->base.unknown_fields[1].tag == 7777);
  1045. assert (mess2->base.unknown_fields[1].wire_type == PROTOBUF_C_WIRE_TYPE_64BIT);
  1046. assert (mess2->base.unknown_fields[1].len == 8);
  1047. assert (memcmp (mess2->base.unknown_fields[1].data, fields[1].data, 8) == 0);
  1048. TEST_VERSUS_STATIC_ARRAY (len, data, test_unknown_fields_1);
  1049. free (data);
  1050. foo__empty_mess__free_unpacked (mess2, NULL);
  1051. }
  1052. static void
  1053. test_enum_descriptor (const ProtobufCEnumDescriptor *desc)
  1054. {
  1055. unsigned i;
  1056. for (i = 0; i < desc->n_values; i++)
  1057. {
  1058. const ProtobufCEnumValue *sv = desc->values + i;
  1059. const ProtobufCEnumValue *vv;
  1060. const ProtobufCEnumValue *vn;
  1061. vv = protobuf_c_enum_descriptor_get_value (desc, sv->value);
  1062. vn = protobuf_c_enum_descriptor_get_value_by_name (desc, sv->name);
  1063. assert (sv == vv);
  1064. assert (sv == vn);
  1065. }
  1066. for (i = 0; i < desc->n_value_names; i++)
  1067. {
  1068. const char *name = desc->values_by_name[i].name;
  1069. const ProtobufCEnumValue *v;
  1070. v = protobuf_c_enum_descriptor_get_value_by_name (desc, name);
  1071. assert (v != NULL);
  1072. }
  1073. }
  1074. static void
  1075. test_enum_by_name (const ProtobufCEnumDescriptor *desc,
  1076. const char *name,
  1077. int expected_value)
  1078. {
  1079. const ProtobufCEnumValue *v;
  1080. v = protobuf_c_enum_descriptor_get_value_by_name (desc, name);
  1081. assert (v != NULL);
  1082. assert (v->value == expected_value);
  1083. }
  1084. static void
  1085. test_enum_lookups (void)
  1086. {
  1087. test_enum_descriptor (&foo__test_enum__descriptor);
  1088. test_enum_descriptor (&foo__test_enum_small__descriptor);
  1089. test_enum_descriptor (&foo__test_enum_dup_values__descriptor);
  1090. #define TEST_ENUM_DUP_VALUES(str, shortname) \
  1091. test_enum_by_name (&foo__test_enum_dup_values__descriptor, \
  1092. str, FOO__TEST_ENUM_DUP_VALUES__##shortname)
  1093. TEST_ENUM_DUP_VALUES ("VALUE_A", VALUE_A);
  1094. TEST_ENUM_DUP_VALUES ("VALUE_B", VALUE_B);
  1095. TEST_ENUM_DUP_VALUES ("VALUE_C", VALUE_C);
  1096. TEST_ENUM_DUP_VALUES ("VALUE_D", VALUE_D);
  1097. TEST_ENUM_DUP_VALUES ("VALUE_E", VALUE_E);
  1098. TEST_ENUM_DUP_VALUES ("VALUE_F", VALUE_F);
  1099. TEST_ENUM_DUP_VALUES ("VALUE_AA", VALUE_AA);
  1100. TEST_ENUM_DUP_VALUES ("VALUE_BB", VALUE_BB);
  1101. #undef TEST_ENUM_DUP_VALUES
  1102. }
  1103. static void
  1104. test_message_descriptor (const ProtobufCMessageDescriptor *desc)
  1105. {
  1106. unsigned i;
  1107. for (i = 0; i < desc->n_fields; i++)
  1108. {
  1109. const ProtobufCFieldDescriptor *f = desc->fields + i;
  1110. const ProtobufCFieldDescriptor *fv;
  1111. const ProtobufCFieldDescriptor *fn;
  1112. fv = protobuf_c_message_descriptor_get_field (desc, f->id);
  1113. fn = protobuf_c_message_descriptor_get_field_by_name (desc, f->name);
  1114. assert (f == fv);
  1115. assert (f == fn);
  1116. }
  1117. }
  1118. static void
  1119. test_message_lookups (void)
  1120. {
  1121. test_message_descriptor (&foo__test_mess__descriptor);
  1122. test_message_descriptor (&foo__test_mess_optional__descriptor);
  1123. test_message_descriptor (&foo__test_mess_required_enum__descriptor);
  1124. }
  1125. static void
  1126. assert_required_default_values_are_default (Foo__DefaultRequiredValues *mess)
  1127. {
  1128. assert (mess->v_int32 == -42);
  1129. assert (mess->v_uint32 == 666);
  1130. assert (mess->v_int64 == 100000);
  1131. assert (mess->v_uint64 == 100001);
  1132. assert (mess->v_float == 2.5);
  1133. assert (mess->v_double == 4.5);
  1134. assert (strcmp (mess->v_string, "hi mom\n") == 0);
  1135. assert (mess->v_bytes.len = /* a */ 1
  1136. + /* space */ 1
  1137. + /* NUL */ 1
  1138. + /* space */ 1
  1139. + /* "character" */ 9);
  1140. assert (memcmp (mess->v_bytes.data, "a \0 character", 13) == 0);
  1141. }
  1142. static void
  1143. test_required_default_values (void)
  1144. {
  1145. Foo__DefaultRequiredValues mess = FOO__DEFAULT_REQUIRED_VALUES__INIT;
  1146. Foo__DefaultRequiredValues *mess2;
  1147. size_t len; uint8_t *data;
  1148. assert_required_default_values_are_default (&mess);
  1149. mess2 = test_compare_pack_methods (&mess.base, &len, &data);
  1150. free (data);
  1151. assert_required_default_values_are_default (mess2);
  1152. foo__default_required_values__free_unpacked (mess2, NULL);
  1153. }
  1154. static void
  1155. assert_optional_default_values_are_default (Foo__DefaultOptionalValues *mess)
  1156. {
  1157. assert (!mess->has_v_int32);
  1158. assert (mess->v_int32 == -42);
  1159. assert (!mess->has_v_uint32);
  1160. assert (mess->v_uint32 == 666);
  1161. assert (!mess->has_v_int64);
  1162. assert (mess->v_int64 == 100000);
  1163. assert (!mess->has_v_uint64);
  1164. assert (mess->v_uint64 == 100001);
  1165. assert (!mess->has_v_float);
  1166. assert (mess->v_float == 2.5);
  1167. assert (!mess->has_v_double);
  1168. assert (mess->v_double == 4.5);
  1169. assert (strcmp (mess->v_string, "hi mom\n") == 0);
  1170. assert (!mess->has_v_bytes);
  1171. assert (mess->v_bytes.len = /* a */ 1
  1172. + /* space */ 1
  1173. + /* NUL */ 1
  1174. + /* space */ 1
  1175. + /* "character" */ 9);
  1176. assert (memcmp (mess->v_bytes.data, "a \0 character", 13) == 0);
  1177. }
  1178. static void
  1179. test_optional_default_values (void)
  1180. {
  1181. Foo__DefaultOptionalValues mess = FOO__DEFAULT_OPTIONAL_VALUES__INIT;
  1182. Foo__DefaultOptionalValues *mess2;
  1183. size_t len; uint8_t *data;
  1184. assert_optional_default_values_are_default (&mess);
  1185. mess2 = test_compare_pack_methods (&mess.base, &len, &data);
  1186. assert (len == 0); /* no non-default values */
  1187. free (data);
  1188. assert_optional_default_values_are_default (mess2);
  1189. foo__default_optional_values__free_unpacked (mess2, NULL);
  1190. }
  1191. static struct alloc_data {
  1192. uint32_t alloc_count;
  1193. int32_t allocs_left;
  1194. } test_allocator_data;
  1195. static void *test_alloc(void *allocator_data, size_t size)
  1196. {
  1197. struct alloc_data *ad = allocator_data;
  1198. void *rv = NULL;
  1199. if (ad->allocs_left-- > 0)
  1200. rv = malloc (size);
  1201. /* fprintf (stderr, "alloc %d = %p\n", size, rv); */
  1202. if (rv)
  1203. ad->alloc_count++;
  1204. return rv;
  1205. }
  1206. static void test_free (void *allocator_data, void *data)
  1207. {
  1208. struct alloc_data *ad = allocator_data;
  1209. /* fprintf (stderr, "free %p\n", data); */
  1210. free (data);
  1211. if (data)
  1212. ad->alloc_count--;
  1213. }
  1214. static ProtobufCAllocator test_allocator = {
  1215. test_alloc, test_free, 0, 0, &test_allocator_data
  1216. };
  1217. #define SETUP_TEST_ALLOC_BUFFER(pbuf, len) \
  1218. uint8_t bytes[] = "some bytes", *pbuf; \
  1219. size_t len, _len2; \
  1220. Foo__DefaultRequiredValues _req = FOO__DEFAULT_REQUIRED_VALUES__INIT; \
  1221. Foo__AllocValues _mess = FOO__ALLOC_VALUES__INIT; \
  1222. _mess.a_string = "some string"; \
  1223. _mess.r_string = repeated_strings_2; \
  1224. _mess.n_r_string = sizeof(repeated_strings_2) / sizeof(*repeated_strings_2); \
  1225. _mess.a_bytes.len = sizeof(bytes); \
  1226. _mess.a_bytes.data = bytes; \
  1227. _mess.a_mess = &_req; \
  1228. len = foo__alloc_values__get_packed_size (&_mess); \
  1229. pbuf = malloc (len); \
  1230. assert (pbuf); \
  1231. _len2 = foo__alloc_values__pack (&_mess, pbuf); \
  1232. assert (len == _len2);
  1233. static void
  1234. test_alloc_graceful_cleanup (uint8_t *packed, size_t len, int good_allocs)
  1235. {
  1236. Foo__AllocValues *mess;
  1237. test_allocator_data.alloc_count = 0;
  1238. test_allocator_data.allocs_left = good_allocs;
  1239. mess = foo__alloc_values__unpack (&test_allocator, len, packed);
  1240. assert (test_allocator_data.allocs_left < 0 ? !mess : !!mess);
  1241. if (mess)
  1242. foo__alloc_values__free_unpacked (mess, &test_allocator);
  1243. assert (0 == test_allocator_data.alloc_count);
  1244. }
  1245. static void
  1246. test_alloc_free_all (void)
  1247. {
  1248. SETUP_TEST_ALLOC_BUFFER (packed, len);
  1249. test_alloc_graceful_cleanup (packed, len, INT32_MAX);
  1250. free (packed);
  1251. }
  1252. /* TODO: test alloc failure for slab, unknown fields */
  1253. static void
  1254. test_alloc_fail (void)
  1255. {
  1256. int i = 0;
  1257. SETUP_TEST_ALLOC_BUFFER (packed, len);
  1258. do test_alloc_graceful_cleanup (packed, len, i++);
  1259. while (test_allocator_data.allocs_left < 0);
  1260. free (packed);
  1261. }
  1262. /* === simple testing framework === */
  1263. typedef void (*TestFunc) (void);
  1264. typedef struct {
  1265. const char *name;
  1266. TestFunc func;
  1267. } Test;
  1268. static Test tests[] =
  1269. {
  1270. { "small enums", test_enum_small },
  1271. { "big enums", test_enum_big },
  1272. { "test field numbers", test_field_numbers },
  1273. { "test required int32", test_required_int32 },
  1274. { "test required sint32", test_required_sint32 },
  1275. { "test required sfixed32", test_required_sfixed32 },
  1276. { "test required int64", test_required_int64 },
  1277. { "test required sint64", test_required_sint64 },
  1278. { "test required sfixed64", test_required_sfixed64 },
  1279. { "test required uint32", test_required_uint32 },
  1280. { "test required fixed32", test_required_fixed32 },
  1281. { "test required uint64", test_required_uint64 },
  1282. { "test required fixed64", test_required_fixed64 },
  1283. { "test required float", test_required_float },
  1284. { "test required double", test_required_double },
  1285. { "test required bool", test_required_bool },
  1286. { "test required TestEnumSmall", test_required_TestEnumSmall },
  1287. { "test required TestEnum", test_required_TestEnum },
  1288. { "test required string", test_required_string },
  1289. { "test required bytes", test_required_bytes },
  1290. { "test required SubMess", test_required_SubMess },
  1291. { "test empty optional" ,test_empty_optional },
  1292. { "test optional int32", test_optional_int32 },
  1293. { "test optional sint32", test_optional_sint32 },
  1294. { "test optional sfixed32", test_optional_sfixed32 },
  1295. { "test optional int64", test_optional_int64 },
  1296. { "test optional sint64", test_optional_sint64 },
  1297. { "test optional sfixed64", test_optional_sfixed64 },
  1298. { "test optional uint32", test_optional_uint32 },
  1299. { "test optional fixed32", test_optional_fixed32 },
  1300. { "test optional uint64", test_optional_uint64 },
  1301. { "test optional fixed64", test_optional_fixed6

Large files files are truncated, but you can click here to view the full file