From 9ea9962e0710fc36b87e6e8ea2bbe4e9ce870e3f Mon Sep 17 00:00:00 2001 From: PENGUINLIONG Date: Thu, 22 Dec 2022 10:24:01 +0800 Subject: [PATCH] [aot] Fixed C-API behavior tests (#6939) Fixed allocation leakage, unchecked values, camel cases and uninitialized structs in behavior tests. Reflected in codes, this PR has done the following: 1. Use lifetime guarded C++ wrapper for usage that are *not* the tested interface. 2. Removed several *success* tests that might overlap with those in other tests. (This is not done completely, will make another PR to refactorize that.) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- c_api/include/taichi/cpp/taichi.hpp | 22 ++ c_api/tests/c_api_behavior_test.cpp | 447 +++++++++------------------- 2 files changed, 158 insertions(+), 311 deletions(-) diff --git a/c_api/include/taichi/cpp/taichi.hpp b/c_api/include/taichi/cpp/taichi.hpp index 1b96a0906030b..b88323e709aea 100644 --- a/c_api/include/taichi/cpp/taichi.hpp +++ b/c_api/include/taichi/cpp/taichi.hpp @@ -153,6 +153,21 @@ class Memory { unmap(); } + TiMemorySlice slice(size_t offset, size_t size) const { + if (offset + size > size_) { + ti_set_last_error(TI_ERROR_ARGUMENT_OUT_OF_RANGE, "size"); + return {}; + } + TiMemorySlice slice{}; + slice.memory = memory_; + slice.offset = offset; + slice.size = size; + return slice; + } + TiMemorySlice slice() const { + return slice(0, size_); + } + constexpr size_t size() const { return size_; } @@ -271,6 +286,13 @@ class NdArray { write((const T *)src.data(), src.size() * (sizeof(U) / sizeof(T))); } + TiMemorySlice slice(size_t offset, size_t size) const { + return memory_.slice(offset, size); + } + TiMemorySlice slice() const { + return memory_.slice(); + } + constexpr TiDataType elem_type() const { return ndarray_.elem_type; } diff --git a/c_api/tests/c_api_behavior_test.cpp b/c_api/tests/c_api_behavior_test.cpp index 204c5b17629b1..8165e3e15e413 100644 --- a/c_api/tests/c_api_behavior_test.cpp +++ b/c_api/tests/c_api_behavior_test.cpp @@ -1,3 +1,6 @@ +// NOTE: This test intends to fixate error code reporting of all 'unexpected' +// usage of the C-APIs. Normal usages should be covered in other test files like +// `c_api_interface_test.cpp`. #include "gtest/gtest.h" #include "c_api_test_utils.h" #include "taichi/cpp/taichi.hpp" @@ -8,8 +11,6 @@ TEST_F(CapiTest, TestBehaviorCreateRuntime) { TiRuntime runtime = ti_create_runtime(arch); TI_ASSERT(runtime == TI_NULL_HANDLE); EXPECT_TAICHI_ERROR(TI_ERROR_NOT_SUPPORTED); - ti_destroy_runtime(runtime); - EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); }; // Attempt to create runtime for unknown arch. @@ -39,7 +40,7 @@ TEST_F(CapiTest, TestBehaviorGetRuntimeCapabilities) { return; } - TiRuntime runtime = ti_create_runtime(arch); + ti::Runtime runtime(arch); { // Two nulls, considerred nop. ti_get_runtime_capabilities(runtime, nullptr, nullptr); @@ -95,7 +96,6 @@ TEST_F(CapiTest, TestBehaviorGetRuntimeCapabilities) { TI_ASSERT(capabilities.at(i).level != 0); } } - ti_destroy_runtime(runtime); }; inner(TI_ARCH_VULKAN); } @@ -106,78 +106,43 @@ TEST_F(CapiTest, TestBehaviorAllocateMemory) { TI_WARN("arch {} is not supported, so this test is skipped", arch); return; } - // Attempt to allocate memory with size of 1024 - TiRuntime runtime = ti_create_runtime(arch); - TiMemoryAllocateInfo allocateInfo; - allocateInfo.size = 1024; - allocateInfo.usage = TI_MEMORY_USAGE_STORAGE_BIT; - TiMemory memory = ti_allocate_memory(runtime, &allocateInfo); - TI_ASSERT(memory != TI_NULL_HANDLE); - ti_free_memory(runtime, memory); - ti_destroy_runtime(runtime); - }; - inner(TI_ARCH_VULKAN); -} -TEST_F(CapiTest, TestBehaviorAllocInvalidMemory) { - auto inner = [&](TiArch arch) { - if (!ti::is_arch_available(arch)) { - TI_WARN("arch {} is not supported, so this test is skipped", arch); - return; - } - // Attemp to run out of memory - TiRuntime runtime = ti_create_runtime(arch); - TiMemoryAllocateInfo allocateInfo; - allocateInfo.size = 1000000000000000000; - allocateInfo.usage = TI_MEMORY_USAGE_STORAGE_BIT; - TiMemory memory = ti_allocate_memory(runtime, &allocateInfo); - EXPECT_TAICHI_ERROR(TI_ERROR_OUT_OF_MEMORY); - TI_ASSERT(memory == TI_NULL_HANDLE); - ti_destroy_runtime(runtime); - }; - inner(TI_ARCH_VULKAN); -} + ti::Runtime runtime(arch); -TEST_F(CapiTest, TestBehaviorAllocMemoryNoArg) { - auto inner = [&](TiArch arch) { - if (!ti::is_arch_available(arch)) { - TI_WARN("arch {} is not supported, so this test is skipped", arch); - return; + // Out of memory + { + TiMemoryAllocateInfo allocate_info{}; + allocate_info.size = 1000000000000000000; + allocate_info.usage = TI_MEMORY_USAGE_STORAGE_BIT; + TiMemory memory = ti_allocate_memory(runtime, &allocate_info); + EXPECT_TAICHI_ERROR(TI_ERROR_OUT_OF_MEMORY); + TI_ASSERT(memory == TI_NULL_HANDLE); } - // runtime and allocate_info are both null - ti_allocate_memory(TI_NULL_HANDLE, nullptr); - EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); - }; - inner(TI_ARCH_VULKAN); -} -TEST_F(CapiTest, TestBehaviorAllocMemoryNoAllocInfo) { - auto inner = [&](TiArch arch) { - if (!ti::is_arch_available(arch)) { - TI_WARN("arch {} is not supported, so this test is skipped", arch); - return; + // runtime and allocate_info are both null + { + TiMemory memory = ti_allocate_memory(TI_NULL_HANDLE, nullptr); + EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); + TI_ASSERT(memory == TI_NULL_HANDLE); } - // runtime is not null, allocate_info is null - TiRuntime runtime = ti_create_runtime(arch); - ti_allocate_memory(runtime, nullptr); - EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); - ti_destroy_runtime(runtime); - }; - inner(TI_ARCH_VULKAN); -} -TEST_F(CapiTest, TestBehaviorAllocMemoryNoRuntime) { - auto inner = [&](TiArch arch) { - if (!ti::is_arch_available(arch)) { - TI_WARN("arch {} is not supported, so this test is skipped", arch); - return; + // runtime is not null, allocate_info is null + { + TiMemory memory = ti_allocate_memory(runtime, nullptr); + EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); + TI_ASSERT(memory == TI_NULL_HANDLE); } + // runtime is null, allocate is not null; - TiMemoryAllocateInfo allocateInfo; - allocateInfo.size = 1024; - ti_allocate_memory(TI_NULL_HANDLE, &allocateInfo); - EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); + { + TiMemoryAllocateInfo allocate_info{}; + allocate_info.size = 1024; + TiMemory memory = ti_allocate_memory(TI_NULL_HANDLE, &allocate_info); + EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); + TI_ASSERT(memory == TI_NULL_HANDLE); + } }; + inner(TI_ARCH_VULKAN); } @@ -188,16 +153,7 @@ TEST_F(CapiTest, TestBehaviorFreeMemory) { return; } - { - TiRuntime runtime = ti_create_runtime(arch); - TiMemoryAllocateInfo *allocate_info = new TiMemoryAllocateInfo; - allocate_info->size = 1024; - allocate_info->usage = TI_MEMORY_USAGE_STORAGE_BIT; - TiMemory memory = ti_allocate_memory(runtime, allocate_info); - ti_free_memory(runtime, memory); - ASSERT_TAICHI_SUCCESS(); - ti_destroy_runtime(runtime); - } + ti::Runtime runtime(arch); // runtime & allocate_info are both null { @@ -207,19 +163,13 @@ TEST_F(CapiTest, TestBehaviorFreeMemory) { // runtime is null and allocate_info is valid { - TiRuntime runtime = ti_create_runtime(TI_ARCH_VULKAN); - TiMemoryAllocateInfo allocate_info; - allocate_info.size = 1024; - allocate_info.usage = TI_MEMORY_USAGE_STORAGE_BIT; - TiMemory memory = ti_allocate_memory(runtime, &allocate_info); + TiMemory memory = runtime.allocate_memory(1024); ti_free_memory(TI_NULL_HANDLE, memory); EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); - ti_destroy_runtime(runtime); } // runtime is not null and allocate_info is null { - TiRuntime runtime = ti_create_runtime(arch); ti_free_memory(runtime, nullptr); EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); } @@ -234,68 +184,42 @@ TEST_F(CapiTest, TestBehaviorMapMemory) { return; } - TiMemoryAllocateInfo allocate_info; - allocate_info.size = 1024; - allocate_info.usage = TI_MEMORY_USAGE_STORAGE_BIT; - - { - TiRuntime runtime = ti_create_runtime(arch); - TiMemory memory = ti_allocate_memory(runtime, &allocate_info); - TI_ASSERT(memory != TI_NULL_HANDLE); - ASSERT_TAICHI_SUCCESS(); - ti_map_memory(runtime, memory); - ASSERT_TAICHI_SUCCESS(); - ti_unmap_memory(runtime, memory); - ti_destroy_runtime(runtime); - } + ti::Runtime runtime(arch); + ti::Memory memory = runtime.allocate_memory(1024); // runtime & memory are both null { - TiRuntime runtime = ti_create_runtime(arch); - ti_map_memory(TI_NULL_HANDLE, TI_NULL_HANDLE); + void *ptr = ti_map_memory(TI_NULL_HANDLE, TI_NULL_HANDLE); EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); - ti_destroy_runtime(runtime); + TI_ASSERT(ptr == nullptr); } // runtime is null, memory is valid { - TiRuntime runtime = ti_create_runtime(arch); - TiMemory memory = ti_allocate_memory(runtime, &allocate_info); - ti_map_memory(TI_NULL_HANDLE, memory); + void *ptr = ti_map_memory(TI_NULL_HANDLE, memory); EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); - ti_unmap_memory(runtime, memory); - ti_destroy_runtime(runtime); + TI_ASSERT(ptr == nullptr); } // runtime is valid, memory is null { - TiRuntime runtime = ti_create_runtime(arch); - ti_map_memory(runtime, TI_NULL_HANDLE); + void *ptr = ti_map_memory(runtime, TI_NULL_HANDLE); EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); - ti_destroy_runtime(runtime); + TI_ASSERT(ptr == nullptr); } }; inner(TI_ARCH_VULKAN); } TEST_F(CapiTest, TestBehaviorUnmapMemory) { - TiMemoryAllocateInfo allocate_info; - allocate_info.size = 1024; auto inner = [&](TiArch arch) { if (!ti::is_arch_available(arch)) { TI_WARN("arch {} is nor supported, so the test is skipped", arch); return; } - { - TiRuntime runtime = ti_create_runtime(arch); - allocate_info.usage = TI_MEMORY_USAGE_STORAGE_BIT; - TiMemory memory = ti_allocate_memory(runtime, &allocate_info); - ti_map_memory(runtime, memory); - ti_unmap_memory(runtime, memory); - ASSERT_TAICHI_SUCCESS(); - ti_destroy_runtime(runtime); - } + ti::Runtime runtime(arch); + ti::Memory memory = runtime.allocate_memory(1024); // runtime & memory are both null { @@ -305,38 +229,32 @@ TEST_F(CapiTest, TestBehaviorUnmapMemory) { // runtime is null, memory is valid { - TiRuntime runtime = ti_create_runtime(TI_ARCH_VULKAN); - TiMemory memory = ti_allocate_memory(runtime, &allocate_info); + ti_map_memory(TI_NULL_HANDLE, memory); ti_unmap_memory(TI_NULL_HANDLE, memory); EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); - ti_free_memory(runtime, memory); - ti_destroy_runtime(runtime); + ti_unmap_memory(runtime, memory); } - // runtime is valid, memory is valid + // runtime is valid, memory is null { - TiRuntime runtime = ti_create_runtime(TI_ARCH_VULKAN); ti_unmap_memory(runtime, TI_NULL_HANDLE); EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); - ti_destroy_runtime(runtime); } }; inner(TI_ARCH_VULKAN); } -TiImageAllocateInfo get_image_allocate_info() { - TiImageExtent extent; - extent.height = 512; - extent.width = 512; - extent.depth = 1; - extent.array_layer_count = 1; - TiImageAllocateInfo imageAllocateInfo; - imageAllocateInfo.dimension = TI_IMAGE_DIMENSION_2D; - imageAllocateInfo.format = TI_FORMAT_RGBA8; - imageAllocateInfo.extent = extent; - imageAllocateInfo.usage = TI_IMAGE_USAGE_STORAGE_BIT; - imageAllocateInfo.mip_level_count = 1; - return imageAllocateInfo; +inline TiImageAllocateInfo make_image_allocate_info() { + TiImageAllocateInfo allocate_info{}; + allocate_info.dimension = TI_IMAGE_DIMENSION_2D; + allocate_info.format = TI_FORMAT_RGBA8; + allocate_info.extent.height = 16; + allocate_info.extent.width = 16; + allocate_info.extent.depth = 1; + allocate_info.extent.array_layer_count = 1; + allocate_info.usage = TI_IMAGE_USAGE_STORAGE_BIT | TI_IMAGE_USAGE_SAMPLED_BIT; + allocate_info.mip_level_count = 1; + return allocate_info; } TEST_F(CapiTest, TestBehaviorAllocateImage) { @@ -346,103 +264,52 @@ TEST_F(CapiTest, TestBehaviorAllocateImage) { return; } - // Attemp to allocate a normal 2D image - { - TiImageExtent extent; - extent.height = 512; - extent.width = 512; - extent.depth = 1; - extent.array_layer_count = 1; - TiImageAllocateInfo imageAllocateInfo; - imageAllocateInfo.dimension = TI_IMAGE_DIMENSION_2D; - imageAllocateInfo.format = TI_FORMAT_RGBA8; - imageAllocateInfo.extent = extent; - imageAllocateInfo.usage = TI_IMAGE_USAGE_STORAGE_BIT; - imageAllocateInfo.mip_level_count = 1; - TiRuntime runtime = ti_create_runtime(arch); - TiImage image = ti_allocate_image(runtime, &imageAllocateInfo); - ASSERT_TAICHI_SUCCESS(); - TI_ASSERT(image != TI_NULL_HANDLE); - - imageAllocateInfo.usage = TI_IMAGE_USAGE_SAMPLED_BIT; - image = ti_allocate_image(runtime, &imageAllocateInfo); - ASSERT_TAICHI_SUCCESS(); - TI_ASSERT(image != TI_NULL_HANDLE); - imageAllocateInfo.usage = TI_IMAGE_USAGE_STORAGE_BIT; - - ti_free_image(runtime, image); - ti_destroy_runtime(runtime); - } + ti::Runtime runtime(arch); // Attemp to allocate a 2D image with invalid demension { - TiRuntime runtime = ti_create_runtime(arch); - auto imageAllocateInfo = get_image_allocate_info(); - imageAllocateInfo.dimension = TI_IMAGE_DIMENSION_MAX_ENUM; - TiImage image = ti_allocate_image(runtime, &imageAllocateInfo); + TiImageAllocateInfo allocate_info = make_image_allocate_info(); + allocate_info.dimension = TI_IMAGE_DIMENSION_MAX_ENUM; + TiImage image = ti_allocate_image(runtime, &allocate_info); EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_OUT_OF_RANGE); TI_ASSERT(image == TI_NULL_HANDLE); - ti_free_image(runtime, image); - ti_destroy_runtime(runtime); } // Attemp to allocate a 2D image with a invalid format { - TiRuntime runtime = ti_create_runtime(arch); - auto imageAllocateInfo = get_image_allocate_info(); - imageAllocateInfo.format = TI_FORMAT_MAX_ENUM; - TiImage image = ti_allocate_image(runtime, &imageAllocateInfo); + TiImageAllocateInfo allocate_info = make_image_allocate_info(); + allocate_info.format = TI_FORMAT_MAX_ENUM; + TiImage image = ti_allocate_image(runtime, &allocate_info); EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_OUT_OF_RANGE); - imageAllocateInfo.format = TI_FORMAT_BGRA8; - ti_free_image(runtime, image); - ti_destroy_runtime(runtime); + TI_ASSERT(image == TI_NULL_HANDLE); } - // runtime & imageAllocateInfo are both null + // runtime & allocate_info are both null { - TiRuntime runtime = ti_create_runtime(arch); - auto image = ti_allocate_image(TI_NULL_HANDLE, TI_NULL_HANDLE); + TiImage image = ti_allocate_image(TI_NULL_HANDLE, TI_NULL_HANDLE); EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); - ti_free_image(runtime, image); - ti_destroy_runtime(runtime); + TI_ASSERT(image == TI_NULL_HANDLE); } - // runtime is null, imageAllocateInfo is valid + // runtime is null, allocate_info is valid { - TiRuntime runtime = ti_create_runtime(arch); - TiImageAllocateInfo imageAllocateInfo = get_image_allocate_info(); - TiImage image = ti_allocate_image(TI_NULL_HANDLE, &imageAllocateInfo); + TiImageAllocateInfo allocate_info = make_image_allocate_info(); + TiImage image = ti_allocate_image(TI_NULL_HANDLE, &allocate_info); EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); - ti_free_image(runtime, image); - ti_destroy_runtime(runtime); + TI_ASSERT(image == TI_NULL_HANDLE); } - // runtime is valid, imageAllocateInfo is null; + // runtime is valid, allocate_info is null; { - TiRuntime runtime = ti_create_runtime(arch); TiImage image = ti_allocate_image(runtime, TI_NULL_HANDLE); EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); - ti_free_image(runtime, image); - EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); - ti_destroy_runtime(runtime); + TI_ASSERT(image == TI_NULL_HANDLE); } }; inner(TI_ARCH_VULKAN); } TEST_F(CapiTest, TestBehaviorFreeImage) { - TiImageExtent extent; - extent.height = 512; - extent.width = 512; - extent.depth = 1; - extent.array_layer_count = 1; - TiImageAllocateInfo imageAllocateInfo; - imageAllocateInfo.dimension = TI_IMAGE_DIMENSION_2D; - imageAllocateInfo.format = TI_FORMAT_RGBA8; - imageAllocateInfo.extent = extent; - imageAllocateInfo.usage = TI_IMAGE_USAGE_STORAGE_BIT; - imageAllocateInfo.mip_level_count = 1; - auto inner = [&](TiArch arch) { // Attemp to free a normal 2D image if (!ti::is_arch_available(arch)) { @@ -450,10 +317,7 @@ TEST_F(CapiTest, TestBehaviorFreeImage) { return; } - TiRuntime runtime = ti_create_runtime(arch); - TiImage image = ti_allocate_image(runtime, &imageAllocateInfo); - ti_free_image(runtime, image); - ASSERT_TAICHI_SUCCESS(); + ti::Runtime runtime(arch); // Runtime & image are both invalid { @@ -463,6 +327,7 @@ TEST_F(CapiTest, TestBehaviorFreeImage) { // Runtime is null, Image is valid { + TiImage image = runtime.allocate_image(make_image_allocate_info()); ti_free_image(TI_NULL_HANDLE, image); EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); } @@ -472,101 +337,61 @@ TEST_F(CapiTest, TestBehaviorFreeImage) { ti_free_image(runtime, TI_NULL_HANDLE); EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); } - - ti_destroy_runtime(runtime); }; inner(TI_ARCH_VULKAN); } -TEST_F(CapiTest, TestBehaviorCreateEvent) { +TEST_F(CapiTest, TestBehaviorCopyMemoryDTD) { auto inner = [&](TiArch arch) { if (!ti::is_arch_available(arch)) { TI_WARN("arch {} is not supported, so the test is skipped", arch); return; } - TiRuntime runtime = ti_create_runtime(arch); - TiEvent event = ti_create_event(runtime); - ASSERT_TAICHI_SUCCESS(); - // Runtime is null + ti::Runtime runtime(arch); + ti::Memory src = runtime.allocate_memory(2048); + ti::Memory dst = runtime.allocate_memory(2048); + { - event = ti_create_event(TI_NULL_HANDLE); - EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); + TiMemorySlice src_memory = src.slice(128, 64); + TiMemorySlice dst_memory = dst.slice(1024, 64); + ti_copy_memory_device_to_device(runtime, &dst_memory, &src_memory); + ASSERT_TAICHI_SUCCESS(); } - ti_destroy_runtime(runtime); - }; - inner(TI_ARCH_VULKAN); -} -TEST_F(CapiTest, TestBehaviorDestroyEvent) { - auto inner = [&](TiArch arch) { - if (!ti::is_arch_available(arch)) { - TI_WARN("arch {} is not supported, so the test is skipped", arch); - return; + // Attempt copy memory from the big one to the small one + { + TiMemorySlice src_memory = src.slice(0, 256); + TiMemorySlice dst_memory = dst.slice(0, 64); + ti_copy_memory_device_to_device(runtime, &dst_memory, &src_memory); + EXPECT_TAICHI_ERROR(TI_ERROR_INVALID_ARGUMENT); } - TiRuntime runtime = ti_create_runtime(arch); - TiEvent event = ti_create_event(runtime); - ti_destroy_event(event); - ASSERT_TAICHI_SUCCESS(); - - // Attemp to destroy a null event + // runtime is null; { - ti_destroy_event(TI_NULL_HANDLE); + TiMemorySlice src_memory = src.slice(); + TiMemorySlice dst_memory = dst.slice(); + ti_copy_memory_device_to_device(TI_NULL_HANDLE, &dst_memory, &src_memory); EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); } - ti_destroy_runtime(runtime); - }; - inner(TI_ARCH_VULKAN); -} - -TEST_F(CapiTest, TestBehaviorCopyMemoryDTD) { - TiMemoryAllocateInfo MallocateInfo; - MallocateInfo.size = 2048; - MallocateInfo.usage = TI_MEMORY_USAGE_STORAGE_BIT; - MallocateInfo.export_sharing = TI_TRUE; - auto inner = [&](TiArch arch) { - if (!ti::is_arch_available(arch)) { - TI_WARN("arch {} is not supported, so the test is skipped", arch); - return; - } - TiRuntime runtime = ti_create_runtime(arch); - TiMemory memory = ti_allocate_memory(runtime, &MallocateInfo); - TiMemorySlice src_memory; - src_memory.memory = memory; - src_memory.offset = 128; - src_memory.size = 64; - TiMemorySlice dst_memory; - dst_memory.memory = memory; - dst_memory.offset = 1024; - dst_memory.size = 64; - ti_copy_memory_device_to_device(runtime, &dst_memory, &src_memory); - ASSERT_TAICHI_SUCCESS(); - - // Attempt copy memory from the big one to the small one - src_memory.size = 256; - src_memory.offset = 512; - dst_memory.offset = 1152; - ti_copy_memory_device_to_device(runtime, &dst_memory, &src_memory); - EXPECT_TAICHI_ERROR(TI_ERROR_INVALID_ARGUMENT); - dst_memory.size = 64; - - // runtime is null; - ti_copy_memory_device_to_device(TI_NULL_HANDLE, &dst_memory, &src_memory); - EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); // dst memory is null; - dst_memory.memory = TI_NULL_HANDLE; - ti_copy_memory_device_to_device(runtime, &dst_memory, &src_memory); - EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); + { + TiMemorySlice src_memory = src.slice(); + TiMemorySlice dst_memory = dst.slice(); + dst_memory.memory = TI_NULL_HANDLE; + ti_copy_memory_device_to_device(runtime, &dst_memory, &src_memory); + EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); + } // src memory is null; - src_memory.memory = TI_NULL_HANDLE; - ti_copy_memory_device_to_device(runtime, &dst_memory, &src_memory); - EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); - - ti_free_memory(runtime, memory); - ti_destroy_runtime(runtime); + { + TiMemorySlice src_memory = src.slice(); + TiMemorySlice dst_memory = dst.slice(); + src_memory.memory = TI_NULL_HANDLE; + ti_copy_memory_device_to_device(runtime, &dst_memory, &src_memory); + EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); + } }; inner(TI_ARCH_VULKAN); } @@ -580,7 +405,7 @@ TEST_F(CapiTest, TestBehaviorLoadAOTModuleVulkan) { TI_WARN("arch {} is not supported, so the test is skipped", arch); return; } - TiRuntime runtime = ti_create_runtime(arch); + ti::Runtime runtime(arch); // AOT module from tcm file, normal usage. { @@ -623,7 +448,6 @@ TEST_F(CapiTest, TestBehaviorLoadAOTModuleVulkan) { EXPECT_TAICHI_ERROR(TI_ERROR_CORRUPTED_DATA); TI_ASSERT(module == TI_NULL_HANDLE); } - ti_destroy_runtime(runtime); }; test_behavior_load_aot_module_impl(TI_ARCH_VULKAN); @@ -638,13 +462,6 @@ TEST_F(CapiTest, TestBehaviorDestroyAotModuleVulkan) { return; } - TiRuntime runtime = ti_create_runtime(arch); - TiAotModule module = ti_load_aot_module(runtime, module_path.c_str()); - TI_ASSERT(module != TI_NULL_HANDLE); - ti_destroy_aot_module(module); - ASSERT_TAICHI_SUCCESS(); - ti_destroy_runtime(runtime); - // Attempt to destroy a null handle. ti_destroy_aot_module(TI_NULL_HANDLE); EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); @@ -662,29 +479,37 @@ TEST_F(CapiTest, TestBehaviorGetCgraphVulkan) { return; } - TiRuntime runtime = ti_create_runtime(arch); - TiAotModule module = ti_load_aot_module(runtime, module_path.c_str()); - TiComputeGraph cgraph = - ti_get_aot_module_compute_graph(module, "run_graph"); - ASSERT_TAICHI_SUCCESS(); - TI_ASSERT(cgraph != TI_NULL_HANDLE); + ti::Runtime runtime(arch); + ti::AotModule module = runtime.load_aot_module(module_path.c_str()); + + { + TiComputeGraph cgraph = + ti_get_aot_module_compute_graph(module, "run_graph"); + ASSERT_TAICHI_SUCCESS(); + TI_ASSERT(cgraph != TI_NULL_HANDLE); + } // Attemp to get compute graph with null module. - cgraph = ti_get_aot_module_compute_graph(TI_NULL_HANDLE, "run_graph"); - EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); - TI_ASSERT(cgraph == TI_NULL_HANDLE); + { + TiComputeGraph cgraph = + ti_get_aot_module_compute_graph(TI_NULL_HANDLE, "run_graph"); + EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); + TI_ASSERT(cgraph == TI_NULL_HANDLE); + } // Attemp to get compute graph without graph name. - cgraph = ti_get_aot_module_compute_graph(module, nullptr); - EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); - TI_ASSERT(cgraph == TI_NULL_HANDLE); + { + TiComputeGraph cgraph = ti_get_aot_module_compute_graph(module, nullptr); + EXPECT_TAICHI_ERROR(TI_ERROR_ARGUMENT_NULL); + TI_ASSERT(cgraph == TI_NULL_HANDLE); + } // Attemp to get compute graph with invalid name. - cgraph = ti_get_aot_module_compute_graph(module, "#$#%*("); - EXPECT_TAICHI_ERROR(TI_ERROR_NAME_NOT_FOUND); - TI_ASSERT(cgraph == TI_NULL_HANDLE); - - ti_destroy_runtime(runtime); + { + TiComputeGraph cgraph = ti_get_aot_module_compute_graph(module, "#$#%*("); + EXPECT_TAICHI_ERROR(TI_ERROR_NAME_NOT_FOUND); + TI_ASSERT(cgraph == TI_NULL_HANDLE); + } }; test_behavior_get_cgraph_impl(TI_ARCH_VULKAN);