diff options
Diffstat (limited to 'tools/slang-test/unit-test-memory-arena.cpp')
| -rw-r--r-- | tools/slang-test/unit-test-memory-arena.cpp | 49 |
1 files changed, 30 insertions, 19 deletions
diff --git a/tools/slang-test/unit-test-memory-arena.cpp b/tools/slang-test/unit-test-memory-arena.cpp index 69eed520a..20a7e047d 100644 --- a/tools/slang-test/unit-test-memory-arena.cpp +++ b/tools/slang-test/unit-test-memory-arena.cpp @@ -123,15 +123,23 @@ static void memoryArenaUnitTest() blocks.Add(arena.allocate(blockSize * 2)); blocks.Add(arena.allocate(100)); - while (blocks.Count()) + arena.deallocateAll(); + blocks.Add(arena.allocate(100)); + blocks.Add(arena.allocate(blockSize * 2)); + + arena.reset(); + { - arena.deallocateLast(blocks.Last()); - blocks.RemoveLast(); + uint32_t data[] = { 1, 2, 3 }; + + const uint32_t* copy = arena.allocateAndCopyArray(data, SLANG_COUNT_OF(data)); + + SLANG_CHECK(::memcmp(copy, data, sizeof(data)) == 0); } } { - + int count = 0; const size_t blockSize = 1024; for (TestMode mode = TestMode(0); int(mode) < int(TestMode::eCount); mode = TestMode(int(mode) + 1)) @@ -143,32 +151,30 @@ static void memoryArenaUnitTest() List<Block> blocks; - for (int i = 0; i < 1000; i++) + for (int i = 0; i < 10000; i++) { - int var = randGen.nextInt32() & 0x3ff; + count++; + + const int var = randGen.nextInt32() & 0x3ff; if (var < 3 && blocks.Count() > 0) { - if (var == 0) - { - // Do a single dealloc - arena.deallocateLast(blocks.Last().m_data); - blocks.RemoveLast(); - } - else if (var == 1) + if (var == 1) { // Deallocate everything arena.deallocateAll(); blocks.Clear(); + } + else if (var == 2) + { + arena.reset(); + blocks.Clear(); } else { - // Do a multiple dealloc - int index = randGen.nextInt32UpTo(int(blocks.Count())); + size_t usedMemory = arena.calcTotalMemoryUsed(); + size_t allocatedMemory = arena.calcTotalMemoryAllocated(); - // Deallocate all afterwards - arena.deallocateAllFrom(blocks[index].m_data); - - blocks.SetSize(index); + SLANG_CHECK(allocatedMemory >= usedMemory); } } else @@ -180,6 +186,11 @@ static void memoryArenaUnitTest() { sizeInBytes += blockSize; } + else if ((randGen.nextInt32() & 0xff) < 2) + { + // Let's try for a block that's awkwardly sized + sizeInBytes = blockSize / 3 + 10; + } const uint8_t value = uint8_t(randGen.nextInt32()); |
