]> granicus.if.org Git - icinga2/commitdiff
Implement Array::Unique() and add unit tests
authorMichael Friedrich <michael.friedrich@icinga.com>
Wed, 9 May 2018 14:55:14 +0000 (16:55 +0200)
committerMichael Friedrich <michael.friedrich@icinga.com>
Wed, 9 May 2018 15:25:55 +0000 (17:25 +0200)
refs #4732

lib/base/array-script.cpp
lib/base/array.cpp
lib/base/array.hpp
test/CMakeLists.txt
test/base-array.cpp

index 3f83dd934651ee0962827161fea1f470e09b7c12..c12585b1fd2ce2ad8a4442d2c9975367145f0e9e 100644 (file)
@@ -251,15 +251,7 @@ static Array::Ptr ArrayUnique()
        ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
        Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
        REQUIRE_NOT_NULL(self);
-
-       std::set<Value> result;
-
-       ObjectLock olock(self);
-       for (const Value& item : self) {
-               result.insert(item);
-       }
-
-       return Array::FromSet(result);
+       return self->Unique();
 }
 
 static void ArrayFreeze()
index ac2ade93e080af6efc129b9a35774f3178146a7c..30cf7eba38d4a8c6010354388448510532402ee0 100644 (file)
@@ -305,6 +305,19 @@ String Array::ToString() const
        return msgbuf.str();
 }
 
+Array::Ptr Array::Unique() const
+{
+       std::set<Value> result;
+
+       ObjectLock olock(this);
+
+       for (const Value& item : m_Data) {
+               result.insert(item);
+       }
+
+       return Array::FromSet(result);
+}
+
 void Array::Freeze()
 {
        ObjectLock olock(this);
index 768cee131a89e3c1021616863e82ac5a3fbfc064..62bcf7e2ee9d76e22820d0b56dec963a0be168e8 100644 (file)
@@ -112,6 +112,7 @@ public:
 
        String ToString() const override;
 
+       Array::Ptr Unique() const;
        void Freeze();
 
        Value GetFieldByName(const String& field, bool sandboxed, const DebugInfo& debugInfo) const override;
index 5fce798331e252cc45ca411ae953dba3de7b0527..29a16646f8f79dc28c5306c0ca1719ff1f4838cb 100644 (file)
@@ -63,6 +63,7 @@ add_boost_test(base
     base_array/resize
     base_array/insert
     base_array/remove
+    base_array/unique
     base_array/foreach
     base_array/clone
     base_array/json
index 86be0f510e0c84aab36a9dc7704ed4a01885b044..74bbd692c4d47042808487bbc3468dc18c529a14 100644 (file)
@@ -102,6 +102,27 @@ BOOST_AUTO_TEST_CASE(remove)
        BOOST_CHECK(array->GetLength() == 0);
 }
 
+BOOST_AUTO_TEST_CASE(unique)
+{
+       Array::Ptr array = new Array();
+       array->Add("group1");
+       array->Add("group2");
+       array->Add("group1");
+       array->Add("group2");
+
+       Array::Ptr result;
+
+       {
+               ObjectLock olock(array);
+               result = array->Unique();
+       }
+
+       BOOST_CHECK(result->GetLength() == 2);
+       result->Sort();
+
+       BOOST_CHECK(result->Get(0) == "group1");
+       BOOST_CHECK(result->Get(1) == "group2");
+}
 BOOST_AUTO_TEST_CASE(foreach)
 {
        Array::Ptr array = new Array();