From 7ce4fedf589985d0af51977e7d44b665403b9448 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sat, 3 Feb 2018 21:09:22 +0000 Subject: [PATCH] Added emplace member functions to flat_map, flat_multimap, flat_set and flat_multiset --- src/flat_map.h | 188 ++++++++ src/flat_multimap.h | 106 ++++- src/flat_multiset.h | 92 +++- src/flat_set.h | 164 +++++++ test/codeblocks/ETL.layout | 918 ++++++++++++++++++------------------ test/test_flat_map.cpp | 305 +++++++++++- test/test_flat_multimap.cpp | 243 ++++++++++ test/test_flat_multiset.cpp | 239 ++++++++++ test/test_flat_set.cpp | 234 +++++++++ test/test_set.cpp | 43 ++ 10 files changed, 2052 insertions(+), 480 deletions(-) diff --git a/src/flat_map.h b/src/flat_map.h index 821e3baf..c257ec7d 100644 --- a/src/flat_map.h +++ b/src/flat_map.h @@ -336,6 +336,194 @@ namespace etl } } + //************************************************************************* + /// Emplaces a value to the map. + //************************************************************************* + std::pair emplace(const value_type& value) + { + ETL_ASSERT(!full(), ETL_ERROR(flat_map_full)); + + // Create it. + value_type* pvalue = storage.allocate(); + ::new (pvalue) value_type(value); + + iterator i_element = lower_bound(value.first); + + std::pair result(i_element, false); + + // Doesn't already exist? + if ((i_element == end() || (i_element->first != value.first))) + { + ++construct_count; + result = refmap_t::insert_at(i_element, *pvalue); + } + else + { + pvalue->~value_type(); + storage.release(pvalue); + } + + return result; + } + + //************************************************************************* + /// Emplaces a value to the map. + //************************************************************************* + std::pair emplace(const key_type& key, const mapped_type& value) + { + ETL_ASSERT(!full(), ETL_ERROR(flat_map_full)); + + // Create it. + value_type* pvalue = storage.allocate(); + ::new (pvalue) value_type(key, value); + + iterator i_element = lower_bound(key); + + std::pair result(i_element, false); + + // Doesn't already exist? + if ((i_element == end() || (i_element->first != key))) + { + ++construct_count; + result = refmap_t::insert_at(i_element, *pvalue); + } + else + { + pvalue->~value_type(); + storage.release(pvalue); + } + + return result; + } + + //************************************************************************* + /// Emplaces a value to the map. + //************************************************************************* + template + std::pair emplace(const key_type& key, const T1& value1) + { + ETL_ASSERT(!full(), ETL_ERROR(flat_map_full)); + + // Create it. + value_type* pvalue = storage.allocate(); + ::new ((void*)etl::addressof(pvalue->first)) key_type(key); + ::new ((void*)etl::addressof(pvalue->second)) mapped_type(value1); + + iterator i_element = lower_bound(key); + + std::pair result(i_element, false); + + // Doesn't already exist? + if ((i_element == end() || (i_element->first != key))) + { + ++construct_count; + result = refmap_t::insert_at(i_element, *pvalue); + } + else + { + pvalue->~value_type(); + storage.release(pvalue); + } + + return result; + } + + //************************************************************************* + /// Emplaces a value to the map. + //************************************************************************* + template + std::pair emplace(const key_type& key, const T1& value1, const T2& value2) + { + ETL_ASSERT(!full(), ETL_ERROR(flat_map_full)); + + // Create it. + value_type* pvalue = storage.allocate(); + ::new ((void*)etl::addressof(pvalue->first)) key_type(key); + ::new ((void*)etl::addressof(pvalue->second)) mapped_type(value1, value2); + + iterator i_element = lower_bound(key); + + std::pair result(i_element, false); + + // Doesn't already exist? + if ((i_element == end() || (i_element->first != key))) + { + ++construct_count; + result = refmap_t::insert_at(i_element, *pvalue); + } + else + { + pvalue->~value_type(); + storage.release(pvalue); + } + + return result; + } + + //************************************************************************* + /// Emplaces a value to the map. + //************************************************************************* + template + std::pair emplace(const key_type& key, const T1& value1, const T2& value2, const T3& value3) + { + ETL_ASSERT(!full(), ETL_ERROR(flat_map_full)); + + // Create it. + value_type* pvalue = storage.allocate(); + ::new ((void*)etl::addressof(pvalue->first)) key_type(key); + ::new ((void*)etl::addressof(pvalue->second)) mapped_type(value1, value2, value3); + + iterator i_element = lower_bound(key); + + std::pair result(i_element, false); + + // Doesn't already exist? + if ((i_element == end() || (i_element->first != key))) + { + ++construct_count; + result = refmap_t::insert_at(i_element, *pvalue); + } + else + { + pvalue->~value_type(); + storage.release(pvalue); + } + + return result; + } + + //************************************************************************* + /// Emplaces a value to the map. + //************************************************************************* + template + std::pair emplace(const key_type& key, const T1& value1, const T2& value2, const T3& value3, const T4& value4) + { + ETL_ASSERT(!full(), ETL_ERROR(flat_map_full)); + + // Create it. + value_type* pvalue = storage.allocate(); + ::new ((void*)etl::addressof(pvalue->first)) key_type(key); + ::new ((void*)etl::addressof(pvalue->second)) mapped_type(value1, value2, value3, value4); + + iterator i_element = lower_bound(key); + + std::pair result(i_element, false); + + // Doesn't already exist? + if ((i_element == end() || (i_element->first != key))) + { + ++construct_count; + result = refmap_t::insert_at(i_element, *pvalue); + } + else + { + pvalue->~value_type(); + storage.release(pvalue); + } + + return result; + } + //********************************************************************* /// Erases an element. ///\param key The key to erase. diff --git a/src/flat_multimap.h b/src/flat_multimap.h index 07f99def..b8fecd82 100644 --- a/src/flat_multimap.h +++ b/src/flat_multimap.h @@ -264,7 +264,7 @@ namespace etl //********************************************************************* /// Inserts a value to the flast_multi. - /// If asserts or exceptions are enabled, emits flat_map_full if the flat_map is already full. + /// If asserts or exceptions are enabled, emits flat_multimap_full if the flat_map is already full. ///\param position The position to insert at. ///\param value The value to insert. //********************************************************************* @@ -289,6 +289,110 @@ namespace etl } } + //************************************************************************* + /// Emplaces a value to the map. + //************************************************************************* + std::pair emplace(const value_type& value) + { + ETL_ASSERT(!full(), ETL_ERROR(flat_multimap_full)); + + // Create it. + value_type* pvalue = storage.allocate(); + ::new (pvalue) value_type(value); + iterator i_element = lower_bound(value.first); + ++construct_count; + + return refmap_t::insert_at(i_element, *pvalue); + } + + //************************************************************************* + /// Emplaces a value to the map. + //************************************************************************* + std::pair emplace(const key_type& key, const mapped_type& value) + { + ETL_ASSERT(!full(), ETL_ERROR(flat_multimap_full)); + + // Create it. + value_type* pvalue = storage.allocate(); + ::new (pvalue) value_type(key, value); + iterator i_element = lower_bound(key); + ++construct_count; + + return refmap_t::insert_at(i_element, *pvalue); + } + + //************************************************************************* + /// Emplaces a value to the map. + //************************************************************************* + template + std::pair emplace(const key_type& key, const T1& value1) + { + ETL_ASSERT(!full(), ETL_ERROR(flat_multimap_full)); + + // Create it. + value_type* pvalue = storage.allocate(); + ::new ((void*)etl::addressof(pvalue->first)) key_type(key); + ::new ((void*)etl::addressof(pvalue->second)) mapped_type(value1); + iterator i_element = lower_bound(key); + ++construct_count; + + return refmap_t::insert_at(i_element, *pvalue); + } + + //************************************************************************* + /// Emplaces a value to the map. + //************************************************************************* + template + std::pair emplace(const key_type& key, const T1& value1, const T2& value2) + { + ETL_ASSERT(!full(), ETL_ERROR(flat_multimap_full)); + + // Create it. + value_type* pvalue = storage.allocate(); + ::new ((void*)etl::addressof(pvalue->first)) key_type(key); + ::new ((void*)etl::addressof(pvalue->second)) mapped_type(value1, value2); + iterator i_element = lower_bound(key); + ++construct_count; + + return refmap_t::insert_at(i_element, *pvalue); + } + + //************************************************************************* + /// Emplaces a value to the map. + //************************************************************************* + template + std::pair emplace(const key_type& key, const T1& value1, const T2& value2, const T3& value3) + { + ETL_ASSERT(!full(), ETL_ERROR(flat_multimap_full)); + + // Create it. + value_type* pvalue = storage.allocate(); + ::new ((void*)etl::addressof(pvalue->first)) key_type(key); + ::new ((void*)etl::addressof(pvalue->second)) mapped_type(value1, value2, value3); + iterator i_element = lower_bound(key); + ++construct_count; + + return refmap_t::insert_at(i_element, *pvalue); + } + + //************************************************************************* + /// Emplaces a value to the map. + //************************************************************************* + template + std::pair emplace(const key_type& key, const T1& value1, const T2& value2, const T3& value3, const T4& value4) + { + ETL_ASSERT(!full(), ETL_ERROR(flat_multimap_full)); + + // Create it. + value_type* pvalue = storage.allocate(); + ::new ((void*)etl::addressof(pvalue->first)) key_type(key); + ::new ((void*)etl::addressof(pvalue->second)) mapped_type(value1, value2, value3, value4); + iterator i_element = lower_bound(key); + ++construct_count; + + return refmap_t::insert_at(i_element, *pvalue); + } + //********************************************************************* /// Erases an element. ///\param key The key to erase. diff --git a/src/flat_multiset.h b/src/flat_multiset.h index 6d4a05a3..63d44b02 100644 --- a/src/flat_multiset.h +++ b/src/flat_multiset.h @@ -230,8 +230,6 @@ namespace etl iterator i_element = std::lower_bound(begin(), end(), value, TKeyCompare()); - ETL_ASSERT(!full(), ETL_ERROR(flat_multiset_full)); - value_type* pvalue = storage.allocate(); ::new (pvalue) value_type(value); ++construct_count; @@ -267,6 +265,96 @@ namespace etl } } + //************************************************************************* + /// Emplaces a value to the set. + //************************************************************************* + template + std::pair emplace(parameter_t value) + { + ETL_ASSERT(!full(), ETL_ERROR(flat_multiset_full)); + + // Create it. + value_type* pvalue = storage.allocate(); + ::new (pvalue) value_type(value); + + iterator i_element = lower_bound(*pvalue); + + ++construct_count; + return std::pair(refset_t::insert_at(i_element, *pvalue)); + } + + //************************************************************************* + /// Emplaces a value to the set. + //************************************************************************* + template + std::pair emplace(const T1& value1) + { + ETL_ASSERT(!full(), ETL_ERROR(flat_multiset_full)); + + // Create it. + value_type* pvalue = storage.allocate(); + ::new (pvalue) value_type(value1); + + iterator i_element = lower_bound(*pvalue); + + ++construct_count; + return std::pair(refset_t::insert_at(i_element, *pvalue)); + } + + //************************************************************************* + /// Emplaces a value to the set. + //************************************************************************* + template + std::pair emplace(const T1& value1, const T2& value2) + { + ETL_ASSERT(!full(), ETL_ERROR(flat_multiset_full)); + + // Create it. + value_type* pvalue = storage.allocate(); + ::new (pvalue) value_type(value1, value2); + + iterator i_element = lower_bound(*pvalue); + + ++construct_count; + return std::pair(refset_t::insert_at(i_element, *pvalue)); + } + + //************************************************************************* + /// Emplaces a value to the set. + //************************************************************************* + template + std::pair emplace(const T1& value1, const T2& value2, const T3& value3) + { + ETL_ASSERT(!full(), ETL_ERROR(flat_multiset_full)); + + // Create it. + value_type* pvalue = storage.allocate(); + ::new (pvalue) value_type(value1, value2, value3); + + iterator i_element = lower_bound(*pvalue); + + ++construct_count; + return std::pair(refset_t::insert_at(i_element, *pvalue)); + } + + //************************************************************************* + /// Emplaces a value to the set. + //************************************************************************* + template + std::pair emplace(const T1& value1, const T2& value2, const T3& value3, const T4& value4) + { + ETL_ASSERT(!full(), ETL_ERROR(flat_multiset_full)); + + // Create it. + value_type* pvalue = storage.allocate(); + ::new (pvalue) value_type(value1, value2, value3, value4); + + iterator i_element = lower_bound(*pvalue); + + ++construct_count; + return std::pair(refset_t::insert_at(i_element, *pvalue)); + } + //********************************************************************* /// Erases an element. ///\param key The key to erase. diff --git a/src/flat_set.h b/src/flat_set.h index 0b5f17f0..29d16801 100644 --- a/src/flat_set.h +++ b/src/flat_set.h @@ -269,6 +269,170 @@ namespace etl } } + //************************************************************************* + /// Emplaces a value to the set. + //************************************************************************* + std::pair emplace(parameter_t value) + { + ETL_ASSERT(!full(), ETL_ERROR(flat_set_full)); + + std::pair result; + + // Create it. + value_type* pvalue = storage.allocate(); + ::new (pvalue) value_type(value); + + iterator i_element = lower_bound(*pvalue); + + // Doesn't already exist? + if ((i_element == end() || (*i_element != *pvalue))) + { + ++construct_count; + result = refset_t::insert_at(i_element, *pvalue); + } + else + { + // Destroy it. + pvalue->~value_type(); + storage.release(pvalue); + result = std::pair(end(), false); + } + + return result; + } + + //************************************************************************* + /// Emplaces a value to the set. + //************************************************************************* + template + std::pair emplace(const T1& value1) + { + ETL_ASSERT(!full(), ETL_ERROR(flat_set_full)); + + std::pair result; + + // Create it. + value_type* pvalue = storage.allocate(); + ::new (pvalue) value_type(value1); + + iterator i_element = lower_bound(*pvalue); + + // Doesn't already exist? + if ((i_element == end() || (*i_element != *pvalue))) + { + ++construct_count; + result = refset_t::insert_at(i_element, *pvalue); + } + else + { + // Destroy it. + pvalue->~value_type(); + storage.release(pvalue); + result = std::pair(end(), false); + } + + return result; + } + + //************************************************************************* + /// Emplaces a value to the set. + //************************************************************************* + template + std::pair emplace(const T1& value1, const T2& value2) + { + ETL_ASSERT(!full(), ETL_ERROR(flat_set_full)); + + std::pair result; + + // Create it. + value_type* pvalue = storage.allocate(); + ::new (pvalue) value_type(value1, value2); + + iterator i_element = lower_bound(*pvalue); + + // Doesn't already exist? + if ((i_element == end() || (*i_element != *pvalue))) + { + ++construct_count; + result = refset_t::insert_at(i_element, *pvalue); + } + else + { + // Destroy it. + pvalue->~value_type(); + storage.release(pvalue); + result = std::pair(end(), false); + } + + return result; + } + + //************************************************************************* + /// Emplaces a value to the set. + //************************************************************************* + template + std::pair emplace(const T1& value1, const T2& value2, const T3& value3) + { + ETL_ASSERT(!full(), ETL_ERROR(flat_set_full)); + + std::pair result; + + // Create it. + value_type* pvalue = storage.allocate(); + ::new (pvalue) value_type(value1, value2, value3); + + iterator i_element = lower_bound(*pvalue); + + // Doesn't already exist? + if ((i_element == end() || (*i_element != *pvalue))) + { + ++construct_count; + result = refset_t::insert_at(i_element, *pvalue); + } + else + { + // Destroy it. + pvalue->~value_type(); + storage.release(pvalue); + result = std::pair(end(), false); + } + + return result; + } + + //************************************************************************* + /// Emplaces a value to the set. + //************************************************************************* + template + std::pair emplace(const T1& value1, const T2& value2, const T3& value3, const T4& value4) + { + ETL_ASSERT(!full(), ETL_ERROR(flat_set_full)); + + std::pair result; + + // Create it. + value_type* pvalue = storage.allocate(); + ::new (pvalue) value_type(value1, value2, value3, value4); + + iterator i_element = lower_bound(*pvalue); + + // Doesn't already exist? + if ((i_element == end() || (*i_element != *pvalue))) + { + ++construct_count; + result = refset_t::insert_at(i_element, *pvalue); + } + else + { + // Destroy it. + pvalue->~value_type(); + storage.release(pvalue); + result = std::pair(end(), false); + } + + return result; + } + //********************************************************************* /// Erases an element. ///\param key The key to erase. diff --git a/test/codeblocks/ETL.layout b/test/codeblocks/ETL.layout index eac65713..7fb40ace 100644 --- a/test/codeblocks/ETL.layout +++ b/test/codeblocks/ETL.layout @@ -2,474 +2,69 @@ - + - + - + - + - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -477,14 +72,14 @@ - + - + - + - + @@ -492,19 +87,49 @@ - + - + - + - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -517,44 +142,64 @@ - - - - - - - - - - - + - + - + - + - + - + - + - + - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -567,19 +212,374 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/test_flat_map.cpp b/test/test_flat_map.cpp index cc09d1da..d00387e0 100644 --- a/test/test_flat_map.cpp +++ b/test/test_flat_map.cpp @@ -64,6 +64,113 @@ namespace typedef std::map Compare_DataDC; typedef std::map Compare_DataNDC; + struct D1 + { + D1(const std::string& a_) + : a(a_) + { + } + + std::string a; + }; + + struct D2 + { + D2(const std::string& a_, const std::string& b_) + : a(a_), + b(b_) + { + } + + std::string a; + std::string b; + }; + + struct D3 + { + D3(const std::string& a_, const std::string& b_, const std::string& c_) + : a(a_), + b(b_), + c(c_) + { + } + + std::string a; + std::string b; + std::string c; + }; + + struct D4 + { + D4(const std::string& a_, const std::string& b_, const std::string& c_, const std::string& d_) + : a(a_), + b(b_), + c(c_), + d(d_) + { + } + + std::string a; + std::string b; + std::string c; + std::string d; + }; + + bool operator == (const D1& lhs, const D1& rhs) + { + return (lhs.a == rhs.a); + } + + bool operator == (const D2& lhs, const D2& rhs) + { + return (lhs.a == rhs.a) && (lhs.b == rhs.b); + } + + bool operator == (const D3& lhs, const D3& rhs) + { + return (lhs.a == rhs.a) && (lhs.b == rhs.b) && (lhs.c == rhs.c); + } + + bool operator == (const D4& lhs, const D4& rhs) + { + return (lhs.a == rhs.a) && (lhs.b == rhs.b) && (lhs.c == rhs.c) && (lhs.d == rhs.d); + } + + bool operator != (const D1& lhs, const D1& rhs) + { + return !(lhs == rhs); + } + + bool operator != (const D2& lhs, const D2& rhs) + { + return !(lhs == rhs); + } + + bool operator != (const D3& lhs, const D3& rhs) + { + return !(lhs == rhs); + } + + bool operator != (const D4& lhs, const D4& rhs) + { + return !(lhs == rhs); + } + + typedef std::pair Element1; + typedef std::pair Element2; + typedef std::pair Element3; + typedef std::pair Element4; + + typedef etl::flat_map Data1; + typedef etl::flat_map Data2; + typedef etl::flat_map Data3; + typedef etl::flat_map Data4; + + typedef std::map Compare1; + typedef std::map Compare2; + typedef std::map Compare3; + typedef std::map Compare4; + //************************************************************************* template bool Check_Equal(T1 begin1, T1 end1, T2 begin2) @@ -165,24 +272,6 @@ namespace std::vector int_data; - //************************************************************************* - template - bool Check_Equal(T1 begin1, T1 end1, T2 begin2) - { - while (begin1 != end1) - { - if ((begin1->first != begin2->first) || (begin1->second != begin2->second)) - { - return false; - } - - ++begin1; - ++begin2; - } - - return true; - } - //************************************************************************* struct SetupFixture { @@ -606,6 +695,186 @@ namespace CHECK_THROW(data.insert(excess_data.begin(), excess_data.end()), etl::flat_map_full); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_emplace_value1) + { + Compare1 compare; + Data1 data; + + std::pair result; + + result = data.emplace(0, "0"); + compare.emplace(0, D1("0")); + + bool isEqual = Check_Equal(data.begin(), + data.end(), + compare.begin()); + + CHECK(isEqual); + CHECK(result.second); + CHECK(*result.first == Element1(0, D1("0"))); + + result = data.emplace(std::make_pair(2, D1("2"))); + compare.emplace(std::make_pair(2, D1("2"))); + + isEqual = Check_Equal(data.begin(), + data.end(), + compare.begin()); + + CHECK(isEqual); + CHECK(result.second); + CHECK(*result.first == Element1(2, D1("2"))); + + result = data.emplace(1, "1"); + compare.emplace(1, D1("1")); + + isEqual = Check_Equal(data.begin(), + data.end(), + compare.begin()); + + CHECK(isEqual); + CHECK(result.second); + CHECK(*result.first == Element1(1, D1("1"))); + + result = data.emplace(1, D1("1")); + CHECK(!result.second); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_emplace_value2) + { + Compare2 compare; + Data2 data; + + std::pair result; + + result = data.emplace(0, "0", "1"); + compare.emplace(0, D2("0", "1")); + + bool isEqual = Check_Equal(data.begin(), + data.end(), + compare.begin()); + + CHECK(isEqual); + CHECK(result.second); + CHECK(*result.first == Element2(0, D2("0", "1"))); + + result = data.emplace(std::make_pair(2, D2("2", "3"))); + compare.emplace(std::make_pair(2, D2("2", "3"))); + + isEqual = Check_Equal(data.begin(), + data.end(), + compare.begin()); + + CHECK(isEqual); + CHECK(result.second); + CHECK(*result.first == Element2(2, D2("2", "3"))); + + result = data.emplace(1, D2("1", "2")); + compare.emplace(1, D2("1", "2")); + + isEqual = Check_Equal(data.begin(), + data.end(), + compare.begin()); + + CHECK(isEqual); + CHECK(result.second); + CHECK(*result.first == Element2(1, D2("1", "2"))); + + result = data.emplace(1, D2("1", "2")); + CHECK(!result.second); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_emplace_value3) + { + Compare3 compare; + Data3 data; + + std::pair result; + + result = data.emplace(0, "0", "1", "2"); + compare.emplace(0, D3("0", "1", "2")); + + bool isEqual = Check_Equal(data.begin(), + data.end(), + compare.begin()); + + CHECK(isEqual); + CHECK(result.second); + CHECK(*result.first == Element3(0, D3("0", "1", "2"))); + + result = data.emplace(std::make_pair(2, D3("2", "3", "4"))); + compare.emplace(std::make_pair(2, D3("2", "3", "4"))); + + isEqual = Check_Equal(data.begin(), + data.end(), + compare.begin()); + + CHECK(isEqual); + CHECK(result.second); + CHECK(*result.first == Element3(2, D3("2", "3", "4"))); + + result = data.emplace(1, D3("1", "2", "3")); + compare.emplace(1, D3("1", "2", "3")); + + isEqual = Check_Equal(data.begin(), + data.end(), + compare.begin()); + + CHECK(isEqual); + CHECK(result.second); + CHECK(*result.first == Element3(1, D3("1", "2", "3"))); + + result = data.emplace(1, D3("1", "2", "3")); + CHECK(!result.second); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_emplace_value4) + { + Compare4 compare; + Data4 data; + + std::pair result; + + result = data.emplace(0, "0", "1", "2", "3"); + compare.emplace(0, D4("0", "1", "2", "3")); + + bool isEqual = Check_Equal(data.begin(), + data.end(), + compare.begin()); + + CHECK(isEqual); + CHECK(result.second); + CHECK(*result.first == Element4(0, D4("0", "1", "2", "3"))); + + result = data.emplace(std::make_pair(2, D4("2", "3", "4", "5"))); + compare.emplace(std::make_pair(2, D4("2", "3", "4", "5"))); + + isEqual = Check_Equal(data.begin(), + data.end(), + compare.begin()); + + CHECK(isEqual); + CHECK(result.second); + CHECK(*result.first == Element4(2, D4("2", "3", "4", "5"))); + + result = data.emplace(1, D4("1", "2", "3", "4")); + compare.emplace(1, D4("1", "2", "3", "4")); + + isEqual = Check_Equal(data.begin(), + data.end(), + compare.begin()); + + CHECK(isEqual); + CHECK(result.second); + CHECK(*result.first == Element4(1, D4("1", "2", "3", "4"))); + + result = data.emplace(1, D4("1", "2", "3", "4")); + CHECK(!result.second); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_erase_key) { diff --git a/test/test_flat_multimap.cpp b/test/test_flat_multimap.cpp index 59c3a32d..afec18e1 100644 --- a/test/test_flat_multimap.cpp +++ b/test/test_flat_multimap.cpp @@ -90,6 +90,113 @@ namespace std::vector int_data; + struct D1 + { + D1(const std::string& a_) + : a(a_) + { + } + + std::string a; + }; + + struct D2 + { + D2(const std::string& a_, const std::string& b_) + : a(a_), + b(b_) + { + } + + std::string a; + std::string b; + }; + + struct D3 + { + D3(const std::string& a_, const std::string& b_, const std::string& c_) + : a(a_), + b(b_), + c(c_) + { + } + + std::string a; + std::string b; + std::string c; + }; + + struct D4 + { + D4(const std::string& a_, const std::string& b_, const std::string& c_, const std::string& d_) + : a(a_), + b(b_), + c(c_), + d(d_) + { + } + + std::string a; + std::string b; + std::string c; + std::string d; + }; + + bool operator == (const D1& lhs, const D1& rhs) + { + return (lhs.a == rhs.a); + } + + bool operator == (const D2& lhs, const D2& rhs) + { + return (lhs.a == rhs.a) && (lhs.b == rhs.b); + } + + bool operator == (const D3& lhs, const D3& rhs) + { + return (lhs.a == rhs.a) && (lhs.b == rhs.b) && (lhs.c == rhs.c); + } + + bool operator == (const D4& lhs, const D4& rhs) + { + return (lhs.a == rhs.a) && (lhs.b == rhs.b) && (lhs.c == rhs.c) && (lhs.d == rhs.d); + } + + bool operator != (const D1& lhs, const D1& rhs) + { + return !(lhs == rhs); + } + + bool operator != (const D2& lhs, const D2& rhs) + { + return !(lhs == rhs); + } + + bool operator != (const D3& lhs, const D3& rhs) + { + return !(lhs == rhs); + } + + bool operator != (const D4& lhs, const D4& rhs) + { + return !(lhs == rhs); + } + + typedef std::pair Element1; + typedef std::pair Element2; + typedef std::pair Element3; + typedef std::pair Element4; + + typedef etl::flat_multimap Data1; + typedef etl::flat_multimap Data2; + typedef etl::flat_multimap Data3; + typedef etl::flat_multimap Data4; + + typedef std::multimap Compare1; + typedef std::multimap Compare2; + typedef std::multimap Compare3; + typedef std::multimap Compare4; + //************************************************************************* template bool Check_Equal(T1 begin1, T1 end1, T2 begin2) @@ -436,6 +543,142 @@ namespace CHECK(std::is_sorted(data.begin(), data.end())); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_emplace_value1) + { + Compare1 compare; + Data1 data; + + data.emplace(0, "0"); + compare.emplace(0, D1("0")); + + data.emplace(1, D1("1")); + compare.emplace(1, D1("1")); + + data.emplace(std::make_pair(2, D1("2"))); + compare.emplace(std::make_pair(2, D1("2"))); + + // Do it again. + data.emplace(0, "0"); + compare.emplace(0, D1("0")); + + data.emplace(1, D1("1")); + compare.emplace(1, D1("1")); + + data.emplace(std::make_pair(2, D1("2"))); + compare.emplace(std::make_pair(2, D1("2"))); + + CHECK_EQUAL(compare.size(), data.size()); + + bool isEqual = Check_Equal(data.begin(), + data.end(), + compare.begin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_emplace_value2) + { + Compare2 compare; + Data2 data; + + data.emplace(0, "0", "1"); + compare.emplace(0, D2("0", "1")); + + data.emplace(1, D2("1", "2")); + compare.emplace(1, D2("1", "2")); + + data.emplace(std::make_pair(2, D2("2", "3"))); + compare.emplace(std::make_pair(2, D2("2", "3"))); + + // Do it again. + data.emplace(0, "0", "1"); + compare.emplace(0, D2("0", "1")); + + data.emplace(1, D2("1", "2")); + compare.emplace(1, D2("1", "2")); + + data.emplace(std::make_pair(2, D2("2", "3"))); + compare.emplace(std::make_pair(2, D2("2", "3"))); + + CHECK_EQUAL(compare.size(), data.size()); + + bool isEqual = Check_Equal(data.begin(), + data.end(), + compare.begin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_emplace_value3) + { + Compare3 compare; + Data3 data; + + data.emplace(0, "0", "1", "2"); + compare.emplace(0, D3("0", "1", "2")); + + data.emplace(1, D3("1", "2", "3")); + compare.emplace(1, D3("1", "2", "3")); + + data.emplace(std::make_pair(2, D3("2", "3", "4"))); + compare.emplace(std::make_pair(2, D3("2", "3", "4"))); + + // Do it again. + data.emplace(0, "0", "1", "2"); + compare.emplace(0, D3("0", "1", "2")); + + data.emplace(1, D3("1", "2", "3")); + compare.emplace(1, D3("1", "2", "3")); + + data.emplace(std::make_pair(2, D3("2", "3", "4"))); + compare.emplace(std::make_pair(2, D3("2", "3", "4"))); + + CHECK_EQUAL(compare.size(), data.size()); + + bool isEqual = Check_Equal(data.begin(), + data.end(), + compare.begin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_emplace_value4) + { + Compare4 compare; + Data4 data; + + data.emplace(0, "0", "1", "2", "3"); + compare.emplace(0, D4("0", "1", "2", "3")); + + data.emplace(1, D4("1", "2", "3", "4")); + compare.emplace(1, D4("1", "2", "3", "4")); + + data.emplace(std::make_pair(2, D4("2", "3", "4", "5"))); + compare.emplace(std::make_pair(2, D4("2", "3", "4", "5"))); + + // Do it again. + data.emplace(0, "0", "1", "2", "3"); + compare.emplace(0, D4("0", "1", "2", "3")); + + data.emplace(1, D4("1", "2", "3", "4")); + compare.emplace(1, D4("1", "2", "3", "4")); + + data.emplace(std::make_pair(2, D4("2", "3", "4", "5"))); + compare.emplace(std::make_pair(2, D4("2", "3", "4", "5"))); + + CHECK_EQUAL(compare.size(), data.size()); + + bool isEqual = Check_Equal(data.begin(), + data.end(), + compare.begin()); + + CHECK(isEqual); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_erase_key) { diff --git a/test/test_flat_multiset.cpp b/test/test_flat_multiset.cpp index fb775560..447de187 100644 --- a/test/test_flat_multiset.cpp +++ b/test/test_flat_multiset.cpp @@ -56,6 +56,109 @@ namespace typedef std::multiset Compare_DataDC; typedef std::multiset Compare_DataNDC; + struct D1 + { + D1(const std::string& a_) + : a(a_) + { + } + + std::string a; + }; + + struct D2 + { + D2(const std::string& a_, const std::string& b_) + : a(a_), + b(b_) + { + } + + std::string a; + std::string b; + }; + + struct D3 + { + D3(const std::string& a_, const std::string& b_, const std::string& c_) + : a(a_), + b(b_), + c(c_) + { + } + + std::string a; + std::string b; + std::string c; + }; + + struct D4 + { + D4(const std::string& a_, const std::string& b_, const std::string& c_, const std::string& d_) + : a(a_), + b(b_), + c(c_), + d(d_) + { + } + + std::string a; + std::string b; + std::string c; + std::string d; + }; + + bool operator == (const D1& lhs, const D1& rhs) + { + return (lhs.a == rhs.a); + } + + bool operator == (const D2& lhs, const D2& rhs) + { + return (lhs.a == rhs.a) && (lhs.b == rhs.b); + } + + bool operator == (const D3& lhs, const D3& rhs) + { + return (lhs.a == rhs.a) && (lhs.b == rhs.b) && (lhs.c == rhs.c); + } + + bool operator == (const D4& lhs, const D4& rhs) + { + return (lhs.a == rhs.a) && (lhs.b == rhs.b) && (lhs.c == rhs.c) && (lhs.d == rhs.d); + } + + bool operator < (const D1& lhs, const D1& rhs) + { + return (lhs.a < rhs.a); + } + + bool operator < (const D2& lhs, const D2& rhs) + { + return (lhs.a < rhs.a) && (lhs.b < rhs.b); + } + + bool operator < (const D3& lhs, const D3& rhs) + { + return (lhs.a < rhs.a) && (lhs.b < rhs.b) && (lhs.c < rhs.c); + } + + bool operator < (const D4& lhs, const D4& rhs) + { + return (lhs.a < rhs.a) && (lhs.b < rhs.b) && (lhs.c < rhs.c) && (lhs.d < rhs.d); + } + + typedef etl::flat_multiset Data1; + typedef etl::flat_multiset Data2; + typedef etl::flat_multiset Data3; + typedef etl::flat_multiset Data4; + + typedef std::multiset Compare1; + typedef std::multiset Compare2; + typedef std::multiset Compare3; + typedef std::multiset Compare4; + + //************************************************************************* std::ostream& operator <<(std::ostream& os, DataNDC::iterator itr) { @@ -393,6 +496,142 @@ namespace CHECK(std::is_sorted(data.begin(), data.end())); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_emplace_value1) + { + Compare1 compare; + Data1 data; + + data.emplace("0"); + compare.emplace("0"); + + data.emplace("1"); + compare.emplace("1"); + + data.emplace("2"); + compare.emplace("2"); + + // Do it again. + data.emplace("0"); + compare.emplace("0"); + + data.emplace("1"); + compare.emplace("1"); + + data.emplace("2"); + compare.emplace("2"); + + CHECK_EQUAL(compare.size(), data.size()); + + bool isEqual = std::equal(data.begin(), + data.end(), + compare.begin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_emplace_value2) + { + Compare2 compare; + Data2 data; + + data.emplace("0", "1"); + compare.emplace("0", "1"); + + data.emplace("1", "2"); + compare.emplace("1", "2"); + + data.emplace("2", "3"); + compare.emplace("2", "3"); + + // Do it again. + data.emplace("0", "1"); + compare.emplace("0", "1"); + + data.emplace("1", "2"); + compare.emplace("1", "2"); + + data.emplace("2", "3"); + compare.emplace("2", "3"); + + CHECK_EQUAL(compare.size(), data.size()); + + bool isEqual = std::equal(data.begin(), + data.end(), + compare.begin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_emplace_value3) + { + Compare3 compare; + Data3 data; + + data.emplace("0", "1", "2"); + compare.emplace("0", "1", "2"); + + data.emplace("1", "2", "3"); + compare.emplace("1", "2", "3"); + + data.emplace("2", "3", "4"); + compare.emplace("2", "3", "4"); + + // Do it again. + data.emplace("0", "1", "2"); + compare.emplace("0", "1", "2"); + + data.emplace("1", "2", "3"); + compare.emplace("1", "2", "3"); + + data.emplace("2", "3", "4"); + compare.emplace("2", "3", "4"); + + CHECK_EQUAL(compare.size(), data.size()); + + bool isEqual = std::equal(data.begin(), + data.end(), + compare.begin()); + + CHECK(isEqual); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_emplace_value4) + { + Compare4 compare; + Data4 data; + + data.emplace("0", "1", "2", "3"); + compare.emplace("0", "1", "2", "3"); + + data.emplace("1", "2", "3", "4"); + compare.emplace("1", "2", "3", "4"); + + data.emplace("2", "3", "4", "5"); + compare.emplace("2", "3", "4", "5"); + + // Do it again. + data.emplace("0", "1", "2", "3"); + compare.emplace("0", "1", "2", "3"); + + data.emplace("1", "2", "3", "4"); + compare.emplace("1", "2", "3", "4"); + + data.emplace("2", "3", "4", "5"); + compare.emplace("2", "3", "4", "5"); + + CHECK_EQUAL(compare.size(), data.size()); + + bool isEqual = std::equal(data.begin(), + data.end(), + compare.begin()); + + CHECK(isEqual); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_erase_key) { diff --git a/test/test_flat_set.cpp b/test/test_flat_set.cpp index 25ff5cc7..9bcbc73a 100644 --- a/test/test_flat_set.cpp +++ b/test/test_flat_set.cpp @@ -104,6 +104,128 @@ namespace std::vector int_data; + struct D1 + { + D1(const std::string& a_) + : a(a_) + { + } + + std::string a; + }; + + struct D2 + { + D2(const std::string& a_, const std::string& b_) + : a(a_), + b(b_) + { + } + + std::string a; + std::string b; + }; + + struct D3 + { + D3(const std::string& a_, const std::string& b_, const std::string& c_) + : a(a_), + b(b_), + c(c_) + { + } + + std::string a; + std::string b; + std::string c; + }; + + struct D4 + { + D4(const std::string& a_, const std::string& b_, const std::string& c_, const std::string& d_) + : a(a_), + b(b_), + c(c_), + d(d_) + { + } + + std::string a; + std::string b; + std::string c; + std::string d; + }; + + bool operator == (const D1& lhs, const D1& rhs) + { + return (lhs.a == rhs.a); + } + + bool operator == (const D2& lhs, const D2& rhs) + { + return (lhs.a == rhs.a) && (lhs.b == rhs.b); + } + + bool operator == (const D3& lhs, const D3& rhs) + { + return (lhs.a == rhs.a) && (lhs.b == rhs.b) && (lhs.c == rhs.c); + } + + bool operator == (const D4& lhs, const D4& rhs) + { + return (lhs.a == rhs.a) && (lhs.b == rhs.b) && (lhs.c == rhs.c) && (lhs.d == rhs.d); + } + + bool operator != (const D1& lhs, const D1& rhs) + { + return !(lhs == rhs); + } + + bool operator != (const D2& lhs, const D2& rhs) + { + return !(lhs == rhs); + } + + bool operator != (const D3& lhs, const D3& rhs) + { + return !(lhs == rhs); + } + + bool operator != (const D4& lhs, const D4& rhs) + { + return !(lhs == rhs); + } + + bool operator < (const D1& lhs, const D1& rhs) + { + return (lhs.a < rhs.a); + } + + bool operator < (const D2& lhs, const D2& rhs) + { + return (lhs.a < rhs.a) && (lhs.b < rhs.b); + } + + bool operator < (const D3& lhs, const D3& rhs) + { + return (lhs.a < rhs.a) && (lhs.b < rhs.b) && (lhs.c < rhs.c); + } + + bool operator < (const D4& lhs, const D4& rhs) + { + return (lhs.a < rhs.a) && (lhs.b < rhs.b) && (lhs.c < rhs.c) && (lhs.d < rhs.d); + } + + typedef etl::flat_set Data1; + typedef etl::flat_set Data2; + typedef etl::flat_set Data3; + typedef etl::flat_set Data4; + + typedef std::set Compare1; + typedef std::set Compare2; + typedef std::set Compare3; + typedef std::set Compare4; + //************************************************************************* struct SetupFixture { @@ -382,6 +504,118 @@ namespace CHECK(std::is_sorted(data.begin(), data.end())); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_emplace_value1) + { + Compare1 compare; + Data1 data; + + data.emplace("0"); + compare.emplace("0"); + + data.emplace("1"); + compare.emplace("1"); + + data.emplace("2"); + compare.emplace("2"); + + // Do it again. + data.emplace("0"); + compare.emplace("0"); + + data.emplace("1"); + compare.emplace("1"); + + data.emplace("2"); + compare.emplace("2"); + + CHECK_EQUAL(compare.size(), data.size()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_emplace_value2) + { + Compare2 compare; + Data2 data; + + data.emplace("0", "1"); + compare.emplace("0", "1"); + + data.emplace("1", "2"); + compare.emplace("1", "2"); + + data.emplace("2", "3"); + compare.emplace("2", "3"); + + // Do it again. + data.emplace("0", "1"); + compare.emplace("0", "1"); + + data.emplace("1", "2"); + compare.emplace("1", "2"); + + data.emplace("2", "3"); + compare.emplace("2", "3"); + + CHECK_EQUAL(compare.size(), data.size()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_emplace_value3) + { + Compare3 compare; + Data3 data; + + data.emplace("0", "1", "2"); + compare.emplace("0", "1", "2"); + + data.emplace("1", "2", "3"); + compare.emplace("1", "2", "3"); + + data.emplace("2", "3", "4"); + compare.emplace("2", "3", "4"); + + // Do it again. + data.emplace("0", "1", "2"); + compare.emplace("0", "1", "2"); + + data.emplace("1", "2", "3"); + compare.emplace("1", "2", "3"); + + data.emplace("2", "3", "4"); + compare.emplace("2", "3", "4"); + + CHECK_EQUAL(compare.size(), data.size()); + } + + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_emplace_value4) + { + Compare4 compare; + Data4 data; + + data.emplace("0", "1", "2", "3"); + compare.emplace("0", "1", "2", "3"); + + data.emplace("1", "2", "3", "4"); + compare.emplace("1", "2", "3", "4"); + + data.emplace("2", "3", "4", "5"); + compare.emplace("2", "3", "4", "5"); + + // Do it again. + data.emplace("0", "1", "2", "3"); + compare.emplace("0", "1", "2", "3"); + + data.emplace("1", "2", "3", "4"); + compare.emplace("1", "2", "3", "4"); + + data.emplace("2", "3", "4", "5"); + compare.emplace("2", "3", "4", "5"); + + CHECK_EQUAL(compare.size(), data.size()); + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_erase_key) { diff --git a/test/test_set.cpp b/test/test_set.cpp index 06b5607a..98422f6f 100644 --- a/test/test_set.cpp +++ b/test/test_set.cpp @@ -461,6 +461,49 @@ namespace CHECK_THROW(data.insert(excess_data.begin(), excess_data.end()), etl::set_full); } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_emplace_value) + { + //Compare_Data compare_data; + //Data data; + + //std::pair data_result = data.emplace(0); + //std::pair compare_result = compare_data.emplace(0); + + //// Check that both return successful return results + //CHECK_EQUAL(*data_result.first, *compare_result.first); + + //// Try adding a duplicate (should return iterator pointing to duplicate) + //data_result = data.insert(0); + //compare_result = compare_data.insert(0); + + //// Check that both return successful return results + //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()); + //CHECK(isEqual); + + //data.insert(2); + //compare_data.insert(2); + + //isEqual = Check_Equal(data.begin(), + // data.end(), + // compare_data.begin()); + + //CHECK(isEqual); + + //data.insert(1); + //compare_data.insert(1); + + //isEqual = Check_Equal(data.begin(), + // data.end(), + // compare_data.begin()); + + //CHECK(isEqual); + } //************************************************************************* TEST_FIXTURE(SetupFixture, test_equal_range)