From 32df3128a6a33dd008182c807c426b15800d5a9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Staniewski?= Date: Thu, 31 Oct 2024 15:35:27 +0000 Subject: [PATCH] Add tests for utils_common_linux --- test/CMakeLists.txt | 7 +++++ test/utils/utils_linux.cpp | 53 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 test/utils/utils_linux.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8450f049fc..a48c88c325 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -152,6 +152,13 @@ add_umf_test( SRCS utils/utils.cpp LIBS ${UMF_UTILS_FOR_TEST}) +if(LINUX) + add_umf_test( + NAME utils_linux_common + SRCS utils/utils_linux.cpp + LIBS ${UMF_UTILS_FOR_TEST}) +endif() + add_umf_test(NAME provider_coarse SRCS provider_coarse.cpp) if(UMF_BUILD_LIBUMF_POOL_DISJOINT) diff --git a/test/utils/utils_linux.cpp b/test/utils/utils_linux.cpp new file mode 100644 index 0000000000..f88e9daf28 --- /dev/null +++ b/test/utils/utils_linux.cpp @@ -0,0 +1,53 @@ +// Copyright (C) 2024 Intel Corporation +// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#include "base.hpp" +#include "utils/utils_common.h" + +using umf_test::test; +TEST_F(test, utils_translate_mem_visibility_flag) { + umf_memory_visibility_t in_flag = static_cast(0); + unsigned out_flag; + auto ret = utils_translate_mem_visibility_flag(in_flag, &out_flag); + EXPECT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT); +} + +TEST_F(test, utils_shm_open_invalid_args) { + auto ret = utils_shm_open(NULL); + EXPECT_EQ(ret, -1); + + ret = utils_shm_open("invalid_path"); + EXPECT_EQ(ret, -1); +} + +TEST_F(test, utils_get_file_size_invalid_args) { + size_t size; + auto ret = utils_get_file_size(-1, &size); + EXPECT_EQ(ret, -1); + + int fd = utils_create_anonymous_fd(); + ret = utils_get_file_size(fd, &size); + EXPECT_EQ(ret, 0); + EXPECT_EQ(size, 0); +} + +TEST_F(test, utils_set_file_size_invalid_args) { + auto ret = utils_set_file_size(-1, 256); + EXPECT_EQ(ret, -1); +} + +TEST_F(test, utils_shm_create_invalid_args) { + auto ret = utils_shm_create(NULL, 0); + EXPECT_EQ(ret, -1); + + ret = utils_shm_create("", 256); + EXPECT_EQ(ret, -1); + + // Ensure that a valid size results in a success + ret = utils_shm_create("/abc", 256); + EXPECT_GE(ret, 0); + + ret = utils_shm_create("/abc", -1); + EXPECT_EQ(ret, -1); +}