From ec6b244f4744de723e894f41030c03e41ce129a0 Mon Sep 17 00:00:00 2001 From: Gammasoft Date: Wed, 8 Jan 2025 19:14:43 +0100 Subject: [PATCH] Add dictionary constructors and unit tests --- .../xtd/collections/generic/dictionary.hpp | 22 +++ .../generic/tests/dictionary_tests.cpp | 125 +++++++++++++++++- 2 files changed, 146 insertions(+), 1 deletion(-) diff --git a/src/xtd.core/include/xtd/collections/generic/dictionary.hpp b/src/xtd.core/include/xtd/collections/generic/dictionary.hpp index a9ffafc1b4d..56ebdd5d761 100644 --- a/src/xtd.core/include/xtd/collections/generic/dictionary.hpp +++ b/src/xtd.core/include/xtd/collections/generic/dictionary.hpp @@ -122,6 +122,21 @@ namespace xtd { /// } /// ``` dictionary() noexcept = default; + + explicit dictionary(size_type bucket_count, const hasher& hash = hasher {}, const equator& equal = equator {}, const allocator_type& alloc = allocator_type {}) noexcept : data_(xtd::new_ptr(bucket_count, hash, equal, alloc)) {} + dictionary(size_type bucket_count, const allocator_type& alloc) noexcept : data_(xtd::new_ptr(bucket_count, hasher {}, equator {}, alloc)) {} + dictionary(size_type bucket_count, const hasher& hash, const allocator_type& alloc) noexcept : data_(xtd::new_ptr(bucket_count, hash, equator {}, alloc)) {} + explicit dictionary(const allocator_type& alloc) noexcept : data_(xtd::new_ptr(alloc)) {} + template + explicit dictionary(input_iterator_t first, input_iterator_t last, size_type bucket_count = 0, const hasher& hash = hasher {}, const equator& equal = equator {}, const allocator_type& alloc = allocator_type {}) noexcept : data_(xtd::new_ptr(first, last, bucket_count, hash, equal, alloc)) {} + template + explicit dictionary(input_iterator_t first, input_iterator_t last, size_type bucket_count, const allocator_type& alloc) noexcept : data_(xtd::new_ptr(first, last, bucket_count, hasher {}, equator {}, alloc)) {} + template + explicit dictionary(input_iterator_t first, input_iterator_t last, size_type bucket_count, const hasher& hash, const allocator_type& alloc) noexcept : data_(xtd::new_ptr(first, last, bucket_count, hash, equator {}, alloc)) {} + dictionary(const dictionary& other) noexcept : data_(xtd::new_ptr(other.data_->items, other.data_->version, allocator_type {})) {} + dictionary(const dictionary& other, const allocator_type& alloc) noexcept : data_(xtd::new_ptr(other.data_->items, other.data_->version, alloc)) {} + dictionary(const std::unordered_map& other) noexcept : data_(xtd::new_ptr(other.begin(), other.end(), 0, hasher {}, equator {}, allocator_type {})) {} + dictionary(const std::unordered_map& other, const allocator_type& alloc) noexcept : data_(xtd::new_ptr(other.begin(), other.end(), 0, hasher {}, equator {}, alloc)) {} /// @} /// @name Public Properties @@ -242,6 +257,13 @@ namespace xtd { private: struct data { + data() = default; + data(size_type bucket_count, const hasher& hash, const equator& equal, const allocator_type& alloc) noexcept : items(bucket_count, hash, equal, alloc) {} + explicit data(const allocator_type& alloc) noexcept : items(alloc) {} + template + 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} {} + base_type items; size_type version = 0; xtd::object sync_root; diff --git a/tests/xtd.core.unit_tests/src/xtd/collections/generic/tests/dictionary_tests.cpp b/tests/xtd.core.unit_tests/src/xtd/collections/generic/tests/dictionary_tests.cpp index 25cabf84380..48fd78915f4 100644 --- a/tests/xtd.core.unit_tests/src/xtd/collections/generic/tests/dictionary_tests.cpp +++ b/tests/xtd.core.unit_tests/src/xtd/collections/generic/tests/dictionary_tests.cpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include using namespace xtd; using namespace xtd::collections::generic; @@ -109,5 +109,128 @@ namespace xtd::collections::generic::tests { assert::is_zero(items.capacity()); assert::is_zero(items.count()); } + + void test_method_(constructor_with_bucket_count) { + auto items = dictionary {42}; + assert::is_zero(items.capacity()); + assert::is_zero(items.count()); + } + + void test_method_(constructor_with_bucket_count_and_hash) { + auto items = dictionary {42, helpers::hasher {}}; + assert::is_zero(items.capacity()); + assert::is_zero(items.count()); + } + + void test_method_(constructor_with_bucket_count_hash_and_equator) { + auto items = dictionary {42, helpers::hasher {}, helpers::equator {}}; + assert::is_zero(items.capacity()); + assert::is_zero(items.count()); + } + + void test_method_(constructor_with_bucket_count_hash_equator_and_hasher) { + auto items = dictionary {42, helpers::hasher {}, helpers::equator {}, helpers::allocator> {}}; + assert::is_zero(items.capacity()); + assert::is_zero(items.count()); + } + + void test_method_(constructor_with_bucket_count_and_allocator) { + auto items = dictionary {42, helpers::allocator> {}}; + assert::is_zero(items.capacity()); + assert::is_zero(items.count()); + } + + void test_method_(constructor_with_bucket_count_hasher_and_allocator) { + auto items = dictionary {42, helpers::hasher {}, helpers::allocator> {}}; + assert::is_zero(items.capacity()); + assert::is_zero(items.count()); + } + + void test_method_(constructor_with_allocator) { + auto items = dictionary {helpers::allocator> {}}; + assert::is_zero(items.capacity()); + assert::is_zero(items.count()); + } + + void test_method_(constructor_with_first_and_last_iterators) { + auto init = std::initializer_list> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5_z, "five"}}; + auto items = dictionary {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> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5_z, "five"}}; + auto items = dictionary {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> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5_z, "five"}}; + auto items = dictionary {init.begin(), init.end(), 42, helpers::hasher {}}; + 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> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5_z, "five"}}; + auto items = dictionary {init.begin(), init.end(), 42, helpers::hasher {}, helpers::equator {}}; + 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> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5_z, "five"}}; + auto items = dictionary {init.begin(), init.end(), 42, helpers::hasher {}, helpers::equator {}, helpers::allocator> {}}; + 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> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5_z, "five"}}; + auto items = dictionary {init.begin(), init.end(), 42, helpers::allocator> {}}; + 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> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5_z, "five"}}; + auto items = dictionary {init.begin(), init.end(), 42, helpers::hasher {}, helpers::allocator> {}}; + 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> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5_z, "five"}}; + auto items1 = dictionary {init.begin(), init.end()}; + auto items2 = dictionary {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> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5_z, "five"}}; + auto items1 = dictionary {init.begin(), init.end()}; + auto items2 = dictionary {items1, helpers::allocator> {}}; + 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> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5_z, "five"}}; + auto items1 = std::unordered_map {init.begin(), init.end()}; + auto items2 = dictionary {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> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5_z, "five"}}; + auto items1 = std::unordered_map {init.begin(), init.end()}; + auto items2 = dictionary {items1, helpers::allocator> {}}; + assert::are_equal(5_z, items2.capacity()); + assert::are_equal(5_z, items2.count()); + } }; }