xref: /aosp_15_r20/external/pigweed/pw_build/linker_symbol_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2024 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_build/linker_symbol.h"
16 
17 #include "gtest/gtest.h"
18 
19 namespace pw {
20 namespace {
21 
22 // These symbols are defined in linker_symbol_test.ld
23 extern "C" LinkerSymbol<int> FOO_SYM;
24 extern "C" LinkerSymbol BAR_SYM;  // Test template default (uintptr_t)
25 extern "C" LinkerSymbol<int> NEGATIVE_SYM;
26 extern "C" LinkerSymbol<char> CHAR_SYM;
27 
28 enum class MyEnum {
29   kValue7 = 7,
30 };
31 extern "C" LinkerSymbol<MyEnum> ENUM_SYM;
32 
TEST(LinkerSymbolTest,ValueWorks)33 TEST(LinkerSymbolTest, ValueWorks) {
34   // You can use value() to get the value as the specified type.
35   auto value = FOO_SYM.value();
36   static_assert(std::is_same_v<decltype(value), int>);
37   EXPECT_EQ(value, 42);
38 }
39 
TEST(LinkerSymbolTest,NegativeValueWorks)40 TEST(LinkerSymbolTest, NegativeValueWorks) {
41   // LinkerSymbol works with negative integers.
42   EXPECT_EQ(NEGATIVE_SYM.value(), -567);
43 }
44 
TEST(LinkerSymbolTest,CharValueWorks)45 TEST(LinkerSymbolTest, CharValueWorks) {
46   // LinkerSymbol works with characters.
47   EXPECT_EQ(CHAR_SYM.value(), 'a');
48 }
49 
TEST(LinkerSymbolTest,EnumValueWorks)50 TEST(LinkerSymbolTest, EnumValueWorks) {
51   // LinkerSymbol works with enums.
52   EXPECT_EQ(ENUM_SYM.value(), MyEnum::kValue7);
53 }
54 
TEST(LinkerSymbolTest,ValueWorksDefaultType)55 TEST(LinkerSymbolTest, ValueWorksDefaultType) {
56   // You can use value() to get the value as the default type (uintptr_t).
57   auto value = BAR_SYM.value();
58   static_assert(std::is_same_v<decltype(value), uintptr_t>);
59   EXPECT_EQ(value, 0xDEADBEEFu);
60 }
61 
TEST(LinkerSymbolTest,CStyleCastWorks)62 TEST(LinkerSymbolTest, CStyleCastWorks) {
63   // You can use a C-style cast, if you insist.
64   EXPECT_EQ((uintptr_t)&FOO_SYM, 42u);
65 }
66 
67 }  // namespace
68 }  // namespace pw
69