From 7cb1967a89f938d4eb7c0e9bd988e59c8626ba4f Mon Sep 17 00:00:00 2001 From: NeimadG Date: Tue, 5 Nov 2019 09:46:40 +0100 Subject: [PATCH 1/2] br test set lowerbound (#163) * add test of etl::set:lower_bound * fix etl::set::lower_bound * fix map, extra test map/multiset/multimap --- include/etl/map.h | 18 ++++---- include/etl/set.h | 18 ++++---- test/test_map.cpp | 95 ++++++++++++++++++++++++++++++++++++++ test/test_multimap.cpp | 97 +++++++++++++++++++++++++++++++++++++++ test/test_multiset.cpp | 97 +++++++++++++++++++++++++++++++++++++++ test/test_set.cpp | 101 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 410 insertions(+), 16 deletions(-) mode change 100644 => 100755 include/etl/map.h mode change 100644 => 100755 include/etl/set.h mode change 100644 => 100755 test/test_map.cpp mode change 100644 => 100755 test/test_multimap.cpp mode change 100644 => 100755 test/test_multiset.cpp mode change 100644 => 100755 test/test_set.cpp diff --git a/include/etl/map.h b/include/etl/map.h old mode 100644 new mode 100755 index 4da5f44e..2c410152 --- a/include/etl/map.h +++ b/include/etl/map.h @@ -1502,17 +1502,18 @@ namespace etl Node* find_lower_node(Node* position, key_parameter_t key) const { // Something at this position? keep going - Node* lower_node = position; - while (lower_node) + Node* lower_node = nullptr; + while (position) { // Downcast lower node to Data_Node reference for key comparisons - Data_Node& data_node = imap::data_cast(*lower_node); + Data_Node& data_node = imap::data_cast(*position); // Compare the key value to the current lower node key value if (node_comp(key, data_node)) { - if (lower_node->children[kLeft]) + lower_node = position; + if (position->children[kLeft]) { - lower_node = lower_node->children[kLeft]; + position = position->children[kLeft]; } else { @@ -1522,12 +1523,13 @@ namespace etl } else if (node_comp(data_node, key)) { - lower_node = lower_node->children[kRight]; + position = position->children[kRight]; } else { - // Found equal node - break; + // Make note of current position, but keep looking to left for more + lower_node = position; + position = position->children[kLeft]; } } diff --git a/include/etl/set.h b/include/etl/set.h old mode 100644 new mode 100755 index 2d000497..62500b13 --- a/include/etl/set.h +++ b/include/etl/set.h @@ -1410,17 +1410,18 @@ namespace etl Node* find_lower_node(Node* position, key_parameter_t key) const { // Something at this position? keep going - Node* lower_node = position; - while (lower_node) + Node* lower_node = nullptr; + while (position) { // Downcast lower node to Data_Node reference for key comparisons - Data_Node& data_node = iset::data_cast(*lower_node); + Data_Node& data_node = iset::data_cast(*position); // Compare the key value to the current lower node key value if (node_comp(key, data_node)) { - if (lower_node->children[kLeft]) + lower_node = position; + if (position->children[kLeft]) { - lower_node = lower_node->children[kLeft]; + position = position->children[kLeft]; } else { @@ -1430,12 +1431,13 @@ namespace etl } else if (node_comp(data_node, key)) { - lower_node = lower_node->children[kRight]; + position = position->children[kRight]; } else { - // Found equal node - break; + // Make note of current position, but keep looking to left for more + lower_node = position; + position = position->children[kLeft]; } } diff --git a/test/test_map.cpp b/test/test_map.cpp old mode 100644 new mode 100755 index 686089e1..721a2529 --- a/test/test_map.cpp +++ b/test/test_map.cpp @@ -118,6 +118,8 @@ namespace std::map excess_data; std::map different_data; std::map random_data; + std::map initial_data_even; + std::map test_data; SetupFixture() { @@ -169,6 +171,40 @@ namespace random_data["3"] = 3; random_data["7"] = 7; random_data["4"] = 4; + + //even values + initial_data_even["00"] = 0; + initial_data_even["02"] = 2; + initial_data_even["04"] = 4; + initial_data_even["06"] = 6; + initial_data_even["08"] = 8; + initial_data_even["10"] = 10; + initial_data_even["12"] = 12; + initial_data_even["14"] = 14; + initial_data_even["16"] = 16; + initial_data_even["18"] = 18; + + //test set + test_data["00"] = 0; + test_data["01"] = 1; + test_data["02"] = 2; + test_data["03"] = 3; + test_data["04"] = 4; + test_data["05"] = 5; + test_data["06"] = 6; + test_data["07"] = 7; + test_data["08"] = 8; + test_data["09"] = 9; + test_data["10"] = 10; + test_data["11"] = 11; + test_data["12"] = 12; + test_data["13"] = 13; + test_data["14"] = 14; + test_data["15"] = 15; + test_data["16"] = 16; + test_data["17"] = 17; + test_data["18"] = 18; + test_data["19"] = 19; } }; @@ -1092,5 +1128,64 @@ namespace CHECK(!compare(b, a)); #endif } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_compare_lower_upper_bound) + { + Data data(initial_data_even.begin(), initial_data_even.end()); + Compare_Data compare(initial_data_even.begin(), initial_data_even.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) + { + std::string i = it->first; + + //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))); + } + + std::pair stlret = compare.equal_range(i); + 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((*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))); + } + } + } + } }; } diff --git a/test/test_multimap.cpp b/test/test_multimap.cpp old mode 100644 new mode 100755 index c221a34d..328039ee --- a/test/test_multimap.cpp +++ b/test/test_multimap.cpp @@ -34,6 +34,7 @@ SOFTWARE. #include #include #include +#include #include "etl/multimap.h" @@ -118,6 +119,8 @@ namespace std::multimap excess_data; std::multimap different_data; std::multimap random_data; + std::multimap initial_data_even; + std::multimap test_data; SetupFixture() { @@ -169,6 +172,41 @@ namespace random_data.insert(std::pair("2", 3)); random_data.insert(std::pair("4", 7)); random_data.insert(std::pair("3", 4)); + + + //even values + initial_data_even.insert(std::pair("00", 0)); + initial_data_even.insert(std::pair("02", 2)); + initial_data_even.insert(std::pair("04", 4)); + initial_data_even.insert(std::pair("06", 6)); + initial_data_even.insert(std::pair("08", 8)); + initial_data_even.insert(std::pair("10", 10)); + initial_data_even.insert(std::pair("12", 12)); + initial_data_even.insert(std::pair("14", 14)); + initial_data_even.insert(std::pair("16", 16)); + initial_data_even.insert(std::pair("18", 18)); + + //test set + test_data.insert(std::pair("00", 0)); + test_data.insert(std::pair("01", 1)); + test_data.insert(std::pair("02", 2)); + test_data.insert(std::pair("03", 3)); + test_data.insert(std::pair("04", 4)); + test_data.insert(std::pair("05", 5)); + test_data.insert(std::pair("06", 6)); + test_data.insert(std::pair("07", 7)); + test_data.insert(std::pair("08", 8)); + test_data.insert(std::pair("09", 9)); + test_data.insert(std::pair("10", 10)); + test_data.insert(std::pair("11", 11)); + test_data.insert(std::pair("12", 12)); + test_data.insert(std::pair("13", 13)); + test_data.insert(std::pair("14", 14)); + test_data.insert(std::pair("15", 15)); + test_data.insert(std::pair("16", 16)); + test_data.insert(std::pair("17", 17)); + test_data.insert(std::pair("18", 18)); + test_data.insert(std::pair("19", 19)); } }; @@ -1047,5 +1085,64 @@ namespace CHECK(!compare(b, a)); #endif } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_compare_lower_upper_bound) + { + Data data(initial_data_even.begin(), initial_data_even.end()); + Compare_Data compare(initial_data_even.begin(), initial_data_even.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) + { + std::string i = it->first; + + //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))); + } + + std::pair stlret = compare.equal_range(i); + 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((*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))); + } + } + } + } }; } diff --git a/test/test_multiset.cpp b/test/test_multiset.cpp old mode 100644 new mode 100755 index 28397153..da8721ba --- a/test/test_multiset.cpp +++ b/test/test_multiset.cpp @@ -34,6 +34,7 @@ SOFTWARE. #include #include #include +#include #include "etl/multiset.h" @@ -110,6 +111,8 @@ namespace std::multiset excess_data; std::multiset different_data; std::multiset random_data; + std::multiset initial_data_even; + std::multiset test_data; SetupFixture() { @@ -161,6 +164,41 @@ namespace random_data.insert(2); random_data.insert(4); random_data.insert(3); + + + //even values + initial_data_even.insert(0); + initial_data_even.insert(2); + initial_data_even.insert(4); + initial_data_even.insert(6); + initial_data_even.insert(8); + initial_data_even.insert(10); + initial_data_even.insert(12); + initial_data_even.insert(14); + initial_data_even.insert(16); + initial_data_even.insert(18); + + //test set + test_data.insert(0); + test_data.insert(1); + test_data.insert(2); + test_data.insert(3); + test_data.insert(4); + test_data.insert(5); + test_data.insert(6); + test_data.insert(7); + test_data.insert(8); + test_data.insert(9); + test_data.insert(10); + test_data.insert(11); + test_data.insert(12); + test_data.insert(13); + test_data.insert(14); + test_data.insert(15); + test_data.insert(16); + test_data.insert(17); + test_data.insert(18); + test_data.insert(19); } }; @@ -1005,5 +1043,64 @@ namespace CHECK(!compare(b, a)); #endif } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_compare_lower_upper_bound) + { + Data data(initial_data_even.begin(), initial_data_even.end()); + Compare_Data compare(initial_data_even.begin(), initial_data_even.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))); + } + + std::pair stlret = compare.equal_range(i); + 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((*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))); + } + } + } + } }; } diff --git a/test/test_set.cpp b/test/test_set.cpp old mode 100644 new mode 100755 index 7892c8b5..26f8a1fd --- a/test/test_set.cpp +++ b/test/test_set.cpp @@ -108,6 +108,8 @@ namespace std::vector excess_data; std::vector different_data; std::vector random_data; + std::vector initial_data_even; + std::vector test_data; SetupFixture() { @@ -168,10 +170,50 @@ namespace 4, }; + int n_even[] = + { + 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 + } ; + initial_data.assign(std::begin(n), std::end(n)); excess_data.assign(std::begin(n2), std::end(n2)); different_data.assign(std::begin(n3), std::end(n3)); random_data.assign(std::begin(n4), std::end(n4)); + initial_data_even.assign(std::begin(n_even), std::end(n_even)); + test_data.assign(std::begin(n5), std::end(n5)); } }; @@ -982,5 +1024,64 @@ namespace CHECK(!compare(b, a)); #endif } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_compare_lower_upper_bound) + { + Data data(initial_data_even.begin(), initial_data_even.end()); + Compare_Data compare(initial_data_even.begin(), initial_data_even.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)); + } + + std::pair stlret = compare.equal_range(i); + 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); + } + } + + //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)); + } + } + } + } }; } From 2281a159aa6cfe89f1516d030535449daf8831e9 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Tue, 5 Nov 2019 09:15:15 +0000 Subject: [PATCH 2/2] Updated versions --- include/etl/version.h | 2 +- library.json | 2 +- library.properties | 2 +- support/Release notes.txt | 4 ++++ 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/include/etl/version.h b/include/etl/version.h index d6c8fd61..b90b1f9a 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -39,7 +39,7 @@ SOFTWARE. #define ETL_VERSION_MAJOR 14 #define ETL_VERSION_MINOR 35 -#define ETL_VERSION_PATCH 3 +#define ETL_VERSION_PATCH 4 #define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) "." ETL_STRINGIFY(ETL_VERSION_MINOR) "." ETL_STRINGIFY(ETL_VERSION_PATCH) #define ETL_VERSION_W ETL_STRINGIFY(ETL_VERSION_MAJOR) L"." ETL_STRINGIFY(ETL_VERSION_MINOR) L"." ETL_STRINGIFY(ETL_VERSION_PATCH) diff --git a/library.json b/library.json index abbd76d5..2074978d 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "Embedded Template Library", - "version": "14.35.3", + "version": "14.35.4", "authors": { "name": "John Wellbelove", "email": "" diff --git a/library.properties b/library.properties index 449e4e02..6bb34937 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Embedded Template Library -version=14.35.3 +version=14.35.4 author= John Wellbelove maintainer=John Wellbelove license=MIT diff --git a/support/Release notes.txt b/support/Release notes.txt index 626c4a21..feab4143 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,7 @@ +=============================================================================== +14.35.4 +Bug fix for etl::set & etl::map lower_bound. + =============================================================================== 14.35.3 Added assert when calling uninitialised delegate.