1fd4e5da5Sopenharmony_ci// Copyright (c) 2019 Google LLC
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#include "source/fuzz/instruction_descriptor.h"
16fd4e5da5Sopenharmony_ci
17fd4e5da5Sopenharmony_ci#include "gtest/gtest.h"
18fd4e5da5Sopenharmony_ci#include "source/fuzz/fuzzer_util.h"
19fd4e5da5Sopenharmony_ci#include "test/fuzz/fuzz_test_util.h"
20fd4e5da5Sopenharmony_ci
21fd4e5da5Sopenharmony_cinamespace spvtools {
22fd4e5da5Sopenharmony_cinamespace fuzz {
23fd4e5da5Sopenharmony_cinamespace {
24fd4e5da5Sopenharmony_ci
25fd4e5da5Sopenharmony_ciTEST(InstructionDescriptorTest, BasicTest) {
26fd4e5da5Sopenharmony_ci  std::string shader = R"(
27fd4e5da5Sopenharmony_ci               OpCapability Shader
28fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
29fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
30fd4e5da5Sopenharmony_ci               OpEntryPoint Fragment %4 "main"
31fd4e5da5Sopenharmony_ci               OpExecutionMode %4 OriginUpperLeft
32fd4e5da5Sopenharmony_ci               OpSource ESSL 310
33fd4e5da5Sopenharmony_ci          %2 = OpTypeVoid
34fd4e5da5Sopenharmony_ci          %3 = OpTypeFunction %2
35fd4e5da5Sopenharmony_ci          %6 = OpTypeInt 32 0
36fd4e5da5Sopenharmony_ci          %7 = OpTypePointer Function %6
37fd4e5da5Sopenharmony_ci          %9 = OpConstant %6 0
38fd4e5da5Sopenharmony_ci         %10 = OpTypeInt 32 1
39fd4e5da5Sopenharmony_ci         %11 = OpTypePointer Function %10
40fd4e5da5Sopenharmony_ci         %13 = OpConstant %10 2
41fd4e5da5Sopenharmony_ci         %32 = OpConstant %10 0
42fd4e5da5Sopenharmony_ci          %4 = OpFunction %2 None %3
43fd4e5da5Sopenharmony_ci          %5 = OpLabel
44fd4e5da5Sopenharmony_ci        %164 = OpVariable %11 Function
45fd4e5da5Sopenharmony_ci        %165 = OpVariable %11 Function
46fd4e5da5Sopenharmony_ci               OpBranch %16
47fd4e5da5Sopenharmony_ci         %16 = OpLabel
48fd4e5da5Sopenharmony_ci               OpStore %164 %32
49fd4e5da5Sopenharmony_ci               OpStore %165 %13
50fd4e5da5Sopenharmony_ci               OpReturn
51fd4e5da5Sopenharmony_ci               OpFunctionEnd
52fd4e5da5Sopenharmony_ci  )";
53fd4e5da5Sopenharmony_ci
54fd4e5da5Sopenharmony_ci  const auto env = SPV_ENV_UNIVERSAL_1_4;
55fd4e5da5Sopenharmony_ci  const auto consumer = nullptr;
56fd4e5da5Sopenharmony_ci  const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
57fd4e5da5Sopenharmony_ci  spvtools::ValidatorOptions validator_options;
58fd4e5da5Sopenharmony_ci  ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
59fd4e5da5Sopenharmony_ci                                               kConsoleMessageConsumer));
60fd4e5da5Sopenharmony_ci
61fd4e5da5Sopenharmony_ci  for (auto& function : *context->module()) {
62fd4e5da5Sopenharmony_ci    for (auto& block : function) {
63fd4e5da5Sopenharmony_ci      for (auto inst_it = block.cbegin(); inst_it != block.cend(); ++inst_it) {
64fd4e5da5Sopenharmony_ci        ASSERT_EQ(&*inst_it,
65fd4e5da5Sopenharmony_ci                  FindInstruction(MakeInstructionDescriptor(block, inst_it),
66fd4e5da5Sopenharmony_ci                                  context.get()));
67fd4e5da5Sopenharmony_ci      }
68fd4e5da5Sopenharmony_ci    }
69fd4e5da5Sopenharmony_ci  }
70fd4e5da5Sopenharmony_ci}
71fd4e5da5Sopenharmony_ci
72fd4e5da5Sopenharmony_ci}  // namespace
73fd4e5da5Sopenharmony_ci}  // namespace fuzz
74fd4e5da5Sopenharmony_ci}  // namespace spvtools
75