summaryrefslogtreecommitdiff
path: root/tests/tests.c
blob: 99e13cb0f645b02e5cd0623c4c7a1a87f799a65e (plain)
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


#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "upb/def.h"
#include "upb/handlers.h"
#include "upb/pb/decoder.h"
#include "upb/pb/glue.h"
#include "upb_test.h"

static upb_symtab *load_test_proto() {
  upb_symtab *s = upb_symtab_new();
  ASSERT(s);
  upb_status status = UPB_STATUS_INIT;
  if (!upb_load_descriptor_file_into_symtab(s, "tests/test.proto.pb", &status)) {
    fprintf(stderr, "Error loading descriptor file: %s\n", upb_status_getstr(&status));
    exit(1);
  }
  upb_status_uninit(&status);
  return s;
}

static upb_flow_t upb_test_onvalue(void *closure, upb_value fval, upb_value val) {
  (void)closure;
  (void)fval;
  (void)val;
  return UPB_CONTINUE;
}

static void test_upb_jit() {
  upb_symtab *s = load_test_proto();
  const upb_def *def = upb_symtab_lookup(s, "SimplePrimitives");
  ASSERT(def);

  upb_handlers *h = upb_handlers_new();
  upb_handlerset hset = {NULL, NULL, &upb_test_onvalue, NULL, NULL, NULL, NULL};
  upb_handlers_reghandlerset(h, upb_downcast_msgdef_const(def), &hset);
  upb_decoder d;
  upb_decoder_init(&d, h);
  upb_decoder_uninit(&d);
  upb_symtab_unref(s);
  upb_def_unref(def);
  upb_handlers_unref(h);
}

static void test_upb_symtab() {
  upb_symtab *s = load_test_proto();

  // Test cycle detection by making a cyclic def's main refcount go to zero
  // and then be incremented to one again.
  const upb_def *def = upb_symtab_lookup(s, "A");
  ASSERT(def);
  upb_symtab_unref(s);
  const upb_msgdef *m = upb_downcast_msgdef_const(def);
  upb_msg_iter i = upb_msg_begin(m);
  upb_fielddef *f = upb_msg_iter_field(i);
  ASSERT(upb_hassubdef(f));
  upb_def *def2 = f->def;

  i = upb_msg_next(m, i);
  ASSERT(upb_msg_done(i));  // "A" should only have one field.

  ASSERT(upb_downcast_msgdef(def2));
  upb_def_ref(def2);
  upb_def_unref(def);
  upb_def_unref(def2);
}

int main()
{
#define TEST(func) do { \
  int assertions_before = num_assertions; \
  printf("Running " #func "..."); fflush(stdout); \
  func(); \
  printf("ok (%d assertions).\n", num_assertions - assertions_before); \
  } while (0)

  TEST(test_upb_symtab);
  TEST(test_upb_jit);
  printf("All tests passed (%d assertions).\n", num_assertions);
  return 0;
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback