Skip to content

Commit

Permalink
Add testing for vk_enum_string_helper.h
Browse files Browse the repository at this point in the history
Use magic_enum to test vk_enum_string_helper.h

closes #46
  • Loading branch information
juan-lunarg committed Aug 16, 2023
1 parent c8a4566 commit 6999ef9
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 18 deletions.
3 changes: 3 additions & 0 deletions scripts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ endif()
if (VULKAN_HEADERS_INSTALL_DIR)
list(APPEND CMAKE_PREFIX_PATH ${VULKAN_HEADERS_INSTALL_DIR})
endif()
if (MAGIC_ENUM_INSTALL_DIR)
list(APPEND CMAKE_PREFIX_PATH ${MAGIC_ENUM_INSTALL_DIR})
endif()

if (CMAKE_CROSSCOMPILING)
set(CMAKE_FIND_ROOT_PATH ${CMAKE_FIND_ROOT_PATH} ${CMAKE_PREFIX_PATH} PARENT_SCOPE)
Expand Down
14 changes: 0 additions & 14 deletions scripts/generators/enum_string_helper_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,6 @@ def generate(self):
#include <vulkan/vulkan.h>
''')

# TODO - this should be moved into different generated util file
out.append('\nstatic inline bool IsDuplicatePnext(VkStructureType input_value) {\n')
out.append(' switch (input_value) {\n')

for struct in [x for x in self.vk.structs.values() if x.allowDuplicate and x.sType is not None]:
# The sType will always be first member of struct
out.append(f' case {struct.sType}:\n')
out.append(' return true;\n')
out.append(' default:\n')
out.append(' return false;\n')
out.append(' }\n')
out.append('}\n')
out.append('\n')

# If there are no fields (empty enum) ignore
for enum in [x for x in self.vk.enums.values() if len(x.fields) > 0]:
groupType = enum.name if enum.bitWidth == 32 else 'uint64_t'
Expand Down
18 changes: 17 additions & 1 deletion scripts/known_good.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,26 @@
"optional": [
"tests"
]
},
{
"name": "magic_enum",
"url": "https://github.com/Neargye/magic_enum",
"sub_dir": "magic_enum",
"build_dir": "magic_enum/build",
"install_dir": "magic_enum/build/install",
"commit": "v0.9.3",
"cmake_options": [
"-DMAGIC_ENUM_OPT_BUILD_EXAMPLES=OFF",
"-DMAGIC_ENUM_OPT_BUILD_TESTS=OFF"
],
"optional": [
"tests"
]
}
],
"install_names": {
"Vulkan-Headers": "VULKAN_HEADERS_INSTALL_DIR",
"googletest": "GOOGLETEST_INSTALL_DIR"
"googletest": "GOOGLETEST_INSTALL_DIR",
"magic_enum": "MAGIC_ENUM_INSTALL_DIR"
}
}
17 changes: 16 additions & 1 deletion tests/generated/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
# Copyright 2023 LunarG, Inc.
#
# SPDX-License-Identifier: Apache-2.0
set(CMAKE_FOLDER "${CMAKE_FOLDER}/generated_code/tests")

find_package(magic_enum REQUIRED CONFIG)
find_package(GTest REQUIRED CONFIG)

include(GoogleTest)

if(${CMAKE_C_COMPILER_ID} MATCHES "(GNU|Clang)")
add_compile_options(-Wpedantic -Wall -Wextra -Werror)
endif()
Expand All @@ -14,4 +21,12 @@ endif()
# Test vk_enum_string_helper.h
add_executable(vk_enum_string_helper vk_enum_string_helper.cpp)
target_include_directories(vk_enum_string_helper PRIVATE ${VUL_SOURCE_DIR}/include)
target_link_libraries(vk_enum_string_helper PRIVATE Vulkan::Headers)
target_link_libraries(vk_enum_string_helper PRIVATE
Vulkan::Headers
GTest::gtest
GTest::gtest_main
magic_enum::magic_enum
)

gtest_discover_tests(vk_enum_string_helper)

71 changes: 69 additions & 2 deletions tests/generated/vk_enum_string_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,72 @@
// SPDX-License-Identifier: Apache-2.0
#include <vulkan/vk_enum_string_helper.h>

// TODO: Add actual testing
int main() {}
#include <magic_enum.hpp>

#include <gtest/gtest.h>

TEST(vk_enum_string_helper, string_VkResult) {
constexpr auto values = magic_enum::enum_values<VkResult>();
for (auto val : values) {
auto magic_str = magic_enum::enum_name(val);

auto str = string_VkResult(val);

EXPECT_STREQ(magic_str.data(), str);
}
}

TEST(vk_enum_string_helper, string_VkStructureType) {
constexpr auto values = magic_enum::enum_values<VkStructureType>();
for (auto val : values) {
auto magic_str = magic_enum::enum_name(val);

auto str = string_VkStructureType(val);

EXPECT_STREQ(magic_str.data(), str);
}
}

TEST(vk_enum_string_helper, string_VkPipelineCacheHeaderVersion) {
constexpr auto values = magic_enum::enum_values<VkPipelineCacheHeaderVersion>();
for (auto val : values) {
auto magic_str = magic_enum::enum_name(val);

auto str = string_VkPipelineCacheHeaderVersion(val);

EXPECT_STREQ(magic_str.data(), str);
}
}

TEST(vk_enum_string_helper, string_VkImageLayout) {
constexpr auto values = magic_enum::enum_values<VkImageLayout>();
for (auto val : values) {
auto magic_str = magic_enum::enum_name(val);

auto str = string_VkImageLayout(val);

EXPECT_STREQ(magic_str.data(), str);
}
}

TEST(vk_enum_string_helper, string_VkObjectType) {
constexpr auto values = magic_enum::enum_values<VkObjectType>();
for (auto val : values) {
auto magic_str = magic_enum::enum_name(val);

auto str = string_VkObjectType(val);

EXPECT_STREQ(magic_str.data(), str);
}
}

TEST(vk_enum_string_helper, string_VkFormat) {
constexpr auto values = magic_enum::enum_values<VkFormat>();
for (auto val : values) {
auto magic_str = magic_enum::enum_name(val);

auto str = string_VkFormat(val);

EXPECT_STREQ(magic_str.data(), str);
}
}

0 comments on commit 6999ef9

Please sign in to comment.