Add a few unit tests for !<integer>. More to come.

Start using GMock: modify CMakeLists, fix googletest URL in readme.

Add useful utilities to the TestFixture class.  Also make it conform to
go/gunit recommendations about setup/teardown.
This commit is contained in:
Dejan Mircevski 2015-09-02 14:48:33 -04:00 committed by David Neto
parent 9fa9157c4d
commit 0a8f219d1e
4 changed files with 149 additions and 16 deletions

View File

@ -145,11 +145,11 @@ if(EXISTS ${GTEST_DIR})
add_subdirectory(${GTEST_DIR})
endif()
if (TARGET gtest)
message(STATUS "Found googletest, building tests.")
if (TARGET gmock)
message(STATUS "Found Google Mock, building tests.")
include_directories(SYSTEM
${gtest_SOURCE_DIR}
${gmock_SOURCE_DIR}/include
${gtest_SOURCE_DIR}/include)
add_executable(UnitSPIRV
@ -180,7 +180,7 @@ if (TARGET gtest)
${CMAKE_CURRENT_SOURCE_DIR}/test/Validate.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/ValidateID.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/main.cpp)
target_link_libraries(UnitSPIRV SPIRV-TOOLS gtest)
target_link_libraries(UnitSPIRV SPIRV-TOOLS gmock)
else()
message(STATUS "Did not find googletest, tests will not be built."
"To enable tests place googletest in '<spirv-dir>/external/googletest'.")

View File

@ -289,10 +289,10 @@ The validator operates on the binary form.
The project contains a number of tests, implemented in the `UnitSPIRV`
executable, used to drive the development and correctness of the tools, these
use the [googletest](https://code.google.com/p/googletest/) framework. The
[googletest](https://code.google.com/p/googletest/) source is not provided with
use the [googletest](https://github.com/google/googletest) framework. The
[googletest](https://github.com/google/googletest) source is not provided with
this project, to enable the tests place the
[googletest](https://code.google.com/p/googletest/) source in the
[googletest](https://github.com/google/googletest) source in the
`<spirv-dir>/external/googletest` directory, rerun CMake if you have already
done so previously, CMake will detect the existence of
`<spirv-dir>/external/googletest` then build as normal.

View File

@ -24,10 +24,16 @@
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
#include <cassert>
#include <vector>
#include <gmock/gmock.h>
#include "TestFixture.h"
namespace {
using ::testing::ElementsAre;
using test_fixture::TextToBinaryTest;
TEST_F(TextToBinaryTest, ImmediateIntOpCode) {
@ -52,4 +58,103 @@ TEST_F(TextToBinaryTest, ImmediateIntOperand) {
}
}
using ImmediateIntTest = TextToBinaryTest;
// TODO(deki): uncomment assertions below and make them pass.
TEST_F(ImmediateIntTest, AnyWordInSimpleStatement) {
SpirvVector original = CompileSuccessfully("OpConstant %1 %2 123");
// EXPECT_EQ(original, CompileSuccessfully("!0x0004002B %1 %2 123"));
EXPECT_EQ(original, CompileSuccessfully("OpConstant !1 %2 123"));
// EXPECT_EQ(original, CompileSuccessfully("OpConstant %1 !2 123"));
EXPECT_EQ(original, CompileSuccessfully("OpConstant %1 %2 !123"));
// EXPECT_EQ(original, CompileSuccessfully("!0x0004002B %1 !2 123"));
EXPECT_EQ(original, CompileSuccessfully("OpConstant !1 %2 !123"));
// EXPECT_EQ(original, CompileSuccessfully("!0x0004002B !1 !2 !123"));
}
TEST_F(ImmediateIntTest, AnyWordInAssignmentStatement) {
SpirvVector original = CompileSuccessfully("%2 = OpArrayLength %12 %1 123");
EXPECT_EQ(original, CompileSuccessfully("%2 = OpArrayLength %12 %1 123"));
}
TEST_F(ImmediateIntTest, InvalidStatement) {
EXPECT_THAT(Subvector(CompileSuccessfully("!4 !3 !2 !1"), kFirstInstruction),
ElementsAre(4, 3, 2, 1));
}
TEST_F(ImmediateIntTest, InvalidStatementBetweenValidOnes) {
EXPECT_THAT(
Subvector(CompileSuccessfully("OpTypeFloat %10 32 !5 !6 !7 OpEmitVertex"),
kFirstInstruction),
ElementsAre(0x00030016, 10, 32, 5, 6, 7, 0x000100DA));
}
TEST_F(ImmediateIntTest, NextOpcodeRecognized) {
SpirvVector original = CompileSuccessfully(R"(
OpLoad %10 %1 %2 Volatile
OpCompositeInsert %11 %4 %1 %3 0 1 2
)");
SpirvVector alternate = CompileSuccessfully(R"(
OpLoad %10 %1 %2 !1
OpCompositeInsert %11 %4 %1 %3 0 1 2
)");
EXPECT_EQ(original, alternate);
}
TEST_F(ImmediateIntTest, WrongLengthButNextOpcodeStillRecognized) {
SpirvVector original = CompileSuccessfully(R"(
OpLoad %10 %1 %2 Volatile
OpCopyMemorySized %3 %4 %1
)");
// TODO(deki): uncomment assertions below and make them pass.
#if 0
SpirvVector alternate = CompileSuccessfully(R"(
!0x0002003D %10 %1 %2 !1
OpCopyMemorySized %3 %4 %1
)");
EXPECT_EQ(0x0002003D, alternate[kFirstInstruction]);
EXPECT_EQ(Subvector(original, kFirstInstruction + 1),
Subvector(alternate, kFirstInstruction + 1));
#endif
}
// TODO(deki): implement.
TEST_F(ImmediateIntTest, NextAssignmentRecognized) {
// Like NextOpcodeRecognized, but next statement is in assignment form.
}
TEST_F(ImmediateIntTest, ConsecutiveImmediateOpcodes) {
// Two instructions in a row each have !<integer> opcode.
}
TEST_F(ImmediateIntTest, LiteralOperands) {
// !<integer> followed by a literal-number operand.
// !<integer> followed by a literal-string operand.
// Combos thereof.
}
TEST_F(ImmediateIntTest, IdOperands) {
// !<integer> followed by ID operand(s).
}
TEST_F(ImmediateIntTest, ImmediateOperands) {
// !<integer> followed by !<integer> operand(s).
}
TEST_F(ImmediateIntTest, ForbiddenOperands) {
// !<integer> followed by, eg, an enum or '=' or a random bareword.
}
// NB: when/if these cases are handled, it will require reworking the
// description in readme.md, which currently dictates that each word past
// !<integer> must be a literal, an ID, or another immediate (ie, not a '=').
TEST_F(ImmediateIntTest, AssignmentLHS) {
// !<integer> = OpIAdd %i32 %op0 %op1
}
TEST_F(ImmediateIntTest, AssignmentLHSAndOpCode) {
// !<integer> = !<integer> %i32 %op0 %op1
}
} // anonymous namespace

View File

@ -32,28 +32,56 @@ namespace test_fixture {
// Common setup for TextToBinary tests. SetText() should be called to populate
// the actual test text.
template<typename T>
template <typename T>
class TextToBinaryTestBase : public T {
public:
// Shorthand for SPIR-V compilation result.
using SpirvVector = std::vector<uint32_t>;
// Offset into a SpirvVector at which the first instruction starts.
const SpirvVector::size_type kFirstInstruction = 5;
TextToBinaryTestBase()
: opcodeTable(nullptr),
operandTable(nullptr),
diagnostic(nullptr),
text(),
binary(nullptr) {}
virtual void SetUp() {
ASSERT_EQ(SPV_SUCCESS, spvOpcodeTableGet(&opcodeTable));
ASSERT_EQ(SPV_SUCCESS, spvOperandTableGet(&operandTable));
ASSERT_EQ(SPV_SUCCESS, spvExtInstTableGet(&extInstTable));
binary(nullptr) {
EXPECT_EQ(SPV_SUCCESS, spvOpcodeTableGet(&opcodeTable));
EXPECT_EQ(SPV_SUCCESS, spvOperandTableGet(&operandTable));
EXPECT_EQ(SPV_SUCCESS, spvExtInstTableGet(&extInstTable));
char textStr[] = "substitute the text member variable with your test";
text = {textStr, strlen(textStr)};
}
virtual void TearDown() {
virtual ~TextToBinaryTestBase() {
if (diagnostic) spvDiagnosticDestroy(diagnostic);
}
// Returns subvector v[from:end).
SpirvVector Subvector(const SpirvVector& v, SpirvVector::size_type from) {
assert(from < v.size());
return SpirvVector(v.begin() + from, v.end());
}
// Compiles SPIR-V text, asserting compilation success. Returns the compiled
// code.
SpirvVector CompileSuccessfully(const std::string& text) {
SetText(text);
spv_result_t status =
spvTextToBinary(&this->text, opcodeTable, operandTable, extInstTable,
&binary, &diagnostic);
EXPECT_EQ(SPV_SUCCESS, status);
SpirvVector code_copy;
if (status == SPV_SUCCESS) {
code_copy = SpirvVector(binary->code, binary->code + binary->wordCount);
spvBinaryDestroy(binary);
} else {
spvDiagnosticPrint(diagnostic);
}
return code_copy;
}
void SetText(const std::string& code) {
textString = code;
text.str = textString.c_str();
@ -74,4 +102,4 @@ using TextToBinaryTest = TextToBinaryTestBase<::testing::Test>;
} // namespace test_fixture
#endif// _TEXT_FIXTURE_H_
#endif // _TEXT_FIXTURE_H_