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