From patchwork Tue Aug 16 01:54:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 17128 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id A61B9C3272 for ; Tue, 16 Aug 2022 01:54:32 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4FD2461FBC; Tue, 16 Aug 2022 03:54:32 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1660614872; bh=hHJclU+jgLIrh4iJSwZNCcWf+UXGK/ilaMiZ2D55/rk=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=kHAvwdI+MmMS884763plKq6gVUNdP05R0hkjKXH1OneIuGSb+WRiY/KXdMoJy6i1G zF4rQn8ogQO2MfB33fEY1NhT8OeB6ufgXN6Z6Pre0OajFEfVBeNokcPs6SMQNXtHlS sHBKs3M2NYdf3D13Nyv0nBvFeJk3M8vKoEMRh2ifDWaYSAJTb1/IHwvJY8BYmqEo1i eU+Ia90vGD76aIh5yGxbFvpWC/uXPPTr3aKRJap4hwGc+06MqwrLoalHaxlY8fLQw7 i6EGHiKXwEG3LuUAkbuS4zebOIMFUJSp0fHHAuRIWy3ygmDjkIYKs+W1+ZNJ+XLw4m 54Xi8vG8r7bUA== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id B985D61FBC for ; Tue, 16 Aug 2022 03:54:29 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="u9F4jHEp"; dkim-atps=neutral Received: from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 57E5A496; Tue, 16 Aug 2022 03:54:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1660614869; bh=hHJclU+jgLIrh4iJSwZNCcWf+UXGK/ilaMiZ2D55/rk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=u9F4jHEp6melVzXOHNS19HKZ3nsrMyGpUJTtyQ7As43CPIn3ff1PGxXtXTsnxCqC6 9ZXiyFjkdRhCgZToID+/+it1uti3Mr/UmwW6cfTfI9HOjPjJuLKLdgcDaSKTjtddsm qqLAPtQgHZKRXgsO8NAK8h8tI1ZK65IuQdV4Tw5s= To: libcamera-devel@lists.libcamera.org Date: Tue, 16 Aug 2022 04:54:06 +0300 Message-Id: <20220816015414.7462-2-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220816015414.7462-1-laurent.pinchart@ideasonboard.com> References: <20220816015414.7462-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 1/9] test: yaml-parser: Simplify code by centralizing parse error checks X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Laurent Pinchart via libcamera-devel From: Laurent Pinchart Reply-To: Laurent Pinchart Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Centralize most parse failure checks in a single function to avoid a larger number of copies of nearly identical checks. Signed-off-by: Laurent Pinchart Reviewed-by: Paul Elder Reviewed-by: Jacopo Mondi --- test/yaml-parser.cpp | 309 +++++++++++++------------------------------ 1 file changed, 95 insertions(+), 214 deletions(-) diff --git a/test/yaml-parser.cpp b/test/yaml-parser.cpp index 93ba88b8bcd5..6729e1bd430e 100644 --- a/test/yaml-parser.cpp +++ b/test/yaml-parser.cpp @@ -72,6 +72,83 @@ protected: return TestPass; } + enum class Type { + String, + Int32, + UInt32, + Double, + Size, + List, + Dictionary, + }; + + int testObjectType(const YamlObject &obj, const char *name, Type type) + { + bool isList = type == Type::List || type == Type::Size; + bool isScalar = !isList && type != Type::Dictionary; + bool isInteger = type == Type::Int32 || type == Type::UInt32; + bool isSigned = type == Type::Int32; + + if ((isScalar && !obj.isValue()) || (!isScalar && obj.isValue())) { + std::cerr + << "Object " << name << " type mismatch when compared to " + << "value" << std::endl; + return TestFail; + } + + if ((isList && !obj.isList()) || (!isList && obj.isList())) { + std::cerr + << "Object " << name << " type mismatch when compared to " + << "list" << std::endl; + return TestFail; + } + + if ((type == Type::Dictionary && !obj.isDictionary()) || + (type != Type::Dictionary && obj.isDictionary())) { + std::cerr + << "Object " << name << " type mismatch when compared to " + << "dictionary" << std::endl; + return TestFail; + } + + if (!isScalar && obj.get()) { + std::cerr + << "Object " << name << " didn't fail to parse as " + << "string" << std::endl; + return TestFail; + } + + if (!isInteger && obj.get()) { + std::cerr + << "Object " << name << " didn't fail to parse as " + << "int32_t" << std::endl; + return TestFail; + } + + if ((!isInteger || isSigned) && obj.get()) { + std::cerr + << "Object " << name << " didn't fail to parse as " + << "uint32_t" << std::endl; + return TestFail; + } + + if (!isInteger && type != Type::Double && obj.get()) { + std::cerr + << "Object " << name << " didn't fail to parse as " + << "double" << std::endl; + return TestFail; + } + + if (type != Type::Size && obj.get()) { + std::cerr + << "Object " << name << " didn't fail to parse as " + << "Size" << std::endl; + return TestFail; + } + + return TestPass; + } + int run() { /* Test invalid YAML file */ @@ -107,58 +184,24 @@ protected: return TestFail; } - if (!root->contains("string")) { - cerr << "Missing string object in YAML root" << std::endl; - return TestFail; - } - - if (!root->contains("double")) { - cerr << "Missing double object in YAML root" << std::endl; - return TestFail; - } - - if (!root->contains("int32_t")) { - cerr << "Missing int32_t object in YAML root" << std::endl; - return TestFail; - } - - if (!root->contains("uint32_t")) { - cerr << "Missing uint32_t object in YAML root" << std::endl; - return TestFail; - } - - if (!root->contains("size")) { - cerr << "Missing Size object in YAML root" << std::endl; - return TestFail; - } - - if (!root->contains("list")) { - cerr << "Missing list object in YAML root" << std::endl; - return TestFail; - } - - if (!root->contains("dictionary")) { - cerr << "Missing dictionary object in YAML root" << std::endl; - return TestFail; - } - - if (!root->contains("level1")) { - cerr << "Missing leveled object in YAML root" << std::endl; - return TestFail; + std::vector rootElemNames = { + "string", "double", "int32_t", "uint32_t", "size", + "list", "dictionary", "level1", + }; + + for (const char *name : rootElemNames) { + if (!root->contains(name)) { + cerr << "Missing " << name << " object in YAML root" + << std::endl; + return TestFail; + } } /* Test string object */ auto &strObj = (*root)["string"]; - if (strObj.isDictionary()) { - cerr << "String object parse as Dictionary" << std::endl; + if (testObjectType(strObj, "string", Type::String) != TestPass) return TestFail; - } - - if (strObj.isList()) { - cerr << "String object parse as List" << std::endl; - return TestFail; - } if (strObj.get().value_or("") != "libcamera" || strObj.get("") != "libcamera") { @@ -166,38 +209,11 @@ protected: return TestFail; } - if (strObj.get()) { - cerr << "String object parse as integer" << std::endl; - return TestFail; - } - - if (strObj.get()) { - cerr << "String object parse as unsigned integer" << std::endl; - return TestFail; - } - - if (strObj.get()) { - cerr << "String object parse as double" << std::endl; - return TestFail; - } - - if (strObj.get()) { - cerr << "String object parse as Size" << std::endl; - return TestFail; - } - /* Test int32_t object */ auto &int32Obj = (*root)["int32_t"]; - if (int32Obj.isDictionary()) { - cerr << "Integer object parse as Dictionary" << std::endl; + if (testObjectType(int32Obj, "int32_t", Type::Int32) != TestPass) return TestFail; - } - - if (int32Obj.isList()) { - cerr << "Integer object parse as Integer" << std::endl; - return TestFail; - } if (int32Obj.get().value_or(0) != -100 || int32Obj.get(0) != -100) { @@ -217,28 +233,11 @@ protected: return TestFail; } - if (int32Obj.get()) { - cerr << "Negative integer object parse as unsigned integer" << std::endl; - return TestFail; - } - - if (int32Obj.get()) { - cerr << "Integer object parse as Size" << std::endl; - return TestFail; - } - /* Test uint32_t object */ auto &uint32Obj = (*root)["uint32_t"]; - if (uint32Obj.isDictionary()) { - cerr << "Unsigned integer object parse as Dictionary" << std::endl; + if (testObjectType(uint32Obj, "uint32_t", Type::UInt32) != TestPass) return TestFail; - } - - if (uint32Obj.isList()) { - cerr << "Unsigned integer object parse as List" << std::endl; - return TestFail; - } if (uint32Obj.get().value_or(0) != 100 || uint32Obj.get(0) != 100) { @@ -264,23 +263,11 @@ protected: return TestFail; } - if (uint32Obj.get()) { - cerr << "Unsigned integer object parsed as Size" << std::endl; - return TestFail; - } - /* Test double value */ auto &doubleObj = (*root)["double"]; - if (doubleObj.isDictionary()) { - cerr << "Double object parse as Dictionary" << std::endl; + if (testObjectType(doubleObj, "double", Type::Double) != TestPass) return TestFail; - } - - if (doubleObj.isList()) { - cerr << "Double object parse as List" << std::endl; - return TestFail; - } if (doubleObj.get().value_or("") != "3.14159" || doubleObj.get("") != "3.14159") { @@ -294,53 +281,11 @@ protected: return TestFail; } - if (doubleObj.get()) { - cerr << "Double object parse as integer" << std::endl; - return TestFail; - } - - if (doubleObj.get()) { - cerr << "Double object parse as unsigned integer" << std::endl; - return TestFail; - } - - if (doubleObj.get()) { - cerr << "Double object parse as Size" << std::endl; - return TestFail; - } - /* Test Size value */ auto &sizeObj = (*root)["size"]; - if (sizeObj.isDictionary()) { - cerr << "Size object parse as Dictionary" << std::endl; + if (testObjectType(sizeObj, "size", Type::Size) != TestPass) return TestFail; - } - - if (!sizeObj.isList()) { - cerr << "Size object parse as List" << std::endl; - return TestFail; - } - - if (sizeObj.get()) { - cerr << "Size object parse as string" << std::endl; - return TestFail; - } - - if (sizeObj.get()) { - cerr << "Size object parse as double" << std::endl; - return TestFail; - } - - if (sizeObj.get()) { - cerr << "Size object parse as integer" << std::endl; - return TestFail; - } - - if (sizeObj.get()) { - cerr << "Size object parse as unsigned integer" << std::endl; - return TestFail; - } if (sizeObj.get().value_or(Size(0, 0)) != Size(1920, 1080) || sizeObj.get(Size(0, 0)) != Size(1920, 1080)) { @@ -351,40 +296,8 @@ protected: /* Test list object */ auto &listObj = (*root)["list"]; - if (listObj.isDictionary()) { - cerr << "List object parse as Dictionary" << std::endl; + if (testObjectType(listObj, "list", Type::List) != TestPass) return TestFail; - } - - if (!listObj.isList()) { - cerr << "List object fail to parse as List" << std::endl; - return TestFail; - } - - if (listObj.get()) { - cerr << "List object parse as string" << std::endl; - return TestFail; - } - - if (listObj.get()) { - cerr << "List object parse as double" << std::endl; - return TestFail; - } - - if (listObj.get()) { - cerr << "List object parse as integer" << std::endl; - return TestFail; - } - - if (listObj.get()) { - cerr << "List object parse as unsigne integer" << std::endl; - return TestFail; - } - - if (listObj.get()) { - cerr << "String list object parse as Size" << std::endl; - return TestFail; - } static constexpr std::array listValues{ "James", @@ -424,40 +337,8 @@ protected: /* Test dictionary object */ auto &dictObj = (*root)["dictionary"]; - if (!dictObj.isDictionary()) { - cerr << "Dictionary object fail to parse as Dictionary" << std::endl; + if (testObjectType(dictObj, "dictionary", Type::Dictionary) != TestPass) return TestFail; - } - - if (dictObj.isList()) { - cerr << "Dictionary object parse as List" << std::endl; - return TestFail; - } - - if (dictObj.get()) { - cerr << "Dictionary object parse as string" << std::endl; - return TestFail; - } - - if (dictObj.get()) { - cerr << "Dictionary object parse as double" << std::endl; - return TestFail; - } - - if (dictObj.get()) { - cerr << "Dictionary object parse as integer" << std::endl; - return TestFail; - } - - if (dictObj.get()) { - cerr << "Dictionary object parse as unsigned integer" << std::endl; - return TestFail; - } - - if (dictObj.get()) { - cerr << "Dictionary object parse as Size" << std::endl; - return TestFail; - } static constexpr std::array, 3> dictValues{ { { "a", 1 },