Browse Source

TgaImporter: unify the error test cases.

Make them instanced where possible, removing a lot of duplicated code.
pull/605/head
Vladimír Vondruš 4 years ago
parent
commit
cf24c0140d
  1. 153
      src/MagnumPlugins/TgaImporter/Test/TgaImporterTest.cpp

153
src/MagnumPlugins/TgaImporter/Test/TgaImporterTest.cpp

@ -45,12 +45,10 @@ namespace Magnum { namespace Trade { namespace Test { namespace {
struct TgaImporterTest: TestSuite::Tester { struct TgaImporterTest: TestSuite::Tester {
explicit TgaImporterTest(); explicit TgaImporterTest();
void openEmpty(); void invalidEmpty();
void openShort(); void invalidShort();
void paletted();
void invalid(); void invalid();
void unsupportedBits(); void invalidBits();
void color24(); void color24();
void color24Rle(); void color24Rle();
@ -59,8 +57,6 @@ struct TgaImporterTest: TestSuite::Tester {
void grayscale8(); void grayscale8();
void grayscale8Rle(); void grayscale8Rle();
void rleTooLarge();
void openMemory(); void openMemory();
void openTwice(); void openTwice();
void importTwice(); void importTwice();
@ -69,30 +65,6 @@ struct TgaImporterTest: TestSuite::Tester {
PluginManager::Manager<AbstractImporter> _manager{"nonexistent"}; PluginManager::Manager<AbstractImporter> _manager{"nonexistent"};
}; };
constexpr struct {
const char* name;
char imageType;
char bpp;
const char* message;
} UnsupportedBitsData[] {
{"color 16", 2, 16, "unsupported color bits-per-pixel: 16"},
{"grayscale 16", 3, 16, "unsupported grayscale bits-per-pixel: 16"},
{"RLE color 16", 10, 16, "unsupported color bits-per-pixel: 16"},
{"RLE grayscale 16", 11, 16, "unsupported grayscale bits-per-pixel: 16"}
};
constexpr struct {
const char* name;
ImporterFlags flags;
const char* message24;
const char* message32;
} VerboseData[] {
{"", {}, "", ""},
{"verbose", ImporterFlag::Verbose,
"Trade::TgaImporter::image2D(): converting from BGR to RGB\n",
"Trade::TgaImporter::image2D(): converting from BGRA to RGBA\n"}
};
constexpr const char Color24[] = { constexpr const char Color24[] = {
0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3, 0, 24, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3, 0, 24, 0,
1, 2, 3, 2, 3, 4, 1, 2, 3, 2, 3, 4,
@ -110,12 +82,13 @@ constexpr const char Color24Rle[] = {
'\x82', 4, 5, 6 '\x82', 4, 5, 6
}; };
/* MSVC 2015 crashes when seeing constexpr here. Not doing that, then. */ /* Separate from ErrorData so we can just slice existing arrays instead of
creating new ones from scratch */
const struct { const struct {
const char* name; const char* name;
Containers::ArrayView<const char> data; Containers::ArrayView<const char> data;
const char* message; const char* message;
} ShortData[] { } InvalidShortData[] {
{"short header", Containers::arrayView(Color24).prefix(17), {"short header", Containers::arrayView(Color24).prefix(17),
"file too short, expected at least 18 bytes but got 17"}, "file too short, expected at least 18 bytes but got 17"},
{"short data", Containers::arrayView(Color24).exceptSuffix(1), {"short data", Containers::arrayView(Color24).exceptSuffix(1),
@ -126,6 +99,52 @@ const struct {
"RLE file too short at pixel 0"} "RLE file too short at pixel 0"}
}; };
const struct {
const char* name;
char imageType;
char bpp;
const char* message;
} InvalidBitsData[] {
{"color 16", 2, 16, "unsupported color bits-per-pixel: 16"},
{"grayscale 16", 3, 16, "unsupported grayscale bits-per-pixel: 16"},
{"RLE color 16", 10, 16, "unsupported color bits-per-pixel: 16"},
{"RLE grayscale 16", 11, 16, "unsupported grayscale bits-per-pixel: 16"}
};
const struct {
const char* name;
Containers::Array<char> data;
const char* message;
} InvalidData[]{
{"paletted", {InPlaceInit, {
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
}}, "paletted files are not supported"},
{"invalid image type", {InPlaceInit, {
0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
}}, "unsupported image type: 9"},
{"RLE too large", {InPlaceInit, {
0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3, 0, 24, 0,
/* 3 pixels as-is */
'\x02', 1, 2, 3,
2, 3, 4,
3, 4, 5,
/* 1 pixel 4x repeated (one more than it should be) */
'\x83', 4, 5, 6
}}, "RLE data at byte 28 contains 4 pixels but only 3 left to decode"},
};
const struct {
const char* name;
ImporterFlags flags;
const char* message24;
const char* message32;
} VerboseData[] {
{"", {}, "", ""},
{"verbose", ImporterFlag::Verbose,
"Trade::TgaImporter::image2D(): converting from BGR to RGB\n",
"Trade::TgaImporter::image2D(): converting from BGRA to RGBA\n"}
};
/* Shared among all plugins that implement data copying optimizations */ /* Shared among all plugins that implement data copying optimizations */
const struct { const struct {
const char* name; const char* name;
@ -143,17 +162,16 @@ const struct {
}; };
TgaImporterTest::TgaImporterTest() { TgaImporterTest::TgaImporterTest() {
addTests({&TgaImporterTest::openEmpty}); addTests({&TgaImporterTest::invalidEmpty});
addInstancedTests({&TgaImporterTest::openShort}, addInstancedTests({&TgaImporterTest::invalidShort},
Containers::arraySize(ShortData)); Containers::arraySize(InvalidShortData));
addTests({&TgaImporterTest::paletted, addInstancedTests({&TgaImporterTest::invalid},
&TgaImporterTest::invalid}); Containers::arraySize(InvalidData));
addInstancedTests({ addInstancedTests({&TgaImporterTest::invalidBits},
&TgaImporterTest::unsupportedBits}, Containers::arraySize(InvalidBitsData));
Containers::arraySize(UnsupportedBitsData));
addInstancedTests({ addInstancedTests({
&TgaImporterTest::color24, &TgaImporterTest::color24,
@ -163,9 +181,7 @@ TgaImporterTest::TgaImporterTest() {
Containers::arraySize(VerboseData)); Containers::arraySize(VerboseData));
addTests({&TgaImporterTest::grayscale8, addTests({&TgaImporterTest::grayscale8,
&TgaImporterTest::grayscale8Rle, &TgaImporterTest::grayscale8Rle});
&TgaImporterTest::rleTooLarge});
addInstancedTests({&TgaImporterTest::openMemory}, addInstancedTests({&TgaImporterTest::openMemory},
Containers::arraySize(OpenMemoryData)); Containers::arraySize(OpenMemoryData));
@ -180,7 +196,7 @@ TgaImporterTest::TgaImporterTest() {
#endif #endif
} }
void TgaImporterTest::openEmpty() { void TgaImporterTest::invalidEmpty() {
Containers::Pointer<AbstractImporter> importer = _manager.instantiate("TgaImporter"); Containers::Pointer<AbstractImporter> importer = _manager.instantiate("TgaImporter");
std::ostringstream out; std::ostringstream out;
@ -191,8 +207,8 @@ void TgaImporterTest::openEmpty() {
CORRADE_COMPARE(out.str(), "Trade::TgaImporter::openData(): the file is empty\n"); CORRADE_COMPARE(out.str(), "Trade::TgaImporter::openData(): the file is empty\n");
} }
void TgaImporterTest::openShort() { void TgaImporterTest::invalidShort() {
auto&& data = ShortData[testCaseInstanceId()]; auto&& data = InvalidShortData[testCaseInstanceId()];
setTestCaseDescription(data.name); setTestCaseDescription(data.name);
Containers::Pointer<AbstractImporter> importer = _manager.instantiate("TgaImporter"); Containers::Pointer<AbstractImporter> importer = _manager.instantiate("TgaImporter");
@ -205,30 +221,22 @@ void TgaImporterTest::openShort() {
CORRADE_COMPARE(out.str(), Utility::formatString("Trade::TgaImporter::image2D(): {}\n", data.message)); CORRADE_COMPARE(out.str(), Utility::formatString("Trade::TgaImporter::image2D(): {}\n", data.message));
} }
void TgaImporterTest::paletted() {
Containers::Pointer<AbstractImporter> importer = _manager.instantiate("TgaImporter");
const char data[]{ 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
CORRADE_VERIFY(importer->openData(data));
std::ostringstream out;
Error redirectError{&out};
CORRADE_VERIFY(!importer->image2D(0));
CORRADE_COMPARE(out.str(), "Trade::TgaImporter::image2D(): paletted files are not supported\n");
}
void TgaImporterTest::invalid() { void TgaImporterTest::invalid() {
auto&& data = InvalidData[testCaseInstanceId()];
setTestCaseDescription(data.name);
Containers::Pointer<AbstractImporter> importer = _manager.instantiate("TgaImporter"); Containers::Pointer<AbstractImporter> importer = _manager.instantiate("TgaImporter");
const char data[]{ 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
CORRADE_VERIFY(importer->openData(data)); CORRADE_VERIFY(importer->openData(data.data));
std::ostringstream out; std::ostringstream out;
Error redirectError{&out}; Error redirectError{&out};
CORRADE_VERIFY(!importer->image2D(0)); CORRADE_VERIFY(!importer->image2D(0));
CORRADE_COMPARE(out.str(), "Trade::TgaImporter::image2D(): unsupported image type: 9\n"); CORRADE_COMPARE(out.str(), Utility::formatString("Trade::TgaImporter::image2D(): {}\n", data.message));
} }
void TgaImporterTest::unsupportedBits() { void TgaImporterTest::invalidBits() {
auto&& data = UnsupportedBitsData[testCaseInstanceId()]; auto&& data = InvalidBitsData[testCaseInstanceId()];
setTestCaseDescription(data.name); setTestCaseDescription(data.name);
Containers::Pointer<AbstractImporter> importer = _manager.instantiate("TgaImporter"); Containers::Pointer<AbstractImporter> importer = _manager.instantiate("TgaImporter");
@ -418,25 +426,6 @@ void TgaImporterTest::grayscale8Rle() {
}), TestSuite::Compare::Container); }), TestSuite::Compare::Container);
} }
void TgaImporterTest::rleTooLarge() {
Containers::Pointer<AbstractImporter> importer = _manager.instantiate("TgaImporter");
const char data[] = {
0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3, 0, 24, 0,
/* 3 pixels as-is */
'\x02', 1, 2, 3,
2, 3, 4,
3, 4, 5,
/* 1 pixel 4x repeated (one more than it should be) */
'\x83', 4, 5, 6
};
CORRADE_VERIFY(importer->openData(data));
std::ostringstream out;
Error redirectError{&out};
CORRADE_VERIFY(!importer->image2D(0));
CORRADE_COMPARE(out.str(), "Trade::TgaImporter::image2D(): RLE data at byte 28 contains 4 pixels but only 3 left to decode\n");
}
void TgaImporterTest::openMemory() { void TgaImporterTest::openMemory() {
/* same as dxt1() except that it uses openData() & openMemory() to test /* same as dxt1() except that it uses openData() & openMemory() to test
data copying on import */ data copying on import */

Loading…
Cancel
Save