summaryrefslogtreecommitdiff
path: root/tools/slang-test/unit-test-free-list.cpp
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2018-09-12 16:27:42 -0400
committerGitHub <noreply@github.com>2018-09-12 16:27:42 -0400
commitf60135cec62c91a9d7923397fe8796d2b3eaa5cb (patch)
tree777646cb3611bf5809dc18e120e506117e143e11 /tools/slang-test/unit-test-free-list.cpp
parent9a9733091cc7c9628e445313785d561deb229072 (diff)
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.
Diffstat (limited to 'tools/slang-test/unit-test-free-list.cpp')
-rw-r--r--tools/slang-test/unit-test-free-list.cpp55
1 files changed, 55 insertions, 0 deletions
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 <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#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<int*> 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