test_generated_code.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /* Test of generated code, with a special focus on features that are not used in
  2. * descriptor.proto or conformance.proto (since these get some testing from
  3. * upb/def.c and tests/conformance_upb.c, respectively).
  4. */
  5. #include "src/google/protobuf/test_messages_proto3.upb.h"
  6. #include "tests/upb_test.h"
  7. #include "tests/test.upb.h"
  8. const char test_str[] = "abcdefg";
  9. const char test_str2[] = "12345678910";
  10. const char test_str3[] = "rstlnezxcvbnm";
  11. const char test_str4[] = "just another test string";
  12. const upb_strview test_str_view = {test_str, sizeof(test_str) - 1};
  13. const upb_strview test_str_view2 = {test_str2, sizeof(test_str2) - 1};
  14. const upb_strview test_str_view3 = {test_str3, sizeof(test_str3) - 1};
  15. const upb_strview test_str_view4 = {test_str4, sizeof(test_str4) - 1};
  16. const int32_t test_int32 = 10;
  17. const int32_t test_int32_2 = -20;
  18. const int32_t test_int32_3 = 30;
  19. const int32_t test_int32_4 = -40;
  20. static void test_scalars() {
  21. upb_arena *arena = upb_arena_new();
  22. protobuf_test_messages_proto3_TestAllTypesProto3 *msg =
  23. protobuf_test_messages_proto3_TestAllTypesProto3_new(arena);
  24. protobuf_test_messages_proto3_TestAllTypesProto3 *msg2;
  25. upb_strview serialized;
  26. protobuf_test_messages_proto3_TestAllTypesProto3_set_optional_int32(msg, 10);
  27. protobuf_test_messages_proto3_TestAllTypesProto3_set_optional_int64(msg, 20);
  28. protobuf_test_messages_proto3_TestAllTypesProto3_set_optional_uint32(msg, 30);
  29. protobuf_test_messages_proto3_TestAllTypesProto3_set_optional_uint64(msg, 40);
  30. protobuf_test_messages_proto3_TestAllTypesProto3_set_optional_float(msg, 50.5);
  31. protobuf_test_messages_proto3_TestAllTypesProto3_set_optional_double(msg, 60.6);
  32. protobuf_test_messages_proto3_TestAllTypesProto3_set_optional_bool(msg, 1);
  33. protobuf_test_messages_proto3_TestAllTypesProto3_set_optional_string(
  34. msg, test_str_view);
  35. serialized.data = protobuf_test_messages_proto3_TestAllTypesProto3_serialize(
  36. msg, arena, &serialized.size);
  37. msg2 = protobuf_test_messages_proto3_TestAllTypesProto3_parse(
  38. serialized.data, serialized.size, arena);
  39. ASSERT(protobuf_test_messages_proto3_TestAllTypesProto3_optional_int32(
  40. msg2) == 10);
  41. ASSERT(protobuf_test_messages_proto3_TestAllTypesProto3_optional_int64(
  42. msg2) == 20);
  43. ASSERT(protobuf_test_messages_proto3_TestAllTypesProto3_optional_uint32(
  44. msg2) == 30);
  45. ASSERT(protobuf_test_messages_proto3_TestAllTypesProto3_optional_uint64(
  46. msg2) == 40);
  47. ASSERT(protobuf_test_messages_proto3_TestAllTypesProto3_optional_float(
  48. msg2) == 50.5);
  49. ASSERT(protobuf_test_messages_proto3_TestAllTypesProto3_optional_double(
  50. msg2) == 60.6);
  51. ASSERT(protobuf_test_messages_proto3_TestAllTypesProto3_optional_bool(
  52. msg2) == 1);
  53. ASSERT(upb_strview_eql(
  54. protobuf_test_messages_proto3_TestAllTypesProto3_optional_string(msg2),
  55. test_str_view));
  56. upb_arena_free(arena);
  57. }
  58. static void check_string_map_empty(
  59. protobuf_test_messages_proto3_TestAllTypesProto3 *msg) {
  60. size_t iter;
  61. ASSERT(
  62. protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_size(
  63. msg) == 0);
  64. ASSERT(
  65. !protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_next(
  66. msg, &iter));
  67. }
  68. static void check_string_map_one_entry(
  69. protobuf_test_messages_proto3_TestAllTypesProto3 *msg) {
  70. const protobuf_test_messages_proto3_TestAllTypesProto3_MapStringStringEntry
  71. *const_ent;
  72. size_t iter;
  73. upb_strview str;
  74. ASSERT(
  75. protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_size(
  76. msg) == 1);
  77. ASSERT(protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_get(
  78. msg, test_str_view, &str));
  79. ASSERT(upb_strview_eql(str, test_str_view2));
  80. ASSERT(
  81. !protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_get(
  82. msg, test_str_view3, &str));
  83. /* Test that iteration reveals a single k/v pair in the map. */
  84. iter = UPB_MAP_BEGIN;
  85. const_ent = protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_next(
  86. msg, &iter);
  87. ASSERT(const_ent);
  88. ASSERT(upb_strview_eql(
  89. test_str_view,
  90. protobuf_test_messages_proto3_TestAllTypesProto3_MapStringStringEntry_key(
  91. const_ent)));
  92. ASSERT(upb_strview_eql(
  93. test_str_view2,
  94. protobuf_test_messages_proto3_TestAllTypesProto3_MapStringStringEntry_value(
  95. const_ent)));
  96. const_ent = protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_next(
  97. msg, &iter);
  98. ASSERT(!const_ent);
  99. }
  100. static void test_string_double_map() {
  101. upb_arena *arena = upb_arena_new();
  102. upb_strview serialized;
  103. upb_test_MapTest *msg = upb_test_MapTest_new(arena);
  104. upb_test_MapTest *msg2;
  105. double val;
  106. upb_test_MapTest_map_string_double_set(msg, test_str_view, 1.5, arena);
  107. serialized.data = upb_test_MapTest_serialize(msg, arena, &serialized.size);
  108. ASSERT(serialized.data);
  109. msg2 = upb_test_MapTest_parse(serialized.data, serialized.size, arena);
  110. ASSERT(msg2);
  111. ASSERT(upb_test_MapTest_map_string_double_get(msg2, test_str_view, &val));
  112. ASSERT(val == 1.5);
  113. upb_arena_free(arena);
  114. }
  115. static void test_string_map() {
  116. upb_arena *arena = upb_arena_new();
  117. protobuf_test_messages_proto3_TestAllTypesProto3 *msg =
  118. protobuf_test_messages_proto3_TestAllTypesProto3_new(arena);
  119. const protobuf_test_messages_proto3_TestAllTypesProto3_MapStringStringEntry
  120. *const_ent;
  121. size_t iter, count;
  122. check_string_map_empty(msg);
  123. /* Set map[test_str_view] = test_str_view2 */
  124. protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_set(
  125. msg, test_str_view, test_str_view2, arena);
  126. check_string_map_one_entry(msg);
  127. /* Deleting a non-existent key does nothing. */
  128. ASSERT(
  129. !protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_delete(
  130. msg, test_str_view3));
  131. check_string_map_one_entry(msg);
  132. /* Deleting the key sets the map back to empty. */
  133. ASSERT(
  134. protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_delete(
  135. msg, test_str_view));
  136. check_string_map_empty(msg);
  137. /* Set two keys this time:
  138. * map[test_str_view] = test_str_view2
  139. * map[test_str_view3] = test_str_view4
  140. */
  141. protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_set(
  142. msg, test_str_view, test_str_view2, arena);
  143. protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_set(
  144. msg, test_str_view3, test_str_view4, arena);
  145. /* Test iteration */
  146. iter = UPB_MAP_BEGIN;
  147. count = 0;
  148. while (
  149. (const_ent =
  150. protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_next(
  151. msg, &iter)) != NULL) {
  152. upb_strview key =
  153. protobuf_test_messages_proto3_TestAllTypesProto3_MapStringStringEntry_key(
  154. const_ent);
  155. upb_strview val =
  156. protobuf_test_messages_proto3_TestAllTypesProto3_MapStringStringEntry_value(
  157. const_ent);
  158. count++;
  159. if (upb_strview_eql(key, test_str_view)) {
  160. ASSERT(upb_strview_eql(val, test_str_view2));
  161. } else {
  162. ASSERT(upb_strview_eql(key, test_str_view3));
  163. ASSERT(upb_strview_eql(val, test_str_view4));
  164. }
  165. }
  166. ASSERT(count == 2);
  167. /* Clearing the map goes back to empty. */
  168. protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_clear(msg);
  169. check_string_map_empty(msg);
  170. upb_arena_free(arena);
  171. }
  172. static void check_int32_map_empty(
  173. protobuf_test_messages_proto3_TestAllTypesProto3 *msg) {
  174. size_t iter;
  175. ASSERT(
  176. protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_size(
  177. msg) == 0);
  178. ASSERT(
  179. !protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_next(
  180. msg, &iter));
  181. }
  182. static void check_int32_map_one_entry(
  183. protobuf_test_messages_proto3_TestAllTypesProto3 *msg) {
  184. const protobuf_test_messages_proto3_TestAllTypesProto3_MapInt32Int32Entry
  185. *const_ent;
  186. size_t iter;
  187. int32_t val;
  188. ASSERT(
  189. protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_size(
  190. msg) == 1);
  191. ASSERT(protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_get(
  192. msg, test_int32, &val));
  193. ASSERT(val == test_int32_2);
  194. ASSERT(
  195. !protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_get(
  196. msg, test_int32_3, &val));
  197. /* Test that iteration reveals a single k/v pair in the map. */
  198. iter = UPB_MAP_BEGIN;
  199. const_ent = protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_next(
  200. msg, &iter);
  201. ASSERT(const_ent);
  202. ASSERT(
  203. test_int32 ==
  204. protobuf_test_messages_proto3_TestAllTypesProto3_MapInt32Int32Entry_key(
  205. const_ent));
  206. ASSERT(
  207. test_int32_2 ==
  208. protobuf_test_messages_proto3_TestAllTypesProto3_MapInt32Int32Entry_value(
  209. const_ent));
  210. const_ent = protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_next(
  211. msg, &iter);
  212. ASSERT(!const_ent);
  213. }
  214. static void test_int32_map() {
  215. upb_arena *arena = upb_arena_new();
  216. protobuf_test_messages_proto3_TestAllTypesProto3 *msg =
  217. protobuf_test_messages_proto3_TestAllTypesProto3_new(arena);
  218. const protobuf_test_messages_proto3_TestAllTypesProto3_MapInt32Int32Entry
  219. *const_ent;
  220. size_t iter, count;
  221. check_int32_map_empty(msg);
  222. /* Set map[test_int32] = test_int32_2 */
  223. protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_set(
  224. msg, test_int32, test_int32_2, arena);
  225. check_int32_map_one_entry(msg);
  226. /* Deleting a non-existent key does nothing. */
  227. ASSERT(
  228. !protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_delete(
  229. msg, test_int32_3));
  230. check_int32_map_one_entry(msg);
  231. /* Deleting the key sets the map back to empty. */
  232. ASSERT(
  233. protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_delete(
  234. msg, test_int32));
  235. check_int32_map_empty(msg);
  236. /* Set two keys this time:
  237. * map[test_int32] = test_int32_2
  238. * map[test_int32_3] = test_int32_4
  239. */
  240. protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_set(
  241. msg, test_int32, test_int32_2, arena);
  242. protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_set(
  243. msg, test_int32_3, test_int32_4, arena);
  244. /* Test iteration */
  245. iter = UPB_MAP_BEGIN;
  246. count = 0;
  247. while (
  248. (const_ent =
  249. protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_next(
  250. msg, &iter)) != NULL) {
  251. int32_t key =
  252. protobuf_test_messages_proto3_TestAllTypesProto3_MapInt32Int32Entry_key(
  253. const_ent);
  254. int32_t val =
  255. protobuf_test_messages_proto3_TestAllTypesProto3_MapInt32Int32Entry_value(
  256. const_ent);
  257. count++;
  258. if (key == test_int32) {
  259. ASSERT(val == test_int32_2);
  260. } else {
  261. ASSERT(key == test_int32_3);
  262. ASSERT(val == test_int32_4);
  263. }
  264. }
  265. ASSERT(count == 2);
  266. /* Clearing the map goes back to empty. */
  267. protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_clear(msg);
  268. check_int32_map_empty(msg);
  269. upb_arena_free(arena);
  270. }
  271. void test_repeated() {
  272. upb_arena *arena = upb_arena_new();
  273. protobuf_test_messages_proto3_TestAllTypesProto3 *msg =
  274. protobuf_test_messages_proto3_TestAllTypesProto3_new(arena);
  275. size_t size;
  276. const int *elems;
  277. protobuf_test_messages_proto3_TestAllTypesProto3_add_repeated_int32(
  278. msg, 5, arena);
  279. elems = protobuf_test_messages_proto3_TestAllTypesProto3_repeated_int32(
  280. msg, &size);
  281. ASSERT(size == 1);
  282. ASSERT(elems[0] == 5);
  283. upb_arena_free(arena);
  284. }
  285. int run_tests(int argc, char *argv[]) {
  286. test_scalars();
  287. test_string_map();
  288. test_string_double_map();
  289. test_int32_map();
  290. test_repeated();
  291. return 0;
  292. }