migrate utils tests - relates to #342

Wed, 20 Dec 2023 17:57:18 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 20 Dec 2023 17:57:18 +0100
changeset 767
d31f4d4075dc
parent 766
e59b76889f00
child 768
0e1cf2cd500e

migrate utils tests - relates to #342

src/cx/test.h file | annotate | diff | comparison | revisions
tests/.clang-tidy file | annotate | diff | comparison | revisions
tests/CMakeLists.txt file | annotate | diff | comparison | revisions
tests/Makefile file | annotate | diff | comparison | revisions
tests/test_utils.c file | annotate | diff | comparison | revisions
tests/test_utils.cpp file | annotate | diff | comparison | revisions
tests/test_utils.h file | annotate | diff | comparison | revisions
tests/ucxtest.c file | annotate | diff | comparison | revisions
     1.1 --- a/src/cx/test.h	Wed Dec 20 16:46:14 2023 +0100
     1.2 +++ b/src/cx/test.h	Wed Dec 20 17:57:18 2023 +0100
     1.3 @@ -48,16 +48,13 @@
     1.4   * 
     1.5   * CX_TEST(function_name) {
     1.6   *   // memory allocation and other stuff here
     1.7 - *   #CX_TEST_BEGIN
     1.8 - *   // tests with CX_TEST_ASSERT() and/or
     1.9 - *   // calls with CX_TEST_CALL_SUBROUTINE() here
    1.10 - *   #CX_TEST_END
    1.11 + *   #CX_TEST_DO {
    1.12 + *     // tests with CX_TEST_ASSERT() and/or
    1.13 + *     // calls with CX_TEST_CALL_SUBROUTINE() here
    1.14 + *   }
    1.15   *   // cleanup of memory here
    1.16   * }
    1.17   * </pre>
    1.18 - *
    1.19 - * @remark if a test fails, execution continues at the
    1.20 - * #CX_TEST_END macro! So make sure every necessary cleanup happens afterwards.
    1.21   * 
    1.22   * @attention Do not call own functions within a test, that use
    1.23   * CX_TEST_ASSERT() macros and are not defined by using CX_TEST_SUBROUTINE().
    1.24 @@ -252,17 +249,18 @@
    1.25  #define CX_TEST(name) void name(CxTestSuite* _suite_,void *_output_, cx_write_func _writefnc_)
    1.26  
    1.27  /**
    1.28 - * Marks the begin of a test.
    1.29 - * <b>Note:</b> Any CX_TEST_ASSERT() calls must be performed <b>after</b>
    1.30 - * #CX_TEST_BEGIN.
    1.31 - * 
    1.32 - * @see #CX_TEST_END
    1.33 + * Defines the scope of a test.
    1.34 + * @attention Any CX_TEST_ASSERT() calls must be performed in scope of
    1.35 + * #CX_TEST_DO.
    1.36   */
    1.37 -#define CX_TEST_BEGIN _writefnc_("Running ", 1, 8, _output_);\
    1.38 +#define CX_TEST_DO _writefnc_("Running ", 1, 8, _output_);\
    1.39          _writefnc_(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\
    1.40          _writefnc_("... ", 1, 4, _output_);\
    1.41 -        jmp_buf _env_; \
    1.42 -        if (!setjmp(_env_)) {
    1.43 +        jmp_buf _env_;\
    1.44 +        for (unsigned int _cx_test_loop_ = 0 ;\
    1.45 +             _cx_test_loop_ == 0 && !setjmp(_env_);\
    1.46 +             _writefnc_("success.\n", 1, 9, _output_),\
    1.47 +             _suite_->success++, _cx_test_loop_++)
    1.48  
    1.49  /**
    1.50   * Checks a test assertion.
    1.51 @@ -272,11 +270,22 @@
    1.52   * @param condition the condition to check
    1.53   * @param message the message that shall be printed out on failure
    1.54   */
    1.55 -#define CX_TEST_ASSERT(condition,message) if (!(condition)) { \
    1.56 -        _writefnc_(message".\n", 1, 2+strlen(message), _output_); \
    1.57 +#define CX_TEST_ASSERTM(condition,message) if (!(condition)) { \
    1.58 +        char const* _assert_msg_ = message; \
    1.59 +        _writefnc_(_assert_msg_, 1, strlen(_assert_msg_), _output_); \
    1.60 +        _writefnc_(".\n", 1, 2, _output_); \
    1.61          _suite_->failure++; \
    1.62          longjmp(_env_, 1);\
    1.63 -    }
    1.64 +    } (void) 0
    1.65 +
    1.66 +/**
    1.67 + * Checks a test assertion.
    1.68 + * If the assertion is correct, the test carries on. If the assertion is not
    1.69 + * correct, the specified message (terminated by a dot and a line break) is
    1.70 + * written to the test suites output stream.
    1.71 + * @param condition the condition to check
    1.72 + */
    1.73 +#define CX_TEST_ASSERT(condition) CX_TEST_ASSERTM(condition, #condition " failed")
    1.74  
    1.75  /**
    1.76   * Macro for a test subroutine function header.
    1.77 @@ -298,8 +307,7 @@
    1.78   * Subroutines declared with CX_TEST_SUBROUTINE() can be called by using this
    1.79   * macro.
    1.80   * 
    1.81 - * <b>Note:</b> You may <b>only</b> call subroutines within a #CX_TEST_BEGIN-
    1.82 - * #CX_TEST_END-block.
    1.83 + * @remark You may <b>only</b> call subroutines within a #CX_TEST_DO block.
    1.84   * 
    1.85   * @param name the name of the subroutine
    1.86   * @param ... the argument list
    1.87 @@ -309,15 +317,6 @@
    1.88  #define CX_TEST_CALL_SUBROUTINE(name,...) \
    1.89          name(_suite_,_output_,_writefnc_,_env_,__VA_ARGS__);
    1.90  
    1.91 -/**
    1.92 - * Marks the end of a test.
    1.93 - * <b>Note:</b> Any CX_TEST_ASSERT() calls must be performed <b>before</b>
    1.94 - * #CX_TEST_END.
    1.95 - * 
    1.96 - * @see #CX_TEST_BEGIN
    1.97 - */
    1.98 -#define CX_TEST_END _writefnc_("success.\n", 1, 9, _output_); _suite_->success++;}
    1.99 -
   1.100  #ifdef	__cplusplus
   1.101  }
   1.102  #endif
     2.1 --- a/tests/.clang-tidy	Wed Dec 20 16:46:14 2023 +0100
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,2 +0,0 @@
     2.4 -# Disable static initialization warning for test code
     2.5 -Checks: '-cert-err58-cpp'
     3.1 --- a/tests/CMakeLists.txt	Wed Dec 20 16:46:14 2023 +0100
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,41 +0,0 @@
     3.4 -cmake_minimum_required(VERSION 3.14)
     3.5 -project(ucxtest VERSION 3.0 DESCRIPTION "UAP Common Extensions - Tests")
     3.6 -
     3.7 -# Configuration
     3.8 -cmake_policy(SET CMP0077 NEW)
     3.9 -set(CMAKE_CXX_STANDARD 17)
    3.10 -enable_testing()
    3.11 -
    3.12 -# Load Google Test Framework
    3.13 -option(INSTALL_GTEST "By default googletest shall not be installed." OFF)
    3.14 -option(BUILD_GMOCK "In this project we do not need gmock." OFF)
    3.15 -include(FetchContent)
    3.16 -FetchContent_Declare(
    3.17 -        googletest
    3.18 -        GIT_REPOSITORY https://github.com/google/googletest.git
    3.19 -        GIT_TAG e2239ee6043f73722e7aa812a459f54a28552929 # release 1.11.0
    3.20 -)
    3.21 -# For Windows: Prevent overriding the parent project's compiler/linker settings
    3.22 -set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
    3.23 -FetchContent_MakeAvailable(googletest)
    3.24 -include(GoogleTest)
    3.25 -message(STATUS "Google Test made available")
    3.26 -
    3.27 -add_executable(ucxtest
    3.28 -        test_utils.cpp
    3.29 -        test_allocator.cpp
    3.30 -        test_compare.cpp
    3.31 -        test_string.cpp
    3.32 -        test_buffer.cpp
    3.33 -        test_list.cpp
    3.34 -        test_hash_key.cpp
    3.35 -        test_map.cpp
    3.36 -        test_map_generics.c
    3.37 -        test_mempool.cpp
    3.38 -        test_printf.cpp
    3.39 -        selftest.cpp
    3.40 -        util_allocator.cpp
    3.41 -        )
    3.42 -target_include_directories(ucxtest PRIVATE ${CMAKE_SOURCE_DIR}/../src)
    3.43 -target_link_libraries(ucxtest ${CMAKE_BINARY_DIR}/../libucx_static${STLIB_EXT} gtest_main)
    3.44 -gtest_discover_tests(ucxtest)
     4.1 --- a/tests/Makefile	Wed Dec 20 16:46:14 2023 +0100
     4.2 +++ b/tests/Makefile	Wed Dec 20 17:57:18 2023 +0100
     4.3 @@ -27,14 +27,14 @@
     4.4  
     4.5  TEST_DIR=$(build_dir)/tests
     4.6  
     4.7 -SRC = test_utils.c ucxtest.o
     4.8 +SRC = test_utils.c ucxtest.c
     4.9  
    4.10  OBJ_EXT=.o
    4.11  OBJ=$(SRC:%.c=$(TEST_DIR)/%$(OBJ_EXT))
    4.12  
    4.13  all: $(TEST_DIR) $(TEST_DIR)/ucxtest
    4.14  
    4.15 -$(TEST_DIR)/ucxtest: $(build_dir)/libucx_static.a $(OBJ)
    4.16 +$(TEST_DIR)/ucxtest: $(OBJ) $(build_dir)/libucx_static.a
    4.17  	$(CC) -o $@ $+
    4.18  
    4.19  $(build_dir)/libucx_static.a:
    4.20 @@ -53,12 +53,13 @@
    4.21  	@echo "Compiling $<"
    4.22  	$(CC) -o $@ $(CFLAGS) -c $<
    4.23  
    4.24 -$(TEST_DIR)/test_utils$(OBJ_EXT): test_utils.c test_utils.h \
    4.25 - ../src/cx/test.h
    4.26 +$(TEST_DIR)/test_utils$(OBJ_EXT): test_utils.c ../src/cx/test.h \
    4.27 + ../src/cx/utils.h ../src/cx/common.h ../src/cx/buffer.h \
    4.28 + ../src/cx/allocator.h ../src/szmul.c
    4.29  	@echo "Compiling $<"
    4.30  	$(CC) -o $@ $(CFLAGS) -c $<
    4.31  
    4.32 -$(TEST_DIR)/ucxtest$(OBJ_EXT): ucxtest.c test_utils.h ../src/cx/test.h
    4.33 +$(TEST_DIR)/ucxtest$(OBJ_EXT): ucxtest.c ../src/cx/test.h
    4.34  	@echo "Compiling $<"
    4.35  	$(CC) -o $@ $(CFLAGS) -c $<
    4.36  
     5.1 --- a/tests/test_utils.c	Wed Dec 20 16:46:14 2023 +0100
     5.2 +++ b/tests/test_utils.c	Wed Dec 20 17:57:18 2023 +0100
     5.3 @@ -26,11 +26,230 @@
     5.4   * POSSIBILITY OF SUCH DAMAGE.
     5.5   */
     5.6  
     5.7 -#include "test_utils.h"
     5.8 +#include "cx/test.h"
     5.9 +
    5.10 +#include "cx/utils.h"
    5.11 +#include "cx/buffer.h"
    5.12 +
    5.13 +CX_TEST(test_stream_bncopy) {
    5.14 +    CxBuffer source, target;
    5.15 +    char sbuf[32], tbuf[32];
    5.16 +    memset(tbuf, 0, 32);
    5.17 +    cxBufferInit(&source, sbuf, 32, NULL, 0);
    5.18 +    cxBufferInit(&target, tbuf, 32, NULL, 0);
    5.19 +    cxBufferPutString(&source, "This is a stream copy test.");
    5.20 +    cxBufferSeek(&source, 0, SEEK_SET);
    5.21 +    char tmp[4];
    5.22 +
    5.23 +    CX_TEST_DO {
    5.24 +        size_t result = cx_stream_bncopy(&source, &target,
    5.25 +                                         (cx_read_func) cxBufferRead,
    5.26 +                                         (cx_write_func) cxBufferWrite,
    5.27 +                                         tmp, 4, 20);
    5.28 +        CX_TEST_ASSERT(result == 20);
    5.29 +        CX_TEST_ASSERT(target.size == 20);
    5.30 +        CX_TEST_ASSERT(strcmp("This is a stream cop\0", tbuf) == 0);
    5.31 +
    5.32 +        result = cx_stream_bcopy(&source, &target,
    5.33 +                                 (cx_read_func) cxBufferRead,
    5.34 +                                 (cx_write_func) cxBufferWrite,
    5.35 +                                 NULL, 16);
    5.36 +
    5.37 +        CX_TEST_ASSERT(result == 7);
    5.38 +        CX_TEST_ASSERT(target.size == 27);
    5.39 +        CX_TEST_ASSERT(strcmp("This is a stream copy test.\0", tbuf) == 0);
    5.40 +    }
    5.41 +
    5.42 +    cxBufferDestroy(&source);
    5.43 +    cxBufferDestroy(&target);
    5.44 +}
    5.45 +
    5.46 +CX_TEST(test_stream_ncopy) {
    5.47 +    CxBuffer source, target;
    5.48 +    char sbuf[32], tbuf[32];
    5.49 +    memset(tbuf, 0, 32);
    5.50 +    cxBufferInit(&source, sbuf, 32, NULL, 0);
    5.51 +    cxBufferInit(&target, tbuf, 32, NULL, 0);
    5.52 +    cxBufferPutString(&source, "This is a stream copy test.");
    5.53 +    cxBufferSeek(&source, 0, SEEK_SET);
    5.54 +
    5.55 +    CX_TEST_DO {
    5.56 +        size_t result = cx_stream_ncopy(&source, &target,
    5.57 +                                        (cx_read_func) cxBufferRead,
    5.58 +                                        (cx_write_func) cxBufferWrite,
    5.59 +                                        20);
    5.60 +        CX_TEST_ASSERT(result == 20);
    5.61 +        CX_TEST_ASSERT(target.size == 20);
    5.62 +        CX_TEST_ASSERT(strcmp("This is a stream cop\0", tbuf) == 0);
    5.63 +
    5.64 +        result = cx_stream_copy(&source, &target,
    5.65 +                                (cx_read_func) cxBufferRead,
    5.66 +                                (cx_write_func) cxBufferWrite);
    5.67 +
    5.68 +        CX_TEST_ASSERT(result == 7);
    5.69 +        CX_TEST_ASSERT(target.size == 27);
    5.70 +        CX_TEST_ASSERT(strcmp("This is a stream copy test.\0", tbuf) == 0);
    5.71 +    }
    5.72 +
    5.73 +    cxBufferDestroy(&source);
    5.74 +    cxBufferDestroy(&target);
    5.75 +}
    5.76 +
    5.77 +CX_TEST(test_forn) {
    5.78 +    unsigned j;
    5.79 +    j = 0;
    5.80 +    CX_TEST_DO {
    5.81 +        cx_for_n(i, 50) {
    5.82 +            CX_TEST_ASSERT(i == j);
    5.83 +            j++;
    5.84 +        }
    5.85 +    }
    5.86 +}
    5.87 +
    5.88 +CX_TEST(test_swap_ptr) {
    5.89 +    int i = 5;
    5.90 +    int j = 8;
    5.91 +    int *ip = &i;
    5.92 +    int *jp = &j;
    5.93 +    CX_TEST_DO {
    5.94 +        cx_swap_ptr(ip, jp);
    5.95 +        CX_TEST_ASSERT(ip == &j);
    5.96 +        CX_TEST_ASSERT(jp == &i);
    5.97 +    }
    5.98 +}
    5.99 +
   5.100 +CX_TEST(test_szmul) {
   5.101 +    size_t r;
   5.102 +    int e;
   5.103 +
   5.104 +    CX_TEST_DO {
   5.105 +        e = cx_szmul(5, 7, &r);
   5.106 +        CX_TEST_ASSERT(e == 0);
   5.107 +        CX_TEST_ASSERT(r == 35);
   5.108 +
   5.109 +        size_t s = SIZE_MAX & ~3;
   5.110 +
   5.111 +        e = cx_szmul(s / 4, 2, &r);
   5.112 +        CX_TEST_ASSERT(e == 0);
   5.113 +        CX_TEST_ASSERT(r == s / 2);
   5.114 +
   5.115 +        e = cx_szmul(2, s / 4, &r);
   5.116 +        CX_TEST_ASSERT(e == 0);
   5.117 +        CX_TEST_ASSERT(r == s / 2);
   5.118 +
   5.119 +        e = cx_szmul(s / 4, 4, &r);
   5.120 +        CX_TEST_ASSERT(e == 0);
   5.121 +        CX_TEST_ASSERT(r == s);
   5.122 +
   5.123 +        e = cx_szmul(4, s / 4, &r);
   5.124 +        CX_TEST_ASSERT(e == 0);
   5.125 +        CX_TEST_ASSERT(r == s);
   5.126 +
   5.127 +        e = cx_szmul(s / 4, 5, &r);
   5.128 +        CX_TEST_ASSERT(e != 0);
   5.129 +
   5.130 +        e = cx_szmul(5, s / 4, &r);
   5.131 +        CX_TEST_ASSERT(e != 0);
   5.132 +
   5.133 +        e = cx_szmul(SIZE_MAX - 4, 0, &r);
   5.134 +        CX_TEST_ASSERT(e == 0);
   5.135 +        CX_TEST_ASSERT(r == 0);
   5.136 +
   5.137 +        e = cx_szmul(0, SIZE_MAX - 1, &r);
   5.138 +        CX_TEST_ASSERT(e == 0);
   5.139 +        CX_TEST_ASSERT(r == 0);
   5.140 +
   5.141 +        e = cx_szmul(SIZE_MAX, 0, &r);
   5.142 +        CX_TEST_ASSERT(e == 0);
   5.143 +        CX_TEST_ASSERT(r == 0);
   5.144 +
   5.145 +        e = cx_szmul(0, SIZE_MAX, &r);
   5.146 +        CX_TEST_ASSERT(e == 0);
   5.147 +        CX_TEST_ASSERT(r == 0);
   5.148 +
   5.149 +        e = cx_szmul(0, 0, &r);
   5.150 +        CX_TEST_ASSERT(e == 0);
   5.151 +        CX_TEST_ASSERT(r == 0);
   5.152 +    }
   5.153 +}
   5.154 +
   5.155 +#ifdef CX_SZMUL_BUILTIN
   5.156 +
   5.157 +// also test the custom implementation
   5.158 +#undef CX_SZMUL_BUILTIN
   5.159 +
   5.160 +#include "../src/szmul.c"
   5.161 +
   5.162 +#define CX_SZMUL_BUILTIN
   5.163 +
   5.164 +CX_TEST(test_szmul_impl) {
   5.165 +    size_t r;
   5.166 +    int e;
   5.167 +
   5.168 +    CX_TEST_DO {
   5.169 +        e = cx_szmul_impl(5, 7, &r);
   5.170 +        CX_TEST_ASSERT(e == 0);
   5.171 +        CX_TEST_ASSERT(r == 35);
   5.172 +
   5.173 +        size_t s = SIZE_MAX & ~3;
   5.174 +
   5.175 +        e = cx_szmul_impl(s / 4, 2, &r);
   5.176 +        CX_TEST_ASSERT(e == 0);
   5.177 +        CX_TEST_ASSERT(r == s / 2);
   5.178 +
   5.179 +        e = cx_szmul_impl(2, s / 4, &r);
   5.180 +        CX_TEST_ASSERT(e == 0);
   5.181 +        CX_TEST_ASSERT(r == s / 2);
   5.182 +
   5.183 +        e = cx_szmul_impl(s / 4, 4, &r);
   5.184 +        CX_TEST_ASSERT(e == 0);
   5.185 +        CX_TEST_ASSERT(r == s);
   5.186 +
   5.187 +        e = cx_szmul_impl(4, s / 4, &r);
   5.188 +        CX_TEST_ASSERT(e == 0);
   5.189 +        CX_TEST_ASSERT(r == s);
   5.190 +
   5.191 +        e = cx_szmul_impl(s / 4, 5, &r);
   5.192 +        CX_TEST_ASSERT(e != 0);
   5.193 +
   5.194 +        e = cx_szmul_impl(5, s / 4, &r);
   5.195 +        CX_TEST_ASSERT(e != 0);
   5.196 +
   5.197 +        e = cx_szmul_impl(SIZE_MAX - 4, 0, &r);
   5.198 +        CX_TEST_ASSERT(e == 0);
   5.199 +        CX_TEST_ASSERT(r == 0);
   5.200 +
   5.201 +        e = cx_szmul_impl(0, SIZE_MAX - 1, &r);
   5.202 +        CX_TEST_ASSERT(e == 0);
   5.203 +        CX_TEST_ASSERT(r == 0);
   5.204 +
   5.205 +        e = cx_szmul_impl(SIZE_MAX, 0, &r);
   5.206 +        CX_TEST_ASSERT(e == 0);
   5.207 +        CX_TEST_ASSERT(r == 0);
   5.208 +
   5.209 +        e = cx_szmul_impl(0, SIZE_MAX, &r);
   5.210 +        CX_TEST_ASSERT(e == 0);
   5.211 +        CX_TEST_ASSERT(r == 0);
   5.212 +
   5.213 +        e = cx_szmul_impl(0, 0, &r);
   5.214 +        CX_TEST_ASSERT(e == 0);
   5.215 +        CX_TEST_ASSERT(r == 0);
   5.216 +    }
   5.217 +}
   5.218 +
   5.219 +#endif // CX_SZMUL_BUILTIN
   5.220 +
   5.221  
   5.222  CxTestSuite *cx_test_suite_utils(void) {
   5.223      CxTestSuite *suite = cx_test_suite_new("utils");
   5.224  
   5.225 +    cx_test_register(suite, test_stream_bncopy);
   5.226 +    cx_test_register(suite, test_stream_ncopy);
   5.227 +    cx_test_register(suite, test_forn);
   5.228 +    cx_test_register(suite, test_swap_ptr);
   5.229 +    cx_test_register(suite, test_szmul);
   5.230 +    cx_test_register(suite, test_szmul_impl);
   5.231 +
   5.232      return suite;
   5.233  }
   5.234  
     6.1 --- a/tests/test_utils.cpp	Wed Dec 20 16:46:14 2023 +0100
     6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.3 @@ -1,227 +0,0 @@
     6.4 -/*
     6.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     6.6 - *
     6.7 - * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
     6.8 - *
     6.9 - * Redistribution and use in source and binary forms, with or without
    6.10 - * modification, are permitted provided that the following conditions are met:
    6.11 - *
    6.12 - *   1. Redistributions of source code must retain the above copyright
    6.13 - *      notice, this list of conditions and the following disclaimer.
    6.14 - *
    6.15 - *   2. Redistributions in binary form must reproduce the above copyright
    6.16 - *      notice, this list of conditions and the following disclaimer in the
    6.17 - *      documentation and/or other materials provided with the distribution.
    6.18 - *
    6.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    6.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    6.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    6.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    6.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    6.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    6.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    6.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    6.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    6.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    6.29 - * POSSIBILITY OF SUCH DAMAGE.
    6.30 - */
    6.31 -
    6.32 -#include "cx/utils.h"
    6.33 -
    6.34 -#include "cx/buffer.h"
    6.35 -
    6.36 -#include <gtest/gtest.h>
    6.37 -
    6.38 -TEST(Utils, cx_stream_bncopy) {
    6.39 -    CxBuffer source, target;
    6.40 -    char sbuf[32], tbuf[32];
    6.41 -    memset(tbuf, 0, 32);
    6.42 -    cxBufferInit(&source, sbuf, 32, nullptr, 0);
    6.43 -    cxBufferInit(&target, tbuf, 32, nullptr, 0);
    6.44 -    cxBufferPutString(&source, "This is a stream copy test.");
    6.45 -    cxBufferSeek(&source, 0, SEEK_SET);
    6.46 -
    6.47 -    char tmp[4];
    6.48 -    size_t result = cx_stream_bncopy(&source, &target,
    6.49 -                                     (cx_read_func) cxBufferRead,
    6.50 -                                     (cx_write_func) cxBufferWrite,
    6.51 -                                     tmp, 4, 20);
    6.52 -    EXPECT_EQ(20, result);
    6.53 -    EXPECT_EQ(20, target.size);
    6.54 -    EXPECT_STREQ("This is a stream cop\0", tbuf);
    6.55 -
    6.56 -    result = cx_stream_bcopy(&source, &target,
    6.57 -                             (cx_read_func) cxBufferRead,
    6.58 -                             (cx_write_func) cxBufferWrite,
    6.59 -                             nullptr, 16);
    6.60 -
    6.61 -    EXPECT_EQ(7, result);
    6.62 -    EXPECT_EQ(27, target.size);
    6.63 -    EXPECT_STREQ("This is a stream copy test.\0", tbuf);
    6.64 -
    6.65 -    cxBufferDestroy(&source);
    6.66 -    cxBufferDestroy(&target);
    6.67 -}
    6.68 -
    6.69 -TEST(Utils, cx_stream_ncopy) {
    6.70 -    CxBuffer source, target;
    6.71 -    char sbuf[32], tbuf[32];
    6.72 -    memset(tbuf, 0, 32);
    6.73 -    cxBufferInit(&source, sbuf, 32, nullptr, 0);
    6.74 -    cxBufferInit(&target, tbuf, 32, nullptr, 0);
    6.75 -    cxBufferPutString(&source, "This is a stream copy test.");
    6.76 -    cxBufferSeek(&source, 0, SEEK_SET);
    6.77 -
    6.78 -    size_t result = cx_stream_ncopy(&source, &target,
    6.79 -                                    (cx_read_func) cxBufferRead,
    6.80 -                                    (cx_write_func) cxBufferWrite,
    6.81 -                                    20);
    6.82 -    EXPECT_EQ(20, result);
    6.83 -    EXPECT_EQ(20, target.size);
    6.84 -    EXPECT_STREQ("This is a stream cop\0", tbuf);
    6.85 -
    6.86 -    result = cx_stream_copy(&source, &target,
    6.87 -                            (cx_read_func) cxBufferRead,
    6.88 -                            (cx_write_func) cxBufferWrite);
    6.89 -
    6.90 -    EXPECT_EQ(7, result);
    6.91 -    EXPECT_EQ(27, target.size);
    6.92 -    EXPECT_STREQ("This is a stream copy test.\0", tbuf);
    6.93 -
    6.94 -    cxBufferDestroy(&source);
    6.95 -    cxBufferDestroy(&target);
    6.96 -}
    6.97 -
    6.98 -TEST(Utils, ForN) {
    6.99 -    unsigned j;
   6.100 -    j = 0;
   6.101 -    cx_for_n(i, 50) {
   6.102 -        EXPECT_EQ(i, j);
   6.103 -        j++;
   6.104 -    }
   6.105 -}
   6.106 -
   6.107 -TEST(Utils, swap_ptr) {
   6.108 -    int i = 5;
   6.109 -    int j = 8;
   6.110 -    int *ip = &i;
   6.111 -    int *jp = &j;
   6.112 -    cx_swap_ptr(ip, jp);
   6.113 -    EXPECT_EQ(ip, &j);
   6.114 -    EXPECT_EQ(jp, &i);
   6.115 -}
   6.116 -
   6.117 -TEST(Utils, szmul) {
   6.118 -    size_t r;
   6.119 -    int e;
   6.120 -    e = cx_szmul(5, 7, &r);
   6.121 -    EXPECT_EQ(0, e);
   6.122 -    EXPECT_EQ(35, r);
   6.123 -
   6.124 -    size_t s = SIZE_MAX & ~3;
   6.125 -
   6.126 -    e = cx_szmul(s / 4, 2, &r);
   6.127 -    EXPECT_EQ(0, e);
   6.128 -    EXPECT_EQ(s / 2, r);
   6.129 -    e = cx_szmul(2, s / 4, &r);
   6.130 -    EXPECT_EQ(0, e);
   6.131 -    EXPECT_EQ(s / 2, r);
   6.132 -
   6.133 -    e = cx_szmul(s / 4, 4, &r);
   6.134 -    EXPECT_EQ(0, e);
   6.135 -    EXPECT_EQ(s, r);
   6.136 -
   6.137 -    e = cx_szmul(4, s / 4, &r);
   6.138 -    EXPECT_EQ(0, e);
   6.139 -    EXPECT_EQ(s, r);
   6.140 -
   6.141 -    e = cx_szmul(s / 4, 5, &r);
   6.142 -    EXPECT_NE(0, e);
   6.143 -
   6.144 -    e = cx_szmul(5, s / 4, &r);
   6.145 -    EXPECT_NE(0, e);
   6.146 -
   6.147 -    e = cx_szmul(SIZE_MAX - 4, 0, &r);
   6.148 -    EXPECT_EQ(0, e);
   6.149 -    EXPECT_EQ(0, r);
   6.150 -
   6.151 -    e = cx_szmul(0, SIZE_MAX - 1, &r);
   6.152 -    EXPECT_EQ(0, e);
   6.153 -    EXPECT_EQ(0, r);
   6.154 -
   6.155 -    e = cx_szmul(SIZE_MAX, 0, &r);
   6.156 -    EXPECT_EQ(0, e);
   6.157 -    EXPECT_EQ(0, r);
   6.158 -
   6.159 -    e = cx_szmul(0, SIZE_MAX, &r);
   6.160 -    EXPECT_EQ(0, e);
   6.161 -    EXPECT_EQ(0, r);
   6.162 -
   6.163 -    e = cx_szmul(0, 0, &r);
   6.164 -    EXPECT_EQ(0, e);
   6.165 -    EXPECT_EQ(0, r);
   6.166 -}
   6.167 -
   6.168 -#ifdef CX_SZMUL_BUILTIN
   6.169 -
   6.170 -// also test the custom implementation
   6.171 -struct Utils_szmul_impl : ::testing::Test {
   6.172 -#undef CX_SZMUL_BUILTIN
   6.173 -
   6.174 -#include "../src/szmul.c"
   6.175 -
   6.176 -#define CX_SZMUL_BUILTIN
   6.177 -};
   6.178 -
   6.179 -TEST_F(Utils_szmul_impl, Test) {
   6.180 -    size_t r;
   6.181 -    int e;
   6.182 -    e = cx_szmul_impl(5, 7, &r);
   6.183 -    EXPECT_EQ(0, e);
   6.184 -    EXPECT_EQ(35, r);
   6.185 -
   6.186 -    size_t s = SIZE_MAX & ~3;
   6.187 -
   6.188 -    e = cx_szmul_impl(s / 4, 2, &r);
   6.189 -    EXPECT_EQ(0, e);
   6.190 -    EXPECT_EQ(s / 2, r);
   6.191 -    e = cx_szmul_impl(2, s / 4, &r);
   6.192 -    EXPECT_EQ(0, e);
   6.193 -    EXPECT_EQ(s / 2, r);
   6.194 -
   6.195 -    e = cx_szmul_impl(s / 4, 4, &r);
   6.196 -    EXPECT_EQ(0, e);
   6.197 -    EXPECT_EQ(s, r);
   6.198 -
   6.199 -    e = cx_szmul_impl(4, s / 4, &r);
   6.200 -    EXPECT_EQ(0, e);
   6.201 -    EXPECT_EQ(s, r);
   6.202 -
   6.203 -    e = cx_szmul_impl(s / 4, 5, &r);
   6.204 -    EXPECT_NE(0, e);
   6.205 -
   6.206 -    e = cx_szmul_impl(5, s / 4, &r);
   6.207 -    EXPECT_NE(0, e);
   6.208 -
   6.209 -    e = cx_szmul_impl(SIZE_MAX - 4, 0, &r);
   6.210 -    EXPECT_EQ(0, e);
   6.211 -    EXPECT_EQ(0, r);
   6.212 -
   6.213 -    e = cx_szmul_impl(0, SIZE_MAX - 1, &r);
   6.214 -    EXPECT_EQ(0, e);
   6.215 -    EXPECT_EQ(0, r);
   6.216 -
   6.217 -    e = cx_szmul_impl(SIZE_MAX, 0, &r);
   6.218 -    EXPECT_EQ(0, e);
   6.219 -    EXPECT_EQ(0, r);
   6.220 -
   6.221 -    e = cx_szmul_impl(0, SIZE_MAX, &r);
   6.222 -    EXPECT_EQ(0, e);
   6.223 -    EXPECT_EQ(0, r);
   6.224 -
   6.225 -    e = cx_szmul_impl(0, 0, &r);
   6.226 -    EXPECT_EQ(0, e);
   6.227 -    EXPECT_EQ(0, r);
   6.228 -}
   6.229 -
   6.230 -#endif // CX_SZMUL_BUILTIN
     7.1 --- a/tests/test_utils.h	Wed Dec 20 16:46:14 2023 +0100
     7.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.3 @@ -1,46 +0,0 @@
     7.4 -/*
     7.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     7.6 - *
     7.7 - * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved.
     7.8 - *
     7.9 - * Redistribution and use in source and binary forms, with or without
    7.10 - * modification, are permitted provided that the following conditions are met:
    7.11 - *
    7.12 - *   1. Redistributions of source code must retain the above copyright
    7.13 - *      notice, this list of conditions and the following disclaimer.
    7.14 - *
    7.15 - *   2. Redistributions in binary form must reproduce the above copyright
    7.16 - *      notice, this list of conditions and the following disclaimer in the
    7.17 - *      documentation and/or other materials provided with the distribution.
    7.18 - *
    7.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    7.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    7.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    7.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    7.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    7.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    7.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    7.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    7.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    7.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    7.29 - * POSSIBILITY OF SUCH DAMAGE.
    7.30 - */
    7.31 -
    7.32 -#ifndef UCX_TEST_SUITE_UTILS_H
    7.33 -#define UCX_TEST_SUITE_UTILS_H
    7.34 -
    7.35 -#include "cx/test.h"
    7.36 -
    7.37 -#ifdef __cplusplus
    7.38 -extern "C" {
    7.39 -#endif
    7.40 -
    7.41 -
    7.42 -CxTestSuite *cx_test_suite_utils(void);
    7.43 -
    7.44 -
    7.45 -#ifdef __cplusplus
    7.46 -} // extern "C"
    7.47 -#endif
    7.48 -
    7.49 -#endif // UCX_TEST_SUITE_UTILS_H
     8.1 --- a/tests/ucxtest.c	Wed Dec 20 16:46:14 2023 +0100
     8.2 +++ b/tests/ucxtest.c	Wed Dec 20 17:57:18 2023 +0100
     8.3 @@ -26,7 +26,9 @@
     8.4   * POSSIBILITY OF SUCH DAMAGE.
     8.5   */
     8.6  
     8.7 -#include "test_utils.h"
     8.8 +#include "cx/test.h"
     8.9 +
    8.10 +CxTestSuite *cx_test_suite_utils(void);
    8.11  
    8.12  #define run_tests(suite) cx_test_run_stdout(suite); success += (suite)->success; failure += (suite)->failure
    8.13  

mercurial