1fd4e5da5Sopenharmony_ci// Copyright (c) 2015-2016 The Khronos Group 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// Assembler tests for instructions in the "Function" section of the
16fd4e5da5Sopenharmony_ci// SPIR-V spec.
17fd4e5da5Sopenharmony_ci
18fd4e5da5Sopenharmony_ci#include <string>
19fd4e5da5Sopenharmony_ci#include <vector>
20fd4e5da5Sopenharmony_ci
21fd4e5da5Sopenharmony_ci#include "gmock/gmock.h"
22fd4e5da5Sopenharmony_ci#include "test/test_fixture.h"
23fd4e5da5Sopenharmony_ci#include "test/unit_spirv.h"
24fd4e5da5Sopenharmony_ci
25fd4e5da5Sopenharmony_cinamespace spvtools {
26fd4e5da5Sopenharmony_cinamespace {
27fd4e5da5Sopenharmony_ci
28fd4e5da5Sopenharmony_ciusing spvtest::EnumCase;
29fd4e5da5Sopenharmony_ciusing spvtest::MakeInstruction;
30fd4e5da5Sopenharmony_ciusing spvtest::TextToBinaryTest;
31fd4e5da5Sopenharmony_ciusing ::testing::Eq;
32fd4e5da5Sopenharmony_ci
33fd4e5da5Sopenharmony_ci// Test OpFunction
34fd4e5da5Sopenharmony_ci
35fd4e5da5Sopenharmony_ciusing OpFunctionControlTest = spvtest::TextToBinaryTestBase<
36fd4e5da5Sopenharmony_ci    ::testing::TestWithParam<EnumCase<spv::FunctionControlMask>>>;
37fd4e5da5Sopenharmony_ci
38fd4e5da5Sopenharmony_ciTEST_P(OpFunctionControlTest, AnySingleFunctionControlMask) {
39fd4e5da5Sopenharmony_ci  const std::string input = "%result_id = OpFunction %result_type " +
40fd4e5da5Sopenharmony_ci                            GetParam().name() + " %function_type ";
41fd4e5da5Sopenharmony_ci  EXPECT_THAT(CompiledInstructions(input),
42fd4e5da5Sopenharmony_ci              Eq(MakeInstruction(spv::Op::OpFunction,
43fd4e5da5Sopenharmony_ci                                 {1, 2, (uint32_t)GetParam().value(), 3})));
44fd4e5da5Sopenharmony_ci}
45fd4e5da5Sopenharmony_ci
46fd4e5da5Sopenharmony_ci// clang-format off
47fd4e5da5Sopenharmony_ci#define CASE(VALUE,NAME) { spv::FunctionControlMask::VALUE, NAME }
48fd4e5da5Sopenharmony_ciINSTANTIATE_TEST_SUITE_P(TextToBinaryFunctionTest, OpFunctionControlTest,
49fd4e5da5Sopenharmony_ci                        ::testing::ValuesIn(std::vector<EnumCase<spv::FunctionControlMask>>{
50fd4e5da5Sopenharmony_ci                            CASE(MaskNone, "None"),
51fd4e5da5Sopenharmony_ci                            CASE(Inline, "Inline"),
52fd4e5da5Sopenharmony_ci                            CASE(DontInline, "DontInline"),
53fd4e5da5Sopenharmony_ci                            CASE(Pure, "Pure"),
54fd4e5da5Sopenharmony_ci                            CASE(Const, "Const"),
55fd4e5da5Sopenharmony_ci                        }));
56fd4e5da5Sopenharmony_ci#undef CASE
57fd4e5da5Sopenharmony_ci// clang-format on
58fd4e5da5Sopenharmony_ci
59fd4e5da5Sopenharmony_ciTEST_F(OpFunctionControlTest, CombinedFunctionControlMask) {
60fd4e5da5Sopenharmony_ci  // Sample a single combination.  This ensures we've integrated
61fd4e5da5Sopenharmony_ci  // the instruction parsing logic with spvTextParseMask.
62fd4e5da5Sopenharmony_ci  const std::string input =
63fd4e5da5Sopenharmony_ci      "%result_id = OpFunction %result_type Inline|Pure|Const %function_type";
64fd4e5da5Sopenharmony_ci  const uint32_t expected_mask = uint32_t(spv::FunctionControlMask::Inline |
65fd4e5da5Sopenharmony_ci                                          spv::FunctionControlMask::Pure |
66fd4e5da5Sopenharmony_ci                                          spv::FunctionControlMask::Const);
67fd4e5da5Sopenharmony_ci  EXPECT_THAT(
68fd4e5da5Sopenharmony_ci      CompiledInstructions(input),
69fd4e5da5Sopenharmony_ci      Eq(MakeInstruction(spv::Op::OpFunction, {1, 2, expected_mask, 3})));
70fd4e5da5Sopenharmony_ci}
71fd4e5da5Sopenharmony_ci
72fd4e5da5Sopenharmony_ciTEST_F(OpFunctionControlTest, WrongFunctionControl) {
73fd4e5da5Sopenharmony_ci  EXPECT_THAT(CompileFailure("%r = OpFunction %t Inline|Unroll %ft"),
74fd4e5da5Sopenharmony_ci              Eq("Invalid function control operand 'Inline|Unroll'."));
75fd4e5da5Sopenharmony_ci}
76fd4e5da5Sopenharmony_ci
77fd4e5da5Sopenharmony_ci// TODO(dneto): OpFunctionParameter
78fd4e5da5Sopenharmony_ci// TODO(dneto): OpFunctionEnd
79fd4e5da5Sopenharmony_ci// TODO(dneto): OpFunctionCall
80fd4e5da5Sopenharmony_ci
81fd4e5da5Sopenharmony_ci}  // namespace
82fd4e5da5Sopenharmony_ci}  // namespace spvtools
83