include/o3tl/sorted_vector.hxx | 12 ++++++++++ o3tl/qa/test-sorted_vector.cxx | 48 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+)
New commits: commit 0bc059f2a893d812e29d6a72d4f988428b5ea346 Author: Noel Grandin <noel.gran...@collabora.co.uk> AuthorDate: Wed Sep 19 08:55:09 2018 +0200 Commit: Noel Grandin <noel.gran...@collabora.co.uk> CommitDate: Wed Sep 19 10:33:28 2018 +0200 implement find(T*) for o3tl::sorted_vector when it contains unique_ptr<T> and add some unit tests Change-Id: I9a01c9fa2fbbf3a553663a980ee6e958f9819645 Reviewed-on: https://gerrit.libreoffice.org/60737 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk> 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(); }; _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits