1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2024 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 #include "upb/wire/byte_size.h" 9 10 #include <gtest/gtest.h> 11 #include "google/protobuf/test_messages_proto2.upb.h" 12 #include "google/protobuf/test_messages_proto2.upb_minitable.h" 13 #include "upb/base/upcast.h" 14 #include "upb/mem/arena.h" 15 #include "upb/mini_table/message.h" 16 17 namespace { 18 static const upb_MiniTable* kTestMiniTable = 19 &protobuf_0test_0messages__proto2__TestAllTypesProto2_msg_init; 20 TEST(ByteSizeTest,UnpopulatedMsg)21TEST(ByteSizeTest, UnpopulatedMsg) { 22 upb_Arena* arena = upb_Arena_New(); 23 protobuf_test_messages_proto2_TestAllTypesProto2* msg = 24 protobuf_test_messages_proto2_TestAllTypesProto2_new(arena); 25 auto res = upb_ByteSize(UPB_UPCAST(msg), kTestMiniTable); 26 EXPECT_EQ(res, 0); 27 upb_Arena_Free(arena); 28 } 29 TEST(ByteSizeTest,PopulatedMsg)30TEST(ByteSizeTest, PopulatedMsg) { 31 upb_Arena* arena = upb_Arena_New(); 32 protobuf_test_messages_proto2_TestAllTypesProto2* msg = 33 protobuf_test_messages_proto2_TestAllTypesProto2_new(arena); 34 protobuf_test_messages_proto2_TestAllTypesProto2_set_optional_int32(msg, 322); 35 auto res = upb_ByteSize(UPB_UPCAST(msg), kTestMiniTable); 36 EXPECT_EQ(res, 3); 37 upb_Arena_Free(arena); 38 } 39 40 } // namespace 41