blob: a9fceb9c7a9a50164918dcb20fbaf80b4556ae4d [file] [log] [blame]
Wyatt Heplerf02e2842023-03-06 20:56:59 +00001// 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
Carlos Chinchillaa425af32023-12-07 03:02:05 +000017#include "pw_unit_test/framework.h"
Wyatt Heplerf02e2842023-03-06 20:56:59 +000018
Wyatt Hepler52eee722024-08-14 18:42:39 +000019namespace pw::tokenizer {
Wyatt Heplerb816ed52024-08-21 22:05:17 +000020namespace {
Wyatt Heplerf02e2842023-03-06 20:56:59 +000021
22static_assert(MinEncodingBufferSizeBytes<>() == 4);
23static_assert(MinEncodingBufferSizeBytes<bool>() == 4 + 2);
24static_assert(MinEncodingBufferSizeBytes<char>() == 4 + 2);
25static_assert(MinEncodingBufferSizeBytes<short>() == 4 + 3);
26static_assert(MinEncodingBufferSizeBytes<int>() == 4 + 5);
27static_assert(MinEncodingBufferSizeBytes<long long>() == 4 + 10);
28static_assert(MinEncodingBufferSizeBytes<float>() == 4 + 4);
29static_assert(MinEncodingBufferSizeBytes<double>() == 4 + 4);
30static_assert(MinEncodingBufferSizeBytes<const char*>() == 4 + 1);
31static_assert(MinEncodingBufferSizeBytes<void*>() == 4 + 5 ||
32 MinEncodingBufferSizeBytes<void*>() == 4 + 10);
33
34static_assert(MinEncodingBufferSizeBytes<int, double>() == 4 + 5 + 4);
35static_assert(MinEncodingBufferSizeBytes<int, int, const char*>() ==
36 4 + 5 + 5 + 1);
37static_assert(
38 MinEncodingBufferSizeBytes<const char*, long long, int, short>() ==
39 4 + 1 + 10 + 5 + 3);
40
Wyatt Hepler2f4f1ea2023-09-08 23:01:25 +000041TEST(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
47TEST(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
Wyatt Heplerb816ed52024-08-21 22:05:17 +000053} // namespace
Wyatt Hepler52eee722024-08-14 18:42:39 +000054} // namespace pw::tokenizer