From patchwork Tue Aug 16 01:54:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 17129 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 DE876C3272 for ; Tue, 16 Aug 2022 01:54:33 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D443261FC8; 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=RFaj1YfbNtPs/J89bCrjLlmBZYKCilVYZMH7MRWeKW0=; 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=d/XAWfWBe2eKCh4kQ6vj3jQX6+Ey+sbtCdssR+ZLKuO0hwpUIhsXAopZJ8ZMZLo01 JQ33JdmyHKH2hr1iiS9GHxbG+p6sbzfYNVR3tTMlZg3tBlcMQNpl8h2MAdWoEN9Ajn ws3WdXkJbkSOFvW0WMOdj3Z6reITwUWrWdm+kCdoK+V5kDC1So1liZHLiTZZ7gKV52 RUuYjf6hRCpLt+mWkdI+0kv1+/0ptFn7hxujvvSeAo4cZfTg7WJ6xTJRtpNY7XnaIN tFn6Xml0JVXUk8TXVBPF0Yh2Sel/W6/glbonbcmERnAZX4o3SyFFpsSUy6a6uHPCe2 qzZ7y5jtLSARQ== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 520FE603E3 for ; Tue, 16 Aug 2022 03:54:31 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="UzdhceGp"; 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 D7A65496; Tue, 16 Aug 2022 03:54:30 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1660614871; bh=RFaj1YfbNtPs/J89bCrjLlmBZYKCilVYZMH7MRWeKW0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UzdhceGpW9lpmc71NLZCHnxBmNsVMfKlqlHscKTZtQzBaP5t7HgzNviIAH78MUt8G KFf8c/ZMinsDPsjLqlpVk4rAY370dC2UPSQ0O+qM9XDaur6P+iZQyPfkZRLVTuGF+I 5bBziINfsLGFto9M8b7iTMW0uTuKBO67t9xCDORw= To: libcamera-devel@lists.libcamera.org Date: Tue, 16 Aug 2022 04:54:07 +0300 Message-Id: <20220816015414.7462-3-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 2/9] test: yaml-parser: Centralize integer parse 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 the signed and unsigned integer parse checks to avoid code duplication. The diffstat isn't very impressive at this point, but this will help more when adding 8-bit and 16-bit integer tests. Signed-off-by: Laurent Pinchart Reviewed-by: Paul Elder --- test/yaml-parser.cpp | 86 ++++++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 38 deletions(-) diff --git a/test/yaml-parser.cpp b/test/yaml-parser.cpp index 6729e1bd430e..803e70beb782 100644 --- a/test/yaml-parser.cpp +++ b/test/yaml-parser.cpp @@ -149,6 +149,52 @@ protected: return TestPass; } + int testIntegerObject(const YamlObject &obj, const char *name, Type type, + int64_t value) + { + uint64_t unsignedValue = static_cast(value); + std::string strValue = std::to_string(value); + bool isSigned = type == Type::Int32; + + /* All integers can be parsed as strings or double. */ + + if (obj.get().value_or("") != strValue || + obj.get("") != strValue) { + std::cerr + << "Object " << name << " failed to parse as " + << "string" << std::endl; + return TestFail; + } + + if (obj.get().value_or(0.0) != value || + obj.get(0.0) != value) { + std::cerr + << "Object " << name << " failed to parse as " + << "double" << std::endl; + return TestFail; + } + + if (obj.get().value_or(0) != value || + obj.get(0) != value) { + std::cerr + << "Object " << name << " failed to parse as " + << "int32_t" << std::endl; + return TestFail; + } + + if (!isSigned) { + if (obj.get().value_or(0) != unsignedValue || + obj.get(0) != unsignedValue) { + std::cerr + << "Object " << name << " failed to parse as " + << "uint32_t" << std::endl; + return TestFail; + } + } + + return TestPass; + } + int run() { /* Test invalid YAML file */ @@ -215,23 +261,8 @@ protected: if (testObjectType(int32Obj, "int32_t", Type::Int32) != TestPass) return TestFail; - if (int32Obj.get().value_or(0) != -100 || - int32Obj.get(0) != -100) { - cerr << "Integer object parse as wrong value" << std::endl; + if (testIntegerObject(int32Obj, "int32_t", Type::Int32, -100) != TestPass) return TestFail; - } - - if (int32Obj.get().value_or("") != "-100" || - int32Obj.get("") != "-100") { - cerr << "Integer object fail to parse as string" << std::endl; - return TestFail; - } - - if (int32Obj.get().value_or(0.0) != -100.0 || - int32Obj.get(0.0) != -100.0) { - cerr << "Integer object fail to parse as double" << std::endl; - return TestFail; - } /* Test uint32_t object */ auto &uint32Obj = (*root)["uint32_t"]; @@ -239,29 +270,8 @@ protected: if (testObjectType(uint32Obj, "uint32_t", Type::UInt32) != TestPass) return TestFail; - if (uint32Obj.get().value_or(0) != 100 || - uint32Obj.get(0) != 100) { - cerr << "Unsigned integer object fail to parse as integer" << std::endl; + if (testIntegerObject(uint32Obj, "uint32_t", Type::UInt32, 100) != TestPass) return TestFail; - } - - if (uint32Obj.get().value_or("") != "100" || - uint32Obj.get("") != "100") { - cerr << "Unsigned integer object fail to parse as string" << std::endl; - return TestFail; - } - - if (uint32Obj.get().value_or(0.0) != 100.0 || - uint32Obj.get(0.0) != 100.0) { - cerr << "Unsigned integer object fail to parse as double" << std::endl; - return TestFail; - } - - if (uint32Obj.get().value_or(0) != 100 || - uint32Obj.get(0) != 100) { - cerr << "Unsigned integer object parsed as wrong value" << std::endl; - return TestFail; - } /* Test double value */ auto &doubleObj = (*root)["double"];