allocator_test.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright 2010-2025 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx/blob/master/LICENSE
  4. */
  5. #include "test.h"
  6. #include <bx/allocator.h>
  7. struct MockNonFreeingAllocator : public bx::AllocatorI
  8. {
  9. MockNonFreeingAllocator()
  10. : m_sbrk(1)
  11. {
  12. }
  13. ~MockNonFreeingAllocator() override
  14. {
  15. }
  16. void* realloc(void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line) override
  17. {
  18. BX_ASSERT(_ptr == NULL, "MockNonFreeingAllocator can't realloc or free.");
  19. BX_ASSERT(m_sbrk + _size < BX_COUNTOF(m_storage), "");
  20. BX_UNUSED(_ptr, _file, _line);
  21. const uintptr_t sbrk = bx::alignUp(m_sbrk, bx::max(int32_t(_align), 1) );
  22. m_sbrk = sbrk + _size;
  23. return &m_storage[sbrk];
  24. }
  25. uintptr_t m_sbrk;
  26. BX_ALIGN_DECL(256, uint8_t) m_storage[0x10000];
  27. };
  28. bool testAlignment(size_t _expected, void* _ptr)
  29. {
  30. bool aligned = bx::isAligned(_ptr, int32_t(_expected) );
  31. // BX_TRACE("%p, %d", _ptr, _expected);
  32. BX_WARN(aligned, "%p not aligned to %d bytes.", _ptr, _expected);
  33. return aligned;
  34. }
  35. TEST_CASE("Allocator", "")
  36. {
  37. MockNonFreeingAllocator mnfa;
  38. REQUIRE(testAlignment(1, bx::alloc (&mnfa, 1, 1 ) ) );
  39. REQUIRE(testAlignment(2, bx::alloc (&mnfa, 1, 2 ) ) );
  40. REQUIRE(testAlignment(1, bx::alloc (&mnfa, 1, 1 ) ) );
  41. REQUIRE(testAlignment(4, bx::alloc (&mnfa, 1, 4 ) ) );
  42. REQUIRE(testAlignment(1, bx::alloc (&mnfa, 1, 1 ) ) );
  43. REQUIRE(testAlignment(8, bx::alloc (&mnfa, 1, 8 ) ) );
  44. REQUIRE(testAlignment(1, bx::alloc (&mnfa, 1, 1 ) ) );
  45. REQUIRE(testAlignment(16, bx::alloc (&mnfa, 1, 16 ) ) );
  46. REQUIRE(testAlignment(1, bx::alloc (&mnfa, 1, 1 ) ) );
  47. REQUIRE(testAlignment(32, bx::alloc (&mnfa, 1, 32 ) ) );
  48. REQUIRE(testAlignment(1, bx::alloc (&mnfa, 1, 1 ) ) );
  49. REQUIRE(testAlignment(64, bx::alloc (&mnfa, 1, 64 ) ) );
  50. REQUIRE(testAlignment(1, bx::alloc (&mnfa, 1, 1 ) ) );
  51. REQUIRE(testAlignment(128, bx::alloc (&mnfa, 1, 128) ) );
  52. REQUIRE(testAlignment(1, bx::alignedAlloc(&mnfa, 1, 1 ) ) );
  53. REQUIRE(testAlignment(2, bx::alignedAlloc(&mnfa, 1, 2 ) ) );
  54. REQUIRE(testAlignment(1, bx::alignedAlloc(&mnfa, 1, 1 ) ) );
  55. REQUIRE(testAlignment(4, bx::alignedAlloc(&mnfa, 1, 4 ) ) );
  56. REQUIRE(testAlignment(1, bx::alignedAlloc(&mnfa, 1, 1 ) ) );
  57. REQUIRE(testAlignment(8, bx::alignedAlloc(&mnfa, 1, 8 ) ) );
  58. REQUIRE(testAlignment(1, bx::alignedAlloc(&mnfa, 1, 1 ) ) );
  59. REQUIRE(testAlignment(16, bx::alignedAlloc(&mnfa, 1, 16 ) ) );
  60. REQUIRE(testAlignment(1, bx::alignedAlloc(&mnfa, 1, 1 ) ) );
  61. REQUIRE(testAlignment(32, bx::alignedAlloc(&mnfa, 1, 32 ) ) );
  62. REQUIRE(testAlignment(1, bx::alignedAlloc(&mnfa, 1, 1 ) ) );
  63. REQUIRE(testAlignment(64, bx::alignedAlloc(&mnfa, 1, 64 ) ) );
  64. REQUIRE(testAlignment(1, bx::alignedAlloc(&mnfa, 1, 1 ) ) );
  65. REQUIRE(testAlignment(128, bx::alignedAlloc(&mnfa, 1, 128) ) );
  66. }