[icinga-checkins] icinga.org: icinga2/master: Make sure that unnamed items in a dictionary are always in the order they were inserted in .

git at icinga.org git at icinga.org
Thu Feb 7 09:36:40 CET 2013


Module: icinga2
Branch: master
Commit: c04cfb9dac859fecba6e8e808e213db21ecbaec1
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=c04cfb9dac859fecba6e8e808e213db21ecbaec1

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Thu Feb  7 09:36:17 2013 +0100

Make sure that unnamed items in a dictionary are always in the order they were inserted in.

---

 lib/base/dictionary.cpp  |    2 +-
 test/base-dictionary.cpp |   18 ++++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletions(-)

diff --git a/lib/base/dictionary.cpp b/lib/base/dictionary.cpp
index 78dbada..46595d5 100644
--- a/lib/base/dictionary.cpp
+++ b/lib/base/dictionary.cpp
@@ -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();
diff --git a/test/base-dictionary.cpp b/test/base-dictionary.cpp
index d09b74f..c2452b3 100644
--- a/test/base-dictionary.cpp
+++ b/test/base-dictionary.cpp
@@ -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++;
+	}
+}





More information about the icinga-checkins mailing list