Skip to content

Commit

Permalink
Add xtd::collections::generic::comparer unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gammasoft71 committed Sep 12, 2024
1 parent 73b3057 commit 7eaf7bf
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/xtd.core.unit_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ add_references(xtd.core)
add_sources(
src/main.cpp
src/xtd/collections/tests/key_not_found_exception_tests.cpp
src/xtd/collections/generic/tests/comparer_tests.cpp
src/xtd/collections/generic/tests/list_tests.cpp
src/xtd/collections/generic/tests/params_tests.cpp
src/xtd/configuration/tests/file_settings_tests.cpp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#include <xtd/collections/generic/comparer>
#include <xtd/icomparable>
#include <xtd/tunit/collection_assert>
#include <xtd/tunit/assert>
#include <xtd/tunit/test_class_attribute>
#include <xtd/tunit/test_method_attribute>

using namespace xtd;
using namespace xtd::collections::generic;
using namespace xtd::tunit;

namespace xtd::collections::generic::tests {
class test_class_(comparer_tests) {
void test_method_(default_comparer_with_boolean) {
assert::is_negative(comparer<boolean>::default_comparer.compare(false, true), csf_);
assert::is_zero(comparer<boolean>::default_comparer.compare(true, true), csf_);
assert::is_positive(comparer<boolean>::default_comparer.compare(true, false), csf_);
}

void test_method_(default_comparer_with_byte) {
assert::is_negative(comparer<byte>::default_comparer.compare(1, 4), csf_);
assert::is_zero(comparer<byte>::default_comparer.compare(4, 4), csf_);
assert::is_positive(comparer<byte>::default_comparer.compare(4, 1), csf_);
}

void test_method_(default_comparer_with_decimal) {
assert::is_negative(comparer<decimal>::default_comparer.compare(.1f, .4f), csf_);
assert::is_zero(comparer<decimal>::default_comparer.compare(.4f, .4f), csf_);
assert::is_positive(comparer<decimal>::default_comparer.compare(.4f, .1f), csf_);
}

void test_method_(default_comparer_with_double) {
assert::is_negative(comparer<double>::default_comparer.compare(.1f, .4f), csf_);
assert::is_zero(comparer<double>::default_comparer.compare(.4f, .4f), csf_);
assert::is_positive(comparer<double>::default_comparer.compare(.4f, .1f), csf_);
}

void test_method_(default_comparer_with_int16) {
assert::is_negative(comparer<int16>::default_comparer.compare(1, 4), csf_);
assert::is_zero(comparer<int16>::default_comparer.compare(4, 4), csf_);
assert::is_positive(comparer<int16>::default_comparer.compare(4, 1), csf_);
}

void test_method_(default_comparer_with_int32) {
assert::is_negative(comparer<int32>::default_comparer.compare(1, 4), csf_);
assert::is_zero(comparer<int32>::default_comparer.compare(4, 4), csf_);
assert::is_positive(comparer<int32>::default_comparer.compare(4, 1), csf_);
}

void test_method_(default_comparer_with_int64) {
assert::is_negative(comparer<int64>::default_comparer.compare(1, 4), csf_);
assert::is_zero(comparer<int64>::default_comparer.compare(4, 4), csf_);
assert::is_positive(comparer<int64>::default_comparer.compare(4, 1), csf_);
}

void test_method_(default_comparer_with_intptr) {
assert::is_negative(comparer<intptr>::default_comparer.compare(1, 4), csf_);
assert::is_zero(comparer<intptr>::default_comparer.compare(4, 4), csf_);
assert::is_positive(comparer<intptr>::default_comparer.compare(4, 1), csf_);
}

void test_method_(default_comparer_with_sbyte) {
assert::is_negative(comparer<sbyte>::default_comparer.compare(1, 4), csf_);
assert::is_zero(comparer<sbyte>::default_comparer.compare(4, 4), csf_);
assert::is_positive(comparer<sbyte>::default_comparer.compare(4, 1), csf_);
}

void test_method_(default_comparer_with_single) {
assert::is_negative(comparer<single>::default_comparer.compare(.1f, .4f), csf_);
assert::is_zero(comparer<single>::default_comparer.compare(.4f, .4f), csf_);
assert::is_positive(comparer<single>::default_comparer.compare(.4f, .1f), csf_);
}

void test_method_(default_comparer_with_string) {
assert::is_negative(comparer<string>::default_comparer.compare("a", "g"), csf_);
assert::is_zero(comparer<string>::default_comparer.compare("g", "g"), csf_);
assert::is_positive(comparer<string>::default_comparer.compare("g", "a"), csf_);
}

void test_method_(default_comparer_with_uint16) {
assert::is_negative(comparer<uint16>::default_comparer.compare(1, 4), csf_);
assert::is_zero(comparer<uint16>::default_comparer.compare(4, 4), csf_);
assert::is_positive(comparer<uint16>::default_comparer.compare(4, 1), csf_);
}

void test_method_(default_comparer_with_uint32) {
assert::is_negative(comparer<uint32>::default_comparer.compare(1, 4), csf_);
assert::is_zero(comparer<uint32>::default_comparer.compare(4, 4), csf_);
assert::is_positive(comparer<uint32>::default_comparer.compare(4, 1), csf_);
}

void test_method_(default_comparer_with_uint64) {
assert::is_negative(comparer<uint64>::default_comparer.compare(1, 4), csf_);
assert::is_zero(comparer<uint64>::default_comparer.compare(4, 4), csf_);
assert::is_positive(comparer<uint64>::default_comparer.compare(4, 1), csf_);
}

void test_method_(default_comparer_with_uintptr) {
assert::is_negative(comparer<uintptr>::default_comparer.compare(1, 4), csf_);
assert::is_zero(comparer<uintptr>::default_comparer.compare(4, 4), csf_);
assert::is_positive(comparer<uintptr>::default_comparer.compare(4, 1), csf_);
}

void test_method_(default_comparer_with_icomparable_object) {
struct foo : public icomparable<foo> {
foo(int value) : value{value} {}
int value = 0;
int compare_to(const foo& o) const noexcept override {return value < o.value ? -1 : (value > o.value ? 1 : 0);}
};

assert::is_negative(comparer<foo>::default_comparer.compare(foo {1}, foo {4}), csf_);
assert::is_zero(comparer<foo>::default_comparer.compare(foo {4}, foo {4}), csf_);
assert::is_positive(comparer<foo>::default_comparer.compare(foo {4}, foo {1}), csf_);
}

void test_method_(default_comparer_object_with_less_and_grether_operators) {
struct foo {
int value = 0;
bool operator <(const foo& o) const noexcept {return value < o.value;}
bool operator >(const foo& o) const noexcept {return value > o.value;}
};

assert::is_negative(comparer<foo>::default_comparer.compare(foo {1}, foo {4}), csf_);
assert::is_zero(comparer<foo>::default_comparer.compare(foo {4}, foo {4}), csf_);
assert::is_positive(comparer<foo>::default_comparer.compare(foo {4}, foo {1}), csf_);
}
};
}

0 comments on commit 7eaf7bf

Please sign in to comment.