Add initializer list support for Dictionary.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
e4c203027b
commit
3bbb12f485
|
@ -1 +0,0 @@
|
|||
#include "Dictionary.hpp"
|
|
@ -10,9 +10,9 @@ namespace Arbutils::Collections {
|
|||
|
||||
using iterator = typename std::unordered_map<KeyT, ValueT, std::hash<KeyT>, std::equal_to<KeyT>,
|
||||
std::allocator<std::pair<const KeyT, ValueT>>>::iterator;
|
||||
|
||||
public:
|
||||
explicit Dictionary(size_t capacity) : _map(capacity) {}
|
||||
explicit Dictionary(const std::initializer_list<std::pair<const KeyT, ValueT>>& l) : _map(l){}
|
||||
|
||||
inline void Insert(KeyT key, ValueT value) {
|
||||
auto v = _map.insert({key, value});
|
||||
|
|
|
@ -10,6 +10,14 @@ TEST_CASE("Create Dictionary, insert values", "[Utilities]") {
|
|||
dic.Insert(9, 2000);
|
||||
}
|
||||
|
||||
TEST_CASE("Create Dictionary with initializer list", "[Utilities]") {
|
||||
auto dic = Dictionary<int, int>({{5, 100}, {10, 200}, {50, 2}});
|
||||
|
||||
CHECK(dic.Get(5) == 100);
|
||||
CHECK(dic.Get(10) == 200);
|
||||
CHECK(dic.Get(50) == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("Create Dictionary, insert values, get values", "[Utilities]") {
|
||||
auto dic = Dictionary<int, int>(5);
|
||||
dic.Insert(10, 5);
|
||||
|
@ -80,7 +88,7 @@ TEST_CASE("Create Dictionary, insert values, iterate over keys", "[Utilities]")
|
|||
dic.Insert(9, 2000);
|
||||
|
||||
size_t i = 0;
|
||||
for (auto val: dic){
|
||||
for (auto val : dic) {
|
||||
CHECK(dic.Has(val.first));
|
||||
i++;
|
||||
}
|
||||
|
@ -94,12 +102,11 @@ TEST_CASE("Create Dictionary with different types, insert values, iterate over k
|
|||
dic.Insert(9, 105);
|
||||
|
||||
size_t i = 0;
|
||||
for (auto val: dic){
|
||||
for (auto val : dic) {
|
||||
CHECK(dic.Has(val.first));
|
||||
i++;
|
||||
}
|
||||
CHECK(i == 3);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue