summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/CMakeLists.txt66
-rw-r--r--test/test_common.cpp18
-rw-r--r--test/test_result.cpp52
3 files changed, 136 insertions, 0 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
new file mode 100644
index 0000000..9bd9e87
--- /dev/null
+++ b/test/CMakeLists.txt
@@ -0,0 +1,66 @@
+include(FetchContent)
+
+if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
+ set(CVG_FLAGS -Wno-long-long -fsanitize=address -fsanitize=undefined -fprofile-arcs -ftest-coverage)
+else()
+
+endif()
+
+set(gtest_URL https://github.com/google/googletest.git)
+set(gtest_TAG v1.14.0)
+
+FetchContent_Declare(
+ googletest
+ GIT_REPOSITORY ${gtest_URL}
+ GIT_TAG ${gtest_TAG}
+)
+FetchContent_MakeAvailable(googletest)
+include(GoogleTest)
+
+enable_testing()
+
+set(CMAKE_THREAD_LIBS_INIT "-lpthread")
+set(CMAKE_HAVE_THREADS_LIBRARY 1)
+set(CMAKE_USE_WIN32_THREADS_INIT 0)
+set(CMAKE_USE_PTHREADS_INIT 1)
+set(THREADS_PREFER_PTHREAD_FLAG ON)
+
+function(fud_add_test test_name)
+ set(options NO_OPTIONS)
+ set(oneValueArgs NO_ONE_VALUE_ARGS)
+ set(multiValueArgs SOURCES)
+ cmake_parse_arguments(FUD_ADD_TEST "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
+
+ add_executable(${test_name} test_common.cpp ${FUD_ADD_TEST_SOURCES})
+
+ target_include_directories(${test_name} PUBLIC $<TARGET_PROPERTY:libfud>)
+
+ target_link_libraries(${test_name} PUBLIC GTest::gtest_main libfud)
+
+ target_compile_options(${test_name} PRIVATE ${CVG_FLAGS})
+ target_link_options(${test_name} PRIVATE ${CVG_FLAGS})
+
+ set_target_properties(
+ ${test_name} PROPERTIES
+ CXX_STANDARD 20
+ C_STANDARD 23
+ CXX_EXTENSIONS OFF
+ C_EXTENSIONS OFF
+ CXX_STANDARD_REQUIRED ON)
+
+ gtest_discover_tests(${test_name})
+endfunction()
+
+fud_add_test(test_result SOURCES test_result.cpp)
+# fud_add_test(test_ext_algorithm SOURCES test_algorithm.cpp)
+# fud_add_test(test_ext_array SOURCES
+# test_ext_array.cpp
+# test_ext_unique_array.cpp)
+# fud_add_test(test_ext_utf8 SOURCES
+# test_ext_utf8.cpp)
+# fud_add_test(test_ext_string SOURCES
+# test_ext_string.cpp
+# test_ext_string_cxx.cpp)
+# fud_add_test(test_ext_string_format SOURCES
+# test_ext_string_format.cpp)
+
diff --git a/test/test_common.cpp b/test/test_common.cpp
new file mode 100644
index 0000000..a03c8db
--- /dev/null
+++ b/test/test_common.cpp
@@ -0,0 +1,18 @@
+#include "fud_memory.hpp"
+#include <cstdlib>
+
+namespace fud {
+
+void* fudAlloc(size_t size) {
+ return malloc(size);
+}
+
+void* fudRealloc(void* ptr, size_t size) {
+ return realloc(ptr, size);
+}
+
+void fudFree(void* ptr) {
+ return free(ptr);
+}
+
+} // namespace fud
diff --git a/test/test_result.cpp b/test/test_result.cpp
new file mode 100644
index 0000000..d86a170
--- /dev/null
+++ b/test/test_result.cpp
@@ -0,0 +1,52 @@
+/*
+ * ExtLib
+ * Copyright 2024 Dominick Allen
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "fud_status.hpp"
+#include "fud_result.hpp"
+
+#include "gtest/gtest.h"
+
+namespace fud {
+
+using GResult = Result<uint, FudStatus>;
+
+TEST(ResultTest, OkResult)
+{
+ GResult ok_res = GResult::okay(1);
+
+ ASSERT_TRUE(ok_res.isOkay());
+ ASSERT_FALSE(ok_res.isError());
+ ASSERT_EQ(ok_res.getOkay(), 1);
+}
+
+TEST(ResultTest, ErrResult)
+{
+ GResult err_res = GResult::error(FudStatus::Failure);
+
+ ASSERT_TRUE(err_res.isError());
+ ASSERT_FALSE(err_res.isOkay());
+ auto err = err_res.getError();
+
+ ASSERT_EQ(err, FudStatus::Failure);
+
+ const auto status = FudStatus::InvalidInput;
+ GResult err2 = GResult::error(status);
+ ASSERT_TRUE(err2.isError());
+ ASSERT_EQ(err2.getError(), FudStatus::InvalidInput);
+}
+
+} // namespace fud