1fd4e5da5Sopenharmony_ci// Copyright (c) 2017 Google Inc. 2fd4e5da5Sopenharmony_ci// 3fd4e5da5Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License"); 4fd4e5da5Sopenharmony_ci// you may not use this file except in compliance with the License. 5fd4e5da5Sopenharmony_ci// You may obtain a copy of the License at 6fd4e5da5Sopenharmony_ci// 7fd4e5da5Sopenharmony_ci// http://www.apache.org/licenses/LICENSE-2.0 8fd4e5da5Sopenharmony_ci// 9fd4e5da5Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software 10fd4e5da5Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS, 11fd4e5da5Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fd4e5da5Sopenharmony_ci// See the License for the specific language governing permissions and 13fd4e5da5Sopenharmony_ci// limitations under the License. 14fd4e5da5Sopenharmony_ci 15fd4e5da5Sopenharmony_ci// Tests for unique type declaration rules validator. 16fd4e5da5Sopenharmony_ci 17fd4e5da5Sopenharmony_ci#include <string> 18fd4e5da5Sopenharmony_ci 19fd4e5da5Sopenharmony_ci#include "source/text.h" 20fd4e5da5Sopenharmony_ci#include "source/text_handler.h" 21fd4e5da5Sopenharmony_ci#include "test/test_fixture.h" 22fd4e5da5Sopenharmony_ci 23fd4e5da5Sopenharmony_cinamespace spvtools { 24fd4e5da5Sopenharmony_cinamespace { 25fd4e5da5Sopenharmony_ci 26fd4e5da5Sopenharmony_ciusing spvtest::ScopedContext; 27fd4e5da5Sopenharmony_ci 28fd4e5da5Sopenharmony_ci// Converts code to binary and then back to text. 29fd4e5da5Sopenharmony_cispv_result_t ToBinaryAndBack( 30fd4e5da5Sopenharmony_ci const std::string& before, std::string* after, 31fd4e5da5Sopenharmony_ci uint32_t text_to_binary_options = SPV_TEXT_TO_BINARY_OPTION_NONE, 32fd4e5da5Sopenharmony_ci uint32_t binary_to_text_options = SPV_BINARY_TO_TEXT_OPTION_NONE, 33fd4e5da5Sopenharmony_ci spv_target_env env = SPV_ENV_UNIVERSAL_1_0) { 34fd4e5da5Sopenharmony_ci ScopedContext ctx(env); 35fd4e5da5Sopenharmony_ci spv_binary binary; 36fd4e5da5Sopenharmony_ci spv_text text; 37fd4e5da5Sopenharmony_ci 38fd4e5da5Sopenharmony_ci spv_result_t result = 39fd4e5da5Sopenharmony_ci spvTextToBinaryWithOptions(ctx.context, before.c_str(), before.size(), 40fd4e5da5Sopenharmony_ci text_to_binary_options, &binary, nullptr); 41fd4e5da5Sopenharmony_ci if (result != SPV_SUCCESS) { 42fd4e5da5Sopenharmony_ci return result; 43fd4e5da5Sopenharmony_ci } 44fd4e5da5Sopenharmony_ci 45fd4e5da5Sopenharmony_ci result = spvBinaryToText(ctx.context, binary->code, binary->wordCount, 46fd4e5da5Sopenharmony_ci binary_to_text_options, &text, nullptr); 47fd4e5da5Sopenharmony_ci if (result != SPV_SUCCESS) { 48fd4e5da5Sopenharmony_ci return result; 49fd4e5da5Sopenharmony_ci } 50fd4e5da5Sopenharmony_ci 51fd4e5da5Sopenharmony_ci *after = std::string(text->str, text->length); 52fd4e5da5Sopenharmony_ci 53fd4e5da5Sopenharmony_ci spvBinaryDestroy(binary); 54fd4e5da5Sopenharmony_ci spvTextDestroy(text); 55fd4e5da5Sopenharmony_ci 56fd4e5da5Sopenharmony_ci return SPV_SUCCESS; 57fd4e5da5Sopenharmony_ci} 58fd4e5da5Sopenharmony_ci 59fd4e5da5Sopenharmony_ciTEST(ToBinaryAndBack, DontPreserveNumericIds) { 60fd4e5da5Sopenharmony_ci const std::string before = 61fd4e5da5Sopenharmony_ci R"(OpCapability Addresses 62fd4e5da5Sopenharmony_ciOpCapability Kernel 63fd4e5da5Sopenharmony_ciOpCapability GenericPointer 64fd4e5da5Sopenharmony_ciOpCapability Linkage 65fd4e5da5Sopenharmony_ciOpMemoryModel Physical32 OpenCL 66fd4e5da5Sopenharmony_ci%i32 = OpTypeInt 32 1 67fd4e5da5Sopenharmony_ci%u32 = OpTypeInt 32 0 68fd4e5da5Sopenharmony_ci%f32 = OpTypeFloat 32 69fd4e5da5Sopenharmony_ci%200 = OpTypeVoid 70fd4e5da5Sopenharmony_ci%300 = OpTypeFunction %200 71fd4e5da5Sopenharmony_ci%main = OpFunction %200 None %300 72fd4e5da5Sopenharmony_ci%entry = OpLabel 73fd4e5da5Sopenharmony_ci%100 = OpConstant %u32 100 74fd4e5da5Sopenharmony_ci%1 = OpConstant %u32 200 75fd4e5da5Sopenharmony_ci%2 = OpConstant %u32 300 76fd4e5da5Sopenharmony_ciOpReturn 77fd4e5da5Sopenharmony_ciOpFunctionEnd 78fd4e5da5Sopenharmony_ci)"; 79fd4e5da5Sopenharmony_ci 80fd4e5da5Sopenharmony_ci const std::string expected = 81fd4e5da5Sopenharmony_ci R"(OpCapability Addresses 82fd4e5da5Sopenharmony_ciOpCapability Kernel 83fd4e5da5Sopenharmony_ciOpCapability GenericPointer 84fd4e5da5Sopenharmony_ciOpCapability Linkage 85fd4e5da5Sopenharmony_ciOpMemoryModel Physical32 OpenCL 86fd4e5da5Sopenharmony_ci%1 = OpTypeInt 32 1 87fd4e5da5Sopenharmony_ci%2 = OpTypeInt 32 0 88fd4e5da5Sopenharmony_ci%3 = OpTypeFloat 32 89fd4e5da5Sopenharmony_ci%4 = OpTypeVoid 90fd4e5da5Sopenharmony_ci%5 = OpTypeFunction %4 91fd4e5da5Sopenharmony_ci%6 = OpFunction %4 None %5 92fd4e5da5Sopenharmony_ci%7 = OpLabel 93fd4e5da5Sopenharmony_ci%8 = OpConstant %2 100 94fd4e5da5Sopenharmony_ci%9 = OpConstant %2 200 95fd4e5da5Sopenharmony_ci%10 = OpConstant %2 300 96fd4e5da5Sopenharmony_ciOpReturn 97fd4e5da5Sopenharmony_ciOpFunctionEnd 98fd4e5da5Sopenharmony_ci)"; 99fd4e5da5Sopenharmony_ci 100fd4e5da5Sopenharmony_ci std::string after; 101fd4e5da5Sopenharmony_ci EXPECT_EQ(SPV_SUCCESS, 102fd4e5da5Sopenharmony_ci ToBinaryAndBack(before, &after, SPV_TEXT_TO_BINARY_OPTION_NONE, 103fd4e5da5Sopenharmony_ci SPV_BINARY_TO_TEXT_OPTION_NO_HEADER)); 104fd4e5da5Sopenharmony_ci 105fd4e5da5Sopenharmony_ci EXPECT_EQ(expected, after); 106fd4e5da5Sopenharmony_ci} 107fd4e5da5Sopenharmony_ci 108fd4e5da5Sopenharmony_ciTEST(TextHandler, PreserveNumericIds) { 109fd4e5da5Sopenharmony_ci const std::string before = 110fd4e5da5Sopenharmony_ci R"(OpCapability Addresses 111fd4e5da5Sopenharmony_ciOpCapability Kernel 112fd4e5da5Sopenharmony_ciOpCapability GenericPointer 113fd4e5da5Sopenharmony_ciOpCapability Linkage 114fd4e5da5Sopenharmony_ciOpMemoryModel Physical32 OpenCL 115fd4e5da5Sopenharmony_ci%i32 = OpTypeInt 32 1 116fd4e5da5Sopenharmony_ci%u32 = OpTypeInt 32 0 117fd4e5da5Sopenharmony_ci%f32 = OpTypeFloat 32 118fd4e5da5Sopenharmony_ci%200 = OpTypeVoid 119fd4e5da5Sopenharmony_ci%300 = OpTypeFunction %200 120fd4e5da5Sopenharmony_ci%main = OpFunction %200 None %300 121fd4e5da5Sopenharmony_ci%entry = OpLabel 122fd4e5da5Sopenharmony_ci%100 = OpConstant %u32 100 123fd4e5da5Sopenharmony_ci%1 = OpConstant %u32 200 124fd4e5da5Sopenharmony_ci%2 = OpConstant %u32 300 125fd4e5da5Sopenharmony_ciOpReturn 126fd4e5da5Sopenharmony_ciOpFunctionEnd 127fd4e5da5Sopenharmony_ci)"; 128fd4e5da5Sopenharmony_ci 129fd4e5da5Sopenharmony_ci const std::string expected = 130fd4e5da5Sopenharmony_ci R"(OpCapability Addresses 131fd4e5da5Sopenharmony_ciOpCapability Kernel 132fd4e5da5Sopenharmony_ciOpCapability GenericPointer 133fd4e5da5Sopenharmony_ciOpCapability Linkage 134fd4e5da5Sopenharmony_ciOpMemoryModel Physical32 OpenCL 135fd4e5da5Sopenharmony_ci%3 = OpTypeInt 32 1 136fd4e5da5Sopenharmony_ci%4 = OpTypeInt 32 0 137fd4e5da5Sopenharmony_ci%5 = OpTypeFloat 32 138fd4e5da5Sopenharmony_ci%200 = OpTypeVoid 139fd4e5da5Sopenharmony_ci%300 = OpTypeFunction %200 140fd4e5da5Sopenharmony_ci%6 = OpFunction %200 None %300 141fd4e5da5Sopenharmony_ci%7 = OpLabel 142fd4e5da5Sopenharmony_ci%100 = OpConstant %4 100 143fd4e5da5Sopenharmony_ci%1 = OpConstant %4 200 144fd4e5da5Sopenharmony_ci%2 = OpConstant %4 300 145fd4e5da5Sopenharmony_ciOpReturn 146fd4e5da5Sopenharmony_ciOpFunctionEnd 147fd4e5da5Sopenharmony_ci)"; 148fd4e5da5Sopenharmony_ci 149fd4e5da5Sopenharmony_ci std::string after; 150fd4e5da5Sopenharmony_ci EXPECT_EQ(SPV_SUCCESS, 151fd4e5da5Sopenharmony_ci ToBinaryAndBack(before, &after, 152fd4e5da5Sopenharmony_ci SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS, 153fd4e5da5Sopenharmony_ci SPV_BINARY_TO_TEXT_OPTION_NO_HEADER)); 154fd4e5da5Sopenharmony_ci 155fd4e5da5Sopenharmony_ci EXPECT_EQ(expected, after); 156fd4e5da5Sopenharmony_ci} 157fd4e5da5Sopenharmony_ci 158fd4e5da5Sopenharmony_ci} // namespace 159fd4e5da5Sopenharmony_ci} // namespace spvtools 160