Skip to content

Commit fca936a

Browse files
authored
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 <[email protected]>
1 parent de791a5 commit fca936a

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

src/test/OpenEXRTest/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ add_executable(OpenEXRTest
5757
testExistingStreams.h
5858
testFutureProofing.cpp
5959
testFutureProofing.h
60+
testHeader.cpp
61+
testHeader.h
6062
testHuf.cpp
6163
testHuf.h
6264
testIDManifest.cpp
@@ -169,6 +171,7 @@ define_openexr_tests(
169171
testDwaLookups
170172
testExistingStreams
171173
testFutureProofing
174+
testHeader
172175
testHuf
173176
testInputPart
174177
testIsComplete

src/test/OpenEXRTest/main.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "testDwaLookups.h"
3333
#include "testExistingStreams.h"
3434
#include "testFutureProofing.h"
35+
#include "testHeader.h"
3536
#include "testHuf.h"
3637
#include "testIDManifest.h"
3738
#include "testInputPart.h"
@@ -232,6 +233,7 @@ main (int argc, char* argv[])
232233
TEST (testDwaLookups, "core");
233234
TEST (testIDManifest, "core");
234235
TEST (testCpuId, "core");
236+
TEST (testHeader, "basic");
235237

236238
// NB: If you add a test here, make sure to enumerate it in the
237239
// CMakeLists.txt so it runs as part of the test suite

src/test/OpenEXRTest/testHeader.cpp

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
//
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
// Copyright (c) Contributors to the OpenEXR Project.
4+
//
5+
6+
#ifdef NDEBUG
7+
# undef NDEBUG
8+
#endif
9+
10+
#include <ImfBoxAttribute.h>
11+
#include <ImfHeader.h>
12+
13+
#include <exception>
14+
#include <iostream>
15+
#include <string>
16+
17+
#include <assert.h>
18+
19+
using namespace IEX_NAMESPACE;
20+
using namespace IMATH_NAMESPACE;
21+
using namespace OPENEXR_IMF_NAMESPACE;
22+
using namespace std;
23+
24+
template<typename Header>
25+
struct Test
26+
{
27+
void testFind(const string& name)
28+
{
29+
Header header;
30+
auto iterator = header.find(name);
31+
assert(iterator != header.end());
32+
}
33+
34+
void testSubscript(const string& name)
35+
{
36+
Header header;
37+
auto& comparand = header.find("displayWindow").attribute();
38+
auto& attribute = header[name];
39+
assert(&attribute == &comparand);
40+
}
41+
42+
void testIterators(const string& name)
43+
{
44+
Header header;
45+
46+
auto& comparand = header.find("displayWindow").attribute();
47+
48+
for (auto iterator = header.begin(); iterator != header.end(); ++iterator)
49+
{
50+
if (iterator.name() == name)
51+
{
52+
assert(&iterator.attribute() == &comparand);
53+
return;
54+
}
55+
}
56+
57+
assert (false);
58+
}
59+
};
60+
61+
void testEraseAttribute(const string& name)
62+
{
63+
Header header;
64+
assert(header.find(name) != header.end());
65+
header.erase(name);
66+
assert(header.find(name) == header.end());
67+
}
68+
69+
void testEraseAttributeThrowsWithEmptyString()
70+
{
71+
Header header;
72+
73+
try
74+
{
75+
header.erase("");
76+
assert (false);
77+
}
78+
catch (const ArgExc&)
79+
{
80+
assert (true);
81+
}
82+
}
83+
84+
void testHeader (const string& tempDir)
85+
{
86+
try
87+
{
88+
{
89+
Test<Header> headerTest;
90+
headerTest.testFind("displayWindow");
91+
headerTest.testSubscript("displayWindow");
92+
headerTest.testIterators("displayWindow");
93+
}
94+
{
95+
Test<const Header> headerTest;
96+
headerTest.testFind("displayWindow");
97+
headerTest.testSubscript("displayWindow");
98+
headerTest.testIterators("displayWindow");
99+
}
100+
testEraseAttribute("displayWindow");
101+
testEraseAttributeThrowsWithEmptyString();
102+
cout << "ok\n" << endl;
103+
}
104+
catch (const exception& e)
105+
{
106+
cerr << "ERROR -- caught exception: " << e.what () << endl;
107+
assert (false);
108+
}
109+
}

src/test/OpenEXRTest/testHeader.h

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
// Copyright (c) Contributors to the OpenEXR Project.
4+
//
5+
6+
#include <string>
7+
8+
void testHeader (const std::string& tempDir);

0 commit comments

Comments
 (0)