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
15 #include "pw_tokenizer/encode_args.h"
16
17 #include "pw_unit_test/framework.h"
18
19 namespace pw::tokenizer {
20 namespace {
21
22 static_assert(MinEncodingBufferSizeBytes<>() == 4);
23 static_assert(MinEncodingBufferSizeBytes<bool>() == 4 + 2);
24 static_assert(MinEncodingBufferSizeBytes<char>() == 4 + 2);
25 static_assert(MinEncodingBufferSizeBytes<short>() == 4 + 3);
26 static_assert(MinEncodingBufferSizeBytes<int>() == 4 + 5);
27 static_assert(MinEncodingBufferSizeBytes<long long>() == 4 + 10);
28 static_assert(MinEncodingBufferSizeBytes<float>() == 4 + 4);
29 static_assert(MinEncodingBufferSizeBytes<double>() == 4 + 4);
30 static_assert(MinEncodingBufferSizeBytes<const char*>() == 4 + 1);
31 static_assert(MinEncodingBufferSizeBytes<void*>() == 4 + 5 ||
32 MinEncodingBufferSizeBytes<void*>() == 4 + 10);
33
34 static_assert(MinEncodingBufferSizeBytes<int, double>() == 4 + 5 + 4);
35 static_assert(MinEncodingBufferSizeBytes<int, int, const char*>() ==
36 4 + 5 + 5 + 1);
37 static_assert(
38 MinEncodingBufferSizeBytes<const char*, long long, int, short>() ==
39 4 + 1 + 10 + 5 + 3);
40
TEST(TokenizerCEncodingFunctions,EncodeInt)41 TEST(TokenizerCEncodingFunctions, EncodeInt) {
42 uint8_t buffer[5] = {};
43 EXPECT_EQ(pw_tokenizer_EncodeInt(-1, buffer, sizeof(buffer)), 1u);
44 EXPECT_EQ(buffer[0], 1); // -1 encodes to 1 with ZigZag
45 }
46
TEST(TokenizerCEncodingFunctions,EncodeInt64)47 TEST(TokenizerCEncodingFunctions, EncodeInt64) {
48 uint8_t buffer[5] = {};
49 EXPECT_EQ(pw_tokenizer_EncodeInt(1, buffer, sizeof(buffer)), 1u);
50 EXPECT_EQ(buffer[0], 2); // 1 encodes to 2 with ZigZag
51 }
52
53 } // namespace
54 } // namespace pw::tokenizer
55