From 90577226aabbde593e61b234ffa4b2193c53ddef Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Wed, 25 May 2022 01:58:10 +0300 Subject: test: yaml_parser: Extend tests to cover the iterator API Test iteration over lists and dictionaries to test the YamlObject iterator API. Signed-off-by: Laurent Pinchart Reviewed-by: Jacopo Mondi Reviewed-by: Han-Lin Chen --- test/yaml-parser.cpp | 99 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 80 insertions(+), 19 deletions(-) diff --git a/test/yaml-parser.cpp b/test/yaml-parser.cpp index 5ff4c323..ab6e9a01 100644 --- a/test/yaml-parser.cpp +++ b/test/yaml-parser.cpp @@ -5,11 +5,14 @@ * yaml-parser.cpp - YAML parser operations tests */ +#include #include +#include #include #include #include +#include #include "libcamera/internal/yaml_parser.h" @@ -373,15 +376,39 @@ protected: return TestFail; } - if (listObj.size() > 2) { + static constexpr std::array listValues{ + "James", + "Mary", + }; + + if (listObj.size() != listValues.size()) { cerr << "List object parse with wrong size" << std::endl; return TestFail; } - if (listObj[0].get("") != "James" || - listObj[1].get("") != "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("") != value) { + std::cerr << "List element " << i << " has wrong value" + << std::endl; + return TestFail; + } + + i++; } /* Test dictionary object */ @@ -422,19 +449,60 @@ protected: return TestFail; } - if (dictObj.size() != 3) { - cerr << "Dictionary object parse with wrong size" << std::endl; + std::map dictValues{ { + { "a", 1 }, + { "b", 2 }, + { "c", 3 }, + } }; + + size_t dictSize = dictValues.size(); + + if (dictObj.size() != dictSize) { + cerr << "Dictionary object has wrong size" << std::endl; return TestFail; } - auto memeberNames = dictObj.memberNames(); - sort(memeberNames.begin(), memeberNames.end()); + i = 0; + for (const auto &[key, elem] : dictObj.asDict()) { + if (i >= dictSize) { + std::cerr << "Too many elements in dictionary during iteration" + << std::endl; + return TestFail; + } - if (memeberNames.size() != 3) { - cerr << "Dictionary object fail to extra member names" << std::endl; - return TestFail; + const auto item = dictValues.find(key); + if (item == dictValues.end()) { + 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(0) != item->second) { + std::cerr << "Dictionary element " << i << " has wrong value" + << std::endl; + return TestFail; + } + + /* + * Erase the item to make sure that each iteration + * produces a different value. + */ + dictValues.erase(item); + i++; } + /* Make sure utils::map_keys() works on the adapter. */ + (void)utils::map_keys(dictObj.asDict()); + + auto memeberNames = dictObj.memberNames(); + sort(memeberNames.begin(), memeberNames.end()); + if (memeberNames[0] != "a" || memeberNames[1] != "b" || memeberNames[2] != "c") { @@ -442,13 +510,6 @@ protected: return TestFail; } - if (dictObj["a"].get(0) != 1 || - dictObj["b"].get(0) != 2 || - dictObj["c"].get(0) != 3) { - cerr << "Dictionary object fail to parse member value" << std::endl; - return TestFail; - } - /* Test leveled objects */ auto &level1Obj = (*root)["level1"]; -- cgit v1.2.1