target_env_test.cpp 3.8 KB

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