Implements Dictionary class, a wrapper for the unordered_map, with more safety.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
105
tests/DictionaryTests.cpp
Normal file
105
tests/DictionaryTests.cpp
Normal file
@@ -0,0 +1,105 @@
|
||||
#ifdef TESTS_BUILD
|
||||
#include "../extern/catch.hpp"
|
||||
#include "../src/Collections/Dictionary.hpp"
|
||||
using namespace Arbutils::Collections;
|
||||
|
||||
TEST_CASE("Create Dictionary, insert values", "[Utilities]") {
|
||||
auto dic = Dictionary<int, int>(5);
|
||||
dic.Insert(10, 5);
|
||||
dic.Insert(2, 100);
|
||||
dic.Insert(9, 2000);
|
||||
}
|
||||
|
||||
TEST_CASE("Create Dictionary, insert values, get values", "[Utilities]") {
|
||||
auto dic = Dictionary<int, int>(5);
|
||||
dic.Insert(10, 5);
|
||||
dic.Insert(2, 100);
|
||||
dic.Insert(9, 2000);
|
||||
|
||||
CHECK(dic.Get(2) == 100);
|
||||
CHECK(dic.Get(9) == 2000);
|
||||
CHECK(dic.Get(10) == 5);
|
||||
}
|
||||
|
||||
TEST_CASE("Create Dictionary, insert values twice should throw", "[Utilities]") {
|
||||
auto dic = Dictionary<int, int>(5);
|
||||
dic.Insert(10, 5);
|
||||
CHECK_THROWS(dic.Insert(10, 100));
|
||||
|
||||
CHECK(dic.Get(10) == 5);
|
||||
}
|
||||
|
||||
TEST_CASE("Create Dictionary, insert value, then update value", "[Utilities]") {
|
||||
auto dic = Dictionary<int, int>(5);
|
||||
dic.Insert(10, 5);
|
||||
dic[10] = 200;
|
||||
|
||||
CHECK(dic.Get(10) == 200);
|
||||
}
|
||||
|
||||
TEST_CASE("Create Dictionary, insert value, try get value", "[Utilities]") {
|
||||
auto dic = Dictionary<int, int>(5);
|
||||
dic.Insert(10, 5);
|
||||
int result = 0;
|
||||
CHECK(dic.TryGet(10, result));
|
||||
CHECK(result == 5);
|
||||
}
|
||||
|
||||
TEST_CASE("Create Dictionary, insert value, try get non existing value", "[Utilities]") {
|
||||
auto dic = Dictionary<int, int>(5);
|
||||
int result = 0;
|
||||
CHECK_FALSE(dic.TryGet(10, result));
|
||||
CHECK(result == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("Create Dictionary, insert value, Has", "[Utilities]") {
|
||||
auto dic = Dictionary<int, int>(5);
|
||||
dic.Insert(10, 5);
|
||||
CHECK(dic.Has(10));
|
||||
}
|
||||
|
||||
TEST_CASE("Create Dictionary, set value", "[Utilities]") {
|
||||
auto dic = Dictionary<int, int>(5);
|
||||
dic.Set(5, 100);
|
||||
CHECK(dic.Has(5));
|
||||
}
|
||||
|
||||
TEST_CASE("Create Dictionary, insert values, get count", "[Utilities]") {
|
||||
auto dic = Dictionary<int, int>(5);
|
||||
dic.Insert(10, 5);
|
||||
dic.Insert(2, 100);
|
||||
dic.Insert(9, 2000);
|
||||
|
||||
CHECK(dic.Count() == 3);
|
||||
}
|
||||
|
||||
TEST_CASE("Create Dictionary, insert values, iterate over keys", "[Utilities]") {
|
||||
auto dic = Dictionary<int, int>(5);
|
||||
dic.Insert(10, 5);
|
||||
dic.Insert(2, 100);
|
||||
dic.Insert(9, 2000);
|
||||
|
||||
size_t i = 0;
|
||||
for (auto val: dic){
|
||||
CHECK(dic.Has(val.first));
|
||||
i++;
|
||||
}
|
||||
CHECK(i == 3);
|
||||
}
|
||||
|
||||
TEST_CASE("Create Dictionary with different types, insert values, iterate over keys", "[Utilities]") {
|
||||
auto dic = Dictionary<char, char>(5);
|
||||
dic.Insert(10, 5);
|
||||
dic.Insert(2, 100);
|
||||
dic.Insert(9, 105);
|
||||
|
||||
size_t i = 0;
|
||||
for (auto val: dic){
|
||||
CHECK(dic.Has(val.first));
|
||||
i++;
|
||||
}
|
||||
CHECK(i == 3);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user