[libcamera-devel] [RFC PATCH v2 08/14] test: yaml_parser: Extend tests to cover the iterator API
Laurent Pinchart
laurent.pinchart at ideasonboard.com
Sat Jun 4 20:59:33 CEST 2022
Test iteration over lists and dictionaries to test the YamlObject
iterator API.
Signed-off-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
---
test/yaml-parser.cpp | 88 ++++++++++++++++++++++++++++++++++----------
1 file changed, 69 insertions(+), 19 deletions(-)
diff --git a/test/yaml-parser.cpp b/test/yaml-parser.cpp
index 582c9caed836..6dfec49d6be0 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>
@@ -373,15 +374,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 */
@@ -422,18 +447,50 @@ 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 },
+ { "c", 3 },
+ { "b", 2 },
+ } };
+
+ 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();
- if (memeberNames.size() != 3) {
- cerr << "Dictionary object fail to extra member names" << std::endl;
- return TestFail;
- }
-
if (memeberNames[0] != "a" ||
memeberNames[1] != "c" ||
memeberNames[2] != "b") {
@@ -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"];
--
Regards,
Laurent Pinchart
More information about the libcamera-devel
mailing list