From f60135cec62c91a9d7923397fe8796d2b3eaa5cb Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Wed, 12 Sep 2018 16:27:42 -0400 Subject: Feature/memory arena (#631) * First pass at MemoryArena. * First pass at RandomGenerator. * Extract TestContext into external source file. * Fix warning on printf. * Use enum classes for Test enums. OutputMode -> TestOutputMode. * First pass at FreeList unit test. * Auto registering tests. Improvements to RandomGenerator. * Remove the need for unitTest headers - cos can use registering. * Added unitTest for MemoryArena. * Do unit tests. * Fix typo. --- tools/slang-test/unit-test-free-list.cpp | 55 ++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tools/slang-test/unit-test-free-list.cpp (limited to 'tools/slang-test/unit-test-free-list.cpp') diff --git a/tools/slang-test/unit-test-free-list.cpp b/tools/slang-test/unit-test-free-list.cpp new file mode 100644 index 000000000..649c59571 --- /dev/null +++ b/tools/slang-test/unit-test-free-list.cpp @@ -0,0 +1,55 @@ +// unit-test-free-list.cpp + +#include "../../source/core/slang-free-list.h" + +#include +#include +#include + +#include "test-context.h" + +#include "../../source/core/slang-random-generator.h" +#include "../../source/core/list.h" + +using namespace Slang; + +static void freeListUnitTest() +{ + FreeList freeList; + freeList.init(sizeof(int), sizeof(void*), 10); + + DefaultRandomGenerator randGen(0x24343); + + List allocs; + + for (int i = 0; i < 1000; i++) + { + const int numAlloc = randGen.nextInt32UpTo(20); + + for (int j = 0; j < numAlloc; j++) + { + int* ptr = (int*)freeList.allocate(); + *ptr = i; + allocs.Add(ptr); + } + + int numDealloc = randGen.nextInt32UpTo(19); + numDealloc = int(allocs.Count()) < numDealloc ? int(allocs.Count()) : numDealloc; + + for (int j = 0; j < numDealloc; j++) + { + const int index = randGen.nextInt32UpTo(int(allocs.Count())); + + int* alloc = allocs[index]; + + SLANG_CHECK(*alloc <= i); + SLANG_CHECK(*alloc >= 0); + + freeList.deallocate(alloc); + + allocs.FastRemoveAt(index); + } + } +} + +SLANG_UNIT_TEST("FreeList", freeListUnitTest); \ No newline at end of file -- cgit v1.2.3