From fca936a964da5983daecdbed7cd249934701b41a Mon Sep 17 00:00:00 2001 From: Ben Grimes <72311676+MrGlobby@users.noreply.github.com> Date: Mon, 16 Oct 2023 16:17:46 +0100 Subject: [PATCH] Add tests for the Header class. (#1577) These are intended to exercise methods that are currently not covered by other tests according to the coverage report. Signed-off-by: Ben Grimes --- src/test/OpenEXRTest/CMakeLists.txt | 3 + src/test/OpenEXRTest/main.cpp | 2 + src/test/OpenEXRTest/testHeader.cpp | 109 ++++++++++++++++++++++++++++ src/test/OpenEXRTest/testHeader.h | 8 ++ 4 files changed, 122 insertions(+) create mode 100644 src/test/OpenEXRTest/testHeader.cpp create mode 100644 src/test/OpenEXRTest/testHeader.h diff --git a/src/test/OpenEXRTest/CMakeLists.txt b/src/test/OpenEXRTest/CMakeLists.txt index a843bdfacc..f91e6160fb 100644 --- a/src/test/OpenEXRTest/CMakeLists.txt +++ b/src/test/OpenEXRTest/CMakeLists.txt @@ -57,6 +57,8 @@ add_executable(OpenEXRTest testExistingStreams.h testFutureProofing.cpp testFutureProofing.h + testHeader.cpp + testHeader.h testHuf.cpp testHuf.h testIDManifest.cpp @@ -169,6 +171,7 @@ define_openexr_tests( testDwaLookups testExistingStreams testFutureProofing + testHeader testHuf testInputPart testIsComplete diff --git a/src/test/OpenEXRTest/main.cpp b/src/test/OpenEXRTest/main.cpp index a19b85b5e4..942c4b2db4 100644 --- a/src/test/OpenEXRTest/main.cpp +++ b/src/test/OpenEXRTest/main.cpp @@ -32,6 +32,7 @@ #include "testDwaLookups.h" #include "testExistingStreams.h" #include "testFutureProofing.h" +#include "testHeader.h" #include "testHuf.h" #include "testIDManifest.h" #include "testInputPart.h" @@ -232,6 +233,7 @@ main (int argc, char* argv[]) TEST (testDwaLookups, "core"); TEST (testIDManifest, "core"); TEST (testCpuId, "core"); + TEST (testHeader, "basic"); // NB: If you add a test here, make sure to enumerate it in the // CMakeLists.txt so it runs as part of the test suite diff --git a/src/test/OpenEXRTest/testHeader.cpp b/src/test/OpenEXRTest/testHeader.cpp new file mode 100644 index 0000000000..d0854b7d70 --- /dev/null +++ b/src/test/OpenEXRTest/testHeader.cpp @@ -0,0 +1,109 @@ +// +// SPDX-License-Identifier: BSD-3-Clause +// Copyright (c) Contributors to the OpenEXR Project. +// + +#ifdef NDEBUG +# undef NDEBUG +#endif + +#include +#include + +#include +#include +#include + +#include + +using namespace IEX_NAMESPACE; +using namespace IMATH_NAMESPACE; +using namespace OPENEXR_IMF_NAMESPACE; +using namespace std; + +template +struct Test +{ + void testFind(const string& name) + { + Header header; + auto iterator = header.find(name); + assert(iterator != header.end()); + } + + void testSubscript(const string& name) + { + Header header; + auto& comparand = header.find("displayWindow").attribute(); + auto& attribute = header[name]; + assert(&attribute == &comparand); + } + + void testIterators(const string& name) + { + Header header; + + auto& comparand = header.find("displayWindow").attribute(); + + for (auto iterator = header.begin(); iterator != header.end(); ++iterator) + { + if (iterator.name() == name) + { + assert(&iterator.attribute() == &comparand); + return; + } + } + + assert (false); + } +}; + +void testEraseAttribute(const string& name) +{ + Header header; + assert(header.find(name) != header.end()); + header.erase(name); + assert(header.find(name) == header.end()); +} + +void testEraseAttributeThrowsWithEmptyString() +{ + Header header; + + try + { + header.erase(""); + assert (false); + } + catch (const ArgExc&) + { + assert (true); + } +} + +void testHeader (const string& tempDir) +{ + try + { + { + Test
headerTest; + headerTest.testFind("displayWindow"); + headerTest.testSubscript("displayWindow"); + headerTest.testIterators("displayWindow"); + } + { + Test headerTest; + headerTest.testFind("displayWindow"); + headerTest.testSubscript("displayWindow"); + headerTest.testIterators("displayWindow"); + } + testEraseAttribute("displayWindow"); + testEraseAttributeThrowsWithEmptyString(); + cout << "ok\n" << endl; + } + catch (const exception& e) + { + cerr << "ERROR -- caught exception: " << e.what () << endl; + assert (false); + } +} diff --git a/src/test/OpenEXRTest/testHeader.h b/src/test/OpenEXRTest/testHeader.h new file mode 100644 index 0000000000..b8678e1c13 --- /dev/null +++ b/src/test/OpenEXRTest/testHeader.h @@ -0,0 +1,8 @@ +// +// SPDX-License-Identifier: BSD-3-Clause +// Copyright (c) Contributors to the OpenEXR Project. +// + +#include + +void testHeader (const std::string& tempDir);