From a2af6480974dcbfc6940718129ed1031f59c8ac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D0=BD=D0=B8=D0=BC=D0=B8=D1=80=20=D0=9A?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D1=9F=D0=B8=D1=9B?= Date: Fri, 4 Nov 2022 19:09:23 -0700 Subject: [PATCH] Added allocator tests. --- include/bx/inline/allocator.inl | 2 +- tests/allocator_test.cpp | 78 +++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 tests/allocator_test.cpp diff --git a/include/bx/inline/allocator.inl b/include/bx/inline/allocator.inl index 6d5f054..82788d1 100644 --- a/include/bx/inline/allocator.inl +++ b/include/bx/inline/allocator.inl @@ -49,7 +49,7 @@ namespace bx inline void* alignedAlloc(AllocatorI* _allocator, size_t _size, size_t _align, const char* _file, uint32_t _line) { - const size_t align = max(_align, sizeof(uint32_t) );; + const size_t align = max(_align, sizeof(uint32_t) ); const size_t total = _size + align; uint8_t* ptr = (uint8_t*)alloc(_allocator, total, 0, _file, _line); uint8_t* aligned = (uint8_t*)alignPtr(ptr, sizeof(uint32_t), align); diff --git a/tests/allocator_test.cpp b/tests/allocator_test.cpp new file mode 100644 index 0000000..635d01a --- /dev/null +++ b/tests/allocator_test.cpp @@ -0,0 +1,78 @@ +/* + * Copyright 2010-2022 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bx/blob/master/LICENSE + */ + +#include "test.h" + +#include + +struct MockNonFreeingAllocator : public bx::AllocatorI +{ + MockNonFreeingAllocator() + : m_sbrk(1) + { + } + + ~MockNonFreeingAllocator() override + { + } + + void* realloc(void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line) override + { + BX_ASSERT(_ptr == NULL, "MockNonFreeingAllocator can't realloc or free."); + BX_ASSERT(m_sbrk + _size < BX_COUNTOF(m_storage), ""); + BX_UNUSED(_ptr, _file, _line); + + const uint32_t sbrk = bx::alignUp(m_sbrk, bx::max(_align, 1) ); + m_sbrk = sbrk + _size; + + return &m_storage[sbrk]; + } + + uint32_t m_sbrk; + BX_ALIGN_DECL(256, uint8_t) m_storage[0x10000]; +}; + +bool testAlignment(size_t _expected, void* _ptr) +{ + bool aligned = bx::isAligned(_ptr, _expected); +// BX_TRACE("%p, %d", _ptr, _expected); + BX_WARN(aligned, "%p not aligned to %d bytes.", _ptr, _expected); + return aligned; +} + +TEST_CASE("Allocator", "") +{ + MockNonFreeingAllocator mnfa; + + REQUIRE(testAlignment(1, bx::alloc (&mnfa, 1, 1 ) ) ); + REQUIRE(testAlignment(2, bx::alloc (&mnfa, 1, 2 ) ) ); + REQUIRE(testAlignment(1, bx::alloc (&mnfa, 1, 1 ) ) ); + REQUIRE(testAlignment(4, bx::alloc (&mnfa, 1, 4 ) ) ); + REQUIRE(testAlignment(1, bx::alloc (&mnfa, 1, 1 ) ) ); + REQUIRE(testAlignment(8, bx::alloc (&mnfa, 1, 8 ) ) ); + REQUIRE(testAlignment(1, bx::alloc (&mnfa, 1, 1 ) ) ); + REQUIRE(testAlignment(16, bx::alloc (&mnfa, 1, 16 ) ) ); + REQUIRE(testAlignment(1, bx::alloc (&mnfa, 1, 1 ) ) ); + REQUIRE(testAlignment(32, bx::alloc (&mnfa, 1, 32 ) ) ); + REQUIRE(testAlignment(1, bx::alloc (&mnfa, 1, 1 ) ) ); + REQUIRE(testAlignment(64, bx::alloc (&mnfa, 1, 64 ) ) ); + REQUIRE(testAlignment(1, bx::alloc (&mnfa, 1, 1 ) ) ); + REQUIRE(testAlignment(128, bx::alloc (&mnfa, 1, 128) ) ); + + REQUIRE(testAlignment(1, bx::alignedAlloc(&mnfa, 1, 1 ) ) ); + REQUIRE(testAlignment(2, bx::alignedAlloc(&mnfa, 1, 2 ) ) ); + REQUIRE(testAlignment(1, bx::alignedAlloc(&mnfa, 1, 1 ) ) ); + REQUIRE(testAlignment(4, bx::alignedAlloc(&mnfa, 1, 4 ) ) ); + REQUIRE(testAlignment(1, bx::alignedAlloc(&mnfa, 1, 1 ) ) ); + REQUIRE(testAlignment(8, bx::alignedAlloc(&mnfa, 1, 8 ) ) ); + REQUIRE(testAlignment(1, bx::alignedAlloc(&mnfa, 1, 1 ) ) ); + REQUIRE(testAlignment(16, bx::alignedAlloc(&mnfa, 1, 16 ) ) ); + REQUIRE(testAlignment(1, bx::alignedAlloc(&mnfa, 1, 1 ) ) ); + REQUIRE(testAlignment(32, bx::alignedAlloc(&mnfa, 1, 32 ) ) ); + REQUIRE(testAlignment(1, bx::alignedAlloc(&mnfa, 1, 1 ) ) ); + REQUIRE(testAlignment(64, bx::alignedAlloc(&mnfa, 1, 64 ) ) ); + REQUIRE(testAlignment(1, bx::alignedAlloc(&mnfa, 1, 1 ) ) ); + REQUIRE(testAlignment(128, bx::alignedAlloc(&mnfa, 1, 128) ) ); +}