2020-03-22 15:38:48 +00:00
|
|
|
#ifdef TESTS_BUILD
|
2022-02-05 12:37:47 +00:00
|
|
|
#include <doctest.h>
|
2020-03-22 15:38:48 +00:00
|
|
|
#include "../src/Collections/Dictionary.hpp"
|
2022-05-16 14:36:52 +00:00
|
|
|
#include "../src/Collections/StringViewDictionary.hpp"
|
2020-05-26 15:36:39 +00:00
|
|
|
using namespace ArbUt;
|
2020-03-22 15:38:48 +00:00
|
|
|
|
2020-09-25 09:50:53 +00:00
|
|
|
TEST_CASE("Create Dictionary, insert values") {
|
2022-03-23 11:56:12 +00:00
|
|
|
auto dic = Dictionary<i32, i32>(5);
|
2020-03-22 15:38:48 +00:00
|
|
|
dic.Insert(10, 5);
|
|
|
|
dic.Insert(2, 100);
|
|
|
|
dic.Insert(9, 2000);
|
|
|
|
}
|
|
|
|
|
2020-09-25 09:50:53 +00:00
|
|
|
TEST_CASE("Create Dictionary with initializer list") {
|
2022-03-23 11:56:12 +00:00
|
|
|
auto dic = Dictionary<i32, i32>({{5, 100}, {10, 200}, {50, 2}});
|
2020-03-22 15:53:42 +00:00
|
|
|
|
|
|
|
CHECK(dic.Get(5) == 100);
|
|
|
|
CHECK(dic.Get(10) == 200);
|
|
|
|
CHECK(dic.Get(50) == 2);
|
|
|
|
}
|
|
|
|
|
2020-09-25 09:50:53 +00:00
|
|
|
TEST_CASE("Create Dictionary, insert values, get values") {
|
2022-03-23 11:56:12 +00:00
|
|
|
auto dic = Dictionary<i32, i32>(5);
|
2020-03-22 15:38:48 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-09-25 09:50:53 +00:00
|
|
|
TEST_CASE("Create Dictionary, insert values twice should throw") {
|
2022-03-23 11:56:12 +00:00
|
|
|
auto dic = Dictionary<i32, i32>(5);
|
2020-03-22 15:38:48 +00:00
|
|
|
dic.Insert(10, 5);
|
|
|
|
CHECK_THROWS(dic.Insert(10, 100));
|
|
|
|
|
|
|
|
CHECK(dic.Get(10) == 5);
|
|
|
|
}
|
|
|
|
|
2020-09-25 09:50:53 +00:00
|
|
|
TEST_CASE("Create Dictionary, insert value, then update value") {
|
2022-03-23 11:56:12 +00:00
|
|
|
auto dic = Dictionary<i32, i32>(5);
|
2020-03-22 15:38:48 +00:00
|
|
|
dic.Insert(10, 5);
|
|
|
|
dic[10] = 200;
|
|
|
|
|
|
|
|
CHECK(dic.Get(10) == 200);
|
|
|
|
}
|
|
|
|
|
2020-09-25 09:50:53 +00:00
|
|
|
TEST_CASE("Create Dictionary, insert value, try get value") {
|
2022-03-23 11:56:12 +00:00
|
|
|
auto dic = Dictionary<i32, i32>(5);
|
2020-03-22 15:38:48 +00:00
|
|
|
dic.Insert(10, 5);
|
2020-12-12 11:53:47 +00:00
|
|
|
auto v = dic.TryGet(10);
|
|
|
|
CHECK(v.has_value());
|
|
|
|
CHECK(v.value() == 5);
|
2020-03-22 15:38:48 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 09:50:53 +00:00
|
|
|
TEST_CASE("Create Dictionary, insert value, try get non existing value") {
|
2022-03-23 11:56:12 +00:00
|
|
|
auto dic = Dictionary<i32, i32>(5);
|
2020-12-12 11:53:47 +00:00
|
|
|
CHECK_FALSE(dic.TryGet(10).has_value());
|
2020-03-22 15:38:48 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 09:50:53 +00:00
|
|
|
TEST_CASE("Create Dictionary, insert value, Has") {
|
2022-03-23 11:56:12 +00:00
|
|
|
auto dic = Dictionary<i32, i32>(5);
|
2020-03-22 15:38:48 +00:00
|
|
|
dic.Insert(10, 5);
|
|
|
|
CHECK(dic.Has(10));
|
|
|
|
}
|
|
|
|
|
2020-09-25 09:50:53 +00:00
|
|
|
TEST_CASE("Create Dictionary, set value") {
|
2022-03-23 11:56:12 +00:00
|
|
|
auto dic = Dictionary<i32, i32>(5);
|
2020-03-22 15:38:48 +00:00
|
|
|
dic.Set(5, 100);
|
|
|
|
CHECK(dic.Has(5));
|
|
|
|
}
|
|
|
|
|
2020-09-25 09:50:53 +00:00
|
|
|
TEST_CASE("Create Dictionary, insert values, get count") {
|
2022-03-23 11:56:12 +00:00
|
|
|
auto dic = Dictionary<i32, i32>(5);
|
2020-03-22 15:38:48 +00:00
|
|
|
dic.Insert(10, 5);
|
|
|
|
dic.Insert(2, 100);
|
|
|
|
dic.Insert(9, 2000);
|
|
|
|
|
|
|
|
CHECK(dic.Count() == 3);
|
|
|
|
}
|
|
|
|
|
2020-09-25 09:50:53 +00:00
|
|
|
TEST_CASE("Create Dictionary, insert values, iterate over keys") {
|
2022-03-23 11:56:12 +00:00
|
|
|
auto dic = Dictionary<i32, i32>(5);
|
2020-03-22 15:38:48 +00:00
|
|
|
dic.Insert(10, 5);
|
|
|
|
dic.Insert(2, 100);
|
|
|
|
dic.Insert(9, 2000);
|
|
|
|
|
|
|
|
size_t i = 0;
|
2020-03-22 15:53:42 +00:00
|
|
|
for (auto val : dic) {
|
2020-03-22 15:38:48 +00:00
|
|
|
CHECK(dic.Has(val.first));
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
CHECK(i == 3);
|
|
|
|
}
|
|
|
|
|
2020-09-25 09:50:53 +00:00
|
|
|
TEST_CASE("Create Dictionary with different types, insert values, iterate over keys") {
|
2020-03-22 15:38:48 +00:00
|
|
|
auto dic = Dictionary<char, char>(5);
|
|
|
|
dic.Insert(10, 5);
|
|
|
|
dic.Insert(2, 100);
|
|
|
|
dic.Insert(9, 105);
|
|
|
|
|
|
|
|
size_t i = 0;
|
2020-03-22 15:53:42 +00:00
|
|
|
for (auto val : dic) {
|
2020-03-22 15:38:48 +00:00
|
|
|
CHECK(dic.Has(val.first));
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
CHECK(i == 3);
|
|
|
|
}
|
|
|
|
|
2022-05-16 14:36:52 +00:00
|
|
|
|
|
|
|
TEST_CASE("Create StringViewDictionary, insert values, get values by hash") {
|
|
|
|
auto dic = StringViewDictionary<i32>(5);
|
|
|
|
dic.Insert("foo"_cnc, 5);
|
|
|
|
dic.Insert("bar"_cnc, 100);
|
|
|
|
dic.Insert("zet"_cnc, 2000);
|
|
|
|
|
|
|
|
CHECK(dic.GetFromHash("bar"_cnc) == 100);
|
|
|
|
}
|
|
|
|
|
2020-03-22 15:38:48 +00:00
|
|
|
#endif
|