xref: /aosp_15_r20/external/pigweed/pw_allocator/examples/basic.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 "examples/named_u32.h"
16 #include "pw_allocator/allocator.h"
17 #include "pw_allocator/testing.h"
18 #include "pw_unit_test/framework.h"
19 
20 namespace examples {
21 
22 // DOCSTAG: [pw_allocator-examples-basic-allocate]
23 using pw::allocator::Layout;
24 
AllocateNamedU32(pw::Allocator & allocator)25 void* AllocateNamedU32(pw::Allocator& allocator) {
26   return allocator.Allocate(Layout::Of<NamedU32>());
27 }
28 // DOCSTAG: [pw_allocator-examples-basic-allocate]
29 
30 // DOCSTAG: [pw_allocator-examples-basic-deallocate]
DeallocateNamedU32(pw::Allocator & allocator,void * ptr)31 void DeallocateNamedU32(pw::Allocator& allocator, void* ptr) {
32   allocator.Deallocate(ptr);
33 }
34 // DOCSTAG: [pw_allocator-examples-basic-deallocate]
35 
36 // DOCSTAG: [pw_allocator-examples-basic-new_delete]
NewNamedU32(pw::Allocator & allocator,std::string_view name,uint32_t value)37 NamedU32* NewNamedU32(pw::Allocator& allocator,
38                       std::string_view name,
39                       uint32_t value) {
40   return allocator.New<NamedU32>(name, value);
41 }
42 
DeleteNamedU32(pw::Allocator & allocator,NamedU32 * named_u32)43 void DeleteNamedU32(pw::Allocator& allocator, NamedU32* named_u32) {
44   allocator.Delete<NamedU32>(named_u32);
45 }
46 // DOCSTAG: [pw_allocator-examples-basic-new_delete]
47 
48 // DOCSTAG: [pw_allocator-examples-basic-make_unique]
MakeNamedU32(pw::Allocator & allocator,std::string_view name,uint32_t value)49 pw::UniquePtr<NamedU32> MakeNamedU32(pw::Allocator& allocator,
50                                      std::string_view name,
51                                      uint32_t value) {
52   return allocator.MakeUnique<NamedU32>(name, value);
53 }
54 // DOCSTAG: [pw_allocator-examples-basic-make_unique]
55 
56 }  // namespace examples
57 
58 namespace {
59 
60 using AllocatorForTest = ::pw::allocator::test::AllocatorForTest<256>;
61 
62 class BasicExampleTest : public ::testing::Test {
63  protected:
64   AllocatorForTest allocator_;
65 };
66 
TEST_F(BasicExampleTest,AllocateNamedU32)67 TEST_F(BasicExampleTest, AllocateNamedU32) {
68   void* ptr = examples::AllocateNamedU32(allocator_);
69   ASSERT_NE(ptr, nullptr);
70   examples::DeallocateNamedU32(allocator_, ptr);
71 }
72 
TEST_F(BasicExampleTest,NewNamedU32)73 TEST_F(BasicExampleTest, NewNamedU32) {
74   auto* named_u32 = examples::NewNamedU32(allocator_, "test1", 111);
75   ASSERT_NE(named_u32, nullptr);
76   EXPECT_STREQ(named_u32->name().data(), "test1");
77   EXPECT_EQ(named_u32->value(), 111U);
78   examples::DeleteNamedU32(allocator_, named_u32);
79 }
80 
TEST_F(BasicExampleTest,MakeNamedU32)81 TEST_F(BasicExampleTest, MakeNamedU32) {
82   pw::UniquePtr<examples::NamedU32> named_u32 =
83       examples::MakeNamedU32(allocator_, "test2", 222);
84   ASSERT_NE(named_u32, nullptr);
85   EXPECT_STREQ(named_u32->name().data(), "test2");
86   EXPECT_EQ(named_u32->value(), 222U);
87 }
88 
89 }  // namespace
90