1 // Copyright (c) 2017 Google Inc.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 // Tests for unique type declaration rules validator.
19 #include "source/text.h"
20 #include "source/text_handler.h"
21 #include "test_fixture.h"
25 using spvtest::ScopedContext;
27 // Converts code to binary and then back to text.
28 spv_result_t ToBinaryAndBack(
29 const std::string& before, std::string* after,
30 uint32_t text_to_binary_options = SPV_TEXT_TO_BINARY_OPTION_NONE,
31 uint32_t binary_to_text_options = SPV_BINARY_TO_TEXT_OPTION_NONE,
32 spv_target_env env = SPV_ENV_UNIVERSAL_1_0) {
33 ScopedContext ctx(env);
37 spv_result_t result = spvTextToBinaryWithOptions(
38 ctx.context, before.c_str(), before.size(), text_to_binary_options,
40 if (result != SPV_SUCCESS) {
44 result = spvBinaryToText(
45 ctx.context, binary->code, binary->wordCount, binary_to_text_options,
47 if (result != SPV_SUCCESS) {
51 *after = std::string(text->str, text->length);
53 spvBinaryDestroy(binary);
59 TEST(ToBinaryAndBack, DontPreserveNumericIds) {
60 const std::string before =
61 R"(OpCapability Addresses
63 OpCapability GenericPointer
65 OpMemoryModel Physical32 OpenCL
70 %300 = OpTypeFunction %200
71 %main = OpFunction %200 None %300
73 %100 = OpConstant %u32 100
74 %1 = OpConstant %u32 200
75 %2 = OpConstant %u32 300
80 const std::string expected =
81 R"(OpCapability Addresses
83 OpCapability GenericPointer
85 OpMemoryModel Physical32 OpenCL
90 %5 = OpTypeFunction %4
91 %6 = OpFunction %4 None %5
93 %8 = OpConstant %2 100
94 %9 = OpConstant %2 200
95 %10 = OpConstant %2 300
101 EXPECT_EQ(SPV_SUCCESS, ToBinaryAndBack(before, &after,
102 SPV_TEXT_TO_BINARY_OPTION_NONE,
103 SPV_BINARY_TO_TEXT_OPTION_NO_HEADER));
105 EXPECT_EQ(expected, after);
108 TEST(TextHandler, PreserveNumericIds) {
109 const std::string before =
110 R"(OpCapability Addresses
112 OpCapability GenericPointer
114 OpMemoryModel Physical32 OpenCL
115 %i32 = OpTypeInt 32 1
116 %u32 = OpTypeInt 32 0
117 %f32 = OpTypeFloat 32
119 %300 = OpTypeFunction %200
120 %main = OpFunction %200 None %300
122 %100 = OpConstant %u32 100
123 %1 = OpConstant %u32 200
124 %2 = OpConstant %u32 300
129 const std::string expected =
130 R"(OpCapability Addresses
132 OpCapability GenericPointer
134 OpMemoryModel Physical32 OpenCL
139 %300 = OpTypeFunction %200
140 %6 = OpFunction %200 None %300
142 %100 = OpConstant %4 100
143 %1 = OpConstant %4 200
144 %2 = OpConstant %4 300
150 EXPECT_EQ(SPV_SUCCESS,
151 ToBinaryAndBack(before, &after,
152 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS,
153 SPV_BINARY_TO_TEXT_OPTION_NO_HEADER));
155 EXPECT_EQ(expected, after);