xref: /aosp_15_r20/external/pigweed/pw_thread/thread_info_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker // Copyright 2022 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker //
3*61c4878aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker // use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker // the License at
6*61c4878aSAndroid Build Coastguard Worker //
7*61c4878aSAndroid Build Coastguard Worker //     https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker //
9*61c4878aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker // License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker // the License.
14*61c4878aSAndroid Build Coastguard Worker 
15*61c4878aSAndroid Build Coastguard Worker #include "pw_thread/thread_info.h"
16*61c4878aSAndroid Build Coastguard Worker 
17*61c4878aSAndroid Build Coastguard Worker #include <optional>
18*61c4878aSAndroid Build Coastguard Worker 
19*61c4878aSAndroid Build Coastguard Worker #include "pw_span/span.h"
20*61c4878aSAndroid Build Coastguard Worker #include "pw_unit_test/framework.h"
21*61c4878aSAndroid Build Coastguard Worker 
22*61c4878aSAndroid Build Coastguard Worker namespace pw::thread {
23*61c4878aSAndroid Build Coastguard Worker namespace {
24*61c4878aSAndroid Build Coastguard Worker 
TEST(ThreadInfo,ThreadName)25*61c4878aSAndroid Build Coastguard Worker TEST(ThreadInfo, ThreadName) {
26*61c4878aSAndroid Build Coastguard Worker   ThreadInfo thread_info;
27*61c4878aSAndroid Build Coastguard Worker   // Getter.
28*61c4878aSAndroid Build Coastguard Worker   EXPECT_EQ(thread_info.thread_name(), std::nullopt);
29*61c4878aSAndroid Build Coastguard Worker   char buffer[] = "hello, world";
30*61c4878aSAndroid Build Coastguard Worker   span<char, 13> name_string(buffer);
31*61c4878aSAndroid Build Coastguard Worker   span<const std::byte> name =
32*61c4878aSAndroid Build Coastguard Worker       span(reinterpret_cast<const std::byte*>(name_string.data()), 13);
33*61c4878aSAndroid Build Coastguard Worker   // Setter.
34*61c4878aSAndroid Build Coastguard Worker   thread_info.set_thread_name(name);
35*61c4878aSAndroid Build Coastguard Worker   EXPECT_EQ(thread_info.thread_name().value().data(), name.data());
36*61c4878aSAndroid Build Coastguard Worker   // Clear.
37*61c4878aSAndroid Build Coastguard Worker   thread_info.clear_thread_name();
38*61c4878aSAndroid Build Coastguard Worker   EXPECT_EQ(thread_info.thread_name(), std::nullopt);
39*61c4878aSAndroid Build Coastguard Worker }
40*61c4878aSAndroid Build Coastguard Worker 
TEST(ThreadInfo,StackLowAddr)41*61c4878aSAndroid Build Coastguard Worker TEST(ThreadInfo, StackLowAddr) {
42*61c4878aSAndroid Build Coastguard Worker   ThreadInfo thread_info;
43*61c4878aSAndroid Build Coastguard Worker   // Getter.
44*61c4878aSAndroid Build Coastguard Worker   EXPECT_EQ(thread_info.stack_low_addr(), std::nullopt);
45*61c4878aSAndroid Build Coastguard Worker   const unsigned int* null_addr = nullptr;
46*61c4878aSAndroid Build Coastguard Worker 
47*61c4878aSAndroid Build Coastguard Worker   const unsigned int example_addr = 12345678u;
48*61c4878aSAndroid Build Coastguard Worker   const unsigned int* addr = &example_addr;
49*61c4878aSAndroid Build Coastguard Worker   // Setter.
50*61c4878aSAndroid Build Coastguard Worker   thread_info.set_stack_low_addr(reinterpret_cast<uintptr_t>(null_addr));
51*61c4878aSAndroid Build Coastguard Worker   EXPECT_EQ(thread_info.stack_low_addr(),
52*61c4878aSAndroid Build Coastguard Worker             reinterpret_cast<uintptr_t>(null_addr));
53*61c4878aSAndroid Build Coastguard Worker   thread_info.set_stack_low_addr(reinterpret_cast<uintptr_t>(addr));
54*61c4878aSAndroid Build Coastguard Worker   EXPECT_EQ(thread_info.stack_low_addr(), reinterpret_cast<uintptr_t>(addr));
55*61c4878aSAndroid Build Coastguard Worker   // Clear.
56*61c4878aSAndroid Build Coastguard Worker   thread_info.clear_stack_low_addr();
57*61c4878aSAndroid Build Coastguard Worker   EXPECT_EQ(thread_info.stack_low_addr(), std::nullopt);
58*61c4878aSAndroid Build Coastguard Worker }
59*61c4878aSAndroid Build Coastguard Worker 
TEST(ThreadInfo,StackHighAddr)60*61c4878aSAndroid Build Coastguard Worker TEST(ThreadInfo, StackHighAddr) {
61*61c4878aSAndroid Build Coastguard Worker   ThreadInfo thread_info;
62*61c4878aSAndroid Build Coastguard Worker   // Getter.
63*61c4878aSAndroid Build Coastguard Worker   EXPECT_EQ(thread_info.stack_high_addr(), std::nullopt);
64*61c4878aSAndroid Build Coastguard Worker   const unsigned int* null_addr = nullptr;
65*61c4878aSAndroid Build Coastguard Worker 
66*61c4878aSAndroid Build Coastguard Worker   const unsigned int example_addr = 12345678u;
67*61c4878aSAndroid Build Coastguard Worker   const unsigned int* addr = &example_addr;
68*61c4878aSAndroid Build Coastguard Worker   // Setter.
69*61c4878aSAndroid Build Coastguard Worker   thread_info.set_stack_high_addr(reinterpret_cast<uintptr_t>(null_addr));
70*61c4878aSAndroid Build Coastguard Worker   EXPECT_EQ(thread_info.stack_high_addr(),
71*61c4878aSAndroid Build Coastguard Worker             reinterpret_cast<uintptr_t>(null_addr));
72*61c4878aSAndroid Build Coastguard Worker   thread_info.set_stack_high_addr(reinterpret_cast<uintptr_t>(addr));
73*61c4878aSAndroid Build Coastguard Worker   EXPECT_EQ(thread_info.stack_high_addr(), reinterpret_cast<uintptr_t>(addr));
74*61c4878aSAndroid Build Coastguard Worker   // Clear.
75*61c4878aSAndroid Build Coastguard Worker   thread_info.clear_stack_high_addr();
76*61c4878aSAndroid Build Coastguard Worker   EXPECT_EQ(thread_info.stack_high_addr(), std::nullopt);
77*61c4878aSAndroid Build Coastguard Worker }
78*61c4878aSAndroid Build Coastguard Worker 
TEST(ThreadInfo,PeakAddr)79*61c4878aSAndroid Build Coastguard Worker TEST(ThreadInfo, PeakAddr) {
80*61c4878aSAndroid Build Coastguard Worker   ThreadInfo thread_info;
81*61c4878aSAndroid Build Coastguard Worker   // Getter.
82*61c4878aSAndroid Build Coastguard Worker   EXPECT_EQ(thread_info.stack_peak_addr(), std::nullopt);
83*61c4878aSAndroid Build Coastguard Worker   const unsigned int* null_addr = nullptr;
84*61c4878aSAndroid Build Coastguard Worker 
85*61c4878aSAndroid Build Coastguard Worker   const unsigned int example_addr = 12345678u;
86*61c4878aSAndroid Build Coastguard Worker   const unsigned int* addr = &example_addr;
87*61c4878aSAndroid Build Coastguard Worker   // Setter.
88*61c4878aSAndroid Build Coastguard Worker   thread_info.set_stack_peak_addr(reinterpret_cast<uintptr_t>(null_addr));
89*61c4878aSAndroid Build Coastguard Worker   EXPECT_EQ(thread_info.stack_peak_addr(),
90*61c4878aSAndroid Build Coastguard Worker             reinterpret_cast<uintptr_t>(null_addr));
91*61c4878aSAndroid Build Coastguard Worker   thread_info.set_stack_peak_addr(reinterpret_cast<uintptr_t>(addr));
92*61c4878aSAndroid Build Coastguard Worker   EXPECT_EQ(thread_info.stack_peak_addr(), reinterpret_cast<uintptr_t>(addr));
93*61c4878aSAndroid Build Coastguard Worker   // Clear.
94*61c4878aSAndroid Build Coastguard Worker   thread_info.clear_stack_peak_addr();
95*61c4878aSAndroid Build Coastguard Worker   EXPECT_EQ(thread_info.stack_peak_addr(), std::nullopt);
96*61c4878aSAndroid Build Coastguard Worker }
97*61c4878aSAndroid Build Coastguard Worker 
98*61c4878aSAndroid Build Coastguard Worker }  // namespace
99*61c4878aSAndroid Build Coastguard Worker }  // namespace pw::thread
100