Home | History | Annotate | Download | only in test
      1 // Copyright (c) 2015-2016 The Khronos Group Inc.
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 // Assembler tests for literal numbers and literal strings.
     16 
     17 #include <string>
     18 
     19 #include "test/test_fixture.h"
     20 
     21 namespace spvtools {
     22 namespace {
     23 
     24 using spvtest::TextToBinaryTest;
     25 
     26 TEST_F(TextToBinaryTest, LiteralStringInPlaceOfLiteralNumber) {
     27   EXPECT_EQ(
     28       R"(Invalid unsigned integer literal: "I shouldn't be a string")",
     29       CompileFailure(R"(OpSource GLSL "I shouldn't be a string")"));
     30 }
     31 
     32 TEST_F(TextToBinaryTest, GarbageInPlaceOfLiteralString) {
     33   EXPECT_EQ("Invalid literal string 'nice-source-code'.",
     34             CompileFailure("OpSourceExtension nice-source-code"));
     35 }
     36 
     37 TEST_F(TextToBinaryTest, LiteralNumberInPlaceOfLiteralString) {
     38   EXPECT_EQ("Expected literal string, found literal number '1000'.",
     39             CompileFailure("OpSourceExtension 1000"));
     40 }
     41 
     42 TEST_F(TextToBinaryTest, LiteralFloatInPlaceOfLiteralInteger) {
     43   EXPECT_EQ("Invalid unsigned integer literal: 10.5",
     44             CompileFailure("OpSource GLSL 10.5"));
     45 
     46   EXPECT_EQ("Invalid unsigned integer literal: 0.2",
     47             CompileFailure(R"(OpMemberName %type 0.2 "member0.2")"));
     48 
     49   EXPECT_EQ("Invalid unsigned integer literal: 32.42",
     50             CompileFailure("%int = OpTypeInt 32.42 0"));
     51 
     52   EXPECT_EQ("Invalid unsigned integer literal: 4.5",
     53             CompileFailure("%mat = OpTypeMatrix %vec 4.5"));
     54 
     55   EXPECT_EQ("Invalid unsigned integer literal: 1.5",
     56             CompileFailure("OpExecutionMode %main LocalSize 1.5 1.6 1.7"));
     57 
     58   EXPECT_EQ("Invalid unsigned integer literal: 0.123",
     59             CompileFailure("%i32 = OpTypeInt 32 1\n"
     60                            "%c = OpConstant %i32 0.123"));
     61 }
     62 
     63 TEST_F(TextToBinaryTest, LiteralInt64) {
     64   const std::string code =
     65       "%1 = OpTypeInt 64 0\n%2 = OpConstant %1 123456789021\n";
     66   EXPECT_EQ(code, EncodeAndDecodeSuccessfully(code));
     67 }
     68 
     69 TEST_F(TextToBinaryTest, LiteralDouble) {
     70   const std::string code =
     71       "%1 = OpTypeFloat 64\n%2 = OpSpecConstant %1 3.14159265358979\n";
     72   EXPECT_EQ(code, EncodeAndDecodeSuccessfully(code));
     73 }
     74 
     75 TEST_F(TextToBinaryTest, LiteralStringASCIILong) {
     76   // SPIR-V allows strings up to 65535 characters.
     77   // Test the simple case of UTF-8 code points corresponding
     78   // to ASCII characters.
     79   EXPECT_EQ(65535, SPV_LIMIT_LITERAL_STRING_UTF8_CHARS_MAX);
     80   const std::string code =
     81       "OpSourceExtension \"" +
     82       std::string(SPV_LIMIT_LITERAL_STRING_UTF8_CHARS_MAX, 'o') + "\"\n";
     83   EXPECT_EQ(code, EncodeAndDecodeSuccessfully(code));
     84 }
     85 
     86 TEST_F(TextToBinaryTest, LiteralStringUTF8LongEncodings) {
     87   // SPIR-V allows strings up to 65535 characters.
     88   // Test the case of many Unicode characters, each of which has
     89   // a 4-byte UTF-8 encoding.
     90 
     91   // An instruction is at most 65535 words long. The first one
     92   // contains the wordcount and opcode.  So the worst case number of
     93   // 4-byte UTF-8 characters is 65533, since we also need to
     94   // store a terminating null character.
     95 
     96   // This string fits exactly into 65534 words.
     97   const std::string good_string =
     98       spvtest::MakeLongUTF8String(65533)
     99       // The following single character has a 3 byte encoding,
    100       // which fits snugly against the terminating null.
    101       + "\xe8\x80\x80";
    102 
    103   // These strings will overflow any instruction with 0 or 1 other
    104   // arguments, respectively.
    105   const std::string bad_0_arg_string = spvtest::MakeLongUTF8String(65534);
    106   const std::string bad_1_arg_string = spvtest::MakeLongUTF8String(65533);
    107 
    108   const std::string good_code = "OpSourceExtension \"" + good_string + "\"\n";
    109   EXPECT_EQ(good_code, EncodeAndDecodeSuccessfully(good_code));
    110 
    111   // Prove that it works on more than one instruction.
    112   const std::string good_code_2 = "OpSourceContinued \"" + good_string + "\"\n";
    113   EXPECT_EQ(good_code, EncodeAndDecodeSuccessfully(good_code));
    114 
    115   // Failure cases.
    116   EXPECT_EQ("Instruction too long: more than 65535 words.",
    117             CompileFailure("OpSourceExtension \"" + bad_0_arg_string + "\"\n"));
    118   EXPECT_EQ("Instruction too long: more than 65535 words.",
    119             CompileFailure("OpSourceContinued \"" + bad_0_arg_string + "\"\n"));
    120   EXPECT_EQ("Instruction too long: more than 65535 words.",
    121             CompileFailure("OpName %target \"" + bad_1_arg_string + "\"\n"));
    122 }
    123 
    124 }  // namespace
    125 }  // namespace spvtools
    126