1 // Copyright 2022 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #include "pw_protobuf/serialized_size.h"
15
16 #include <cinttypes>
17
18 #include "pw_unit_test/framework.h"
19
20 namespace pw::protobuf {
21 namespace {
22
23 #define TEST_VARINT(type) \
24 TEST(SerializedSize, type) { \
25 static_assert(SizeOfField##type(1, 0) == 1 + 1, \
26 #type " minimum encoded size, key 1"); \
27 static_assert(SizeOfField##type(1) == 1 + kMaxSizeBytes##type, \
28 #type " maximum encoded size, key 1"); \
29 static_assert(SizeOfField##type(16, 0) == 2 + 1, \
30 #type " minimum encoded size, key 16"); \
31 static_assert(SizeOfField##type(16) == 2 + kMaxSizeBytes##type, \
32 #type " maximum encoded size, key 16"); \
33 } \
34 static_assert(true, "require semicolons")
35
36 #define TEST_FIXED(type) \
37 TEST(SerializedSize, SizeOf##type##Field) { \
38 static_assert(SizeOfField##type(1) == 1 + kMaxSizeBytes##type, \
39 #type " key 1"); \
40 static_assert(SizeOfField##type(15) == 1 + kMaxSizeBytes##type, \
41 #type " key 15"); \
42 static_assert(SizeOfField##type(16) == 2 + kMaxSizeBytes##type, \
43 #type " key 16"); \
44 static_assert(SizeOfField##type(17) == 2 + kMaxSizeBytes##type, \
45 #type " key 17"); \
46 } \
47 static_assert(true, "require semicolons")
48
49 #define TEST_DELIMITED(function) \
50 TEST(SerializedSize, function) { \
51 static_assert(function(1, 0) == 1 + 1 + 0, #function " key 1"); \
52 static_assert(function(1, 1) == 1 + 1 + 1, #function " key 1"); \
53 static_assert(function(1, 128) == 1 + 2 + 128, #function " key 1"); \
54 static_assert(function(1, 1000) == 1 + 2 + 1000, #function " key 1"); \
55 static_assert(function(16, 0) == 2 + 1 + 0, #function " key 16"); \
56 static_assert(function(16, 1) == 2 + 1 + 1, #function " key 16"); \
57 static_assert(function(16, 128) == 2 + 2 + 128, #function " key 16"); \
58 static_assert(function(16, 1000) == 2 + 2 + 1000, #function " key 16"); \
59 } \
60 static_assert(true, "require semicolons")
61
TEST(SerializedSize,SizeOfVarintField)62 TEST(SerializedSize, SizeOfVarintField) {
63 static_assert(SizeOfVarintField(1, 0) == 1 + 1);
64 static_assert(SizeOfVarintField(1, 127) == 1 + 1);
65
66 static_assert(SizeOfVarintField(1, INT32_C(-1)) == 1 + 10);
67 static_assert(SizeOfVarintField(1, INT64_C(-1)) == 1 + 10);
68
69 static_assert(SizeOfVarintField(16, 0) == 2 + 1);
70 static_assert(SizeOfVarintField(16, 127) == 2 + 1);
71
72 static_assert(SizeOfVarintField(16, INT32_C(-1)) == 2 + 10);
73 static_assert(SizeOfVarintField(16, INT64_C(-1)) == 2 + 10);
74 }
75
TEST(SerializedSize,SizeOfDelimitedFieldWithoutValue)76 TEST(SerializedSize, SizeOfDelimitedFieldWithoutValue) {
77 static_assert(SizeOfDelimitedFieldWithoutValue(1, 0) == 1 + 1);
78 static_assert(SizeOfDelimitedFieldWithoutValue(1, 1) == 1 + 1);
79 static_assert(SizeOfDelimitedFieldWithoutValue(1, 128) == 1 + 2);
80 static_assert(SizeOfDelimitedFieldWithoutValue(1, 1000) == 1 + 2);
81 static_assert(SizeOfDelimitedFieldWithoutValue(1) == 1 + 5);
82 static_assert(SizeOfDelimitedFieldWithoutValue(16, 0) == 2 + 1);
83 static_assert(SizeOfDelimitedFieldWithoutValue(16, 1) == 2 + 1);
84 static_assert(SizeOfDelimitedFieldWithoutValue(16, 128) == 2 + 2);
85 static_assert(SizeOfDelimitedFieldWithoutValue(16) == 2 + 5);
86 }
87
88 TEST_DELIMITED(SizeOfDelimitedField);
89
90 TEST_FIXED(Float);
91 TEST_FIXED(Double);
92
93 TEST_VARINT(Int32);
94 TEST_VARINT(Int64);
95 TEST_VARINT(Sint32);
96 TEST_VARINT(Sint64);
97 TEST_VARINT(Uint32);
98 TEST_VARINT(Uint64);
99
100 TEST_FIXED(Fixed32);
101 TEST_FIXED(Fixed64);
102 TEST_FIXED(Sfixed32);
103 TEST_FIXED(Sfixed64);
104 TEST_FIXED(Bool);
105
106 TEST_DELIMITED(SizeOfFieldString);
107 TEST_DELIMITED(SizeOfFieldBytes);
108
109 TEST_VARINT(Enum);
110
111 } // namespace
112 } // namespace pw::protobuf
113