From 75b8cfe46b8f50fa98364610b9a1d847bedd91d0 Mon Sep 17 00:00:00 2001 From: Sani7 Date: Sat, 4 Nov 2023 12:51:01 +0100 Subject: [PATCH] Add initial libraries and their respective test code --- .gitignore | 2 + CMakeLists.txt | 26 +++ src/CMakeLists.txt | 21 +++ src/c-libs.h | 7 + src/logger.c | 14 ++ src/logger.h | 124 +++++++++++++ src/safe_array.c | 413 +++++++++++++++++++++++++++++++++++++++++++ src/safe_array.h | 78 ++++++++ tests/CMakeLists.txt | 34 ++++ tests/logger.cpp | 30 ++++ tests/safe_array.cpp | 176 ++++++++++++++++++ tests/test.hpp | 6 + 12 files changed, 931 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 src/CMakeLists.txt create mode 100644 src/c-libs.h create mode 100644 src/logger.c create mode 100644 src/logger.h create mode 100644 src/safe_array.c create mode 100644 src/safe_array.h create mode 100644 tests/CMakeLists.txt create mode 100644 tests/logger.cpp create mode 100644 tests/safe_array.cpp create mode 100644 tests/test.hpp diff --git a/.gitignore b/.gitignore index 259148f..7170c9a 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,5 @@ *.exe *.out *.app + +build/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..7435ace --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,26 @@ +cmake_minimum_required(VERSION 3.5 FATAL_ERROR) + +project(c-libs LANGUAGES CXX C) + +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_EXTENSIONS OFF) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +set(VERSION_MAJOR 1) +set(VERSION_MINOR 0) + +include(GNUInstallDirs) +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY + ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY + ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY + ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}) +set(PROJECT_DIR ${CMAKE_CURRENT_LIST_DIR}) + +# defines targets and sources +add_subdirectory(src) + +# enable testing and define tests +enable_testing() +add_subdirectory(tests) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..31a0bef --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,21 @@ +add_library(safe_array STATIC) + +target_sources(safe_array + PRIVATE + safe_array.c +) + +target_include_directories(safe_array + PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}) + +add_library(logger STATIC) + +target_sources(logger + PRIVATE + logger.c +) + +target_include_directories(logger + PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}) \ No newline at end of file diff --git a/src/c-libs.h b/src/c-libs.h new file mode 100644 index 0000000..760fd60 --- /dev/null +++ b/src/c-libs.h @@ -0,0 +1,7 @@ +#ifndef C_LIBS_H +#define C_LIBS_H + +#include "logger.h" +#include "safe_array.h" + +#endif // C_LIBS_H \ No newline at end of file diff --git a/src/logger.c b/src/logger.c new file mode 100644 index 0000000..a584e7b --- /dev/null +++ b/src/logger.c @@ -0,0 +1,14 @@ +/** + * @file log.c + * @brief Logger implementation + * @author Lorenz C. && Speetjens S. + */ + +#include +#include +#include "logger.h" + +uint32_t logger_get_timestamp(clock_t* start) { + clock_t t1 = clock(); + return (uint32_t)((t1 - *start) * 1000 / CLOCKS_PER_SEC); +} \ No newline at end of file diff --git a/src/logger.h b/src/logger.h new file mode 100644 index 0000000..7fe6c99 --- /dev/null +++ b/src/logger.h @@ -0,0 +1,124 @@ +/** + * @file log.h + * @brief Logger header + * @author Lorenz C. + * + * This logging library provides a simple logging interface with different verbosity levels. + * Each tag can have its own log level. + * Only messages with a log level greater or equal to the log level of the tag and the global log level will be printed. + */ + +#ifndef LOG_H +#define LOG_H + +#include +#include +#include +#include +#include +#include +#include + +#if LOGGER_LEVEL_FATAL +#define LOGGER_LEVEL 5 +#endif +#ifdef LOGGER_LEVEL_CRITICAL +#define LOGGER_LEVEL 4 +#endif +#ifdef LOGGER_LEVEL_WARN +#define LOGGER_LEVEL 3 +#endif +#ifdef LOGGER_LEVEL_INFO +#define LOGGER_LEVEL 2 +#endif +#ifdef LOGGER_LEVEL_ALL +#define LOGGER_LEVEL 1 +#endif + +#ifdef LOGGER_USE_COLOR +#define LOGGER_COLOR 1 +#endif + +#ifndef LOGGER_LEVEL +#define LOGGER_LEVEL 3 +#endif +#ifndef LOGGER_COLOR +#define LOGGER_COLOR 0 +#endif + +#define ANSI_ESC "\x1B" +#define ANSI_CSI "\x9B" +#define ANSI_DCS "\x90" +#define ANSI_OSC "\x9D" + +#define CLEAR_SCREEN "\033c" + +#define CURSOR_RESET ANSI_ESC "[H" +#define CURSOR_UP(n) ANSI_ESC "[" #n "A" +#define CURSOR_DOWN(n) ANSI_ESC "[" #n "B" +#define CURSOR_RIGHT(n) ANSI_ESC "[" #n "C" +#define CURSOR_LEFT(n) ANSI_ESC "[" #n "D" +#define CURSOR_NEXT_N_LINES(n) ANSI_ESC "[" #n "E" +#define CURSOR_PREV_N_LINES(n) ANSI_ESC "[" #n "F" +#define CURSOR_COL(n) ANSI_ESC "[" #n "G" +#define CURSOR_POS ANSI_ESC "[" #n ";" #n "H" +#define CURSOR_SAVE ANSI_ESC "7" +#define CURSOR_RESTORE ANSI_ESC "8" + +#define ERASE_FROM_CURSOR_TO_END ANSI_ESC "[0J" +#define ERASE_FROM_CURSOR_TO_BEGINNING ANSI_ESC "[1J" +#define ERASE_ENTIRE_SCREEN ANSI_ESC "[2J" +#define ERASE_FROM_CURSOR_TO_END_LINE ANSI_ESC "[0K" +#define ERASE_FROM_CURSOR_TO_BEGINNING_LINE ANSI_ESC "[1K" +#define ERASE_ENTIRE_LINE ANSI_ESC "[2K" + +#if LOGGER_COLOR == 1 +#define LOG_RESET_COLOR "\033[0m" +#define LOG_COLOR_F "\033[0;31m" // Red +#define LOG_COLOR_C "\033[0;31m" // Red +#define LOG_COLOR_W "\033[0;33m" // Brown +#define LOG_COLOR_I "\033[0;32m" // Green +#define LOG_COLOR_D LOG_RESET_COLOR // Default +#else +#define LOG_RESET_COLOR +#define LOG_COLOR_F +#define LOG_COLOR_C +#define LOG_COLOR_W +#define LOG_COLOR_I +#define LOG_COLOR_D +#endif + +#if LOGGER_LEVEL <= 1 +#define LOG_DEBUG(tag, fmt, ...) printf(LOG_COLOR_D"[Debug] [%s]: " fmt LOG_RESET_COLOR, tag, ##__VA_ARGS__) +#else +#define LOG_DEBUG(tag, fmt, ...) +#endif +#if LOGGER_LEVEL <= 2 +#define LOG_INFO(tag, fmt, ...) printf(LOG_COLOR_I"[Info] [%s]: " fmt LOG_RESET_COLOR, tag, ##__VA_ARGS__) +#else +#define LOG_INFO(tag, fmt, ...) +#endif +#if LOGGER_LEVEL <= 3 +#define LOG_WARN(tag, fmt, ...) printf(LOG_COLOR_W"[Warning] [%s]: " fmt LOG_RESET_COLOR, tag, ##__VA_ARGS__) +#else +#define LOG_WARN(tag, fmt, ...) +#endif +#if LOGGER_LEVEL <= 4 +#define LOG_CRIT(tag, fmt, ...) printf(LOG_COLOR_C"[Critical] [%s]: " fmt LOG_RESET_COLOR, tag, ##__VA_ARGS__) +#else +#define LOG_CRIT(tag, fmt, ...) +#endif +#if LOGGER_LEVEL <= 4 +#define LOG_FATAL(tag, fmt, ...) printf(LOG_COLOR_F"[Fatal] [%s]: " fmt LOG_RESET_COLOR, tag, ##__VA_ARGS__) +#else +#define LOG_FATAL(tag, fmt, ...) +#endif + +#define LOGGER_INIT clock_t logger_start = clock(); + +/* For internal use only. + * Use the LOG_* macros instead e.g., LOG_DEBUG(TAG, "Debug message"); + */ +uint32_t logger_get_timestamp(clock_t* start); + +#endif /* LOG_H */ \ No newline at end of file diff --git a/src/safe_array.c b/src/safe_array.c new file mode 100644 index 0000000..705dfba --- /dev/null +++ b/src/safe_array.c @@ -0,0 +1,413 @@ +/* + * Copyright © 2023 Sander Speetjens + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the “Software”), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ +#include "safe_array.h" + +/** + * @brief This function generates a safe array + * + * @param size The element size (sizeof(type)) + * @param rows The number of rows in the array + * @return array_t* The pointer to the safe array struct + */ +array_t *array_init(size_t size, size_t rows) +{ + array_t *arr = calloc(sizeof(array_t), 1); + if (!arr) + { + fprintf(stderr, "%s: Failed to allocate memory for array", __func__); + return NULL; + } + arr->data = calloc(size, rows); + if (!arr->data) + { + fprintf(stderr, "%s: Failed to allocate memory for data", __func__); + free(arr); + return NULL; + } + + arr->rows = rows; + arr->size = size; + return arr; +} + +/** + * @brief This function gets an element from the safe array + * + * @param arr The pointer to the safe array struct + * @param index The row index of the element to get + * @return void* The ptr to the element + */ +void *array_get(array_t *arr, size_t index) +{ + if (!arr) + { + fprintf(stderr, "%s: Invalid argument\n", __func__); + return NULL; + } + + if (index >= arr->rows) + { + fprintf(stderr, "%s: index out of bounds: %d > %d\n", __func__, index, arr->rows); + return NULL; + } + + return (char *)arr->data + (index * arr->size); +} + +/** + * @brief This function sets an element in the safe array + * + * @param arr The pointer to the safe array struct + * @param index The row index of the element to set + * @param element The ptr to the value to set + * @return int 0 on success, -1 on failure + */ +int array_set(array_t *arr, size_t index, void *element) +{ + if (!arr || !element) + { + fprintf(stderr, "%s: Invalid argument\n", __func__); + return -1; + } + + if (index >= arr->rows) + { + fprintf(stderr, "%s: index out of bounds: %d > %d\n", __func__, index, arr->rows); + return -1; + } + + void *dest = (char *)arr->data + (index * arr->size); + memcpy(dest, element, arr->size); + + return 0; +} + +/** + * @brief This function frees the safe array, sets the pointer to NULL and the number of rows to 0 + * + * @param arr The pointer to the safe array struct + */ +void array_free(array_t *arr) +{ + if (!arr) + { + fprintf(stderr, "%s: ptr dereferences to NULL\n", __func__); + } + free(arr->data); + free(arr); +} + +/** + * @brief This function generates a 2D safe array + * + * @param size The element size (sizeof(type)) + * @param rows The number of rows in the array + * @param cols The number of columns in the array + * @return Array_2D_t* The pointer to the 2D array struct + */ +array_2d_t *array_2d_init(size_t size, size_t rows, size_t cols) +{ + array_2d_t *arr = calloc(sizeof(array_2d_t), 1); + if (!arr) + { + fprintf(stderr, "%s: Failed to allocate memory for array", __func__); + return NULL; + } + arr->data = calloc(size, rows * cols); + if (!arr->data) + { + fprintf(stderr, "%s: Failed to allocate memory for data", __func__); + free(arr); + return NULL; + } + + arr->rows = rows; + arr->cols = cols; + arr->size = size; + return arr; +} + +/** + * @brief This function gets an element from the 2D safe array + * + * @param x The pointer to the 2D array struct + * @param row The row index of the element to get + * @param col The column index of the element to get + * @return void* The ptr to the element + */ +void *array_2d_get(array_2d_t *x, size_t row, size_t col) +{ + if (row >= x->rows) + { + fprintf(stderr, "%s: row index out of bounds: %d > %d\n", __func__, row, x->rows); + return 0; + } + if (col >= x->cols) + { + fprintf(stderr, "%s: col index out of bounds: %d > %d\n", __func__, col, x->cols); + return 0; + } + + return (char *)x->data + (row * x->size) + (col * x->cols * x->size); +} + +/** + * @brief This function sets an element in the 2D safe array + * + * @param x The pointer to the 2D array struct + * @param row The row index of the element to set + * @param col The column index of the element to set + * @param val The ptr to the value to set + */ +int array_2d_set(array_2d_t *x, size_t row, size_t col, void *val) +{ + if (!x || !val) + { + fprintf(stderr, "%s: Invalid argument\n", __func__); + return -1; + } + + if (row >= x->rows) + { + fprintf(stderr, "%s: row index out of bounds: %d > %d\n", __func__, row, x->rows); + return -1; + } + if (col >= x->cols) + { + fprintf(stderr, "%s: col index out of bounds: %d > %d\n", __func__, col, x->cols); + return -1; + } + + void *dest = (char *)x->data + (row * x->size) + (col * x->cols * x->size); + memcpy(dest, val, x->size); + + return 0; +} + +/** + * @brief This function frees the 2D safe array, sets the pointer to NULL and the number of rows and columns to 0 + * + * @param x The pointer to the 2D array struct + */ +void array_2d_free(array_2d_t *x) +{ + if (!x) + { + fprintf(stderr, "%s: ptr dereferences to NULL\n", __func__); + } + free(x->data); + free(x); +} + +/** + * @brief This function generates a complex safe array + * + * @param rows The number of rows in the array + * @param size The element size (sizeof(type)) + * @return array_c_s_t* The pointer to the complex array struct + */ +array_c_s_t *array_c_s_init(size_t rows, size_t size) +{ + array_c_s_t *x = calloc(sizeof(array_c_s_t), 1); + if (!x) + { + fprintf(stderr, "%s: Failed to allocate memory for array", __func__); + return NULL; + } + x->real = calloc(size, rows); + if (!x->real) + { + fprintf(stderr, "%s: Failed to allocate memory for data", __func__); + free(x); + return NULL; + } + x->imaginary = calloc(size, rows); + if (!x->imaginary) + { + fprintf(stderr, "%s: Failed to allocate memory for data", __func__); + free(x->real); + free(x); + return NULL; + } + + x->rows = rows; + x->size = size; + return x; +} + +/** + * @brief This function gets an imaginary element from the complex safe array + * + * @param x The pointer to the complex array struct + * @param row The row index of the element to get + * @return void* The ptr to the imaginary element + */ +void* array_c_get_imag(array_c_s_t *x, size_t row) +{ + if (row >= x->rows) + { + fprintf(stderr, "%s: index out of bounds: %d > %d\n", __func__, row, x->rows); + return NULL; + } + + return (char *)x->imaginary + (row * x->size); +} + +/** + * @brief This function gets a real element from the complex safe array + * + * @param x The pointer to the complex array struct + * @param row The row index of the element to get + * @return void* The ptr to the real element + */ +void* array_c_get_real(array_c_s_t *x, size_t row) +{ + if (row >= x->rows) + { + fprintf(stderr, "%s: index out of bounds: %d > %d\n", __func__, row, x->rows); + return NULL; + } + + return (char *)x->real + (row * x->size); +} + +/** + * @brief This function sets an element in the complex safe array + * + * @param x The pointer to the complex array struct + * @param row The row index of the element to set + * @param real The ptr to the real value of the element + * @param imaginary The ptr to the imaginary value of the element + */ +int array_c_s_set(array_c_s_t *x, size_t row, void *real, void *imaginary) +{ + if (row >= x->rows) + { + fprintf(stderr, "%s: index out of bounds: %d > %d\n", __func__, row, x->rows); + return -1; + } + + memcpy((char *)x->real + (row * x->size), real, x->size); + memcpy((char *)x->imaginary + (row * x->size), imaginary, x->size); + return 0; +} + +/** + * @brief This function frees the complex safe array, sets the pointer to NULL and the number of rows to 0 + * + * @param x The pointer to the complex array struct + */ +void array_c_s_free(array_c_s_t *x) +{ + if (!x) + { + fprintf(stderr, "%s: ptr dereferences to NULL\n", __func__); + } + free(x->real); + free(x->imaginary); + free(x); +} + +/** + * @brief This function generates a complex safe array in polar form + * + * @param x The pointer to the complex array struct + * @param rows The number of complex elements in the array + * @param init_val The initial value of the array + * @return int The number of elements in the array + */ +array_c_p_t *array_c_p_init(size_t rows, size_t size) +{ + array_c_p_t* x = calloc(sizeof(array_c_p_t), 1); + if (!x) + { + fprintf(stderr, "%s: Failed to allocate memory for array", __func__); + return NULL; + } + x->magnitude = (double *)calloc(rows, sizeof(double)); + if (!x->magnitude) + { + fprintf(stderr, "%s: Failed to allocate memory for data", __func__); + free(x); + return NULL; + } + x->phase = (double *)calloc(rows, sizeof(double)); + if (!x->phase) + { + fprintf(stderr, "%s: Failed to allocate memory for data", __func__); + free(x->magnitude); + free(x); + return NULL; + } + x->size = size; + x->rows = rows; + + return x; +} + +void* array_c_get_mag(array_c_p_t *x, size_t row) +{ + if (row >= x->rows) + { + fprintf(stderr, "%s: index out of bounds: %d > %d\n", __func__, row, x->rows); + return 0; + } + + return (char *)x->magnitude + (row * x->size); +} + +void* array_c_get_phase(array_c_p_t *x, size_t row) +{ + if (row >= x->rows) + { + fprintf(stderr, "%s: index out of bounds: %d > %d\n", __func__, row, x->rows); + return 0; + } + + return (char *)x->phase + (row * x->size); +} + +/** + * @brief This function sets an element in the complex safe array + * + * @param x The pointer to the complex array struct + * @param row The row index of the element to set + * @param magnitude The magnitude value of the element + * @param phase The phase value of the element + */ +int array_c_p_set(array_c_p_t *x, size_t row, void* magnitude, void* phase) +{ + if (row >= x->rows) + { + fprintf(stderr, "%s: index out of bounds: %d > %d\n", __func__, row, x->rows); + return -1; + } + + memcpy((char *)x->magnitude + (row * x->size), magnitude, x->size); + memcpy((char *)x->phase + (row * x->size), phase, x->size); + return 0; +} + +/** + * @brief This function frees the complex safe array, sets the pointer to NULL and the number of rows to 0 + * + * @param x The pointer to the complex array struct + */ +void array_c_p_free(array_c_p_t *x) +{ + free(x->magnitude); + free(x->phase); + x->magnitude = NULL; + x->phase = NULL; + x->rows = 0; +} \ No newline at end of file diff --git a/src/safe_array.h b/src/safe_array.h new file mode 100644 index 0000000..5998a35 --- /dev/null +++ b/src/safe_array.h @@ -0,0 +1,78 @@ +/* + * Copyright © 2023 Sander Speetjens + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the “Software”), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ +#pragma once +#include +#include +#include +#include + +#define ARRAY_GET(arr, index, type) (*(type *)array_get(arr, index)) +#define ARRAY_2D_GET(arr, row, col, type) (*(type *)array_2d_get(arr, row, col)) +#define ARRAY_C_GET_IMAG(arr, row, type) (*(type *)array_c_get_imag(arr, row)) +#define ARRAY_C_GET_REAL(arr, row, type) (*(type *)array_c_get_real(arr, row)) +#define ARRAY_C_GET_MAG(arr, row, type) (*(type *)array_c_get_mag(arr, row)) +#define ARRAY_C_GET_PHASE(arr, row, type) (*(type *)array_c_get_phase(arr, row)) + +typedef struct array_s +{ + void *data; + size_t rows; + size_t size; +} array_t; + +typedef struct array_2d_s +{ + void *data; + size_t rows; + size_t cols; + size_t size; +} array_2d_t; + +typedef struct array_c_s +{ + void *real; + void *imaginary; + size_t rows; + size_t size; +} array_c_s_t; + +typedef struct array_c_p_s +{ + void *magnitude; + void *phase; + size_t rows; + size_t size; +} array_c_p_t; + +// Array.c +array_t *array_init(size_t size, size_t rows); +void *array_get(array_t *arr, size_t index); +int array_set(array_t *arr, size_t index, void *element); +void array_free(array_t *arr); + +array_2d_t *array_2d_init(size_t size, size_t rows, size_t cols); +void *array_2d_get(array_2d_t *x, size_t row, size_t col); +int array_2d_set(array_2d_t *x, size_t row, size_t col, void *val); +void array_2d_free(array_2d_t *x); + +array_c_s_t *array_c_s_init(size_t rows, size_t size); +void* array_c_get_real(array_c_s_t *x, size_t row); +void* array_c_get_imag(array_c_s_t *x, size_t row); +int array_c_s_set(array_c_s_t *x, size_t row, void *real, void *imaginary); +void array_c_s_free(array_c_s_t *x); + +array_c_p_t *array_c_p_init(size_t rows, size_t size); +void *array_c_get_mag(array_c_p_t *x, size_t row); +void *array_c_get_phase(array_c_p_t *x, size_t row); +int array_c_p_set(array_c_p_t *x, size_t row, void* magnitude, void* phase); +void array_c_p_free(array_c_p_t *x); \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..39c2dc2 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,34 @@ +# Third Party +include_directories(${GTEST_INCLUDE_DIR}) + +link_directories(${GTEST_LIB_DIR}) + +find_package(GTest REQUIRED) + +# tests +file(GLOB_RECURSE TEST_SOURCES "*.cpp" "*.hpp") + +add_executable(tests ${TEST_SOURCES}) +target_link_libraries(tests ${LIBS}) + +target_compile_options(tests PRIVATE $<$: + -Wall -Wextra -pedantic-errors -Wconversion -Wsign-conversion + >) +target_link_libraries(tests + PRIVATE + logger + safe_array + gtest + GTest::gtest_main +) + +target_include_directories(tests + PUBLIC + ${CMAKE_MAIN_SOURCE_DIR}/external/** + ${CMAKE_CURRENT_LIST_DIR} + ${CMAKE_MAIN_SOURCE_DIR}/src/** + ${PROJECT_BINARY_DIR} +) + +include(GoogleTest) +gtest_discover_tests(tests) diff --git a/tests/logger.cpp b/tests/logger.cpp new file mode 100644 index 0000000..2c2c887 --- /dev/null +++ b/tests/logger.cpp @@ -0,0 +1,30 @@ +#define LOGGER_LEVEL_ALL +#include "test.hpp" + +TEST(LOGGER, logger) +{ + testing::internal::CaptureStdout(); + LOG_DEBUG("LOGGER", "Debug message\n"); + std::string output = testing::internal::GetCapturedStdout(); + EXPECT_EQ(output, "[Debug] [LOGGER]: Debug message\n"); + + testing::internal::CaptureStdout(); + LOG_INFO("LOGGER", "Info message\n"); + output = testing::internal::GetCapturedStdout(); + EXPECT_EQ(output, "[Info] [LOGGER]: Info message\n"); + + testing::internal::CaptureStdout(); + LOG_WARN("LOGGER", "Warning message\n"); + output = testing::internal::GetCapturedStdout(); + EXPECT_EQ(output, "[Warning] [LOGGER]: Warning message\n"); + + testing::internal::CaptureStdout(); + LOG_CRIT("LOGGER", "Critical message\n"); + output = testing::internal::GetCapturedStdout(); + EXPECT_EQ(output, "[Critical] [LOGGER]: Critical message\n"); + + testing::internal::CaptureStdout(); + LOG_FATAL("LOGGER", "Fatal message\n"); + output = testing::internal::GetCapturedStdout(); + EXPECT_EQ(output, "[Fatal] [LOGGER]: Fatal message\n"); +} \ No newline at end of file diff --git a/tests/safe_array.cpp b/tests/safe_array.cpp new file mode 100644 index 0000000..caec5f1 --- /dev/null +++ b/tests/safe_array.cpp @@ -0,0 +1,176 @@ +#include "test.hpp" + +TEST(array, array_init) +{ + array_t* x = array_init(sizeof(double), 2); + EXPECT_EQ(x->rows, 2); + EXPECT_EQ(ARRAY_GET(x, 0, double), 0); + EXPECT_EQ(ARRAY_GET(x, 0, double), 0); + array_free(x); +} + +TEST(array, Get_Set_array) +{ + array_t* x = array_init(sizeof(double), 2); + + double _val = 1; + array_set(x, 0, &_val); + _val = 2; + array_set(x, 1, &_val); + EXPECT_EQ(ARRAY_GET(x, 0, double), 1); + EXPECT_EQ(ARRAY_GET(x, 1, double), 2); + + testing::internal::CaptureStderr(); + _val = 3; + array_set(x, 2, &_val); + std::string output = testing::internal::GetCapturedStderr(); + EXPECT_EQ(output, "array_set: index out of bounds: 2 > 2\n"); + + testing::internal::CaptureStderr(); + array_get(x, 2); + output = testing::internal::GetCapturedStderr(); + EXPECT_EQ(output, "array_get: index out of bounds: 2 > 2\n"); + + array_free(x); +} + +TEST(array, array_2d_init) +{ + array_2d_t* x = array_2d_init(sizeof(double), 2, 2); + EXPECT_EQ(x->rows, 2); + EXPECT_EQ(x->cols, 2); + EXPECT_EQ(ARRAY_2D_GET(x, 0, 0, double), 0); + EXPECT_EQ(ARRAY_2D_GET(x, 0, 0, double), 0); + EXPECT_EQ(ARRAY_2D_GET(x, 0, 0, double), 0); + EXPECT_EQ(ARRAY_2D_GET(x, 0, 0, double), 0); + array_2d_free(x); +} + +TEST(array, Set_Get_array_2d) +{ + array_2d_t* x = array_2d_init(sizeof(double), 2, 2); + + double _val = 1; + array_2d_set(x, 0, 0, &_val); + _val = 2; + array_2d_set(x, 0, 1, &_val); + _val = 3; + array_2d_set(x, 1, 0, &_val); + _val = 4; + array_2d_set(x, 1, 1, &_val); + + testing::internal::CaptureStderr(); + array_2d_set(x, 2, 0, &_val); + std::string output = testing::internal::GetCapturedStderr(); + EXPECT_EQ(output, "array_2d_set: row index out of bounds: 2 > 2\n"); + + testing::internal::CaptureStderr(); + array_2d_set(x, 0, 2, &_val); + output = testing::internal::GetCapturedStderr(); + EXPECT_EQ(output, "array_2d_set: col index out of bounds: 2 > 2\n"); + + EXPECT_EQ(ARRAY_2D_GET(x, 0, 0, double), 1); + EXPECT_EQ(ARRAY_2D_GET(x, 0, 1, double), 2); + EXPECT_EQ(ARRAY_2D_GET(x, 1, 0, double), 3); + EXPECT_EQ(ARRAY_2D_GET(x, 1, 1, double), 4); + + testing::internal::CaptureStderr(); + array_2d_get(x, 2, 0); + output = testing::internal::GetCapturedStderr(); + EXPECT_EQ(output, "array_2d_get: row index out of bounds: 2 > 2\n"); + + testing::internal::CaptureStderr(); + array_2d_get(x, 0, 2); + output = testing::internal::GetCapturedStderr(); + EXPECT_EQ(output, "array_2d_get: col index out of bounds: 2 > 2\n"); + + array_2d_free(x); +} + +TEST(array, array_c_s_init) +{ + array_c_s_t* x = array_c_s_init(2, sizeof(double)); + EXPECT_EQ(x->rows, 2); + EXPECT_EQ(ARRAY_C_GET_REAL(x, 0, double), 0); + EXPECT_EQ(ARRAY_C_GET_REAL(x, 1, double), 0); + EXPECT_EQ(ARRAY_C_GET_IMAG(x, 0, double), 0); + EXPECT_EQ(ARRAY_C_GET_IMAG(x, 1, double), 0); + array_c_s_free(x); +} + +TEST(array, Set_Get_array_C_S) +{ + array_c_s_t* x = array_c_s_init(2, sizeof(double)); + + double _real = 1; + double _imaginary = 2; + array_c_s_set(x, 0, &_real, &_imaginary); + _real = 3; + _imaginary = 4; + array_c_s_set(x, 1, &_real, &_imaginary); + + testing::internal::CaptureStderr(); + array_c_s_set(x, 2, &_real, &_imaginary); + std::string output = testing::internal::GetCapturedStderr(); + EXPECT_EQ(output, "array_c_s_set: index out of bounds: 2 > 2\n"); + + EXPECT_EQ(ARRAY_C_GET_REAL(x, 0, double), 1); + EXPECT_EQ(ARRAY_C_GET_IMAG(x, 0, double), 2); + EXPECT_EQ(ARRAY_C_GET_REAL(x, 1, double), 3); + EXPECT_EQ(ARRAY_C_GET_IMAG(x, 1, double), 4); + + testing::internal::CaptureStderr(); + array_c_get_real(x, 2); + output = testing::internal::GetCapturedStderr(); + EXPECT_EQ(output, "array_c_get_real: index out of bounds: 2 > 2\n"); + testing::internal::CaptureStderr(); + array_c_get_imag(x, 2); + output = testing::internal::GetCapturedStderr(); + EXPECT_EQ(output, "array_c_get_imag: index out of bounds: 2 > 2\n"); + + array_c_s_free(x); +} + +TEST(array, array_c_p_init) +{ + array_c_p_t* x = array_c_p_init(2, sizeof(double)); + EXPECT_EQ(x->rows, 2); + EXPECT_EQ(ARRAY_C_GET_MAG(x, 0, double), 0); + EXPECT_EQ(ARRAY_C_GET_MAG(x, 1, double), 0); + EXPECT_EQ(ARRAY_C_GET_PHASE(x, 0, double), 0); + EXPECT_EQ(ARRAY_C_GET_PHASE(x, 1, double), 0); + array_c_p_free(x); +} + +TEST(array, Set_Get_array_C_P) +{ + array_c_p_t* x = array_c_p_init(2, sizeof(double)); + + double _mag = 1; + double _phase = 2; + array_c_p_set(x, 0, &_mag, &_phase); + _mag = 3; + _phase = 4; + array_c_p_set(x, 1, &_mag, &_phase); + + testing::internal::CaptureStderr(); + array_c_p_set(x, 2, &_mag, &_phase); + std::string output = testing::internal::GetCapturedStderr(); + EXPECT_EQ(output, "array_c_p_set: index out of bounds: 2 > 2\n"); + + EXPECT_EQ(ARRAY_C_GET_MAG(x, 0, double), 1); + EXPECT_EQ(ARRAY_C_GET_PHASE(x, 0, double), 2); + EXPECT_EQ(ARRAY_C_GET_MAG(x, 1, double), 3); + EXPECT_EQ(ARRAY_C_GET_PHASE(x, 1, double), 4); + + testing::internal::CaptureStderr(); + array_c_get_phase(x, 2); + output = testing::internal::GetCapturedStderr(); + EXPECT_EQ(output, "array_c_get_phase: index out of bounds: 2 > 2\n"); + testing::internal::CaptureStderr(); + array_c_get_mag(x, 2); + output = testing::internal::GetCapturedStderr(); + EXPECT_EQ(output, "array_c_get_mag: index out of bounds: 2 > 2\n"); + + array_c_p_free(x); +} diff --git a/tests/test.hpp b/tests/test.hpp new file mode 100644 index 0000000..5c020a9 --- /dev/null +++ b/tests/test.hpp @@ -0,0 +1,6 @@ +#include + +extern "C" +{ +#include "c-libs.h" +} \ No newline at end of file