From 4d48082f4fd6df53d2c705dd741fd9bd02ce0837 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Wed, 1 Dec 2021 18:42:22 +0000 Subject: [PATCH] contains() & transparent comparator for map, multimap, set and multiset --- include/etl/map.h | 1 + include/etl/multimap.h | 2 +- include/etl/multiset.h | 35 ++-- include/etl/set.h | 30 +++- test/test_multiset.cpp | 394 ++++++++++++++++++++++++++++++++++++++--- test/test_set.cpp | 371 +++++++++++++++++++++++++++++--------- 6 files changed, 712 insertions(+), 121 deletions(-) diff --git a/include/etl/map.h b/include/etl/map.h index 4d7065aa..ce8346df 100644 --- a/include/etl/map.h +++ b/include/etl/map.h @@ -1382,6 +1382,7 @@ namespace etl return find(key) != end(); } + //************************************************************************* template bool contains(const K& k) const { diff --git a/include/etl/multimap.h b/include/etl/multimap.h index d693a04c..e11263ba 100644 --- a/include/etl/multimap.h +++ b/include/etl/multimap.h @@ -750,7 +750,7 @@ namespace etl friend class imultimap; friend class const_iterator; - iterator() + iterator() : p_multimap(ETL_NULLPTR) , p_node(ETL_NULLPTR) { diff --git a/include/etl/multiset.h b/include/etl/multiset.h index b37ca157..2e28531b 100644 --- a/include/etl/multiset.h +++ b/include/etl/multiset.h @@ -661,10 +661,12 @@ namespace etl { return compare(node1.value, node2.value); } + bool node_comp(const Data_Node& node, key_parameter_t key) const { return compare(node.value, key); } + bool node_comp(key_parameter_t key, const Data_Node& node) const { return compare(key, node.value); @@ -678,7 +680,6 @@ namespace etl template bool node_comp(const K& key, const Data_Node& node) const - { return compare(key, node.value); } @@ -1085,22 +1086,36 @@ namespace etl /// Returns two iterators with bounding (lower bound, upper bound) the key /// provided //************************************************************************* - ETL_OR_STD::pair equal_range(const_reference key) + ETL_OR_STD::pair equal_range(key_parameter_t key) { - return ETL_OR_STD::make_pair( - iterator(*this, find_lower_node(root_node, key)), - iterator(*this, find_upper_node(root_node, key))); + return ETL_OR_STD::make_pair(iterator(*this, find_lower_node(root_node, key)), + iterator(*this, find_upper_node(root_node, key))); + } + + //************************************************************************* + template + ETL_OR_STD::pair equal_range(const K& key) + { + return ETL_OR_STD::make_pair(iterator(*this, find_lower_node(root_node, key)), + iterator(*this, find_upper_node(root_node, key))); } //************************************************************************* /// Returns two const iterators with bounding (lower bound, upper bound) /// the key provided. //************************************************************************* - ETL_OR_STD::pair equal_range(const_reference key) const + ETL_OR_STD::pair equal_range(key_parameter_t key) const { - return ETL_OR_STD::make_pair( - const_iterator(*this, find_lower_node(root_node, key)), - const_iterator(*this, find_upper_node(root_node, key))); + return ETL_OR_STD::make_pair(const_iterator(*this, find_lower_node(root_node, key)), + const_iterator(*this, find_upper_node(root_node, key))); + } + + //************************************************************************* + template + ETL_OR_STD::pair equal_range(key_parameter_t key) const + { + return ETL_OR_STD::make_pair(const_iterator(*this, find_lower_node(root_node, key)), + const_iterator(*this, find_upper_node(root_node, key))); } //************************************************************************* @@ -1454,7 +1469,7 @@ namespace etl //************************************************************************* /// Check if the set contains the key. //************************************************************************* - bool contains(const TKey& key) const + bool contains(key_parameter_t key) const { return find(key) != end(); } diff --git a/include/etl/set.h b/include/etl/set.h index d9ed0b20..36fd2e7a 100644 --- a/include/etl/set.h +++ b/include/etl/set.h @@ -956,22 +956,36 @@ namespace etl /// Returns two iterators with bounding (lower bound, upper bound) the /// value provided //************************************************************************* - ETL_OR_STD::pair equal_range(const_reference value) + ETL_OR_STD::pair equal_range(key_parameter_t key) { - return ETL_OR_STD::make_pair( - iterator(*this, find_lower_node(root_node, value)), - iterator(*this, find_upper_node(root_node, value))); + return ETL_OR_STD::make_pair(iterator(*this, find_lower_node(root_node, key)), + iterator(*this, find_upper_node(root_node, key))); + } + + //************************************************************************* + template + ETL_OR_STD::pair equal_range(const K& key) + { + return ETL_OR_STD::make_pair(iterator(*this, find_lower_node(root_node, key)), + iterator(*this, find_upper_node(root_node, key))); } //************************************************************************* /// Returns two const iterators with bounding (lower bound, upper bound) /// the value provided. //************************************************************************* - ETL_OR_STD::pair equal_range(const_reference value) const + ETL_OR_STD::pair equal_range(key_parameter_t key) const { - return ETL_OR_STD::make_pair( - const_iterator(*this, find_lower_node(root_node, value)), - const_iterator(*this, find_upper_node(root_node, value))); + return ETL_OR_STD::make_pair(const_iterator(*this, find_lower_node(root_node, key)), + const_iterator(*this, find_upper_node(root_node, key))); + } + + //************************************************************************* + template + ETL_OR_STD::pair equal_range(const K& key) const + { + return ETL_OR_STD::make_pair(const_iterator(*this, find_lower_node(root_node, key)), + const_iterator(*this, find_upper_node(root_node, key))); } //************************************************************************* diff --git a/test/test_multiset.cpp b/test/test_multiset.cpp index 32c72b76..55fd18e9 100644 --- a/test/test_multiset.cpp +++ b/test/test_multiset.cpp @@ -78,6 +78,26 @@ using Compare_Data_const_iterator = Compare_Data::const_iterator; namespace { + struct Key + { + Key(int k_) + : k(k_) + { + } + + int k; + }; + + bool operator <(const Key& lhs, const int& rhs) + { + return (lhs.k < rhs); + } + + bool operator <(const int& lhs, const Key& rhs) + { + return (lhs < rhs.k); + } + SUITE(test_multiset) { //************************************************************************* @@ -632,18 +652,14 @@ namespace Data data(random_data.begin(), random_data.end()); // Test a number not available - ETL_OR_STD::pair data_result = - data.equal_range(1); - ETL_OR_STD::pair compare_result = - compare_data.equal_range(1); + ETL_OR_STD::pair data_result = data.equal_range(1); + ETL_OR_STD::pair compare_result = compare_data.equal_range(1); // Check that both return the same return results CHECK_EQUAL(*compare_result.first, *data_result.first); CHECK_EQUAL(*compare_result.second, *data_result.second); - bool isEqual = Check_Equal(data.begin(), - data.end(), - compare_data.begin()); + bool isEqual = Check_Equal(data.begin(), data.end(), compare_data.begin()); CHECK(isEqual); } @@ -655,18 +671,14 @@ namespace const Data data(initial_data.begin(), initial_data.end()); // Test a number with several of the same key - ETL_OR_STD::pair data_result = - data.equal_range(2); - ETL_OR_STD::pair compare_result = - compare_data.equal_range(2); + ETL_OR_STD::pair data_result = data.equal_range(2); + ETL_OR_STD::pair compare_result = compare_data.equal_range(2); // Check that both return the same return results CHECK_EQUAL(*compare_result.first, *data_result.first); CHECK_EQUAL(*compare_result.second, *data_result.second); - bool isEqual = Check_Equal(data.begin(), - data.end(), - compare_data.begin()); + bool isEqual = Check_Equal(data.begin(), data.end(), compare_data.begin()); CHECK(isEqual); } @@ -697,9 +709,41 @@ namespace // Check that both return the same return results CHECK_EQUAL(compare_count, data_count); - bool isEqual = Check_Equal(data.begin(), - data.end(), - compare_data.begin()); + bool isEqual = Check_Equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_erase_value_using_transparent_comparator) + { + using CSet = std::multiset>; + CSet compare_data(initial_data.begin(), initial_data.end()); + + using ESet = etl::multiset>; + ESet data(initial_data.begin(), initial_data.end()); + + size_t compare_count = compare_data.erase(2); + size_t data_count = data.erase(Key(2)); + + // Check that both return the same return results + CHECK_EQUAL(compare_count, data_count); + + // Erase another value + compare_count = compare_data.erase(1); + data_count = data.erase(1); + + // Check that both return the same return results + CHECK_EQUAL(compare_count, data_count); + + // Erase another value + compare_count = compare_data.erase(3); + data_count = data.erase(3); + + // Check that both return the same return results + CHECK_EQUAL(compare_count, data_count); + + bool isEqual = Check_Equal(data.begin(), data.end(), compare_data.begin()); CHECK(isEqual); } @@ -718,9 +762,7 @@ namespace CHECK_EQUAL(*i_compare1, *i_data1); - bool isEqual = Check_Equal(data.begin(), - data.end(), - compare_data.begin()); + bool isEqual = Check_Equal(data.begin(), data.end(), compare_data.begin()); CHECK(isEqual); } @@ -817,6 +859,24 @@ namespace CHECK_EQUAL(compare_data.count(99), data.count(99)); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_count_using_transparent_comparator) + { + using CSet = std::multiset>; + const CSet compare_data(initial_data.begin(), initial_data.end()); + + using ESet = etl::multiset>; + const ESet data(initial_data.begin(), initial_data.end()); + + CHECK_EQUAL(compare_data.count(-1), data.count(Key(-1))); + CHECK_EQUAL(compare_data.count(0), data.count(Key(0))); + CHECK_EQUAL(compare_data.count(1), data.count(Key(1))); + CHECK_EQUAL(compare_data.count(2), data.count(Key(2))); + CHECK_EQUAL(compare_data.count(3), data.count(Key(3))); + CHECK_EQUAL(compare_data.count(4), data.count(Key(4))); + CHECK_EQUAL(compare_data.count(99), data.count(Key(99))); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_iterator) { @@ -918,6 +978,54 @@ namespace CHECK(compare_data.end() == i_compare); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_find_using_transparent_comparator) + { + using CSet = std::multiset>; + CSet compare_data(initial_data.begin(), initial_data.end()); + + using ESet = etl::multiset>; + ESet data(initial_data.begin(), initial_data.end()); + + ESet::iterator i_data = data.find(Key(0)); + CSet::iterator i_compare = compare_data.find(0); + + // Check that both return successful return results + CHECK_EQUAL(*i_compare, *i_data); + + i_data = data.find(Key(1)); + i_compare = compare_data.find(1); + + // Check that both return successful return results + CHECK_EQUAL(*i_compare, *i_data); + + i_data = data.find(Key(2)); + i_compare = compare_data.find(2); + + // Check that both return successful return results + CHECK_EQUAL(*i_compare, *i_data); + + i_data = data.find(Key(3)); + i_compare = compare_data.find(3); + + // Check that both return successful return results + CHECK_EQUAL(*i_compare, *i_data); + + i_data = data.find(Key(-1)); + i_compare = compare_data.find(-1); + + // Check that both return successful return results + CHECK(data.end() == i_data); + CHECK(compare_data.end() == i_compare); + + i_data = data.find(Key(99)); + i_compare = compare_data.find(99); + + // Check that both return successful return results + CHECK(data.end() == i_data); + CHECK(compare_data.end() == i_compare); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_find_const) { @@ -963,6 +1071,54 @@ namespace CHECK(compare_data.end() == i_compare); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_find_const_using_transparent_comparator) + { + using CSet = std::multiset>; + CSet compare_data(initial_data.begin(), initial_data.end()); + + using ESet = etl::multiset>; + ESet data(initial_data.begin(), initial_data.end()); + + ESet::const_iterator i_data = data.find(Key(0)); + CSet::const_iterator i_compare = compare_data.find(0); + + // Check that both return successful return results + CHECK_EQUAL(*i_compare, *i_data); + + i_data = data.find(Key(1)); + i_compare = compare_data.find(1); + + // Check that both return successful return results + CHECK_EQUAL(*i_compare, *i_data); + + i_data = data.find(Key(2)); + i_compare = compare_data.find(2); + + // Check that both return successful return results + CHECK_EQUAL(*i_compare, *i_data); + + i_data = data.find(Key(3)); + i_compare = compare_data.find(3); + + // Check that both return successful return results + CHECK_EQUAL(*i_compare, *i_data); + + i_data = data.find(Key(-1)); + i_compare = compare_data.find(-1); + + // Check that both return successful return results + CHECK(data.end() == i_data); + CHECK(compare_data.end() == i_compare); + + i_data = data.find(Key(99)); + i_compare = compare_data.find(99); + + // Check that both return successful return results + CHECK(data.end() == i_data); + CHECK(compare_data.end() == i_compare); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_equal) { @@ -1022,6 +1178,30 @@ namespace #endif } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_lower_bound_using_transparent_comparator) + { + using CSet = std::multiset>; + CSet compare_data(initial_data.begin(), initial_data.end()); + + using ESet = etl::multiset>; + ESet data(initial_data.begin(), initial_data.end()); + + CSet::iterator i_compare = compare_data.lower_bound(2); + ESet::iterator i_data = data.lower_bound(Key(2)); + CHECK_EQUAL(*i_compare, *i_data); + + i_compare = compare_data.lower_bound(-1); + i_data = data.lower_bound(Key(-1)); + CHECK_EQUAL(*i_compare, *i_data); + + i_compare = compare_data.lower_bound(99); + CHECK(compare_data.end() == i_compare); + + i_data = data.lower_bound(Key(99)); + CHECK(data.end() == i_data); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_lower_bound_const) { @@ -1055,6 +1235,30 @@ namespace #endif } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_lower_bound_const_using_transparent_comparator) + { + using CSet = std::multiset>; + CSet compare_data(initial_data.begin(), initial_data.end()); + + using ESet = etl::multiset>; + ESet data(initial_data.begin(), initial_data.end()); + + CSet::const_iterator i_compare = compare_data.lower_bound(4); + ESet::const_iterator i_data = data.lower_bound(Key(4)); + CHECK_EQUAL(*i_compare, *i_data); + + i_compare = compare_data.lower_bound(-1); + i_data = data.lower_bound(Key(-1)); + CHECK_EQUAL(*i_compare, *i_data); + + i_compare = compare_data.lower_bound(99); + CHECK(compare_data.end() == i_compare); + + i_data = data.lower_bound(Key(99)); + CHECK(data.end() == i_data); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_upper_bound) { @@ -1088,10 +1292,34 @@ namespace #endif } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_upper_bound_using_transparent_comparator) + { + using CSet = std::multiset>; + CSet compare_data(initial_data.begin(), initial_data.end()); + + using ESet = etl::multiset>; + ESet data(initial_data.begin(), initial_data.end()); + + CSet::iterator i_compare = compare_data.upper_bound(1); + ESet::iterator i_data = data.upper_bound(Key(1)); + CHECK_EQUAL(*i_compare, *i_data); + + i_compare = compare_data.upper_bound(-1); + i_data = data.upper_bound(Key(-1)); + CHECK_EQUAL(*i_compare, *i_data); + + i_compare = compare_data.upper_bound(99); + CHECK(compare_data.end() == i_compare); + + i_data = data.upper_bound(Key(99)); + CHECK(data.end() == i_data); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_upper_bound_const) { - Compare_Data compare_data(initial_data.begin(), initial_data.end()); + const Compare_Data compare_data(initial_data.begin(), initial_data.end()); const Data data(initial_data.begin(), initial_data.end()); Compare_Data::const_iterator i_compare = compare_data.upper_bound(3); @@ -1121,6 +1349,30 @@ namespace #endif } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_upper_bound_const_using_transparent_comparator) + { + using CSet = std::multiset>; + const CSet compare_data(initial_data.begin(), initial_data.end()); + + using ESet = etl::multiset>; + const ESet data(initial_data.begin(), initial_data.end()); + + CSet::const_iterator i_compare = compare_data.upper_bound(3); + ESet::const_iterator i_data = data.upper_bound(Key(3)); + CHECK_EQUAL(*i_compare, *i_data); + + i_compare = compare_data.upper_bound(-1); + i_data = data.upper_bound(Key(-1)); + CHECK_EQUAL(*i_compare, *i_data); + + i_compare = compare_data.upper_bound(99); + CHECK(compare_data.end() == i_compare); + + i_data = data.upper_bound(Key(99)); + CHECK(data.end() == i_data); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_key_compare) { @@ -1140,6 +1392,21 @@ namespace #endif } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_key_compare_using_transparent_comparator) + { + using ESet = etl::multiset>; + const ESet data(initial_data.begin(), initial_data.end()); + + ESet::key_compare compare = data.key_comp(); + + int a(1); + ESet::key_type b(2); + + CHECK(compare(a, b)); + CHECK(!compare(b, a)); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_value_compare) { @@ -1218,6 +1485,68 @@ namespace } } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_compare_lower_upper_bound_using_transparent_comparator) + { + using CSet = std::multiset>; + CSet compare(initial_data.begin(), initial_data.end()); + + using ESet = etl::multiset>; + ESet data(initial_data.begin(), initial_data.end()); + + std::vector tab(test_data.begin(), test_data.end()); + + //make sure both data and compare contain same elements + std::vector data_elements(data.begin(), data.end()); + std::vector compare_data_elements(compare.begin(), compare.end()); + + CHECK(data_elements == compare_data_elements); + CHECK_EQUAL(data_elements.size(), MAX_SIZE); + + for (std::vector::iterator it = tab.begin(); it != tab.end(); ++it) + { + int i = *it; + + //lower_bound + CHECK_EQUAL(compare.lower_bound(i) == compare.end(), data.lower_bound(i) == data.end()); + //if both end, or none + if ((compare.lower_bound(i) == compare.end()) == (data.lower_bound(i) == data.end())) + { + //if both are not end + if (compare.lower_bound(i) != compare.end()) + { + CHECK((*compare.lower_bound(i)) == (*data.lower_bound(i))); + } + + ETL_OR_STD::pair stlret = compare.equal_range(i); + ETL_OR_STD::pair etlret = data.equal_range(Key(i)); + + CHECK_EQUAL(stlret.first == compare.end(), etlret.first == data.end()); + if ((stlret.first != compare.end()) && (etlret.first != data.end())) + { + CHECK((*stlret.first) == (*etlret.first)); + } + CHECK_EQUAL(stlret.second == compare.end(), etlret.second == data.end()); + if ((stlret.second != compare.end()) && (etlret.second != data.end())) + { + CHECK((*stlret.second) == (*etlret.second)); + } + } + + //upper_bound + CHECK_EQUAL(compare.upper_bound(i) == compare.end(), data.upper_bound(i) == data.end()); + //if both end, or none + if ((compare.upper_bound(i) == compare.end()) == (data.upper_bound(i) == data.end())) + { + //if both are not end + if (compare.upper_bound(i) != compare.end()) + { + CHECK((*compare.upper_bound(i)) == (*data.upper_bound(i))); + } + } + } + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_erase_bug) { @@ -1304,5 +1633,28 @@ namespace CHECK_EQUAL("F", *itr); } #endif + + //************************************************************************* + TEST(test_contains) + { + std::array initial = { 1, 2, 3, 4, 5, 6 }; + etl::multiset> data(initial.begin(), initial.end()); + + CHECK(data.contains(1)); + CHECK(!data.contains(99)); + } + + //************************************************************************* + TEST(test_contains_using_transparent_comparator) + { + std::array initial = { 1, 2, 3, 4, 5, 6 }; + etl::multiset> data(initial.begin(), initial.end()); + + CHECK(data.contains(1)); + CHECK(data.contains(Key(1))); + + CHECK(!data.contains(99)); + CHECK(!data.contains(Key(99))); + } }; } diff --git a/test/test_set.cpp b/test/test_set.cpp index 52f91050..664a3714 100644 --- a/test/test_set.cpp +++ b/test/test_set.cpp @@ -77,6 +77,26 @@ using Compare_Data_const_iterator = Compare_Data::const_iterator; namespace { + struct Key + { + Key(int k_) + : k(k_) + { + } + + int k; + }; + + bool operator <(const Key& lhs, const int& rhs) + { + return (lhs.k < rhs); + } + + bool operator <(const int& lhs, const Key& rhs) + { + return (lhs < rhs.k); + } + SUITE(test_set) { //************************************************************************* @@ -111,97 +131,32 @@ namespace { int n[] = { - 0, - 1, - 2, - 3, - 4, - 5, - 6, - 7, - 8, - 9, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, }; Data::value_type n2[] = { - 0, - 1, - 2, - 3, - 4, - 5, - 6, - 7, - 8, - 9, - 10, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, }; int n3[] = { - 10, - 11, - 12, - 13, - 14, - 15, - 16, - 17, - 18, - 19, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, }; int n4[] = { - 6, - 5, - 0, - 8, - 9, - 2, - 1, - 3, - 7, - 4, + 6, 5, 0, 8, 9, 2, 1, 3, 7, 4, }; int n_even[] = { - 0, - 2, - 4, - 6, - 8, - 10, - 12, - 14, - 16, - 18, + 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, }; int n5[] = { - 0, - 1, - 2, - 3, - 4, - 5, - 6, - 7, - 8, - 9, - 10, - 11, - 12, - 13, - 14, - 15, - 16, - 17, - 18, - 19 + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 } ; initial_data.assign(std::begin(n), std::end(n)); @@ -655,9 +610,7 @@ namespace //CHECK_EQUAL(*data_result.first, *compare_result.first); //// Check that elements in set are the same - //bool isEqual = Check_Equal(data.begin(), - // data.end(), - // compare_data.begin()); + //bool isEqual = Check_Equal(data.begin(), data.end(), compare_data.begin()); //CHECK(isEqual); //data.insert(2); @@ -685,11 +638,9 @@ namespace Compare_Data compare_data(random_data.begin(), random_data.end()); Data data(random_data.begin(), random_data.end()); - ETL_OR_STD::pair data_result = - data.equal_range(2); + ETL_OR_STD::pair data_result = data.equal_range(2); Data::iterator data_lb = data.lower_bound(2); - ETL_OR_STD::pair compare_result = - compare_data.equal_range(2); + ETL_OR_STD::pair compare_result = compare_data.equal_range(2); Compare_Data::iterator compare_data_lb = compare_data.lower_bound(2); // Check that both return the same return results @@ -735,9 +686,24 @@ namespace compare_data.erase(5); data.erase(5); - bool isEqual = Check_Equal(data.begin(), - data.end(), - compare_data.begin()); + bool isEqual = Check_Equal(data.begin(), data.end(), compare_data.begin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_erase_value_using_transparent_comparator) + { + using CMap = std::set>; + using EMap = etl::set>; + + CMap compare_data(initial_data.begin(), initial_data.end()); + EMap data(initial_data.begin(), initial_data.end()); + + compare_data.erase(5); + data.erase(Key(5)); + + bool isEqual = Check_Equal(data.begin(), data.end(), compare_data.begin()); CHECK(isEqual); } @@ -865,6 +831,17 @@ namespace CHECK_EQUAL(data.count(11), size_t(0UL)); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_count_using_transparent_comparator) + { + using EMap = etl::set>; + const EMap data(initial_data.begin(), initial_data.end()); + + CHECK_EQUAL(data.count(Key(3)), size_t(1UL)); + + CHECK_EQUAL(data.count(Key(11)), size_t(0UL)); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_iterator) { @@ -933,6 +910,19 @@ namespace CHECK(data.end() == it); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_find_using_transparent_comparator) + { + using EMap = etl::set>; + EMap data(initial_data.begin(), initial_data.end()); + + EMap::iterator it = data.find(Key(3)); + CHECK_EQUAL(3, *it); + + it = data.find(11); + CHECK(data.end() == it); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_find_const) { @@ -945,6 +935,19 @@ namespace CHECK(data.end() == it); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_find_const_using_transparent_comparator) + { + using EMap = etl::set>; + const EMap data(initial_data.begin(), initial_data.end()); + + EMap::const_iterator it = data.find(Key(3)); + CHECK_EQUAL(3, *it); + + it = data.find(11); + CHECK(data.end() == it); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_equal) { @@ -1004,6 +1007,30 @@ namespace #endif } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_lower_bound_using_transparent_comparator) + { + using CMap = std::set>; + using EMap = etl::set>; + + CMap compare_data(initial_data.begin(), initial_data.end()); + EMap data(initial_data.begin(), initial_data.end()); + + CMap::iterator i_compare = compare_data.lower_bound(8); + EMap::iterator i_data = data.lower_bound(Key(8)); + CHECK_EQUAL(*i_compare, *i_data); + + i_compare = compare_data.lower_bound(-1); + i_data = data.lower_bound(Key(-1)); + CHECK_EQUAL(*i_compare, *i_data); + + i_compare = compare_data.lower_bound(11); + CHECK(compare_data.end() == i_compare); + + i_data = data.lower_bound(Key(11)); + CHECK(data.end() == i_data); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_lower_bound_const) { @@ -1037,6 +1064,30 @@ namespace #endif } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_lower_bound_const_using_transparent_comparator) + { + using CMap = std::set>; + using EMap = etl::set>; + + const CMap compare_data(initial_data.begin(), initial_data.end()); + const EMap data(initial_data.begin(), initial_data.end()); + + CMap::const_iterator i_compare = compare_data.lower_bound(4); + EMap::const_iterator i_data = data.lower_bound(Key(4)); + CHECK_EQUAL(*i_compare, *i_data); + + i_compare = compare_data.lower_bound(-1); + i_data = data.lower_bound(-1); + CHECK_EQUAL(*i_compare, *i_data); + + i_compare = compare_data.lower_bound(11); + CHECK(compare_data.end() == i_compare); + + i_data = data.lower_bound(11); + CHECK(data.end() == i_data); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_upper_bound) { @@ -1070,6 +1121,30 @@ namespace #endif } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_upper_bound_using_transparent_comparator) + { + using CMap = std::set>; + using EMap = etl::set>; + + CMap compare_data(initial_data.begin(), initial_data.end()); + EMap data(initial_data.begin(), initial_data.end()); + + CMap::iterator i_compare = compare_data.upper_bound(2); + EMap::iterator i_data = data.upper_bound(Key(2)); + CHECK_EQUAL(*i_compare, *i_data); + + i_compare = compare_data.upper_bound(-1); + i_data = data.upper_bound(-1); + CHECK_EQUAL(*i_compare, *i_data); + + i_compare = compare_data.upper_bound(11); + CHECK(compare_data.end() == i_compare); + + i_data = data.upper_bound(11); + CHECK(data.end() == i_data); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_upper_bound_const) { @@ -1103,6 +1178,30 @@ namespace #endif } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_upper_bound_const_using_transparent_comparator) + { + using CMap = std::set>; + using EMap = etl::set>; + + CMap compare_data(initial_data.begin(), initial_data.end()); + EMap data(initial_data.begin(), initial_data.end()); + + CMap::const_iterator i_compare = compare_data.upper_bound(7); + EMap::const_iterator i_data = data.upper_bound(Key(7)); + CHECK_EQUAL(*i_compare, *i_data); + + i_compare = compare_data.upper_bound(-1); + i_data = data.upper_bound(-1); + CHECK_EQUAL(*i_compare, *i_data); + + i_compare = compare_data.upper_bound(11); + CHECK(compare_data.end() == i_compare); + + i_data = data.upper_bound(11); + CHECK(data.end() == i_data); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_key_compare) { @@ -1122,6 +1221,21 @@ namespace #endif } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_key_compare_using_transparent_comparator) + { + using EMap = etl::set>; + const EMap data(initial_data.begin(), initial_data.end()); + + EMap::key_compare compare = data.key_comp(); + + int a(1); + Key b(2); + + CHECK(compare(a, b)); + CHECK(!compare(b, a)); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_value_compare) { @@ -1162,6 +1276,7 @@ namespace //lower_bound CHECK_EQUAL(compare.lower_bound(i) == compare.end(), data.lower_bound(i) == data.end()); + //if both end, or none if((compare.lower_bound(i) == compare.end()) == (data.lower_bound(i) == data.end())) { @@ -1175,11 +1290,14 @@ namespace ETL_OR_STD::pair etlret = data.equal_range(i); CHECK_EQUAL(stlret.first == compare.end(), etlret.first == data.end()); + if((stlret.first != compare.end()) && (etlret.first != data.end())) { CHECK_EQUAL(*stlret.first, *etlret.first); } + CHECK_EQUAL(stlret.second == compare.end(), etlret.second == data.end()); + if((stlret.second != compare.end()) && (etlret.second != data.end())) { CHECK_EQUAL(*stlret.second, *etlret.second); @@ -1188,6 +1306,7 @@ namespace //upper_bound CHECK_EQUAL(compare.upper_bound(i) == compare.end(), data.upper_bound(i) == data.end()); + //if both end, or none if((compare.upper_bound(i) == compare.end()) == (data.upper_bound(i) == data.end())) { @@ -1200,6 +1319,73 @@ namespace } } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_compare_lower_upper_bound_using_transparent_comparator) + { + using CMap = std::set>; + using EMap = etl::set>; + + CMap compare(initial_data.begin(), initial_data.end()); + EMap data(initial_data.begin(), initial_data.end()); + + std::vector tab(test_data.begin(), test_data.end()); + + //make sure both data and compare contain same elements + std::vector data_elements(data.begin(), data.end()); + std::vector compare_data_elements(compare.begin(), compare.end()); + + CHECK(data_elements == compare_data_elements); + CHECK_EQUAL(data_elements.size(), MAX_SIZE); + + for (std::vector::iterator it = tab.begin(); it != tab.end(); ++it) + { + int i = *it; + + //lower_bound + CHECK_EQUAL(compare.lower_bound(i) == compare.end(), data.lower_bound(i) == data.end()); + + //if both end, or none + if ((compare.lower_bound(i) == compare.end()) == (data.lower_bound(i) == data.end())) + { + //if both are not end + if (compare.lower_bound(i) != compare.end()) + { + CHECK_EQUAL(*compare.lower_bound(i), *data.lower_bound(i)); + } + + ETL_OR_STD::pair stlret = compare.equal_range(i); + ETL_OR_STD::pair etlret = data.equal_range(Key(i)); + + CHECK_EQUAL(stlret.first == compare.end(), etlret.first == data.end()); + + if ((stlret.first != compare.end()) && (etlret.first != data.end())) + { + CHECK_EQUAL(*stlret.first, *etlret.first); + } + + CHECK_EQUAL(stlret.second == compare.end(), etlret.second == data.end()); + + if ((stlret.second != compare.end()) && (etlret.second != data.end())) + { + CHECK_EQUAL(*stlret.second, *etlret.second); + } + } + + //upper_bound + CHECK_EQUAL(compare.upper_bound(i) == compare.end(), data.upper_bound(i) == data.end()); + + //if both end, or none + if ((compare.upper_bound(i) == compare.end()) == (data.upper_bound(i) == data.end())) + { + //if both are not end + if (compare.upper_bound(i) != compare.end()) + { + CHECK_EQUAL(*compare.upper_bound(i), *data.upper_bound(i)); + } + } + } + } + //************************************************************************* #if ETL_CPP17_SUPPORTED && ETL_USING_INITIALIZER_LIST && !defined(ETL_TEMPLATE_DEDUCTION_GUIDE_TESTS_DISABLED) TEST(test_set_template_deduction) @@ -1251,5 +1437,28 @@ namespace CHECK_EQUAL("F", *itr); } #endif + + //************************************************************************* + TEST(test_contains) + { + std::array initial = { 1, 2, 3, 4, 5, 6 }; + etl::set> data(initial.begin(), initial.end()); + + CHECK(data.contains(1)); + CHECK(!data.contains(99)); + } + + //************************************************************************* + TEST(test_contains_using_transparent_comparator) + { + std::array initial = { 1, 2, 3, 4, 5, 6 }; + etl::set> data(initial.begin(), initial.end()); + + CHECK(data.contains(1)); + CHECK(data.contains(Key(1))); + + CHECK(!data.contains(99)); + CHECK(!data.contains(Key(99))); + } }; }