[libcamera-devel,RFC,06/12] test: yaml_parser: Extend tests to cover the iterator API
diff mbox series

Message ID 20220524225816.6830-7-laurent.pinchart@ideasonboard.com
State Accepted
Headers show
Series
  • Replace boost JSON parser with libyaml in Raspberry Pi IPA
Related show

Commit Message

Laurent Pinchart May 24, 2022, 10:58 p.m. UTC
Test iteration over lists and dictionaries to test the YamlObject
iterator API.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 test/yaml-parser.cpp | 88 ++++++++++++++++++++++++++++++++++----------
 1 file changed, 69 insertions(+), 19 deletions(-)

Patch
diff mbox series

diff --git a/test/yaml-parser.cpp b/test/yaml-parser.cpp
index 944b564bbe22..7ad9936bb82b 100644
--- a/test/yaml-parser.cpp
+++ b/test/yaml-parser.cpp
@@ -5,6 +5,7 @@ 
  * yaml-parser.cpp - YAML parser operations tests
  */
 
+#include <array>
 #include <iostream>
 #include <string>
 #include <unistd.h>
@@ -372,15 +373,39 @@  protected:
 			return TestFail;
 		}
 
-		if (listObj.size() > 2) {
+		static constexpr std::array<const char *, 2> listValues{
+			"James",
+			"Mary",
+		};
+
+		if (listObj.size() != listValues.size()) {
 			cerr << "List object parse with wrong size" << std::endl;
 			return TestFail;
 		}
 
-		if (listObj[0].get<string>("") != "James" ||
-		    listObj[1].get<string>("") != "Mary") {
-			cerr << "List object parse as wrong value" << std::endl;
-			return TestFail;
+		unsigned int i = 0;
+		for (auto &elem : listObj.asList()) {
+			if (i >= listValues.size()) {
+				std::cerr << "Too many elements in list during iteration"
+					  << std::endl;
+				return TestFail;
+			}
+
+			std::string value = listValues[i];
+
+			if (&elem != &listObj[i]) {
+				std::cerr << "List element " << i << " has wrong address"
+					  << std::endl;
+				return TestFail;
+			}
+
+			if (elem.get<std::string>("") != value) {
+				std::cerr << "List element " << i << " has wrong value"
+					  << std::endl;
+				return TestFail;
+			}
+
+			i++;
 		}
 
 		/* Test dictionary object */
@@ -421,19 +446,51 @@  protected:
 			return TestFail;
 		}
 
-		if (dictObj.size() != 3) {
-			cerr << "Dictionary object parse with wrong size" << std::endl;
+		static constexpr std::array<std::pair<const char *, int>, 3> dictValues{ {
+			{ "a", 1 },
+			{ "b", 2 },
+			{ "c", 3 },
+		} };
+
+		if (dictObj.size() != dictValues.size()) {
+			cerr << "Dictionary object has wrong size" << std::endl;
 			return TestFail;
 		}
 
+		i = 0;
+		for (const auto &[key, elem] : dictObj.asDict()) {
+			if (i >= dictValues.size()) {
+				std::cerr << "Too many elements in dictionary during iteration"
+					  << std::endl;
+				return TestFail;
+			}
+
+			const std::pair<const char *, int> &value = dictValues[i];
+
+			if (key != value.first) {
+				std::cerr << "Dictionary key " << i << " has wrong value"
+					  << std::endl;
+				return TestFail;
+			}
+
+			if (&elem != &dictObj[key]) {
+				std::cerr << "Dictionary element " << i << " has wrong address"
+					  << std::endl;
+				return TestFail;
+			}
+
+			if (elem.get<int32_t>(0) != value.second) {
+				std::cerr << "Dictionary element " << i << " has wrong value"
+					  << std::endl;
+				return TestFail;
+			}
+
+			i++;
+		}
+
 		auto memeberNames = dictObj.memberNames();
 		sort(memeberNames.begin(), memeberNames.end());
 
-		if (memeberNames.size() != 3) {
-			cerr << "Dictionary object fail to extra member names" << std::endl;
-			return TestFail;
-		}
-
 		if (memeberNames[0] != "a" ||
 		    memeberNames[1] != "b" ||
 		    memeberNames[2] != "c") {
@@ -441,13 +498,6 @@  protected:
 			return TestFail;
 		}
 
-		if (dictObj["a"].get<int32_t>(0) != 1 ||
-		    dictObj["b"].get<int32_t>(0) != 2 ||
-		    dictObj["c"].get<int32_t>(0) != 3) {
-			cerr << "Dictionary object fail to parse member value" << std::endl;
-			return TestFail;
-		}
-
 		/* Test leveled objects */
 		auto &level1Obj = (*root)["level1"];