Simplify uses of spvBinaryDestroy in tests

Always try to destroy the binary during common methods
of test fixtures.  This is safe if no other code in the test
attempted to destroy the binary.

Take advantage of the fact spvBinaryDestroy is a no-op on a nullptr,
by eliminating the null pointer check in the caller.
This commit is contained in:
David Neto 2015-09-08 15:29:22 -04:00
parent ac6f71b2de
commit cac38f92dd
5 changed files with 10 additions and 17 deletions

View File

@ -42,9 +42,6 @@ TEST_F(TextToBinaryTest, Whitespace) {
)");
EXPECT_EQ(SPV_SUCCESS, spvTextToBinary(&text, opcodeTable, operandTable,
extInstTable, &binary, &diagnostic));
if (binary) {
spvBinaryDestroy(binary);
}
if (diagnostic) {
spvDiagnosticPrint(diagnostic);
}

View File

@ -42,7 +42,6 @@ TEST_F(TextToBinaryTest, ImmediateIntOpCode) {
ASSERT_EQ(SPV_SUCCESS, spvTextToBinary(&text, opcodeTable, operandTable,
extInstTable, &binary, &diagnostic));
EXPECT_EQ(0x00FF00FF, binary->code[5]);
spvBinaryDestroy(binary);
if (diagnostic) {
spvDiagnosticPrint(diagnostic);
}
@ -53,7 +52,6 @@ TEST_F(TextToBinaryTest, ImmediateIntOperand) {
EXPECT_EQ(SPV_SUCCESS, spvTextToBinary(&text, opcodeTable, operandTable,
extInstTable, &binary, &diagnostic));
EXPECT_EQ(0x00FF00FF, binary->code[6]);
spvBinaryDestroy(binary);
if (diagnostic) {
spvDiagnosticPrint(diagnostic);
}

View File

@ -48,7 +48,7 @@ TEST(NamedId, Default) {
ASSERT_EQ(SPV_SUCCESS, spvOperandTableGet(&operandTable));
spv_ext_inst_table extInstTable;
ASSERT_EQ(SPV_SUCCESS, spvExtInstTableGet(&extInstTable));
spv_binary binary;
spv_binary binary = nullptr;
spv_diagnostic diagnostic;
spv_result_t error = spvTextToBinary(&text, opcodeTable, operandTable,
extInstTable, &binary, &diagnostic);

View File

@ -55,6 +55,7 @@ class TextToBinaryTestBase : public T {
}
virtual ~TextToBinaryTestBase() {
DestroyBinary();
if (diagnostic) spvDiagnosticDestroy(diagnostic);
}
@ -75,7 +76,7 @@ class TextToBinaryTestBase : public T {
SpirvVector code_copy;
if (status == SPV_SUCCESS) {
code_copy = SpirvVector(binary->code, binary->code + binary->wordCount);
spvBinaryDestroy(binary);
DestroyBinary();
} else {
spvDiagnosticPrint(diagnostic);
}
@ -90,6 +91,7 @@ class TextToBinaryTestBase : public T {
spvTextToBinary(&this->text, opcodeTable, operandTable,
extInstTable, &binary, &diagnostic))
<< text;
DestroyBinary();
return diagnostic->error;
}
@ -99,6 +101,12 @@ class TextToBinaryTestBase : public T {
text.length = textString.size();
}
// Destroys the binary, if it exists.
void DestroyBinary() {
spvBinaryDestroy(binary);
binary = nullptr;
}
spv_opcode_table opcodeTable;
spv_operand_table operandTable;
spv_ext_inst_table extInstTable;

View File

@ -255,9 +255,6 @@ TEST_F(TextToBinaryTest, StringSpace) {
SetText("OpSourceExtension \"string with spaces\"");
EXPECT_EQ(SPV_SUCCESS, spvTextToBinary(&text, opcodeTable, operandTable,
extInstTable, &binary, &diagnostic));
if (binary) {
spvBinaryDestroy(binary);
}
if (diagnostic) {
spvDiagnosticPrint(diagnostic);
}
@ -288,9 +285,6 @@ TEST_F(TextToBinaryTest, InstructionTwoFormats) {
EXPECT_EQ(SPV_SUCCESS, spvTextToBinary(&text, opcodeTable, operandTable,
extInstTable, &binary, &diagnostic));
if (binary) {
spvBinaryDestroy(binary);
}
if (diagnostic) {
spvDiagnosticPrint(diagnostic);
}
@ -312,7 +306,6 @@ Google
"Expected <opcode> or <result-id> at the beginning of an instruction, "
"found 'Google'.",
diagnostic->error);
if (binary) spvBinaryDestroy(binary);
}
TEST_F(TextToBinaryTest, NoEqualSign) {
@ -328,7 +321,6 @@ TEST_F(TextToBinaryTest, NoEqualSign) {
EXPECT_EQ(5, diagnostic->position.line + 1);
EXPECT_EQ(1, diagnostic->position.column + 1);
EXPECT_STREQ("Expected '=', found end of stream.", diagnostic->error);
if (binary) spvBinaryDestroy(binary);
}
TEST_F(TextToBinaryTest, NoOpCode) {
@ -344,7 +336,6 @@ TEST_F(TextToBinaryTest, NoOpCode) {
EXPECT_EQ(5, diagnostic->position.line + 1);
EXPECT_EQ(1, diagnostic->position.column + 1);
EXPECT_STREQ("Expected opcode, found end of stream.", diagnostic->error);
if (binary) spvBinaryDestroy(binary);
}
TEST_F(TextToBinaryTest, WrongOpCode) {
@ -360,7 +351,6 @@ TEST_F(TextToBinaryTest, WrongOpCode) {
EXPECT_EQ(4, diagnostic->position.line + 1);
EXPECT_EQ(6, diagnostic->position.column + 1);
EXPECT_STREQ("Invalid Opcode prefix 'Wahahaha'.", diagnostic->error);
if (binary) spvBinaryDestroy(binary);
}
TEST_F(TextToBinaryTest, GoodSwitch) {