Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2023 Google LLC. All rights reserved. |
| 3 | // |
| 4 | // Use of this source code is governed by a BSD-style |
| 5 | // license that can be found in the LICENSE file or at |
| 6 | // https://developers.google.com/open-source/licenses/bsd |
| 7 | |
| 8 | // Tests for C++ wrappers. |
| 9 | |
| 10 | #include <string.h> |
| 11 | |
| 12 | #include <fstream> |
| 13 | #include <iostream> |
| 14 | #include <set> |
| 15 | #include <sstream> |
| 16 | |
| 17 | #include "google/protobuf/timestamp.upb.h" |
| 18 | #include "google/protobuf/timestamp.upbdefs.h" |
| 19 | #include <gtest/gtest.h> |
| 20 | #include "upb/json/decode.h" |
| 21 | #include "upb/json/encode.h" |
| 22 | #include "upb/reflection/def.h" |
| 23 | #include "upb/reflection/def.hpp" |
| 24 | #include "upb/test/test_cpp.upb.h" |
| 25 | #include "upb/test/test_cpp.upbdefs.h" |
| 26 | |
| 27 | // Must be last. |
| 28 | #include "upb/port/def.inc" |
| 29 | |
| 30 | TEST(Cpp, Iteration) { |
| 31 | upb::DefPool defpool; |
| 32 | upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(defpool.ptr())); |
| 33 | |
| 34 | // Test range-based for on both fields and oneofs (with the iterator adaptor). |
| 35 | int field_count = 0; |
| 36 | for (auto field : md.fields()) { |
| 37 | UPB_UNUSED(field); |
| 38 | field_count++; |
| 39 | } |
| 40 | EXPECT_EQ(field_count, md.field_count()); |
| 41 | |
| 42 | int oneof_count = 0; |
| 43 | for (auto oneof : md.oneofs()) { |
| 44 | UPB_UNUSED(oneof); |
| 45 | oneof_count++; |
| 46 | } |
| 47 | EXPECT_EQ(oneof_count, md.oneof_count()); |
| 48 | } |
| 49 | |
| 50 | TEST(Cpp, InlinedArena2) { |
| 51 | upb::InlinedArena<64> arena; |
| 52 | upb_Arena_Malloc(arena.ptr(), sizeof(int)); |
| 53 | } |
| 54 | |
| 55 | TEST(Cpp, Default) { |
| 56 | upb::DefPool defpool; |
| 57 | upb::Arena arena; |
| 58 | upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(defpool.ptr())); |
| 59 | upb_test_TestMessage* msg = upb_test_TestMessage_new(arena.ptr()); |
Joshua Haberman | 8647a803 | 2023-10-20 08:30:44 -0700 | [diff] [blame] | 60 | size_t size = upb_JsonEncode(msg, md.ptr(), nullptr, 0, nullptr, 0, nullptr); |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 61 | EXPECT_EQ(2, size); // "{}" |
| 62 | } |
| 63 | |
| 64 | TEST(Cpp, JsonNull) { |
| 65 | upb::DefPool defpool; |
| 66 | upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(defpool.ptr())); |
| 67 | upb::FieldDefPtr i32_f = md.FindFieldByName("i32"); |
| 68 | upb::FieldDefPtr str_f = md.FindFieldByName("str"); |
| 69 | ASSERT_TRUE(i32_f); |
| 70 | ASSERT_TRUE(str_f); |
| 71 | EXPECT_EQ(5, i32_f.default_value().int32_val); |
| 72 | EXPECT_EQ(0, strcmp(str_f.default_value().str_val.data, "abc")); |
| 73 | EXPECT_EQ(3, str_f.default_value().str_val.size); |
| 74 | } |
| 75 | |
| 76 | TEST(Cpp, TimestampEncoder) { |
| 77 | upb::DefPool defpool; |
| 78 | upb::Arena arena; |
| 79 | upb::MessageDefPtr md(google_protobuf_Timestamp_getmsgdef(defpool.ptr())); |
| 80 | google_protobuf_Timestamp* timestamp_upb = |
| 81 | google_protobuf_Timestamp_new(arena.ptr()); |
| 82 | google_protobuf_Timestamp* timestamp_upb_decoded = |
| 83 | google_protobuf_Timestamp_new(arena.ptr()); |
| 84 | |
| 85 | int64_t timestamps[] = { |
| 86 | 253402300799, // 9999-12-31T23:59:59Z |
| 87 | 1641006000, // 2022-01-01T03:00:00Z |
| 88 | 0, // 1970-01-01T00:00:00Z |
| 89 | -31525200, // 1969-01-01T03:00:00Z |
| 90 | -2208988800, // 1900-01-01T00:00:00Z |
| 91 | -62135596800, // 0000-01-01T00:00:00Z |
| 92 | }; |
| 93 | |
| 94 | for (int64_t timestamp : timestamps) { |
| 95 | google_protobuf_Timestamp_set_seconds(timestamp_upb, timestamp); |
| 96 | |
| 97 | char json[128]; |
Joshua Haberman | 8647a803 | 2023-10-20 08:30:44 -0700 | [diff] [blame] | 98 | size_t size = upb_JsonEncode(timestamp_upb, md.ptr(), nullptr, 0, json, |
| 99 | sizeof(json), nullptr); |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 100 | bool result = upb_JsonDecode(json, size, timestamp_upb_decoded, md.ptr(), |
Joshua Haberman | 8647a803 | 2023-10-20 08:30:44 -0700 | [diff] [blame] | 101 | nullptr, 0, arena.ptr(), nullptr); |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 102 | const int64_t timestamp_decoded = |
| 103 | google_protobuf_Timestamp_seconds(timestamp_upb_decoded); |
| 104 | |
| 105 | ASSERT_TRUE(result); |
| 106 | EXPECT_EQ(timestamp, timestamp_decoded); |
| 107 | } |
| 108 | } |