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 ea03916 commit ec6b244
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/xtd.core/include/xtd/collections/generic/dictionary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<data>(bucket_count, hash, equal, alloc)) {}
dictionary(size_type bucket_count, const allocator_type& alloc) noexcept : data_(xtd::new_ptr<data>(bucket_count, hasher {}, equator {}, alloc)) {}
dictionary(size_type bucket_count, const hasher& hash, const allocator_type& alloc) noexcept : data_(xtd::new_ptr<data>(bucket_count, hash, equator {}, alloc)) {}
explicit dictionary(const allocator_type& alloc) noexcept : data_(xtd::new_ptr<data>(alloc)) {}
template <typename input_iterator_t>
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<data>(first, last, bucket_count, hash, equal, alloc)) {}
template <typename input_iterator_t>
explicit dictionary(input_iterator_t first, input_iterator_t last, size_type bucket_count, const allocator_type& alloc) noexcept : data_(xtd::new_ptr<data>(first, last, bucket_count, hasher {}, equator {}, alloc)) {}
template <typename input_iterator_t>
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<data>(first, last, bucket_count, hash, equator {}, alloc)) {}
dictionary(const dictionary& other) noexcept : data_(xtd::new_ptr<data>(other.data_->items, other.data_->version, allocator_type {})) {}
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)) {}
/// @}

/// @name Public Properties
Expand Down Expand Up @@ -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 <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} {}

base_type items;
size_type version = 0;
xtd::object sync_root;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <xtd/tunit/assert>
#include <xtd/tunit/test_class_attribute>
#include <xtd/tunit/test_method_attribute>
#include <xtd/println>
#include <xtd/literals>

using namespace xtd;
using namespace xtd::collections::generic;
Expand Down Expand Up @@ -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<int, string> {42};
assert::is_zero(items.capacity());
assert::is_zero(items.count());
}

void test_method_(constructor_with_bucket_count_and_hash) {
auto items = dictionary<int, string> {42, helpers::hasher<int> {}};
assert::is_zero(items.capacity());
assert::is_zero(items.count());
}

void test_method_(constructor_with_bucket_count_hash_and_equator) {
auto items = dictionary<int, string> {42, helpers::hasher<int> {}, helpers::equator<int> {}};
assert::is_zero(items.capacity());
assert::is_zero(items.count());
}

void test_method_(constructor_with_bucket_count_hash_equator_and_hasher) {
auto items = dictionary<int, string> {42, helpers::hasher<int> {}, helpers::equator<int> {}, helpers::allocator<std::pair<int, string>> {}};
assert::is_zero(items.capacity());
assert::is_zero(items.count());
}

void test_method_(constructor_with_bucket_count_and_allocator) {
auto items = dictionary<int, string> {42, helpers::allocator<std::pair<int, string>> {}};
assert::is_zero(items.capacity());
assert::is_zero(items.count());
}

void test_method_(constructor_with_bucket_count_hasher_and_allocator) {
auto items = dictionary<int, string> {42, helpers::hasher<int> {}, helpers::allocator<std::pair<int, string>> {}};
assert::is_zero(items.capacity());
assert::is_zero(items.count());
}

void test_method_(constructor_with_allocator) {
auto items = dictionary<int, string> {helpers::allocator<std::pair<int, string>> {}};
assert::is_zero(items.capacity());
assert::is_zero(items.count());
}

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 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 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 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 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 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 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 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 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 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 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 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());
}
};
}

0 comments on commit ec6b244

Please sign in to comment.