xref: /aosp_15_r20/external/pigweed/pw_log_tokenized/metadata_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2021 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_log_tokenized/metadata.h"
16 
17 #include "pw_unit_test/framework.h"
18 
19 namespace pw::log_tokenized {
20 namespace {
21 
TEST(Metadata,NoLineBits)22 TEST(Metadata, NoLineBits) {
23   using NoLineBits = GenericMetadata<6, 0, 10, 16>;
24 
25   constexpr NoLineBits test1 = NoLineBits::Set<0, 0, 0>();
26   static_assert(test1.level() == 0);
27   static_assert(test1.module() == 0);
28   static_assert(test1.flags() == 0);
29   static_assert(test1.line_number() == 0);
30 
31   constexpr NoLineBits test2 = NoLineBits::Set<3, 2, 1>();
32   static_assert(test2.level() == 3);
33   static_assert(test2.module() == 2);
34   static_assert(test2.flags() == 1);
35   static_assert(test2.line_number() == 0);
36 
37   constexpr NoLineBits test3 = NoLineBits::Set<63, 65535, 1023>();
38   static_assert(test3.level() == 63);
39   static_assert(test3.module() == 65535);
40   static_assert(test3.flags() == 1023);
41   static_assert(test3.line_number() == 0);
42 }
43 
TEST(Metadata,NoFlagBits)44 TEST(Metadata, NoFlagBits) {
45   using NoFlagBits = GenericMetadata<3, 13, 0, 16>;
46 
47   constexpr NoFlagBits test1 = NoFlagBits::Set<0, 0, 0, 0>();
48   static_assert(test1.level() == 0);
49   static_assert(test1.module() == 0);
50   static_assert(test1.flags() == 0);
51   static_assert(test1.line_number() == 0);
52 
53   constexpr NoFlagBits test2 = NoFlagBits::Set<3, 2, 0, 1>();
54   static_assert(test2.level() == 3);
55   static_assert(test2.module() == 2);
56   static_assert(test2.flags() == 0);
57   static_assert(test2.line_number() == 1);
58 
59   constexpr NoFlagBits test3 = NoFlagBits::Set<7, 65535, 0, (1 << 13) - 1>();
60   static_assert(test3.level() == 7);
61   static_assert(test3.module() == 65535);
62   static_assert(test3.flags() == 0);
63   static_assert(test3.line_number() == (1 << 13) - 1);
64 }
65 
TEST(Metadata,EncodedValue_Zero)66 TEST(Metadata, EncodedValue_Zero) {
67   constexpr Metadata test1 = Metadata::Set<0, 0, 0, 0>();
68   static_assert(test1.value() == 0);
69 }
70 
TEST(Metadata,EncodedValue_Nonzero)71 TEST(Metadata, EncodedValue_Nonzero) {
72   constexpr size_t kExpectedLevel = 3;
73   constexpr size_t kExpectedLine = 2022;
74   constexpr size_t kExpectedFlags = 0b10;
75   constexpr size_t kExpectedModule = 1337;
76   constexpr size_t kExpectedValue =
77       (kExpectedLevel) | (kExpectedLine << PW_LOG_TOKENIZED_LEVEL_BITS) |
78       (kExpectedFlags << (PW_LOG_TOKENIZED_LEVEL_BITS +
79                           PW_LOG_TOKENIZED_LINE_BITS)) |
80       (kExpectedModule << (PW_LOG_TOKENIZED_LEVEL_BITS +
81                            PW_LOG_TOKENIZED_LINE_BITS +
82                            PW_LOG_TOKENIZED_FLAG_BITS));
83   constexpr Metadata test = Metadata::
84       Set<kExpectedLevel, kExpectedModule, kExpectedFlags, kExpectedLine>();
85   static_assert(test.value() == kExpectedValue);
86 }
87 
TEST(Metadata,EncodedValue_NonzeroConstructor)88 TEST(Metadata, EncodedValue_NonzeroConstructor) {
89   constexpr size_t kExpectedLevel = 1;
90   constexpr size_t kExpectedLine = 99;
91   constexpr size_t kExpectedFlags = 0b11;
92   constexpr size_t kExpectedModule = 8900;
93   constexpr size_t kExpectedValue =
94       (kExpectedLevel) | (kExpectedLine << PW_LOG_TOKENIZED_LEVEL_BITS) |
95       (kExpectedFlags << (PW_LOG_TOKENIZED_LEVEL_BITS +
96                           PW_LOG_TOKENIZED_LINE_BITS)) |
97       (kExpectedModule << (PW_LOG_TOKENIZED_LEVEL_BITS +
98                            PW_LOG_TOKENIZED_LINE_BITS +
99                            PW_LOG_TOKENIZED_FLAG_BITS));
100   constexpr Metadata test =
101       Metadata(kExpectedLevel, kExpectedModule, kExpectedFlags, kExpectedLine);
102   static_assert(test.value() == kExpectedValue);
103 }
104 
TEST(Metadata,EncodedValue_Overflow)105 TEST(Metadata, EncodedValue_Overflow) {
106   constexpr size_t kExpectedLevel = 144;
107   constexpr Metadata test = Metadata(kExpectedLevel, 0, 0, 0);
108   static_assert(test.value() == 0);
109 }
110 
111 }  // namespace
112 }  // namespace pw::log_tokenized
113