Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_malloc_freelist / freelist_malloc_test.cc
1 // Copyright 2020 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_malloc_freelist/freelist_malloc.h"
16
17 #include <memory>
18 #include <span>
19
20 #include "gtest/gtest.h"
21 #include "pw_allocator/freelist_heap.h"
22
23 namespace pw::allocator {
24
25 TEST(FreeListMalloc, ReplacingMalloc) {
26   constexpr size_t kAllocSize = 256;
27   constexpr size_t kReallocSize = 512;
28   constexpr size_t kCallocNum = 4;
29   constexpr size_t kCallocSize = 64;
30   constexpr size_t zero = 0;
31
32   auto deleter = [](void* ptr) { free(ptr); };
33
34   std::unique_ptr<void, decltype(deleter)> ptr1(malloc(kAllocSize), deleter);
35   const FreeListHeap::HeapStats& freelist_heap_stats =
36       pw_freelist_heap->heap_stats();
37   ASSERT_NE(ptr1.get(), nullptr);
38   EXPECT_EQ(freelist_heap_stats.bytes_allocated, kAllocSize);
39   EXPECT_EQ(freelist_heap_stats.cumulative_allocated, kAllocSize);
40   EXPECT_EQ(freelist_heap_stats.cumulative_freed, zero);
41
42   std::unique_ptr<void, decltype(deleter)> ptr2(
43       realloc(ptr1.release(), kReallocSize), deleter);
44   ASSERT_NE(ptr2.get(), nullptr);
45   EXPECT_EQ(freelist_heap_stats.bytes_allocated, kReallocSize);
46   EXPECT_EQ(freelist_heap_stats.cumulative_allocated,
47             kAllocSize + kReallocSize);
48   EXPECT_EQ(freelist_heap_stats.cumulative_freed, kAllocSize);
49
50   std::unique_ptr<void, decltype(deleter)> ptr3(calloc(kCallocNum, kCallocSize),
51                                                 deleter);
52   ASSERT_NE(ptr3.get(), nullptr);
53   EXPECT_EQ(freelist_heap_stats.bytes_allocated,
54             kReallocSize + kCallocNum * kCallocSize);
55   EXPECT_EQ(freelist_heap_stats.cumulative_allocated,
56             kAllocSize + kReallocSize + kCallocNum * kCallocSize);
57   EXPECT_EQ(freelist_heap_stats.cumulative_freed, kAllocSize);
58   free(ptr2.release());
59   EXPECT_EQ(freelist_heap_stats.bytes_allocated, kCallocNum * kCallocSize);
60   EXPECT_EQ(freelist_heap_stats.cumulative_allocated,
61             kAllocSize + kReallocSize + kCallocNum * kCallocSize);
62   EXPECT_EQ(freelist_heap_stats.cumulative_freed, kAllocSize + kReallocSize);
63   free(ptr3.release());
64   EXPECT_EQ(freelist_heap_stats.bytes_allocated, zero);
65   EXPECT_EQ(freelist_heap_stats.cumulative_allocated,
66             kAllocSize + kReallocSize + kCallocNum * kCallocSize);
67   EXPECT_EQ(freelist_heap_stats.cumulative_freed,
68             kAllocSize + kReallocSize + kCallocNum * kCallocSize);
69 }
70
71 }  // namespace pw::allocator