File: test_generated_code.c

package info (click to toggle)
upb 0.0.0~git200730-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 1,696 kB
  • sloc: ansic: 16,828; cpp: 5,098; python: 360; pascal: 160; ruby: 21; sh: 19; makefile: 6
file content (400 lines) | stat: -rw-r--r-- 13,078 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/* Test of generated code, with a special focus on features that are not used in
 * descriptor.proto or conformance.proto (since these get some testing from
 * upb/def.c and tests/conformance_upb.c, respectively).
 */

#include "src/google/protobuf/test_messages_proto3.upb.h"
#include "tests/upb_test.h"
#include "tests/test.upb.h"

#define MIN(x, y) ((x) < (y) ? (x) : (y))

const char test_str[] = "abcdefg";
const char test_str2[] = "12345678910";
const char test_str3[] = "rstlnezxcvbnm";
const char test_str4[] = "just another test string";

const upb_strview test_str_view = {test_str, sizeof(test_str) - 1};
const upb_strview test_str_view2 = {test_str2, sizeof(test_str2) - 1};
const upb_strview test_str_view3 = {test_str3, sizeof(test_str3) - 1};
const upb_strview test_str_view4 = {test_str4, sizeof(test_str4) - 1};

const int32_t test_int32 = 10;
const int32_t test_int32_2 = -20;
const int32_t test_int32_3 = 30;
const int32_t test_int32_4 = -40;

static void test_scalars() {
  upb_arena *arena = upb_arena_new();
  protobuf_test_messages_proto3_TestAllTypesProto3 *msg =
      protobuf_test_messages_proto3_TestAllTypesProto3_new(arena);
  protobuf_test_messages_proto3_TestAllTypesProto3 *msg2;
  upb_strview serialized;

  protobuf_test_messages_proto3_TestAllTypesProto3_set_optional_int32(msg, 10);
  protobuf_test_messages_proto3_TestAllTypesProto3_set_optional_int64(msg, 20);
  protobuf_test_messages_proto3_TestAllTypesProto3_set_optional_uint32(msg, 30);
  protobuf_test_messages_proto3_TestAllTypesProto3_set_optional_uint64(msg, 40);
  protobuf_test_messages_proto3_TestAllTypesProto3_set_optional_float(msg, 50.5);
  protobuf_test_messages_proto3_TestAllTypesProto3_set_optional_double(msg, 60.6);
  protobuf_test_messages_proto3_TestAllTypesProto3_set_optional_bool(msg, 1);
  protobuf_test_messages_proto3_TestAllTypesProto3_set_optional_string(
      msg, test_str_view);

  serialized.data = protobuf_test_messages_proto3_TestAllTypesProto3_serialize(
      msg, arena, &serialized.size);

  msg2 = protobuf_test_messages_proto3_TestAllTypesProto3_parse(
      serialized.data, serialized.size, arena);

  ASSERT(protobuf_test_messages_proto3_TestAllTypesProto3_optional_int32(
             msg2) == 10);
  ASSERT(protobuf_test_messages_proto3_TestAllTypesProto3_optional_int64(
             msg2) == 20);
  ASSERT(protobuf_test_messages_proto3_TestAllTypesProto3_optional_uint32(
             msg2) == 30);
  ASSERT(protobuf_test_messages_proto3_TestAllTypesProto3_optional_uint64(
             msg2) == 40);
  ASSERT(protobuf_test_messages_proto3_TestAllTypesProto3_optional_float(
             msg2) == 50.5);
  ASSERT(protobuf_test_messages_proto3_TestAllTypesProto3_optional_double(
             msg2) == 60.6);
  ASSERT(protobuf_test_messages_proto3_TestAllTypesProto3_optional_bool(
             msg2) == 1);
  ASSERT(upb_strview_eql(
      protobuf_test_messages_proto3_TestAllTypesProto3_optional_string(msg2),
      test_str_view));

  upb_arena_free(arena);
}

static void check_string_map_empty(
    protobuf_test_messages_proto3_TestAllTypesProto3 *msg) {
  size_t iter = UPB_MAP_BEGIN;

  ASSERT(
      protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_size(
          msg) == 0);
  ASSERT(
      !protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_next(
          msg, &iter));
}

static void check_string_map_one_entry(
    protobuf_test_messages_proto3_TestAllTypesProto3 *msg) {
  const protobuf_test_messages_proto3_TestAllTypesProto3_MapStringStringEntry
      *const_ent;
  size_t iter;
  upb_strview str;

  ASSERT(
      protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_size(
          msg) == 1);
  ASSERT(protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_get(
      msg, test_str_view, &str));
  ASSERT(upb_strview_eql(str, test_str_view2));

  ASSERT(
      !protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_get(
          msg, test_str_view3, &str));

  /* Test that iteration reveals a single k/v pair in the map. */
  iter = UPB_MAP_BEGIN;
  const_ent = protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_next(
      msg, &iter);
  ASSERT(const_ent);
  ASSERT(upb_strview_eql(
      test_str_view,
      protobuf_test_messages_proto3_TestAllTypesProto3_MapStringStringEntry_key(
          const_ent)));
  ASSERT(upb_strview_eql(
      test_str_view2,
      protobuf_test_messages_proto3_TestAllTypesProto3_MapStringStringEntry_value(
          const_ent)));

  const_ent = protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_next(
      msg, &iter);
  ASSERT(!const_ent);
}

static void test_string_double_map() {
  upb_arena *arena = upb_arena_new();
  upb_strview serialized;
  upb_test_MapTest *msg = upb_test_MapTest_new(arena);
  upb_test_MapTest *msg2;
  double val;

  upb_test_MapTest_map_string_double_set(msg, test_str_view, 1.5, arena);
  ASSERT(msg);
  ASSERT(upb_test_MapTest_map_string_double_get(msg, test_str_view, &val));
  ASSERT(val == 1.5);
  val = 0;

  serialized.data = upb_test_MapTest_serialize(msg, arena, &serialized.size);
  ASSERT(serialized.data);

  msg2 = upb_test_MapTest_parse(serialized.data, serialized.size, arena);
  ASSERT(msg2);
  ASSERT(upb_test_MapTest_map_string_double_get(msg2, test_str_view, &val));
  ASSERT(val == 1.5);

  upb_arena_free(arena);
}

static void test_string_map() {
  upb_arena *arena = upb_arena_new();
  protobuf_test_messages_proto3_TestAllTypesProto3 *msg =
      protobuf_test_messages_proto3_TestAllTypesProto3_new(arena);
  const protobuf_test_messages_proto3_TestAllTypesProto3_MapStringStringEntry
      *const_ent;
  size_t iter, count;

  check_string_map_empty(msg);

  /* Set map[test_str_view] = test_str_view2 */
  protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_set(
      msg, test_str_view, test_str_view2, arena);
  check_string_map_one_entry(msg);

  /* Deleting a non-existent key does nothing. */
  ASSERT(
      !protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_delete(
          msg, test_str_view3));
  check_string_map_one_entry(msg);

  /* Deleting the key sets the map back to empty. */
  ASSERT(
      protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_delete(
          msg, test_str_view));
  check_string_map_empty(msg);

  /* Set two keys this time:
   *   map[test_str_view] = test_str_view2
   *   map[test_str_view3] = test_str_view4
   */
  protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_set(
      msg, test_str_view, test_str_view2, arena);
  protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_set(
      msg, test_str_view3, test_str_view4, arena);

  /* Test iteration */
  iter = UPB_MAP_BEGIN;
  count = 0;

  while (
      (const_ent =
           protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_next(
               msg, &iter)) != NULL) {
    upb_strview key =
        protobuf_test_messages_proto3_TestAllTypesProto3_MapStringStringEntry_key(
            const_ent);
    upb_strview val =
        protobuf_test_messages_proto3_TestAllTypesProto3_MapStringStringEntry_value(
            const_ent);

    count++;
    if (upb_strview_eql(key, test_str_view)) {
      ASSERT(upb_strview_eql(val, test_str_view2));
    } else {
      ASSERT(upb_strview_eql(key, test_str_view3));
      ASSERT(upb_strview_eql(val, test_str_view4));
    }
  }

  ASSERT(count == 2);

  /* Clearing the map goes back to empty. */
  protobuf_test_messages_proto3_TestAllTypesProto3_map_string_string_clear(msg);
  check_string_map_empty(msg);

  upb_arena_free(arena);
}

static void check_int32_map_empty(
    protobuf_test_messages_proto3_TestAllTypesProto3 *msg) {
  size_t iter = UPB_MAP_BEGIN;

  ASSERT(
      protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_size(
          msg) == 0);
  ASSERT(
      !protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_next(
          msg, &iter));
}

static void check_int32_map_one_entry(
    protobuf_test_messages_proto3_TestAllTypesProto3 *msg) {
  const protobuf_test_messages_proto3_TestAllTypesProto3_MapInt32Int32Entry
      *const_ent;
  size_t iter;
  int32_t val;

  ASSERT(
      protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_size(
          msg) == 1);
  ASSERT(protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_get(
      msg, test_int32, &val));
  ASSERT(val == test_int32_2);

  ASSERT(
      !protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_get(
          msg, test_int32_3, &val));

  /* Test that iteration reveals a single k/v pair in the map. */
  iter = UPB_MAP_BEGIN;
  const_ent = protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_next(
      msg, &iter);
  ASSERT(const_ent);
  ASSERT(
      test_int32 ==
      protobuf_test_messages_proto3_TestAllTypesProto3_MapInt32Int32Entry_key(
          const_ent));
  ASSERT(
      test_int32_2 ==
      protobuf_test_messages_proto3_TestAllTypesProto3_MapInt32Int32Entry_value(
          const_ent));

  const_ent = protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_next(
      msg, &iter);
  ASSERT(!const_ent);
}

static void test_int32_map() {
  upb_arena *arena = upb_arena_new();
  protobuf_test_messages_proto3_TestAllTypesProto3 *msg =
      protobuf_test_messages_proto3_TestAllTypesProto3_new(arena);
  const protobuf_test_messages_proto3_TestAllTypesProto3_MapInt32Int32Entry
      *const_ent;
  size_t iter, count;

  check_int32_map_empty(msg);

  /* Set map[test_int32] = test_int32_2 */
  protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_set(
      msg, test_int32, test_int32_2, arena);
  check_int32_map_one_entry(msg);

  /* Deleting a non-existent key does nothing. */
  ASSERT(
      !protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_delete(
          msg, test_int32_3));
  check_int32_map_one_entry(msg);

  /* Deleting the key sets the map back to empty. */
  ASSERT(
      protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_delete(
          msg, test_int32));
  check_int32_map_empty(msg);

  /* Set two keys this time:
   *   map[test_int32] = test_int32_2
   *   map[test_int32_3] = test_int32_4
   */
  protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_set(
      msg, test_int32, test_int32_2, arena);
  protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_set(
      msg, test_int32_3, test_int32_4, arena);

  /* Test iteration */
  iter = UPB_MAP_BEGIN;
  count = 0;

  while (
      (const_ent =
           protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_next(
               msg, &iter)) != NULL) {
    int32_t key =
        protobuf_test_messages_proto3_TestAllTypesProto3_MapInt32Int32Entry_key(
            const_ent);
    int32_t val =
        protobuf_test_messages_proto3_TestAllTypesProto3_MapInt32Int32Entry_value(
            const_ent);

    count++;
    if (key == test_int32) {
      ASSERT(val == test_int32_2);
    } else {
      ASSERT(key == test_int32_3);
      ASSERT(val == test_int32_4);
    }
  }

  ASSERT(count == 2);

  /* Clearing the map goes back to empty. */
  protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_clear(msg);
  check_int32_map_empty(msg);

  upb_arena_free(arena);
}

void test_repeated() {
  upb_arena *arena = upb_arena_new();
  protobuf_test_messages_proto3_TestAllTypesProto3 *msg =
      protobuf_test_messages_proto3_TestAllTypesProto3_new(arena);
  size_t size;
  const int *elems;

  protobuf_test_messages_proto3_TestAllTypesProto3_add_repeated_int32(
      msg, 5, arena);

  elems = protobuf_test_messages_proto3_TestAllTypesProto3_repeated_int32(
      msg, &size);

  ASSERT(size == 1);
  ASSERT(elems[0] == 5);

  upb_arena_free(arena);
}

void test_null_decode_buf() {
  upb_arena *arena = upb_arena_new();
  protobuf_test_messages_proto3_TestAllTypesProto3 *msg =
      protobuf_test_messages_proto3_TestAllTypesProto3_parse(NULL, 0, arena);
  size_t size;

  ASSERT(msg);
  protobuf_test_messages_proto3_TestAllTypesProto3_serialize(msg, arena, &size);
  ASSERT(size == 0);
  upb_arena_free(arena);
}

void test_status_truncation() {
  int i, j;
  upb_status status;
  upb_status status2;
  for (i = 0; i < UPB_STATUS_MAX_MESSAGE + 20; i++) {
    char *msg = malloc(i + 1);
    int end;
    char ch = (i % 96) + 33;  /* Cycle through printable chars. */

    for (j = 0; j < i; j++) {
      msg[j] = ch;
    }
    msg[i] = '\0';

    upb_status_seterrmsg(&status, msg);
    upb_status_seterrf(&status2, "%s", msg);
    end = MIN(i, UPB_STATUS_MAX_MESSAGE - 1);
    ASSERT(strlen(status.msg) == end);
    ASSERT(strlen(status2.msg) == end);

    for (j = 0; j < end; j++) {
      ASSERT(status.msg[j] == ch);
      ASSERT(status2.msg[j] == ch);
    }

    free(msg);
  }
}

int run_tests(int argc, char *argv[]) {
  test_scalars();
  test_string_map();
  test_string_double_map();
  test_int32_map();
  test_repeated();
  test_null_decode_buf();
  test_status_truncation();
  return 0;
}