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#include <string>
16#include <vector>
17
18#include "test/test_fixture.h"
19#include "test/unit_spirv.h"
20
21namespace spvtools {
22namespace {
23
24using NamedIdTest = spvtest::TextToBinaryTest;
25
26TEST_F(NamedIdTest, Default) {
27  const std::string input = R"(
28          OpCapability Shader
29          OpMemoryModel Logical Simple
30          OpEntryPoint Vertex %main "foo"
31  %void = OpTypeVoid
32%fnMain = OpTypeFunction %void
33  %main = OpFunction %void None %fnMain
34%lbMain = OpLabel
35          OpReturn
36          OpFunctionEnd)";
37  const std::string output =
38      "OpCapability Shader\n"
39      "OpMemoryModel Logical Simple\n"
40      "OpEntryPoint Vertex %1 \"foo\"\n"
41      "%2 = OpTypeVoid\n"
42      "%3 = OpTypeFunction %2\n"
43      "%1 = OpFunction %2 None %3\n"
44      "%4 = OpLabel\n"
45      "OpReturn\n"
46      "OpFunctionEnd\n";
47  EXPECT_EQ(output, EncodeAndDecodeSuccessfully(input));
48}
49
50struct IdCheckCase {
51  std::string id;
52  bool valid;
53};
54
55using IdValidityTest =
56    spvtest::TextToBinaryTestBase<::testing::TestWithParam<IdCheckCase>>;
57
58TEST_P(IdValidityTest, IdTypes) {
59  const std::string input = GetParam().id + " = OpTypeVoid";
60  SetText(input);
61  if (GetParam().valid) {
62    CompileSuccessfully(input);
63  } else {
64    CompileFailure(input);
65  }
66}
67
68INSTANTIATE_TEST_SUITE_P(
69    ValidAndInvalidIds, IdValidityTest,
70    ::testing::ValuesIn(std::vector<IdCheckCase>(
71        {{"%1", true},          {"%2abc", true},   {"%3Def", true},
72         {"%4GHI", true},       {"%5_j_k", true},  {"%6J_M", true},
73         {"%n", true},          {"%O", true},      {"%p7", true},
74         {"%Q8", true},         {"%R_S", true},    {"%T_10_U", true},
75         {"%V_11", true},       {"%W_X_13", true}, {"%_A", true},
76         {"%_", true},          {"%__", true},     {"%A_", true},
77         {"%_A_", true},
78
79         {"%@", false},         {"%!", false},     {"%ABC!", false},
80         {"%__A__@", false},    {"%%", false},     {"%-", false},
81         {"%foo_@_bar", false}, {"%", false},
82
83         {"5", false},          {"32", false},     {"foo", false},
84         {"a%bar", false}})));
85
86}  // namespace
87}  // namespace spvtools
88