Implement Array::Unique() and add unit tests

refs #4732
This commit is contained in:
Michael Friedrich 2018-05-09 16:55:14 +02:00
parent ce01adf018
commit 6660a45c41
5 changed files with 37 additions and 9 deletions

View 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()

View 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);

View 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;

View 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

View 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();