From 19c9964e5562d89766ed69e54bfd58a513dc05b1 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 18 Nov 2024 12:11:24 +1100 Subject: [PATCH] AP_MultiHeap: added simple unit test --- .../AP_MultiHeap/tests/test_multiheap.cpp | 57 +++++++++++++++++++ libraries/AP_MultiHeap/tests/wscript | 8 +++ 2 files changed, 65 insertions(+) create mode 100644 libraries/AP_MultiHeap/tests/test_multiheap.cpp create mode 100644 libraries/AP_MultiHeap/tests/wscript diff --git a/libraries/AP_MultiHeap/tests/test_multiheap.cpp b/libraries/AP_MultiHeap/tests/test_multiheap.cpp new file mode 100644 index 0000000000..168ed933cf --- /dev/null +++ b/libraries/AP_MultiHeap/tests/test_multiheap.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include + +const AP_HAL::HAL& hal = AP_HAL::get_HAL(); + +TEST(MultiHeap, Tests) +{ + static MultiHeap h; + + EXPECT_TRUE(h.create(150000, 10, true, 10000)); + EXPECT_TRUE(h.available()); + + const uint32_t max_allocs = 1000; + struct alloc { + void *ptr; + uint32_t size; + }; + auto *allocs = new alloc[max_allocs]; + allocs[0].size = 640; + allocs[0].ptr = h.allocate(allocs[0].size); + for (uint32_t i=1; i<2; i++) { + auto &a = allocs[i]; + a.size = 30; + a.ptr = h.allocate(a.size); + } + allocs[0].ptr = h.change_size(allocs[0].ptr, allocs[0].size, 50); + allocs[0].size = 50; + + for (uint32_t i=0; i<5000; i++) { + uint16_t idx = get_random16() % max_allocs; + auto &a = allocs[idx]; + if (a.ptr == nullptr) { + const uint16_t size = get_random16() % 150; + a.ptr = h.allocate(size); + //printf("a.ptr=%p %u -> %u\n", a.ptr, a.size, size); + EXPECT_TRUE(size==0?a.ptr == nullptr : a.ptr != nullptr); + a.size = size; + } else { + const uint16_t size = get_random16() % 150; + a.ptr = h.change_size(a.ptr, a.size, size); + //printf("a.ptr=%p %u -> %u\n", a.ptr, a.size, size); + EXPECT_TRUE(size==0?a.ptr == nullptr : a.ptr != nullptr); + a.size = size; + } + } + for (uint32_t i=0; i