1 // Copyright (c) 2009-2021, Google LLC
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above copyright
9 //       notice, this list of conditions and the following disclaimer in the
10 //       documentation and/or other materials provided with the distribution.
11 //     * Neither the name of Google LLC nor the
12 //       names of its contributors may be used to endorse or promote products
13 //       derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 /*
27  * Tests for C++ wrappers.
28  */
29 
30 #include <stdio.h>
31 #include <string.h>
32 
33 #include <fstream>
34 #include <iostream>
35 #include <set>
36 #include <sstream>
37 
38 #include "tests/test_cpp.upbdefs.h"
39 #include "tests/test_cpp.upb.h"
40 #include "tests/upb_test.h"
41 #include "upb/def.h"
42 #include "upb/def.hpp"
43 #include "upb/json_decode.h"
44 #include "upb/json_encode.h"
45 #include "upb/upb.h"
46 
47 // Must be last.
48 #include "upb/port_def.inc"
49 
TestIteration()50 void TestIteration() {
51   upb::SymbolTable symtab;
52   upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(symtab.ptr()));
53 
54   // Test range-based for on both fields and oneofs (with the iterator adaptor).
55   int field_count = 0;
56   for (auto field : md.fields()) {
57     UPB_UNUSED(field);
58     field_count++;
59   }
60   ASSERT(field_count == md.field_count());
61 
62   int oneof_count = 0;
63   for (auto oneof : md.oneofs()) {
64     UPB_UNUSED(oneof);
65     oneof_count++;
66   }
67   ASSERT(oneof_count == md.oneof_count());
68 }
69 
TestArena()70 void TestArena() {
71   int n = 100000;
72 
73   struct Decrementer {
74     Decrementer(int* _p) : p(_p) {}
75     ~Decrementer() { (*p)--; }
76     int* p;
77   };
78 
79   {
80     upb::Arena arena;
81     for (int i = 0; i < n; i++) {
82       arena.Own(new Decrementer(&n));
83 
84       // Intersperse allocation and ensure we can write to it.
85       int* val = static_cast<int*>(upb_arena_malloc(arena.ptr(), sizeof(int)));
86       *val = i;
87     }
88 
89     // Test a large allocation.
90     upb_arena_malloc(arena.ptr(), 1000000);
91   }
92   ASSERT(n == 0);
93 
94   {
95     // Test fuse.
96     upb::Arena arena1;
97     upb::Arena arena2;
98 
99     arena1.Fuse(arena2);
100 
101     upb_arena_malloc(arena1.ptr(), 10000);
102     upb_arena_malloc(arena2.ptr(), 10000);
103   }
104 }
105 
TestInlinedArena()106 void TestInlinedArena() {
107   int n = 100000;
108 
109   struct Decrementer {
110     Decrementer(int* _p) : p(_p) {}
111     ~Decrementer() { (*p)--; }
112     int* p;
113   };
114 
115   {
116     upb::InlinedArena<1024> arena;
117     for (int i = 0; i < n; i++) {
118       arena.Own(new Decrementer(&n));
119 
120       // Intersperse allocation and ensure we can write to it.
121       int* val = static_cast<int*>(upb_arena_malloc(arena.ptr(), sizeof(int)));
122       *val = i;
123     }
124 
125     // Test a large allocation.
126     upb_arena_malloc(arena.ptr(), 1000000);
127   }
128   ASSERT(n == 0);
129 }
130 
TestDefault()131 void TestDefault() {
132   upb::SymbolTable symtab;
133   upb::Arena arena;
134   upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(symtab.ptr()));
135   upb_test_TestMessage *msg = upb_test_TestMessage_new(arena.ptr());
136   size_t size = upb_json_encode(msg, md.ptr(), NULL, 0, NULL, 0, NULL);
137   ASSERT(size == 2);  // "{}"
138 }
139 
TestJsonNull()140 void TestJsonNull() {
141   upb::SymbolTable symtab;
142   upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(symtab.ptr()));
143   upb::FieldDefPtr i32_f = md.FindFieldByName("i32");
144   upb::FieldDefPtr str_f = md.FindFieldByName("str");
145   ASSERT(i32_f && str_f);
146   ASSERT(i32_f.default_value().int32_val == 5);
147   ASSERT(strcmp(str_f.default_value().str_val.data, "abc") == 0);
148   ASSERT(str_f.default_value().str_val.size == 3);
149 }
150 
151 extern "C" {
152 
run_tests()153 int run_tests() {
154   TestIteration();
155   TestArena();
156   TestDefault();
157 
158   return 0;
159 }
160 
161 }
162