Skip to content

Commit

Permalink
using xtd::string_comparer class
Browse files Browse the repository at this point in the history
  • Loading branch information
gammasoft71 committed Jan 15, 2025
1 parent 448a001 commit ffe9c09
Showing 1 changed file with 4 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <xtd/environment>
#include <xtd/literals>
#include <xtd/size_object>
#include <xtd/string_comparer>

using namespace xtd;
using namespace xtd::collections::generic;
Expand Down Expand Up @@ -123,43 +124,28 @@ namespace xtd::collections::generic::tests {
}

void test_method_(constructor_with_ienumerable_and_equality_comparer) {
struct lower_string_comparer : iequality_comparer<string> {
bool equals(const string& x, const string& y) const noexcept override {return x.to_lower().equals(y.to_lower());}
xtd::size get_hash_code(const string& obj) const noexcept override {return obj.to_lower().get_hash_code();}
};

const ienumerable<key_value_pair<string, string>>& items1 = array<key_value_pair<string, string>> {{"one", "one"}, {"oNe", "oNe"}, {"OnE", "OnE"}, {"ONE", "ONE"}};
auto items2 = dictionary<string, string>(items1);
collection_assert::are_equivalent(array<key_value_pair<string, string>> {{"one", "one"}, {"oNe", "oNe"}, {"OnE", "OnE"}, {"ONE", "ONE"}}, items2);
assert::throws<argument_exception>([&]{items2 = dictionary<string, string>(items1, lower_string_comparer {});});
assert::throws<argument_exception>([&]{items2 = dictionary<string, string>(items1, string_comparer::ordinal_ignore_case());});
}

void test_method_(constructor_with_equality_comparer) {
struct lower_string_comparer : iequality_comparer<string> {
bool equals(const string& x, const string& y) const noexcept override {return x.to_lower().equals(y.to_lower());}
xtd::size get_hash_code(const string& obj) const noexcept override {return obj.to_lower().get_hash_code();}
};

auto items = dictionary<string, string> {};
items.add("one", "one");
items.add("oNe", "oNe");
items.add("OnE", "OnE");
items.add("ONE", "ONE");
collection_assert::are_equivalent(array<key_value_pair<string, string>> {{"one", "one"}, {"oNe", "oNe"}, {"OnE", "OnE"}, {"ONE", "ONE"}}, items);

items = dictionary<string, string> {lower_string_comparer {}};
items = dictionary<string, string> {string_comparer::ordinal_ignore_case()};
items.add("one", "one");
assert::throws<argument_exception>([&]{items.add("oNe", "oNe");});
assert::throws<argument_exception>([&]{items.add("OnE", "OnE");});
assert::throws<argument_exception>([&]{items.add("ONE", "ONE");});
}

void test_method_(constructor_with_capacity_and_equality_comparer) {
struct lower_string_comparer : iequality_comparer<string> {
bool equals(const string& x, const string& y) const noexcept override {return x.to_lower().equals(y.to_lower());}
xtd::size get_hash_code(const string& obj) const noexcept override {return obj.to_lower().get_hash_code();}
};

auto items = dictionary<string, string> {10_z};
assert::is_greater_or_equal(items.capacity(), 10_z);
items.add("one", "one");
Expand All @@ -168,7 +154,7 @@ namespace xtd::collections::generic::tests {
items.add("ONE", "ONE");
collection_assert::are_equivalent(array<key_value_pair<string, string>> {{"one", "one"}, {"oNe", "oNe"}, {"OnE", "OnE"}, {"ONE", "ONE"}}, items);

items = dictionary<string, string> {10_z, lower_string_comparer {}};
items = dictionary<string, string> {10_z, string_comparer::ordinal_ignore_case()};
assert::is_greater_or_equal(items.capacity(), 10_z);
items.add("one", "one");
assert::throws<argument_exception>([&]{items.add("oNe", "oNe");});
Expand Down

0 comments on commit ffe9c09

Please sign in to comment.