Skip to content

Commit

Permalink
Add dictionary constructors and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gammasoft71 committed Jan 8, 2025
1 parent ec6b244 commit 753513c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
15 changes: 14 additions & 1 deletion src/xtd.core/include/xtd/collections/generic/dictionary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ namespace xtd {
dictionary(const dictionary& other, const allocator_type& alloc) noexcept : data_(xtd::new_ptr<data>(other.data_->items, other.data_->version, alloc)) {}
dictionary(const std::unordered_map<key_t, value_t>& other) noexcept : data_(xtd::new_ptr<data>(other.begin(), other.end(), 0, hasher {}, equator {}, allocator_type {})) {}
dictionary(const std::unordered_map<key_t, value_t>& other, const allocator_type& alloc) noexcept : data_(xtd::new_ptr<data>(other.begin(), other.end(), 0, hasher {}, equator {}, alloc)) {}
dictionary(dictionary&& other) noexcept = default;
dictionary(dictionary&& other, const allocator_type& alloc) noexcept : data_(xtd::new_ptr<data>(std::move(other.data_->items), other.data_->version, alloc)) {}
template <typename init_key_t, typename init_value_t>
dictionary(std::initializer_list<key_value_pair<init_key_t, init_value_t>> init, size_type bucket_count = 0, const hasher& hash = hasher {}, const equator& equal = equator {}, const allocator_type& alloc = allocator_type {}) noexcept : data_(xtd::new_ptr<data>(bucket_count, hash, equal, alloc)) {
for (const auto& [key, value] : init)
data_->items.insert({key, value});
}
template <typename init_key_t, typename init_value_t>
dictionary(std::initializer_list<std::pair<init_key_t, init_value_t>> init, size_type bucket_count = 0, const hasher& hash = hasher {}, const equator& equal = equator {}, const allocator_type& alloc = allocator_type {}) noexcept : data_(xtd::new_ptr<data>(bucket_count, hash, equal, alloc)) {
for (const auto& [key, value] : init)
data_->items.insert({key, value});
}
/// @}

/// @name Public Properties
Expand Down Expand Up @@ -236,7 +248,7 @@ namespace xtd {
protected:
bool reset_ = true;
const dictionary& items_;
dictionary::base_type::const_iterator iterator_ = items_.items().cend();
typename dictionary::base_type::const_iterator iterator_ = items_.items().cend();
size_type version_ = 0;
};
return {new_ptr<internal_enumerator>(*this, data_->version)};
Expand All @@ -263,6 +275,7 @@ namespace xtd {
template <typename input_iterator_t>
data(input_iterator_t first, input_iterator_t last, size_type bucket_count, const hasher& hash, const equator& equal, const allocator_type& alloc) noexcept : items(first, last, bucket_count, hash, equal, alloc) {}
data(const base_type& items, size_type version, const allocator_type& alloc) noexcept : items {items, alloc}, version {version} {}
data(base_type&& items, size_type version, const allocator_type& alloc) noexcept : items {items, alloc}, version {version} {}

base_type items;
size_type version = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,84 +153,96 @@ namespace xtd::collections::generic::tests {
}

void test_method_(constructor_with_first_and_last_iterators) {
auto init = std::initializer_list<std::pair<int, string>> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5_z, "five"}};
auto init = std::initializer_list<std::pair<int, string>> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5, "five"}};
auto items = dictionary<int, string> {init.begin(), init.end()};
assert::are_equal(5_z, items.capacity());
assert::are_equal(5_z, items.count());
}

void test_method_(constructor_with_first_and_last_iterators_and_bucket_count) {
auto init = std::initializer_list<std::pair<int, string>> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5_z, "five"}};
auto init = std::initializer_list<std::pair<int, string>> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5, "five"}};
auto items = dictionary<int, string> {init.begin(), init.end(), 42};
assert::are_equal(5_z, items.capacity());
assert::are_equal(5_z, items.count());
}

void test_method_(constructor_with_first_and_last_iterators_bucket_count_and_hasher) {
auto init = std::initializer_list<std::pair<int, string>> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5_z, "five"}};
auto init = std::initializer_list<std::pair<int, string>> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5, "five"}};
auto items = dictionary<int, string> {init.begin(), init.end(), 42, helpers::hasher<int> {}};
assert::are_equal(5_z, items.capacity());
assert::are_equal(5_z, items.count());
}

void test_method_(constructor_with_first_and_last_iterators_bucket_count_hasher_and_equator) {
auto init = std::initializer_list<std::pair<int, string>> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5_z, "five"}};
auto init = std::initializer_list<std::pair<int, string>> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5, "five"}};
auto items = dictionary<int, string> {init.begin(), init.end(), 42, helpers::hasher<int> {}, helpers::equator<int> {}};
assert::are_equal(5_z, items.capacity());
assert::are_equal(5_z, items.count());
}

void test_method_(constructor_with_first_and_last_iterators_bucket_count_hasher_equator_and_allocator) {
auto init = std::initializer_list<std::pair<int, string>> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5_z, "five"}};
auto init = std::initializer_list<std::pair<int, string>> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5, "five"}};
auto items = dictionary<int, string> {init.begin(), init.end(), 42, helpers::hasher<int> {}, helpers::equator<int> {}, helpers::allocator<std::pair<int, string>> {}};
assert::are_equal(5_z, items.capacity());
assert::are_equal(5_z, items.count());
}

void test_method_(constructor_with_first_and_last_iterators_bucket_count_and_allocator) {
auto init = std::initializer_list<std::pair<int, string>> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5_z, "five"}};
auto init = std::initializer_list<std::pair<int, string>> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5, "five"}};
auto items = dictionary<int, string> {init.begin(), init.end(), 42, helpers::allocator<std::pair<int, string>> {}};
assert::are_equal(5_z, items.capacity());
assert::are_equal(5_z, items.count());
}

void test_method_(constructor_with_first_and_last_iterators_bucket_count_hasher_and_allocator) {
auto init = std::initializer_list<std::pair<int, string>> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5_z, "five"}};
auto init = std::initializer_list<std::pair<int, string>> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5, "five"}};
auto items = dictionary<int, string> {init.begin(), init.end(), 42, helpers::hasher<int> {}, helpers::allocator<std::pair<int, string>> {}};
assert::are_equal(5_z, items.capacity());
assert::are_equal(5_z, items.count());
}

void test_method_(constructor_with_dictionary) {
auto init = std::initializer_list<std::pair<int, string>> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5_z, "five"}};
auto init = std::initializer_list<std::pair<int, string>> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5, "five"}};
auto items1 = dictionary<int, string> {init.begin(), init.end()};
auto items2 = dictionary<int, string> {items1};
assert::are_equal(5_z, items2.capacity());
assert::are_equal(5_z, items2.count());
}

void test_method_(constructor_with_dictionary_and_allocator) {
auto init = std::initializer_list<std::pair<int, string>> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5_z, "five"}};
auto init = std::initializer_list<std::pair<int, string>> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5, "five"}};
auto items1 = dictionary<int, string> {init.begin(), init.end()};
auto items2 = dictionary<int, string> {items1, helpers::allocator<std::pair<int, string>> {}};
assert::are_equal(5_z, items2.capacity());
assert::are_equal(5_z, items2.count());
}

void test_method_(constructor_with_unordered_map) {
auto init = std::initializer_list<std::pair<int, string>> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5_z, "five"}};
auto init = std::initializer_list<std::pair<int, string>> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5, "five"}};
auto items1 = std::unordered_map<int, string> {init.begin(), init.end()};
auto items2 = dictionary<int, string> {items1};
assert::are_equal(5_z, items2.capacity());
assert::are_equal(5_z, items2.count());
}

void test_method_(constructor_with_unordered_map_and_allocator) {
auto init = std::initializer_list<std::pair<int, string>> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5_z, "five"}};
auto init = std::initializer_list<std::pair<int, string>> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5, "five"}};
auto items1 = std::unordered_map<int, string> {init.begin(), init.end()};
auto items2 = dictionary<int, string> {items1, helpers::allocator<std::pair<int, string>> {}};
assert::are_equal(5_z, items2.capacity());
assert::are_equal(5_z, items2.count());
}

void test_method_(constructor_with_value_type_initializer_list) {
auto items = dictionary<int, string> {{key_value_pair<>::create(1, "one"), key_value_pair<>::create(2, "two"), key_value_pair<>::create(3, "three"), key_value_pair<>::create(4, "four"), key_value_pair<>::create(5, "five")}};
assert::are_equal(5_z, items.capacity());
assert::are_equal(5_z, items.count());
}

void test_method_(constructor_with_base_value_type_initializer_list) {
auto items = dictionary<int, string> {{std::make_pair(1, "one"), std::make_pair(2, "two"), std::make_pair(3, "three"), std::make_pair(4, "four"), std::make_pair(5, "five")}};
assert::are_equal(5_z, items.capacity());
assert::are_equal(5_z, items.count());
}
};
}

0 comments on commit 753513c

Please sign in to comment.