Simplify some tests
authorLei Zhang <antiagainst@google.com>
Thu, 12 Nov 2015 15:45:36 +0000 (10:45 -0500)
committerDavid Neto <dneto@google.com>
Thu, 12 Nov 2015 21:27:46 +0000 (16:27 -0500)
test/BinaryToText.cpp
test/TextToBinary.Literal.cpp
test/TextToBinary.cpp

index 0a80c8f..058cb07 100644 (file)
@@ -35,9 +35,9 @@
 
 using ::testing::Eq;
 using spvtest::AutoText;
+using spvtest::TextToBinaryTest;
 
 namespace {
-
 class BinaryToText : public ::testing::Test {
  public:
   virtual void SetUp() {
@@ -214,103 +214,25 @@ INSTANTIATE_TEST_CASE_P(
          "Invalid OpSwitch: selector id 2 is not a scalar integer"},
     }));
 
-TEST(BinaryToTextSmall, OneInstruction) {
-  // TODO(dneto): This test could/should be refactored.
-  spv_binary binary;
-  spv_diagnostic diagnostic = nullptr;
-  const char* input = "OpSource OpenCL_C 12";
-  spv_result_t error =
-      spvTextToBinary(input, strlen(input), &binary, &diagnostic);
-  ASSERT_EQ(SPV_SUCCESS, error);
-  spv_text text = nullptr;
-  error = spvBinaryToText(binary->code, binary->wordCount,
-                          SPV_BINARY_TO_TEXT_OPTION_NONE, &text, &diagnostic);
-  EXPECT_EQ(SPV_SUCCESS, error);
-  if (error) {
-    spvDiagnosticPrint(diagnostic);
-    spvDiagnosticDestroy(diagnostic);
-  }
-  spvTextDestroy(text);
+TEST_F(TextToBinaryTest, OneInstruction) {
+  const std::string input = "OpSource OpenCL_C 12\n";
+  EXPECT_EQ(input, EncodeAndDecodeSuccessfully(input));
 }
 
 // Exercise the case where an operand itself has operands.
 // This could detect problems in updating the expected-set-of-operands
 // list.
-TEST(BinaryToTextSmall, OperandWithOperands) {
+TEST_F(TextToBinaryTest, OperandWithOperands) {
   spv_binary binary;
   spv_diagnostic diagnostic = nullptr;
 
-  AutoText input(R"(OpEntryPoint Kernel %fn "foo"
-                 OpExecutionMode %fn LocalSizeHint 100 200 300
-                 %void = OpTypeVoid
-                 %fnType = OpTypeFunction %void
-                 %fn = OpFunction %void None %fnType
-                 )");
-  spv_result_t error = spvTextToBinary(input.str.c_str(), input.str.length(),
-                                       &binary, &diagnostic);
-  ASSERT_EQ(SPV_SUCCESS, error);
-  spv_text text = nullptr;
-  error = spvBinaryToText(binary->code, binary->wordCount,
-                          SPV_BINARY_TO_TEXT_OPTION_NONE, &text, &diagnostic);
-  EXPECT_EQ(SPV_SUCCESS, error);
-  if (error) {
-    spvDiagnosticPrint(diagnostic);
-    spvDiagnosticDestroy(diagnostic);
-  }
-  spvTextDestroy(text);
-}
-
-TEST(BinaryToTextSmall, LiteralInt64) {
-  spv_binary binary;
-  spv_diagnostic diagnostic = nullptr;
-
-  AutoText input("%1 = OpTypeInt 64 0\n%2 = OpConstant %1 123456789021\n");
-  spv_result_t error = spvTextToBinary(input.str.c_str(), input.str.length(),
-                                       &binary, &diagnostic);
-  ASSERT_EQ(SPV_SUCCESS, error);
-  spv_text text = nullptr;
-  error = spvBinaryToText(binary->code, binary->wordCount,
-                          SPV_BINARY_TO_TEXT_OPTION_NONE, &text, &diagnostic);
-  if (error) {
-    spvDiagnosticPrint(diagnostic);
-    spvDiagnosticDestroy(diagnostic);
-  }
-  ASSERT_EQ(SPV_SUCCESS, error);
-  const std::string header =
-      "; SPIR-V\n; Version: 100\n; Generator: Khronos\n; "
-      "Bound: 3\n; Schema: 0\n";
-  EXPECT_EQ(header + input.str, text->str);
-  spvTextDestroy(text);
-}
-
-TEST(BinaryToTextSmall, LiteralDouble) {
-  spv_binary binary;
-  spv_diagnostic diagnostic = nullptr;
-
-  AutoText input(
-      "%1 = OpTypeFloat 64\n%2 = OpSpecConstant %1 3.1415926535897930");
-  spv_result_t error = spvTextToBinary(input.str.c_str(), input.str.length(),
-                                       &binary, &diagnostic);
-  ASSERT_EQ(SPV_SUCCESS, error);
-  spv_text text = nullptr;
-  error = spvBinaryToText(binary->code, binary->wordCount,
-                          SPV_BINARY_TO_TEXT_OPTION_NONE, &text, &diagnostic);
-  if (error) {
-    spvDiagnosticPrint(diagnostic);
-    spvDiagnosticDestroy(diagnostic);
-  }
-  ASSERT_EQ(SPV_SUCCESS, error);
-  const std::string output =
-      R"(; SPIR-V
-; Version: 100
-; Generator: Khronos
-; Bound: 3
-; Schema: 0
-%1 = OpTypeFloat 64
-%2 = OpSpecConstant %1 3.14159265358979
+  const std::string input = R"(OpEntryPoint Kernel %1 "foo"
+OpExecutionMode %1 LocalSizeHint 100 200 300
+%2 = OpTypeVoid
+%3 = OpTypeFunction %2
+%1 = OpFunction %1 None %3
 )";
-  EXPECT_EQ(output, text->str) << text->str;
-  spvTextDestroy(text);
+  EXPECT_EQ(input, EncodeAndDecodeSuccessfully(input));
 }
 
 using RoundTripInstructionsTest =
@@ -404,7 +326,7 @@ INSTANTIATE_TEST_CASE_P(
               " %5 %6 %7 %8 %9 %10 %11 %12 %13\n"}));
 // clang-format on
 
-using MaskSorting = spvtest::TextToBinaryTest;
+using MaskSorting = TextToBinaryTest;
 
 TEST_F(MaskSorting, MasksAreSortedFromLSBToMSB) {
   EXPECT_THAT(EncodeAndDecodeSuccessfully(
@@ -432,7 +354,7 @@ TEST_F(MaskSorting, MasksAreSortedFromLSBToMSB) {
                  " %5 %6 %7 %8 %9 %10 %11 %12 %13\n"));
 }
 
-using OperandTypeTest = spvtest::TextToBinaryTest;
+using OperandTypeTest = TextToBinaryTest;
 
 TEST_F(OperandTypeTest, OptionalTypedLiteralNumber) {
   const std::string input =
index 9beaf79..9e9246f 100644 (file)
@@ -69,6 +69,18 @@ TEST_F(TextToBinaryTest, LiteralFloatInPlaceOfLiteralInteger) {
                            "%c = OpConstant %i32 0.123"));
 }
 
+TEST_F(TextToBinaryTest, LiteralInt64) {
+  const std::string code =
+      "%1 = OpTypeInt 64 0\n%2 = OpConstant %1 123456789021\n";
+  EXPECT_EQ(code, EncodeAndDecodeSuccessfully(code));
+}
+
+TEST_F(TextToBinaryTest, LiteralDouble) {
+  const std::string code =
+      "%1 = OpTypeFloat 64\n%2 = OpSpecConstant %1 3.14159265358979\n";
+  EXPECT_EQ(code, EncodeAndDecodeSuccessfully(code));
+}
+
 TEST_F(TextToBinaryTest, LiteralStringASCIILong) {
   // SPIR-V allows strings up to 65535 characters.
   // Test the simple case of UTF-8 code points corresponding
index a06e85e..5c1d5c8 100644 (file)
@@ -317,12 +317,10 @@ TEST_F(TextToBinaryTest, InvalidDiagnostic) {
 }
 
 TEST_F(TextToBinaryTest, InvalidPrefix) {
-  SetText("Invalid");
-  ASSERT_EQ(SPV_ERROR_INVALID_TEXT,
-            spvTextToBinary(text.str, text.length, &binary, &diagnostic));
-  if (diagnostic) {
-    spvDiagnosticPrint(diagnostic);
-  }
+  EXPECT_EQ(
+      "Expected <opcode> or <result-id> at the beginning of an instruction, "
+      "found 'Invalid'.",
+      CompileFailure("Invalid"));
 }
 
 TEST_F(TextToBinaryTest, EmptyAssemblyString) {
@@ -332,71 +330,42 @@ TEST_F(TextToBinaryTest, EmptyAssemblyString) {
 }
 
 TEST_F(TextToBinaryTest, StringSpace) {
-  SetText("OpSourceExtension \"string with spaces\"");
-  EXPECT_EQ(SPV_SUCCESS,
-            spvTextToBinary(text.str, text.length, &binary, &diagnostic));
-  if (diagnostic) {
-    spvDiagnosticPrint(diagnostic);
-  }
+  const std::string code = ("OpSourceExtension \"string with spaces\"\n");
+  EXPECT_EQ(code, EncodeAndDecodeSuccessfully(code));
 }
 
 TEST_F(TextToBinaryTest, UnknownBeginningOfInstruction) {
-  SetText(R"(
-     OpSource OpenCL_C 12
-     OpMemoryModel Physical64 OpenCL
-Google
-)");
-
-  EXPECT_EQ(SPV_ERROR_INVALID_TEXT,
-            spvTextToBinary(text.str, text.length, &binary, &diagnostic));
-  EXPECT_EQ(4, diagnostic->position.line + 1);
-  EXPECT_EQ(1, diagnostic->position.column + 1);
-  EXPECT_STREQ(
+  EXPECT_EQ(
       "Expected <opcode> or <result-id> at the beginning of an instruction, "
       "found 'Google'.",
-      diagnostic->error);
+      CompileFailure(
+          "\nOpSource OpenCL_C 12\nOpMemoryModel Physical64 OpenCL\nGoogle\n"));
+  EXPECT_EQ(4, diagnostic->position.line + 1);
+  EXPECT_EQ(1, diagnostic->position.column + 1);
 }
 
 TEST_F(TextToBinaryTest, NoEqualSign) {
-  SetText(R"(
-     OpSource OpenCL_C 12
-     OpMemoryModel Physical64 OpenCL
-%2
-)");
-
-  EXPECT_EQ(SPV_ERROR_INVALID_TEXT,
-            spvTextToBinary(text.str, text.length, &binary, &diagnostic));
+  EXPECT_EQ("Expected '=', found end of stream.",
+            CompileFailure("\nOpSource OpenCL_C 12\n"
+                           "OpMemoryModel Physical64 OpenCL\n%2\n"));
   EXPECT_EQ(5, diagnostic->position.line + 1);
   EXPECT_EQ(1, diagnostic->position.column + 1);
-  EXPECT_STREQ("Expected '=', found end of stream.", diagnostic->error);
 }
 
 TEST_F(TextToBinaryTest, NoOpCode) {
-  SetText(R"(
-     OpSource OpenCL_C 12
-     OpMemoryModel Physical64 OpenCL
-%2 =
-)");
-
-  EXPECT_EQ(SPV_ERROR_INVALID_TEXT,
-            spvTextToBinary(text.str, text.length, &binary, &diagnostic));
+  EXPECT_EQ("Expected opcode, found end of stream.",
+            CompileFailure("\nOpSource OpenCL_C 12\n"
+                           "OpMemoryModel Physical64 OpenCL\n%2 =\n"));
   EXPECT_EQ(5, diagnostic->position.line + 1);
   EXPECT_EQ(1, diagnostic->position.column + 1);
-  EXPECT_STREQ("Expected opcode, found end of stream.", diagnostic->error);
 }
 
 TEST_F(TextToBinaryTest, WrongOpCode) {
-  SetText(R"(
-     OpSource OpenCL_C 12
-     OpMemoryModel Physical64 OpenCL
-%2 = Wahahaha
-)");
-
-  EXPECT_EQ(SPV_ERROR_INVALID_TEXT,
-            spvTextToBinary(text.str, text.length, &binary, &diagnostic));
+  EXPECT_EQ("Invalid Opcode prefix 'Wahahaha'.",
+            CompileFailure("\nOpSource OpenCL_C 12\n"
+                           "OpMemoryModel Physical64 OpenCL\n%2 = Wahahaha\n"));
   EXPECT_EQ(4, diagnostic->position.line + 1);
   EXPECT_EQ(6, diagnostic->position.column + 1);
-  EXPECT_STREQ("Invalid Opcode prefix 'Wahahaha'.", diagnostic->error);
 }
 
 using TextToBinaryFloatValueTest = spvtest::TextToBinaryTestBase<