xref: /aosp_15_r20/external/pigweed/pw_allocator/examples/custom_allocator_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 "examples/custom_allocator.h"
16 
17 #include <cstdint>
18 
19 #include "examples/custom_allocator_test_harness.h"
20 #include "examples/named_u32.h"
21 #include "pw_allocator/fuzzing.h"
22 #include "pw_allocator/testing.h"
23 #include "pw_containers/vector.h"
24 #include "pw_fuzzer/fuzztest.h"
25 #include "pw_unit_test/framework.h"
26 
27 namespace {
28 
29 using AllocatorForTest = ::pw::allocator::test::AllocatorForTest<256>;
30 
31 // DOCSTAG: [pw_allocator-examples-custom_allocator-unit_test]
32 // TODO: b/328076428 - Use pwrev/193642.
TEST(CustomAllocatorExample,MakeUnique)33 TEST(CustomAllocatorExample, MakeUnique) {
34   AllocatorForTest allocator;
35   constexpr size_t kThreshold = sizeof(examples::NamedU32) * 3;
36   examples::CustomAllocator custom(allocator, kThreshold);
37   // log_basic::test::LogCache<32> logs;
38 
39   auto ptr1 = custom.MakeUnique<examples::NamedU32>("test", 111);
40   auto ptr2 = custom.MakeUnique<examples::NamedU32>("test", 222);
41   auto ptr3 = custom.MakeUnique<examples::NamedU32>("test", 333);
42   // EXPECT_FALSE(logs.HasNext());
43 
44   auto ptr4 = custom.MakeUnique<examples::NamedU32>("test", 444);
45   // ASSERT_TRUE(logs.HasNext());
46   // log_basic::test::LogMessage log = logs.Next();
47   // EXPECT_EQ(log.level, PW_LOG_LEVEL_INFO);
48 
49   // StringBuffer<40> expected;
50   // expected << "more than " << kThreshold << " bytes allocated.";
51   // EXPECT_STREQ(log.message.c_str(), expected.c_str());
52 }
53 // DOCSTAG: [pw_allocator-examples-custom_allocator-unit_test]
54 
55 // DOCSTAG: [pw_allocator-examples-custom_allocator-fuzz_test]
NeverCrashes(const pw::Vector<pw::allocator::test::Request> & requests)56 void NeverCrashes(const pw::Vector<pw::allocator::test::Request>& requests) {
57   static examples::CustomAllocatorTestHarness harness;
58   harness.HandleRequests(requests);
59 }
60 
61 constexpr size_t kMaxRequests = 256;
62 constexpr size_t kMaxSize = examples::CustomAllocatorTestHarness::kCapacity / 2;
63 FUZZ_TEST(CustomAllocatorFuzzTest, NeverCrashes)
64     .WithDomains(
65         pw::allocator::test::ArbitraryRequests<kMaxRequests, kMaxSize>());
66 // DOCSTAG: [pw_allocator-examples-custom_allocator-fuzz_test]
67 
68 }  // namespace
69