Wyatt Hepler | f02e284 | 2023-03-06 20:56:59 +0000 | [diff] [blame] | 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 | |
Carlos Chinchilla | a425af3 | 2023-12-07 03:02:05 +0000 | [diff] [blame] | 17 | #include "pw_unit_test/framework.h" |
Wyatt Hepler | f02e284 | 2023-03-06 20:56:59 +0000 | [diff] [blame] | 18 | |
Wyatt Hepler | 52eee72 | 2024-08-14 18:42:39 +0000 | [diff] [blame] | 19 | namespace pw::tokenizer { |
Wyatt Hepler | b816ed5 | 2024-08-21 22:05:17 +0000 | [diff] [blame] | 20 | namespace { |
Wyatt Hepler | f02e284 | 2023-03-06 20:56:59 +0000 | [diff] [blame] | 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 | |
Wyatt Hepler | 2f4f1ea | 2023-09-08 23:01:25 +0000 | [diff] [blame] | 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 | |
| 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 | |
Wyatt Hepler | b816ed5 | 2024-08-21 22:05:17 +0000 | [diff] [blame] | 53 | } // namespace |
Wyatt Hepler | 52eee72 | 2024-08-14 18:42:39 +0000 | [diff] [blame] | 54 | } // namespace pw::tokenizer |