From da72dd4806c6a8caba8eb5e292d97cd6ef0e9f0f Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Thu, 2 Nov 2023 19:59:27 +0100 Subject: [PATCH] Add unit test for `HashSet` and `Dictionary` collection types --- Source/Engine/Tests/TestCollections.cpp | 170 ++++++++++++++++++++++++ 1 file changed, 170 insertions(+) diff --git a/Source/Engine/Tests/TestCollections.cpp b/Source/Engine/Tests/TestCollections.cpp index 21aeb1c36..24c971860 100644 --- a/Source/Engine/Tests/TestCollections.cpp +++ b/Source/Engine/Tests/TestCollections.cpp @@ -3,6 +3,8 @@ #include "Engine/Core/RandomStream.h" #include "Engine/Core/Collections/Array.h" #include "Engine/Core/Collections/BitArray.h" +#include "Engine/Core/Collections/HashSet.h" +#include "Engine/Core/Collections/Dictionary.h" #include TEST_CASE("Array") @@ -108,3 +110,171 @@ TEST_CASE("BitArray") CHECK(a1 == testData); } } + +TEST_CASE("HashSet") +{ + SECTION("Test Allocators") + { + HashSet a1; + HashSet> a2; + HashSet> a3; + for (int32 i = 0; i < 7; i++) + { + a1.Add(i); + a2.Add(i); + a3.Add(i); + } + CHECK(a1.Count() == 7); + CHECK(a2.Count() == 7); + CHECK(a3.Count() == 7); + for (int32 i = 0; i < 7; i++) + { + CHECK(a1.Contains(i)); + CHECK(a2.Contains(i)); + CHECK(a3.Contains(i)); + } + } + + SECTION("Test Resizing") + { + HashSet a1; + for (int32 i = 0; i < 4000; i++) + a1.Add(i); + CHECK(a1.Count() == 4000); + int32 capacity = a1.Capacity(); + for (int32 i = 0; i < 4000; i++) + { + CHECK(a1.Contains(i)); + } + a1.Clear(); + CHECK(a1.Count() == 0); + CHECK(a1.Capacity() == capacity); + for (int32 i = 0; i < 4000; i++) + a1.Add(i); + CHECK(a1.Count() == 4000); + CHECK(a1.Capacity() == capacity); + for (int32 i = 0; i < 4000; i++) + a1.Remove(i); + CHECK(a1.Count() == 0); + CHECK(a1.Capacity() == capacity); + for (int32 i = 0; i < 4000; i++) + a1.Add(i); + CHECK(a1.Count() == 4000); + CHECK(a1.Capacity() == capacity); + } + + SECTION("Test Default Capacity") + { + HashSet a1; + a1.Add(1); + CHECK(a1.Capacity() <= DICTIONARY_DEFAULT_CAPACITY); + } + + SECTION("Test Add/Remove") + { + HashSet a1; + for (int32 i = 0; i < 4000; i++) + { + a1.Add(i); + a1.Remove(i); + } + CHECK(a1.Count() == 0); + CHECK(a1.Capacity() <= DICTIONARY_DEFAULT_CAPACITY); + a1.Clear(); + for (int32 i = 1; i <= 10; i++) + a1.Add(-i); + for (int32 i = 0; i < 4000; i++) + { + a1.Add(i); + a1.Remove(i); + } + CHECK(a1.Count() == 10); + CHECK(a1.Capacity() <= DICTIONARY_DEFAULT_CAPACITY); + } +} + +TEST_CASE("Dictionary") +{ + SECTION("Test Allocators") + { + Dictionary a1; + Dictionary> a2; + Dictionary> a3; + for (int32 i = 0; i < 7; i++) + { + a1.Add(i, i); + a2.Add(i, i); + a3.Add(i, i); + } + CHECK(a1.Count() == 7); + CHECK(a2.Count() == 7); + CHECK(a3.Count() == 7); + for (int32 i = 0; i < 7; i++) + { + CHECK(a1.ContainsKey(i)); + CHECK(a2.ContainsKey(i)); + CHECK(a3.ContainsKey(i)); + CHECK(a1.ContainsValue(i)); + CHECK(a2.ContainsValue(i)); + CHECK(a3.ContainsValue(i)); + } + } + + SECTION("Test Resizing") + { + Dictionary a1; + for (int32 i = 0; i < 4000; i++) + a1.Add(i, i); + CHECK(a1.Count() == 4000); + int32 capacity = a1.Capacity(); + for (int32 i = 0; i < 4000; i++) + { + CHECK(a1.ContainsKey(i)); + CHECK(a1.ContainsValue(i)); + } + a1.Clear(); + CHECK(a1.Count() == 0); + CHECK(a1.Capacity() == capacity); + for (int32 i = 0; i < 4000; i++) + a1.Add(i, i); + CHECK(a1.Count() == 4000); + CHECK(a1.Capacity() == capacity); + for (int32 i = 0; i < 4000; i++) + a1.Remove(i); + CHECK(a1.Count() == 0); + CHECK(a1.Capacity() == capacity); + for (int32 i = 0; i < 4000; i++) + a1.Add(i, i); + CHECK(a1.Count() == 4000); + CHECK(a1.Capacity() == capacity); + } + + SECTION("Test Default Capacity") + { + Dictionary a1; + a1.Add(1, 1); + CHECK(a1.Capacity() <= DICTIONARY_DEFAULT_CAPACITY); + } + + SECTION("Test Add/Remove") + { + Dictionary a1; + for (int32 i = 0; i < 4000; i++) + { + a1.Add(i, i); + a1.Remove(i); + } + CHECK(a1.Count() == 0); + CHECK(a1.Capacity() <= DICTIONARY_DEFAULT_CAPACITY); + a1.Clear(); + for (int32 i = 1; i <= 10; i++) + a1.Add(-i, -i); + for (int32 i = 0; i < 4000; i++) + { + a1.Add(i, i); + a1.Remove(i); + } + CHECK(a1.Count() == 10); + CHECK(a1.Capacity() <= DICTIONARY_DEFAULT_CAPACITY); + } +}