But it's still a valid module.
spv_diagnostic* pDiagnostic) {
if (!pDiagnostic) return SPV_ERROR_INVALID_DIAGNOSTIC;
libspirv::AssemblyContext context(text, pDiagnostic);
- if (!text->str || !text->length)
- return context.diagnostic() << "Text stream is empty.";
+ if (!text->str)
+ return context.diagnostic() << "Missing assembly text.";
if (!grammar.isValid()) {
return SPV_ERROR_INVALID_TABLE;
std::vector<spv_instruction_t> instructions;
- if (context.advance()) return context.diagnostic() << "Text stream is empty.";
+ // Skip past whitespace and comments.
+ context.advance();
spv_ext_inst_type_t extInstType = SPV_EXT_INST_TYPE_NONE;
while (context.hasText()) {
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
+#include "gmock/gmock.h"
#include "TestFixture.h"
#include "UnitSPIRV.h"
namespace {
+using spvtest::Concatenate;
+using spvtest::MakeInstruction;
+using spvtest::MakeVector;
using spvtest::TextToBinaryTest;
+using testing::Eq;
TEST_F(TextToBinaryTest, Whitespace) {
- SetText(R"(
+ std::string input = R"(
; I'm a proud comment at the begining of the file
; I hide: OpCapability Shader
OpMemoryModel Logical Simple ; comment after instruction
;;;;;;;; many ;'s
%glsl450 = OpExtInstImport "GLSL.std.450"
; comment indented
-)");
- EXPECT_EQ(SPV_SUCCESS,
- spvTextToBinary(text.str, text.length, opcodeTable, operandTable,
- extInstTable, &binary, &diagnostic));
- if (diagnostic) {
- spvDiagnosticPrint(diagnostic);
- }
+)";
+
+ EXPECT_THAT(
+ CompiledInstructions(input),
+ Eq(Concatenate({MakeInstruction(SpvOpMemoryModel,
+ {uint32_t(SpvAddressingModelLogical),
+ uint32_t(SpvMemoryModelSimple)}),
+ MakeInstruction(SpvOpExtInstImport, {1},
+ MakeVector("GLSL.std.450"))})));
}
} // anonymous namespace
// Returns subvector v[from:end).
SpirvVector Subvector(const SpirvVector& v, SpirvVector::size_type from) {
- assert(from < v.size());
+ assert(from <= v.size());
return SpirvVector(v.begin() + from, v.end());
}
// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
#include "TestFixture.h"
+#include "gmock/gmock.h"
#include "UnitSPIRV.h"
#include "util/bitutils.h"
using libspirv::AssemblyGrammar;
using spvtest::TextToBinaryTest;
using spvtest::AutoText;
+using testing::Eq;
TEST(GetWord, Simple) {
EXPECT_EQ("", AssemblyContext(AutoText(""), nullptr).getWord());
ASSERT_EQ(SPV_ERROR_INVALID_TEXT,
spvTextToBinary(nullptr, 0, opcodeTable, operandTable, extInstTable,
&binary, &diagnostic));
+ EXPECT_NE(nullptr, diagnostic);
+ EXPECT_THAT(diagnostic->error, Eq(std::string("Missing assembly text.")));
}
TEST_F(TextToBinaryTest, InvalidTable) {
}
}
+TEST_F(TextToBinaryTest, EmptyAssemblyString) {
+ // An empty assembly module is valid!
+ // It should produce a valid module with zero instructions.
+ EXPECT_THAT(CompiledInstructions(""), Eq(std::vector<uint32_t>{}));
+}
+
TEST_F(TextToBinaryTest, StringSpace) {
SetText("OpSourceExtension \"string with spaces\"");
EXPECT_EQ(SPV_SUCCESS,