]> granicus.if.org Git - icinga2/commitdiff
Make sure that unnamed items in a dictionary are always in the order they were insert...
authorGunnar Beutner <gunnar.beutner@netways.de>
Thu, 7 Feb 2013 08:36:17 +0000 (09:36 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Thu, 7 Feb 2013 08:36:17 +0000 (09:36 +0100)
lib/base/dictionary.cpp
test/base-dictionary.cpp

index 78dbadaa26f43906ae2993fea380b4bd41e6abd4..46595d5d437f19255167b1e312cea365b69b7c19 100644 (file)
@@ -115,7 +115,7 @@ String Dictionary::Add(const Value& value)
        long index = GetLength();
        do {
                stringstream s;
-               s << "_" << index;
+               s << "_" << std::hex << std::setw(8) << std::setfill('0') << index;
                index++;
 
                key = s.str();
index d09b74fc86b3d743de167edf77978a7c4d228c3b..c2452b39d2ce6446292b580c7ea7c4fa5dbaf180 100644 (file)
@@ -53,3 +53,21 @@ BOOST_AUTO_TEST_CASE(unnamed)
 
        BOOST_REQUIRE(distance(dictionary->Begin(), dictionary->End()) == 3);
 }
+
+BOOST_AUTO_TEST_CASE(unnamed_order)
+{
+       Dictionary::Ptr dictionary = boost::make_shared<Dictionary>();
+
+       for (int i = 0; i < 1000; i++)
+               dictionary->Add(i);
+
+       /* unnamed items are guaranteed to be in whatever order they were
+        * inserted in. */
+       String key;
+       Value value;
+       int i = 0;
+       BOOST_FOREACH(tie(key, value), dictionary) {
+               BOOST_REQUIRE(value == i);
+               i++;
+       }
+}