MixedAllocatorTest.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/UnitTest/TestTypes.h>
  9. #include <AzTest/AzTest.h>
  10. #include <SurfaceData/MixedStackHeapAllocator.h>
  11. namespace UnitTest
  12. {
  13. struct MixedAllocatorTestFixture
  14. : public LeakDetectionFixture
  15. {
  16. public:
  17. MixedAllocatorTestFixture()
  18. {
  19. SetShouldCleanUpGenericClassInfo(false);
  20. }
  21. };
  22. TEST_F(MixedAllocatorTestFixture, MixedStackHeapAllocator_GetNameSetNameWorks)
  23. {
  24. const int StackElements = 4;
  25. // Setting the name via construction should work.
  26. const char name[] = "Mixed allocator";
  27. SurfaceData::mixed_stack_heap_allocator<float, StackElements> allocator(name);
  28. EXPECT_EQ(strcmp(allocator.get_name(), name), 0);
  29. // Setting the name via set_name should work.
  30. const char newName[] = "Renamed allocator";
  31. allocator.set_name(newName);
  32. EXPECT_EQ(strcmp(allocator.get_name(), newName), 0);
  33. }
  34. TEST_F(MixedAllocatorTestFixture, MixedStackHeapAllocator_SingleStackAllocationWorks)
  35. {
  36. const int StackElements = 4;
  37. SurfaceData::mixed_stack_heap_allocator<float, StackElements> allocator;
  38. // Choose a size and alignment that fits within our requested stack buffer.
  39. size_t allocSize = sizeof(float) * StackElements;
  40. size_t allocAlignment = alignof(float);
  41. auto numAllocatedBytesBefore = AZ::AllocatorInstance<AZ::SystemAllocator>::Get().NumAllocatedBytes();
  42. // Verify we can allocate the requested amount of data.
  43. auto data = allocator.allocate(allocSize, allocAlignment);
  44. ASSERT_NE(data, nullptr);
  45. auto numAllocatedBytesAfter = AZ::AllocatorInstance<AZ::SystemAllocator>::Get().NumAllocatedBytes();
  46. // Verify that none of the data came from the system allocator (heap).
  47. EXPECT_EQ(numAllocatedBytesBefore, numAllocatedBytesAfter);
  48. // Verify that a deallocation is successful.
  49. allocator.deallocate(data, allocSize, allocAlignment);
  50. }
  51. TEST_F(MixedAllocatorTestFixture, MixedStackHeapAllocator_SingleHeapAllocationWorks)
  52. {
  53. const int StackElements = 4;
  54. SurfaceData::mixed_stack_heap_allocator<float, StackElements> allocator;
  55. // Choose a size that's larger than our allocated stack buffer.
  56. size_t allocSize = sizeof(float) * (StackElements + 1);
  57. size_t allocAlignment = alignof(float);
  58. auto numAllocatedBytesBefore = AZ::AllocatorInstance<AZ::SystemAllocator>::Get().NumAllocatedBytes();
  59. // Verify we can allocate the requested amount of data.
  60. auto data = allocator.allocate(allocSize, allocAlignment);
  61. ASSERT_NE(data, nullptr);
  62. auto numAllocatedBytesAfter = AZ::AllocatorInstance<AZ::SystemAllocator>::Get().NumAllocatedBytes();
  63. // Verify that all of the data came from the system allocator (heap).
  64. // The actual allocated size can be larger than what was requested, so compare with >= instead of ==.
  65. EXPECT_GE(numAllocatedBytesAfter - numAllocatedBytesBefore, allocSize);
  66. // Verify that a deallocation is successful.
  67. allocator.deallocate(data, allocSize, allocAlignment);
  68. }
  69. TEST_F(MixedAllocatorTestFixture, MixedStackHeapAllocator_StackAllocationResizeWorks_OnlyWithinStackBufferSize)
  70. {
  71. const int StackElements = 4;
  72. SurfaceData::mixed_stack_heap_allocator<float, StackElements> allocator;
  73. // Choose a size that matches allocated stack buffer.
  74. size_t allocSize = sizeof(float) * StackElements;
  75. size_t allocResizeSmallerSize = sizeof(float) * (StackElements - 1);
  76. size_t allocResizeLargerSize = sizeof(float) * (StackElements + 1);
  77. size_t allocAlignment = alignof(float);
  78. auto numAllocatedBytesBefore = AZ::AllocatorInstance<AZ::SystemAllocator>::Get().NumAllocatedBytes();
  79. // Allocate the requested amount of data.
  80. auto data = allocator.allocate(allocSize, allocAlignment);
  81. ASSERT_NE(data, nullptr);
  82. // Resize to something smaller than the allocated stack buffer. This should succeed.
  83. auto reallocatedData = allocator.reallocate(data, allocResizeSmallerSize);
  84. EXPECT_NE(reallocatedData, nullptr);
  85. // Resize to something larger than the allocated stack buffer. This should return 0, as it isn't suported.
  86. reallocatedData = allocator.reallocate(data, allocResizeLargerSize);
  87. EXPECT_EQ(reallocatedData, nullptr);
  88. auto numAllocatedBytesAfter = AZ::AllocatorInstance<AZ::SystemAllocator>::Get().NumAllocatedBytes();
  89. // Verify that all of the data came from the stack.
  90. EXPECT_GE(numAllocatedBytesAfter - numAllocatedBytesBefore, 0);
  91. // Verify that a deallocation is successful.
  92. allocator.deallocate(reallocatedData, allocSize, allocAlignment);
  93. }
  94. } // namespace UnitTest