#! Contains the tests of what `malunal.vulkan.bindings` writes, of a small #! registry of their own, written as `vk.xml` is: the names it gives, the types #! of members and parameters, constants' values, the functions the loader #! exports and the types of the rest, a platform's file, the versions and #! extensions bound, and what it refuses. import std::containers; import std::testing; # A registry of a few of each: Vulkan SC's parts, which are left out, among # them. const small_registry: strview = """ // Version of this file #define VK_HEADER_VERSION 7 // Version of this file #define VK_HEADER_VERSION 3 typedef uint32_t VkFlags; typedef uint64_t VkFlags64; typedef uint32_t VkBool32; VK_DEFINE_HANDLE(VkInstance) VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkThing) typedef VkFlags64 VkStageFlags2; typedef void (VKAPI_PTR *PFN_vkVoidFunction)(void); typedef VkBool32 (VKAPI_PTR *PFN_vkHeardCallback)( uint32_t severity, const char* pMessage, void* pUserData); VkStructureType sType const void* pNext uint32_t nameCount const char* const* ppNames char label[VK_MAX_LABEL_SIZE] uint64_t moduleWhich module VkStageFlags2 stages float matrix[3][4] PFN_vkHeardCallback pfnHeard uint32_t scOnly uint32_t width uint32_t height VkStructureType sType void* pNext VkBool32 shaderFloat64 uint32_t deviceUUID uint32_t maxImageDimension2D uint32_t index:24 uint32_t mask:8 VkStructureType sType const void* pNext HWND hwnd VkResult vkCreateThing VkInstance instance const VkThingCreateInfo* pCreateInfo const void* pAllocator VkThing* pThing void vkListThings VkInstance instance uint32_t* pCount VkExtent2D* pThings const float blend[4] PFN_vkVoidFunction vkGetInstanceProcAddr VkInstance instance const char* pName void vkHearEXT VkInstance instance VkPhysicalDeviceVulkan13Features* pFeatures VkResult vkCreateWin32ThingKHR VkInstance instance const VkWin32ThingCreateInfoKHR* pCreateInfo """; # What's wanted of the small registry: its newest version, and the extensions # named, the platform's exported. def wanting_of(major: u32, minor: u32, names: span): wanting { var made := wanting{ major = major, minor = minor, extensions = vector::::new(), exported = vector::::new() }; for &each in names { made.extensions.append(each); } made.exported.append("VK_KHR_win32_thing"); return made; } # The text of a file written, or nothing where none of the name was. def text_of(files: &vector, name: strview): strview { for &each in files { return each.text.span if each.name.span == name; } return ""; } # Whether a file holds a line. def holds_line(text: strview, line: strview): bool { return text.find(line) is some(_); } @test def names_types_and_members_as_sakura_does(t: &mut tester): void { val names: strview[2] = ["VK_EXT_heard", "VK_KHR_win32_thing"]; val wanted := wanting_of(0, 0, names[:]); var files := vector::::new(); var problems := vector::::new(); t.expects(bindings_of(small_registry, &wanted, &mut files, &mut problems) && problems.length == 0); val types := text_of(&files, "types.sakura"); t.expects(holds_line(types, "pub struct thing_create_info {\n")); t.expects(holds_line(types, " pub var s_type: u32 = VK_STRUCTURE_TYPE_THING_CREATE_INFO; #! VkStructureType\n")); t.expects(holds_line(types, " pub var next: *u8;\n")); t.expects(holds_line(types, " pub var name_count: u32;\n")); t.expects(holds_line(types, " pub var names: **u8;\n")); t.expects(holds_line(types, " pub var label: u8[64];\n")); t.expects(holds_line(types, " pub var module_: u64; #! Which module\n")); t.expects(holds_line(types, " pub var stages: u64; #! VkStageFlags2\n")); # C's 3 arrays of 4, as Sakura writes them: innermost first. t.expects(holds_line(types, " pub var matrix: f32[4][3];\n")); t.expects(holds_line(types, " pub var heard: PFN_vkHeardCallback;\n")); t.expects(!holds_line(types, "sc_only")); # By its `sType`, and in snake case. t.expects(holds_line(types, "pub struct physical_device_vulkan_1_3_features {\n")); t.expects(holds_line(types, " pub var next: *mut u8;\n")); t.expects(holds_line(types, " pub var shader_float64: u32; #! VkBool32\n")); t.expects(holds_line(types, " pub var device_uuid: u32;\n")); t.expects(holds_line(types, " pub var max_image_dimension_2d: u32;\n")); t.expects(holds_line(types, "pub struct extent_2d {\n")); # Bitfields as one word, an alias by its vendor's letters, and a callback. t.expects(holds_line(types, "pub struct packed_khr {\n pub var index_and_mask: u32; #! Bits, the lowest first: index, 24, mask, 8.\n")); t.expects(holds_line(types, "pub type thing_create_info_khr = thing_create_info;\n")); t.expects(holds_line(types, "pub type PFN_vkHeardCallback = cfn;\n")); } @test def gives_constants_their_values(t: &mut tester): void { val names: strview[1] = ["VK_EXT_heard"]; val wanted := wanting_of(0, 0, names[:]); var files := vector::::new(); var problems := vector::::new(); t.expects(bindings_of(small_registry, &wanted, &mut files, &mut problems)); val overview := text_of(&files, "vulkan.sakura"); t.expects(holds_line(overview, "pub const VK_HEADER_VERSION: u32 = 7;\n")); t.expects(holds_line(overview, "pub const VK_API_VERSION_1_1: u32 = (1 << 22) | (1 << 12);\n")); val constants := text_of(&files, "constants.sakura"); t.expects(holds_line(constants, "pub const VK_REMAINING: u32 = 0xFFFFFFFF;\n")); t.expects(holds_line(constants, "pub const VK_WHOLE: u64 = 0xFFFFFFFFFFFFFFFF;\n")); t.expects(holds_line(constants, "pub const VK_CLAMP: f32 = 1000.0;\n")); t.expects(holds_line(constants, "pub const VK_MAX_LABEL_SIZE: u32 = 64;\n")); t.expects(holds_line(constants, "pub const VK_ERROR_OUT_OF_HOST_MEMORY: i32 = -1;\n")); t.expects(holds_line(constants, "pub const VK_ERROR_NOT_HEARD_EXT: i32 = -1000002001;\n")); t.expects(holds_line(constants, "pub const VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES: u32 = 1000011000;\n")); t.expects(holds_line(constants, "pub const VK_STAGE_2_FAR_BIT: u64 = 0x10000000000;\n")); t.expects(holds_line(constants, "# VK_EXT_heard\npub const VK_EXT_HEARD_SPEC_VERSION: u32 = 2;\npub const VK_EXT_HEARD_EXTENSION_NAME: strview = \"VK_EXT_heard\";\n")); } @test def declares_what_the_loader_exports_and_types_the_rest(t: &mut tester): void { val names: strview[1] = ["VK_EXT_heard"]; val wanted := wanting_of(0, 0, names[:]); var files := vector::::new(); var problems := vector::::new(); t.expects(bindings_of(small_registry, &wanted, &mut files, &mut problems)); val commands := text_of(&files, "commands.sakura"); t.expects(holds_line(commands, "extern import \"vulkan-1.dll\" when os == windows;\n")); t.expects(holds_line(commands, "pub def vkCreateThing(instance: u64, create_info: &thing_create_info, allocator: *u8, thing: &mut u64): i32;\n")); t.expects(holds_line(commands, "pub def vkListThings(instance: u64, count: &mut u32, things: *mut extent_2d, blend: &f32[4]): void;\n")); t.expects(holds_line(commands, "pub def vkGetInstanceProcAddr(instance: u64, name: *u8): PFN_vkVoidFunction;\n")); t.expects(!holds_line(commands, "pub def vkHearEXT")); t.expects(holds_line(commands, "pub type PFN_vkHearEXT = cfn;\n")); t.expects(holds_line(commands, "pub type PFN_vkCreateThingKHR = PFN_vkCreateThing;\n")); t.expects(!holds_line(commands, "vkSafetyOnly")); } @test def writes_a_platforms_file_for_its_system(t: &mut tester): void { val names: strview[2] = ["VK_EXT_heard", "VK_KHR_win32_thing"]; val wanted := wanting_of(0, 0, names[:]); var files := vector::::new(); var problems := vector::::new(); t.expects(bindings_of(small_registry, &wanted, &mut files, &mut problems)); val win32 := text_of(&files, "win32.sakura"); t.expects(holds_line(win32, "extern import \"vulkan-1.dll\" when os == windows;\n\nextern module malunal::vulkan when os == windows;\n")); t.expects(holds_line(win32, "pub const VK_STRUCTURE_TYPE_WIN32_THING_CREATE_INFO_KHR: u32 = 1000009000;\n")); t.expects(holds_line(win32, "pub struct win32_thing_create_info_khr {\n")); t.expects(holds_line(win32, " pub var hwnd: u64; #! HWND\n")); t.expects(holds_line(win32, "pub def vkCreateWin32ThingKHR(instance: u64, create_info: &win32_thing_create_info_khr): i32;\n")); t.expects(!holds_line(text_of(&files, "types.sakura"), "win32_thing")); t.expects(text_of(&files, "xlib.sakura").length == 0); } @test def binds_an_older_version_without_whats_newer(t: &mut tester): void { val names: strview[1] = ["VK_EXT_heard"]; val wanted := wanting_of(1, 0, names[:]); var files := vector::::new(); var problems := vector::::new(); t.expects(bindings_of(small_registry, &wanted, &mut files, &mut problems)); t.expects(!holds_line(text_of(&files, "vulkan.sakura"), "VK_API_VERSION_1_1")); t.expects(holds_line(text_of(&files, "vulkan.sakura"), "Vulkan 1.1.7, to Vulkan 1.0")); t.expects(!holds_line(text_of(&files, "commands.sakura"), "vkListThings")); # What the extension needs only with 1.1. t.expects(!holds_line(text_of(&files, "commands.sakura"), "PFN_vkCreateThingKHR")); t.expects(!holds_line(text_of(&files, "types.sakura"), "thing_create_info_khr")); } @test def refuses_what_cant_be_bound(t: &mut tester): void { val cases: strview[4] = ["VK_EXT_nothing", "VK_EXT_disabled", "VK_MVK_moved", "VK_KHR_win32_thing"]; val said: strview[4] = [ "The registry has no extension VK_EXT_nothing.", "VK_EXT_disabled isn't Vulkan's, or is disabled.", "VK_MVK_moved is of the platform macos", "VK_KHR_win32_thing needs VK_EXT_heard", ]; for i in 0..4u { val wanted := wanting_of(0, 0, cases[i:1]); var files := vector::::new(); var problems := vector::::new(); t.expects(!bindings_of(small_registry, &wanted, &mut files, &mut problems) && problems.length == 1); t.expects(problems.length == 1 && problems[0].span.starts_with(said[i])); } val heard: strview[1] = ["VK_EXT_heard"]; val too_new := wanting_of(2, 0, heard[:]); var files := vector::::new(); var problems := vector::::new(); t.expects(!bindings_of(small_registry, &too_new, &mut files, &mut problems)); t.expects(problems.length == 1 && problems[0].span == "The registry has no Vulkan 2.0."); } @test def writes_c_names_in_snake_case(t: &mut tester): void { t.expects(snake_of("sType").span == "s_type" && snake_of("deviceUUID").span == "device_uuid"); t.expects(snake_of("shaderFloat64").span == "shader_float64" && snake_of("maxImageDimension2D").span == "max_image_dimension_2d"); t.expects(snake_of("Extent2D").span == "extent_2d" && snake_of("textureCompressionASTC_HDR").span == "texture_compression_astc_hdr"); t.expects(snake_of("storageBuffer16BitAccess").span == "storage_buffer16_bit_access" && snake_of("image2DViewOf3D").span == "image_2d_view_of_3d"); t.expects(snake_of("driverID").span == "driver_id" && snake_of("SurfaceFormatKHR").span == "surface_format_khr"); t.expects(without_prefix("ppEnabledLayerNames") == "EnabledLayerNames" && without_prefix("pfnCallback") == "Callback"); t.expects(without_prefix("plane") == "plane" && without_prefix("pNext") == "Next"); }