aboutsummaryrefslogtreecommitdiff
path: root/test/val/val_literals_test.cpp
blob: 6eadf321cce38ded86c79b9a1f89f0558c759c35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Validation tests for ilegal literals

#include <string>
#include <utility>

#include "gmock/gmock.h"
#include "test/val/val_fixtures.h"

namespace spvtools {
namespace val {
namespace {

using ::testing::HasSubstr;

using ValidateLiterals = spvtest::ValidateBase<std::string>;
using ValidateLiteralsShader = spvtest::ValidateBase<std::string>;
using ValidateLiteralsKernel = spvtest::ValidateBase<std::string>;

std::string GenerateShaderCode() {
  std::string str = R"(
          OpCapability Shader
          OpCapability Linkage
          OpCapability Int16
          OpCapability Int64
          OpCapability Float16
          OpCapability Float64
          OpMemoryModel Logical GLSL450
%int16  = OpTypeInt 16 1
%uint16 = OpTypeInt 16 0
%int32  = OpTypeInt 32 1
%uint32 = OpTypeInt 32 0
%int64  = OpTypeInt 64 1
%uint64 = OpTypeInt 64 0
%half   = OpTypeFloat 16
%float  = OpTypeFloat 32
%double = OpTypeFloat 64
%10     = OpTypeVoid
    )";
  return str;
}

std::string GenerateKernelCode() {
  std::string str = R"(
          OpCapability Kernel
          OpCapability Addresses
          OpCapability Linkage
          OpCapability Int8
          OpMemoryModel Physical64 OpenCL
%uint8  = OpTypeInt 8 0
    )";
  return str;
}

TEST_F(ValidateLiterals, LiteralsShaderGood) {
  std::string str = GenerateShaderCode() + R"(
%11 = OpConstant %int16   !0x00007FFF
%12 = OpConstant %int16   !0xFFFF8000
%13 = OpConstant %int16   !0xFFFFABCD
%14 = OpConstant %uint16  !0x0000ABCD
%15 = OpConstant %int16  -32768
%16 = OpConstant %uint16  65535
%17 = OpConstant %int32  -2147483648
%18 = OpConstant %uint32  4294967295
%19 = OpConstant %int64  -9223372036854775808
%20 = OpConstant %uint64  18446744073709551615
%21 = OpConstant %half    !0x0000FFFF
%22 = OpConstant %float   !0xFFFFFFFF
%23 = OpConstant %double  !0xFFFFFFFF !0xFFFFFFFF
  )";
  CompileSuccessfully(str);
  ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
}

TEST_F(ValidateLiterals, InvalidInt) {
  std::string str = GenerateShaderCode() + R"(
%11 = OpTypeInt 32 90
  )";
  CompileSuccessfully(str);
  EXPECT_EQ(SPV_ERROR_INVALID_VALUE, ValidateInstructions());
  EXPECT_THAT(getDiagnosticString(),
              HasSubstr("OpTypeInt has invalid signedness:"));
}

TEST_P(ValidateLiteralsShader, LiteralsShaderBad) {
  std::string str = GenerateShaderCode() + GetParam();
  std::string inst_id = "11";
  CompileSuccessfully(str);
  EXPECT_EQ(SPV_ERROR_INVALID_VALUE, ValidateInstructions());
  EXPECT_THAT(
      getDiagnosticString(),
      HasSubstr("The high-order bits of a literal number in instruction <id> " +
                inst_id +
                " must be 0 for a floating-point type, "
                "or 0 for an integer type with Signedness of 0, "
                "or sign extended when Signedness is 1"));
}

INSTANTIATE_TEST_SUITE_P(
    LiteralsShaderCases, ValidateLiteralsShader,
    ::testing::Values("%11 = OpConstant %int16  !0xFFFF0000",  // Sign bit is 0
                      "%11 = OpConstant %int16  !0x00008000",  // Sign bit is 1
                      "%11 = OpConstant %int16  !0xABCD8000",  // Sign bit is 1
                      "%11 = OpConstant %int16  !0xABCD0000",
                      "%11 = OpConstant %uint16 !0xABCD0000",
                      "%11 = OpConstant %half   !0xABCD0000",
                      "%11 = OpConstant %half   !0x00010000"));

TEST_F(ValidateLiterals, LiteralsKernelGood) {
  std::string str = GenerateKernelCode() + R"(
%4  = OpConstant %uint8  !0x000000AB
%6  = OpConstant %uint8  255
  )";
  CompileSuccessfully(str);
  ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
}

TEST_P(ValidateLiteralsKernel, LiteralsKernelBad) {
  std::string str = GenerateKernelCode() + GetParam();
  std::string inst_id = "2";
  CompileSuccessfully(str);
  EXPECT_EQ(SPV_ERROR_INVALID_VALUE, ValidateInstructions());
  EXPECT_THAT(
      getDiagnosticString(),
      HasSubstr("The high-order bits of a literal number in instruction <id> " +
                inst_id +
                " must be 0 for a floating-point type, "
                "or 0 for an integer type with Signedness of 0, "
                "or sign extended when Signedness is 1"));
}

INSTANTIATE_TEST_SUITE_P(
    LiteralsKernelCases, ValidateLiteralsKernel,
    ::testing::Values("%2 = OpConstant %uint8  !0xABCDEF00",
                      "%2 = OpConstant %uint8  !0xABCDEFFF"));

}  // namespace
}  // namespace val
}  // namespace spvtools