summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/o3tl/sorted_vector.hxx12
-rw-r--r--o3tl/qa/test-sorted_vector.cxx48
2 files changed, 60 insertions, 0 deletions
diff --git a/include/o3tl/sorted_vector.hxx b/include/o3tl/sorted_vector.hxx
index 254d627604df..7c95a1ace24e 100644
--- a/include/o3tl/sorted_vector.hxx
+++ b/include/o3tl/sorted_vector.hxx
@@ -224,6 +224,7 @@ class sorted_vector<Value,Compare,Find,false> : public sorted_vector<Value, Comp
{
public:
using sorted_vector<Value, Compare, Find, true>::sorted_vector;
+ typedef sorted_vector<Value, Compare, Find, true> super_sorted_vector;
sorted_vector(sorted_vector const&) = delete;
sorted_vector& operator=(sorted_vector const&) = delete;
@@ -231,6 +232,17 @@ public:
sorted_vector() = default;
sorted_vector(sorted_vector&&) = default;
sorted_vector& operator=(sorted_vector&&) = default;
+
+ /**
+ * implement find for sorted_vectors containing std::unique_ptr
+ */
+ typename super_sorted_vector::const_iterator find( typename Value::element_type const * x ) const
+ {
+ Value tmp(const_cast<typename Value::element_type*>(x));
+ auto ret = super_sorted_vector::find(tmp);
+ tmp.release();
+ return ret;
+ }
};
diff --git a/o3tl/qa/test-sorted_vector.cxx b/o3tl/qa/test-sorted_vector.cxx
index 3de3f005f6c6..dd622e0cea0d 100644
--- a/o3tl/qa/test-sorted_vector.cxx
+++ b/o3tl/qa/test-sorted_vector.cxx
@@ -12,7 +12,9 @@
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
+#include <o3tl/make_unique.hxx>
#include <o3tl/sorted_vector.hxx>
+#include <rtl/ustring.hxx>
using namespace ::o3tl;
@@ -250,6 +252,50 @@ public:
CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(0), aVec.size() );
}
+ void testUniquePtr1()
+ {
+ o3tl::sorted_vector<std::unique_ptr<OUString>, o3tl::less_uniqueptr_to<OUString>> aVec;
+
+ auto str_c = aVec.insert(o3tl::make_unique<OUString>("c")).first->get();
+ auto str_b1 = aVec.insert(o3tl::make_unique<OUString>("b")).first->get();
+ CPPUNIT_ASSERT(!aVec.insert(o3tl::make_unique<OUString>("b")).second);
+ aVec.insert(o3tl::make_unique<OUString>("a"));
+ CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(3), aVec.size() );
+ CPPUNIT_ASSERT_EQUAL( OUString("a"), *aVec[0] );
+ CPPUNIT_ASSERT_EQUAL( OUString("b"), *aVec[1] );
+ CPPUNIT_ASSERT_EQUAL( OUString("c"), *aVec[2] );
+
+ CPPUNIT_ASSERT( aVec.find(str_c) != aVec.end() );
+ CPPUNIT_ASSERT( aVec.find(str_b1) != aVec.end() );
+
+ OUString tmp("b");
+ CPPUNIT_ASSERT( aVec.find(&tmp) != aVec.end() );
+ OUString tmp2("z");
+ CPPUNIT_ASSERT( bool(aVec.find(&tmp2) == aVec.end()) );
+ }
+
+ void testUniquePtr2()
+ {
+ o3tl::sorted_vector<std::unique_ptr<OUString>, o3tl::less_uniqueptr_to<OUString>,
+ o3tl::find_partialorder_ptrequals> aVec;
+
+ auto str_c = aVec.insert(o3tl::make_unique<OUString>("c")).first->get();
+ auto str_b1 = aVec.insert(o3tl::make_unique<OUString>("b")).first->get();
+ auto str_b2 = aVec.insert(o3tl::make_unique<OUString>("b")).first->get();
+ aVec.insert(o3tl::make_unique<OUString>("a"));
+ CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(4), aVec.size() );
+ CPPUNIT_ASSERT_EQUAL( OUString("a"), *aVec[0] );
+ CPPUNIT_ASSERT_EQUAL( OUString("b"), *aVec[1] );
+ CPPUNIT_ASSERT_EQUAL( OUString("b"), *aVec[2] );
+ CPPUNIT_ASSERT_EQUAL( OUString("c"), *aVec[3] );
+
+ CPPUNIT_ASSERT( aVec.find(str_c) != aVec.end() );
+ CPPUNIT_ASSERT( aVec.find(str_b1) != aVec.end() );
+ CPPUNIT_ASSERT( aVec.find(str_b2) != aVec.end() );
+
+ OUString tmp2("z");
+ CPPUNIT_ASSERT( bool(aVec.find(&tmp2) == aVec.end()) );
+ }
// Change the following lines only, if you add, remove or rename
// member functions of the current class,
@@ -262,6 +308,8 @@ public:
CPPUNIT_TEST(testLowerBound);
CPPUNIT_TEST(testBasics_FindPtr);
CPPUNIT_TEST(testErase_FindPtr);
+ CPPUNIT_TEST(testUniquePtr1);
+ CPPUNIT_TEST(testUniquePtr2);
CPPUNIT_TEST_SUITE_END();
};