From a69958fcd6c7bf394859f5c5177c4a685f512f45 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Mon, 15 Aug 2022 23:38:19 +0300 Subject: libcamera: yaml_parser: Enable YamlObject::get() for int8_t and uint8_t The YamlObject::get() function template is implemented for 16-bit and 32-bit integers. Add an 8-bit specialization that will be used in the rkisp1 IPA module, and extend the unit tests accordingly. Signed-off-by: Laurent Pinchart Reviewed-by: Paul Elder Reviewed-by: Jacopo Mondi --- test/yaml-parser.cpp | 86 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 74 insertions(+), 12 deletions(-) (limited to 'test') diff --git a/test/yaml-parser.cpp b/test/yaml-parser.cpp index 28f8cc88..2d92463a 100644 --- a/test/yaml-parser.cpp +++ b/test/yaml-parser.cpp @@ -24,6 +24,8 @@ using namespace std; static const string testYaml = "string: libcamera\n" "double: 3.14159\n" + "int8_t: -100\n" + "uint8_t: 100\n" "int16_t: -1000\n" "uint16_t: 1000\n" "int32_t: -100000\n" @@ -76,6 +78,8 @@ protected: enum class Type { String, + Int8, + UInt8, Int16, UInt16, Int32, @@ -90,10 +94,13 @@ protected: { bool isList = type == Type::List || type == Type::Size; bool isScalar = !isList && type != Type::Dictionary; + bool isInteger8 = type == Type::Int8 || type == Type::UInt8; bool isInteger16 = type == Type::Int16 || type == Type::UInt16; bool isInteger32 = type == Type::Int32 || type == Type::UInt32; - bool isInteger = isInteger16 || isInteger32; - bool isSigned = type == Type::Int16 || type == Type::Int32; + bool isIntegerUpTo16 = isInteger8 || isInteger16; + bool isIntegerUpTo32 = isIntegerUpTo16 || isInteger32; + bool isSigned = type == Type::Int8 || type == Type::Int16 || + type == Type::Int32; if ((isScalar && !obj.isValue()) || (!isScalar && obj.isValue())) { std::cerr @@ -124,35 +131,49 @@ protected: return TestFail; } - if (!isInteger16 && obj.get()) { + if (!isInteger8 && obj.get()) { + std::cerr + << "Object " << name << " didn't fail to parse as " + << "int8_t" << std::endl; + return TestFail; + } + + if ((!isInteger8 || isSigned) && obj.get()) { + std::cerr + << "Object " << name << " didn't fail to parse as " + << "uint8_t" << std::endl; + return TestFail; + } + + if (!isIntegerUpTo16 && obj.get()) { std::cerr << "Object " << name << " didn't fail to parse as " << "int16_t" << std::endl; return TestFail; } - if ((!isInteger16 || isSigned) && obj.get()) { + if ((!isIntegerUpTo16 || isSigned) && obj.get()) { std::cerr << "Object " << name << " didn't fail to parse as " << "uint16_t" << std::endl; return TestFail; } - if (!isInteger && obj.get()) { + if (!isIntegerUpTo32 && obj.get()) { std::cerr << "Object " << name << " didn't fail to parse as " << "int32_t" << std::endl; return TestFail; } - if ((!isInteger || isSigned) && obj.get()) { + if ((!isIntegerUpTo32 || 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()) { + if (!isIntegerUpTo32 && type != Type::Double && obj.get()) { std::cerr << "Object " << name << " didn't fail to parse as " << "double" << std::endl; @@ -174,8 +195,10 @@ protected: { uint64_t unsignedValue = static_cast(value); std::string strValue = std::to_string(value); + bool isInteger8 = type == Type::Int8 || type == Type::UInt8; bool isInteger16 = type == Type::Int16 || type == Type::UInt16; - bool isSigned = type == Type::Int16 || type == Type::Int32; + bool isSigned = type == Type::Int8 || type == Type::Int16 || + type == Type::Int32; /* All integers can be parsed as strings or double. */ @@ -195,7 +218,27 @@ protected: return TestFail; } - if (isInteger16) { + if (isInteger8) { + if (obj.get().value_or(0) != value || + obj.get(0) != value) { + std::cerr + << "Object " << name << " failed to parse as " + << "int8_t" << std::endl; + return TestFail; + } + } + + if (isInteger8 && !isSigned) { + if (obj.get().value_or(0) != unsignedValue || + obj.get(0) != unsignedValue) { + std::cerr + << "Object " << name << " failed to parse as " + << "uint8_t" << std::endl; + return TestFail; + } + } + + if (isInteger8 || isInteger16) { if (obj.get().value_or(0) != value || obj.get(0) != value) { std::cerr @@ -205,7 +248,7 @@ protected: } } - if (isInteger16 && !isSigned) { + if ((isInteger8 || isInteger16) && !isSigned) { if (obj.get().value_or(0) != unsignedValue || obj.get(0) != unsignedValue) { std::cerr @@ -272,8 +315,9 @@ protected: } std::vector rootElemNames = { - "string", "double", "int16_t", "uint16_t", "int32_t", - "uint32_t", "size", "list", "dictionary", "level1", + "string", "double", "int8_t", "uint8_t", "int16_t", + "uint16_t", "int32_t", "uint32_t", "size", "list", + "dictionary", "level1", }; for (const char *name : rootElemNames) { @@ -296,6 +340,24 @@ protected: return TestFail; } + /* Test int8_t object */ + auto &int8Obj = (*root)["int8_t"]; + + if (testObjectType(int8Obj, "int8_t", Type::Int8) != TestPass) + return TestFail; + + if (testIntegerObject(int8Obj, "int8_t", Type::Int8, -100) != TestPass) + return TestFail; + + /* Test uint8_t object */ + auto &uint8Obj = (*root)["uint8_t"]; + + if (testObjectType(uint8Obj, "uint8_t", Type::UInt8) != TestPass) + return TestFail; + + if (testIntegerObject(uint8Obj, "uint8_t", Type::UInt8, 100) != TestPass) + return TestFail; + /* Test int16_t object */ auto &int16Obj = (*root)["int16_t"]; -- cgit v1.2.1