test_cpp.cc
Go to the documentation of this file.
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"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 // ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
19 // 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 
40 #include "gtest/gtest.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/test_cpp.upb.h"
46 #include "upb/test_cpp.upbdefs.h"
47 #include "upb/upb.h"
48 
49 // Must be last.
50 #include "upb/port_def.inc"
51 
52 TEST(Cpp, Iteration) {
54  upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(symtab.ptr()));
55 
56  // Test range-based for on both fields and oneofs (with the iterator adaptor).
57  int field_count = 0;
58  for (auto field : md.fields()) {
60  field_count++;
61  }
62  EXPECT_EQ(field_count, md.field_count());
63 
64  int oneof_count = 0;
65  for (auto oneof : md.oneofs()) {
66  UPB_UNUSED(oneof);
67  oneof_count++;
68  }
69  EXPECT_EQ(oneof_count, md.oneof_count());
70 }
71 
72 TEST(Cpp, Arena) {
73  int n = 100000;
74 
75  struct Decrementer {
76  Decrementer(int* _p) : p(_p) {}
77  ~Decrementer() { (*p)--; }
78  int* p;
79  };
80 
81  {
83  for (int i = 0; i < n; i++) {
84  arena.Own(new Decrementer(&n));
85 
86  // Intersperse allocation and ensure we can write to it.
87  int* val = static_cast<int*>(upb_Arena_Malloc(arena.ptr(), sizeof(int)));
88  *val = i;
89  }
90 
91  // Test a large allocation.
92  upb_Arena_Malloc(arena.ptr(), 1000000);
93  }
94  EXPECT_EQ(0, n);
95 
96  {
97  // Test fuse.
98  upb::Arena arena1;
99  upb::Arena arena2;
100 
101  arena1.Fuse(arena2);
102 
103  upb_Arena_Malloc(arena1.ptr(), 10000);
104  upb_Arena_Malloc(arena2.ptr(), 10000);
105  }
106 }
107 
108 TEST(Cpp, InlinedArena) {
109  int n = 100000;
110 
111  struct Decrementer {
112  Decrementer(int* _p) : p(_p) {}
113  ~Decrementer() { (*p)--; }
114  int* p;
115  };
116 
117  {
119  for (int i = 0; i < n; i++) {
120  arena.Own(new Decrementer(&n));
121 
122  // Intersperse allocation and ensure we can write to it.
123  int* val = static_cast<int*>(upb_Arena_Malloc(arena.ptr(), sizeof(int)));
124  *val = i;
125  }
126 
127  // Test a large allocation.
128  upb_Arena_Malloc(arena.ptr(), 1000000);
129  }
130  EXPECT_EQ(0, n);
131 }
132 
133 TEST(Cpp, Default) {
136  upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(symtab.ptr()));
137  upb_test_TestMessage* msg = upb_test_TestMessage_new(arena.ptr());
138  size_t size = upb_JsonEncode(msg, md.ptr(), NULL, 0, NULL, 0, NULL);
139  EXPECT_EQ(2, size); // "{}"
140 }
141 
142 TEST(Cpp, JsonNull) {
144  upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(symtab.ptr()));
145  upb::FieldDefPtr i32_f = md.FindFieldByName("i32");
146  upb::FieldDefPtr str_f = md.FindFieldByName("str");
147  ASSERT_TRUE(i32_f);
148  ASSERT_TRUE(str_f);
149  EXPECT_EQ(5, i32_f.default_value().int32_val);
150  EXPECT_EQ(0, strcmp(str_f.default_value().str_val.data, "abc"));
151  EXPECT_EQ(3, str_f.default_value().str_val.size);
152 }
153 
154 TEST(Cpp, TimestampEncoder) {
158  google_protobuf_Timestamp* timestamp_upb =
160  google_protobuf_Timestamp* timestamp_upb_decoded =
162 
163  long timestamps[] = {
164  253402300799, // 9999-12-31T23:59:59Z
165  1641006000, // 2022-01-01T03:00:00Z
166  0, // 1970-01-01T00:00:00Z
167  -31525200, // 1969-01-01T03:00:00Z
168  -2208988800, // 1900-01-01T00:00:00Z
169  -62135596800, // 0000-01-01T00:00:00Z
170  };
171 
172  for (long timestamp : timestamps) {
173  google_protobuf_Timestamp_set_seconds(timestamp_upb, timestamp);
174 
175  char json[128];
176  size_t size = upb_JsonEncode(timestamp_upb, md.ptr(), NULL, 0, json,
177  sizeof(json), NULL);
178  bool result = upb_JsonDecode(json, size, timestamp_upb_decoded, md.ptr(),
179  NULL, 0, arena.ptr(), NULL);
180  const long timestamp_decoded =
181  google_protobuf_Timestamp_seconds(timestamp_upb_decoded);
182 
184  EXPECT_EQ(timestamp, timestamp_decoded);
185  }
186 }
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
google_protobuf_Timestamp_seconds
UPB_INLINE int64_t google_protobuf_Timestamp_seconds(const google_protobuf_Timestamp *msg)
Definition: timestamp.upb.h:63
upb::SymbolTable
Definition: def.hpp:377
upb::MessageDefPtr
Definition: def.hpp:168
google_protobuf_Timestamp_set_seconds
UPB_INLINE void google_protobuf_Timestamp_set_seconds(google_protobuf_Timestamp *msg, int64_t value)
Definition: timestamp.upb.h:73
TEST
TEST(Cpp, Iteration)
Definition: test_cpp.cc:52
upb_StringView::data
const char * data
Definition: upb/upb/upb.h:73
string.h
Arena
Definition: arena.c:39
upb::FieldDefPtr
Definition: def.hpp:50
upb_MessageValue::str_val
upb_StringView str_val
Definition: upb/upb/reflection.h:51
upb_JsonEncode
size_t upb_JsonEncode(const upb_Message *msg, const upb_MessageDef *m, const upb_DefPool *ext_pool, int options, char *buf, size_t size, upb_Status *status)
Definition: json_encode.c:757
upb_MessageValue::int32_val
int32_t int32_val
Definition: upb/upb/reflection.h:44
xds_manager.p
p
Definition: xds_manager.py:60
def.hpp
json_encode.h
arena
grpc_core::ScopedArenaPtr arena
Definition: binder_transport_test.cc:237
json_decode.h
google::protobuf.descriptor_pool.Default
def Default()
Definition: bloaty/third_party/protobuf/python/google/protobuf/descriptor_pool.py:1252
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
google_protobuf_Timestamp_getmsgdef
const UPB_INLINE upb_MessageDef * google_protobuf_Timestamp_getmsgdef(upb_DefPool *s)
Definition: timestamp.upbdefs.h:24
google_protobuf_Timestamp_new
UPB_INLINE google_protobuf_Timestamp * google_protobuf_Timestamp_new(upb_Arena *arena)
Definition: timestamp.upb.h:31
upb_StringView::size
size_t size
Definition: upb/upb/upb.h:74
upb.h
upb_Arena_Malloc
UPB_INLINE void * upb_Arena_Malloc(upb_Arena *a, size_t size)
Definition: upb/upb/upb.h:222
upb::Arena::Fuse
void Fuse(Arena &other)
Definition: upb.hpp:93
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
msg
std::string msg
Definition: client_interceptors_end2end_test.cc:372
google_protobuf_Timestamp
struct google_protobuf_Timestamp google_protobuf_Timestamp
Definition: timestamp.upb.h:24
symtab
upb_symtab * symtab
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:774
upb::Arena::ptr
upb_Arena * ptr()
Definition: upb.hpp:76
benchmark.md
md
Definition: benchmark.py:86
upb::Arena
Definition: upb.hpp:68
timestamp.upbdefs.h
field
const FieldDescriptor * field
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/parser_unittest.cc:2692
upb::InlinedArena
Definition: upb.hpp:102
upb::FieldDefPtr::default_value
MessageValue default_value() const
Definition: def.hpp:112
def.h
UPB_UNUSED
#define UPB_UNUSED(var)
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.c:128
upb_JsonDecode
bool upb_JsonDecode(const char *buf, size_t size, upb_Message *msg, const upb_MessageDef *m, const upb_DefPool *symtab, int options, upb_Arena *arena, upb_Status *status)
Definition: json_decode.c:1486
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
timestamp.upb.h
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:31