1 // Copyright (c) 2016 Google 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 <vector>
16 
17 #include "gmock/gmock.h"
18 #include "source/spirv_target_env.h"
19 #include "test/unit_spirv.h"
20 
21 namespace spvtools {
22 namespace {
23 
24 using ::testing::AnyOf;
25 using ::testing::Eq;
26 using ::testing::StartsWith;
27 using ::testing::ValuesIn;
28 
29 using TargetEnvTest = ::testing::TestWithParam<spv_target_env>;
TEST_P(TargetEnvTest,CreateContext)30 TEST_P(TargetEnvTest, CreateContext) {
31   spv_target_env env = GetParam();
32   spv_context context = spvContextCreate(env);
33   ASSERT_NE(nullptr, context);
34   spvContextDestroy(context);  // Avoid leaking
35 }
36 
TEST_P(TargetEnvTest,ValidDescription)37 TEST_P(TargetEnvTest, ValidDescription) {
38   const char* description = spvTargetEnvDescription(GetParam());
39   ASSERT_NE(nullptr, description);
40   ASSERT_THAT(description, StartsWith("SPIR-V "));
41 }
42 
TEST_P(TargetEnvTest,ValidSpirvVersion)43 TEST_P(TargetEnvTest, ValidSpirvVersion) {
44   auto spirv_version = spvVersionForTargetEnv(GetParam());
45   ASSERT_THAT(spirv_version, AnyOf(0x10000, 0x10100, 0x10200, 0x10300));
46 }
47 
48 INSTANTIATE_TEST_CASE_P(AllTargetEnvs, TargetEnvTest,
49                         ValuesIn(spvtest::AllTargetEnvironments()));
50 
TEST(GetContextTest,InvalidTargetEnvProducesNull)51 TEST(GetContextTest, InvalidTargetEnvProducesNull) {
52   // Use a value beyond the last valid enum value.
53   spv_context context = spvContextCreate(static_cast<spv_target_env>(30));
54   EXPECT_EQ(context, nullptr);
55 }
56 
57 // A test case for parsing an environment string.
58 struct ParseCase {
59   const char* input;
60   bool success;        // Expect to successfully parse?
61   spv_target_env env;  // The parsed environment, if successful.
62 };
63 
64 using TargetParseTest = ::testing::TestWithParam<ParseCase>;
65 
TEST_P(TargetParseTest,InvalidTargetEnvProducesNull)66 TEST_P(TargetParseTest, InvalidTargetEnvProducesNull) {
67   spv_target_env env;
68   bool parsed = spvParseTargetEnv(GetParam().input, &env);
69   EXPECT_THAT(parsed, Eq(GetParam().success));
70   EXPECT_THAT(env, Eq(GetParam().env));
71 }
72 
73 INSTANTIATE_TEST_CASE_P(
74     TargetParsing, TargetParseTest,
75     ValuesIn(std::vector<ParseCase>{
76         {"spv1.0", true, SPV_ENV_UNIVERSAL_1_0},
77         {"spv1.1", true, SPV_ENV_UNIVERSAL_1_1},
78         {"spv1.2", true, SPV_ENV_UNIVERSAL_1_2},
79         {"spv1.3", true, SPV_ENV_UNIVERSAL_1_3},
80         {"vulkan1.0", true, SPV_ENV_VULKAN_1_0},
81         {"vulkan1.1", true, SPV_ENV_VULKAN_1_1},
82         {"opencl2.1", true, SPV_ENV_OPENCL_2_1},
83         {"opencl2.2", true, SPV_ENV_OPENCL_2_2},
84         {"opengl4.0", true, SPV_ENV_OPENGL_4_0},
85         {"opengl4.1", true, SPV_ENV_OPENGL_4_1},
86         {"opengl4.2", true, SPV_ENV_OPENGL_4_2},
87         {"opengl4.3", true, SPV_ENV_OPENGL_4_3},
88         {"opengl4.5", true, SPV_ENV_OPENGL_4_5},
89         {"opencl1.2", true, SPV_ENV_OPENCL_1_2},
90         {"opencl1.2embedded", true, SPV_ENV_OPENCL_EMBEDDED_1_2},
91         {"opencl2.0", true, SPV_ENV_OPENCL_2_0},
92         {"opencl2.0embedded", true, SPV_ENV_OPENCL_EMBEDDED_2_0},
93         {"opencl2.1embedded", true, SPV_ENV_OPENCL_EMBEDDED_2_1},
94         {"opencl2.2embedded", true, SPV_ENV_OPENCL_EMBEDDED_2_2},
95         {"webgpu0", true, SPV_ENV_WEBGPU_0},
96         {"opencl2.3", false, SPV_ENV_UNIVERSAL_1_0},
97         {"opencl3.0", false, SPV_ENV_UNIVERSAL_1_0},
98         {"vulkan1.2", false, SPV_ENV_UNIVERSAL_1_0},
99         {"vulkan2.0", false, SPV_ENV_UNIVERSAL_1_0},
100         {nullptr, false, SPV_ENV_UNIVERSAL_1_0},
101         {"", false, SPV_ENV_UNIVERSAL_1_0},
102         {"abc", false, SPV_ENV_UNIVERSAL_1_0},
103     }));
104 
105 }  // namespace
106 }  // namespace spvtools
107