From b0b0ec6d122b73ce20c0038fe5a8a8e745b19b9d Mon Sep 17 00:00:00 2001 From: Andrei Pavel Date: Thu, 22 Jul 2021 22:29:28 +0300 Subject: [PATCH] [#1077] better Element.sortMaps unit test --- src/lib/cc/tests/data_unittests.cc | 38 ++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/lib/cc/tests/data_unittests.cc b/src/lib/cc/tests/data_unittests.cc index 253c2d8893..2eceb1a482 100644 --- a/src/lib/cc/tests/data_unittests.cc +++ b/src/lib/cc/tests/data_unittests.cc @@ -1420,17 +1420,45 @@ TEST(Element, sortStrings) { } TEST(Element, sortMaps) { - ElementPtr e1(Element::fromJSON(R"({"id": 1, "subnet": "10.0.1.0/24"})")); + ElementPtr e1(Element::fromJSON(R"({"id": 1, "subnet": "10.0.2.0/24"})")); ElementPtr e2(Element::fromJSON(R"({"id": 2, "subnet": "10.0.1.0/24"})")); - ElementPtr l(Element::createList()); - l->add(e2); + ElementPtr l; + + // Test sorting by "id". Order shouldn't change. + l = Element::createList(); l->add(e1); - EXPECT_EQ(*l->get(0), *e2); - EXPECT_EQ(*l->get(1), *e1); + l->add(e2); boost::dynamic_pointer_cast(l)->sort("id"); ASSERT_EQ(l->size(), 2); EXPECT_EQ(*l->get(0), *e1); EXPECT_EQ(*l->get(1), *e2); + + // Test sorting by "id". Order should change. + l = Element::createList(); + l->add(e2); + l->add(e1); + boost::dynamic_pointer_cast(l)->sort("id"); + ASSERT_EQ(l->size(), 2); + EXPECT_EQ(*l->get(0), *e1); + EXPECT_EQ(*l->get(1), *e2); + + // Test sorting by "subnet". Order should change. + l = Element::createList(); + l->add(e1); + l->add(e2); + boost::dynamic_pointer_cast(l)->sort("subnet"); + ASSERT_EQ(l->size(), 2); + EXPECT_EQ(*l->get(0), *e2); + EXPECT_EQ(*l->get(1), *e1); + + // Test sorting by "subnet". Order shouldn't change. + l = Element::createList(); + l->add(e2); + l->add(e1); + boost::dynamic_pointer_cast(l)->sort("subnet"); + ASSERT_EQ(l->size(), 2); + EXPECT_EQ(*l->get(0), *e2); + EXPECT_EQ(*l->get(1), *e1); } TEST(Element, removeEmptyContainersRecursively) {