Sun, 26 Sep 2021 14:45:51 +0200
Automated merge
test/CMakeLists.txt | file | annotate | diff | comparison | revisions |
1.1 --- a/CMakeLists.txt Sun Sep 26 14:41:49 2021 +0200 1.2 +++ b/CMakeLists.txt Sun Sep 26 14:45:51 2021 +0200 1.3 @@ -16,14 +16,16 @@ 1.4 add_subdirectory(docs/src) 1.5 1.6 # API Documentation 1.7 +message(CHECK_START "Seaching for Doxygen") 1.8 find_package(Doxygen) 1.9 -option(BUILD_DOCUMENTATION "Create API documentation." ${DOXYGEN_FOUND}) 1.10 +if(DOXYGEN_FOUND) 1.11 + message(CHECK_PASS "found.") 1.12 +else() 1.13 + message(CHECK_FAIL "not found - documentation will not be generated.") 1.14 +endif() 1.15 +option(BUILD_API_DOC "Create API documentation." ON) 1.16 1.17 -if(BUILD_DOCUMENTATION) 1.18 - if(NOT DOXYGEN_FOUND) 1.19 - message(FATAL_ERROR "Doxygen is needed to build the documentation.") 1.20 - endif() 1.21 - 1.22 +if(BUILD_API_DOC AND DOXYGEN_FOUND) 1.23 set(DOXY_INPUT ${CMAKE_SOURCE_DIR}/src) 1.24 set(DOXY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/docs) 1.25 set(DOXY_PROJECT_LOGO ${CMAKE_SOURCE_DIR}/uaplogo.png)
2.1 --- a/src/linked_list.c Sun Sep 26 14:41:49 2021 +0200 2.2 +++ b/src/linked_list.c Sun Sep 26 14:45:51 2021 +0200 2.3 @@ -171,7 +171,8 @@ 2.4 } 2.5 2.6 void cxLinkedListDestroy(CxList list) { 2.7 - 2.8 + // TODO: free contents 2.9 + cxFree(list->data.allocator, list); 2.10 } 2.11 2.12 size_t cxLinkedListRecalculateSize(CxList list) {
3.1 --- a/test/CMakeLists.txt Sun Sep 26 14:41:49 2021 +0200 3.2 +++ b/test/CMakeLists.txt Sun Sep 26 14:45:51 2021 +0200 3.3 @@ -14,7 +14,7 @@ 3.4 ) 3.5 3.6 foreach(test ${TESTS}) 3.7 - add_executable(${test} ${test}.c) 3.8 + add_executable(${test} util_allocator.c ${test}.c) 3.9 target_link_libraries(${test} PRIVATE ucx_static ${CUNIT_LIBRARY}) 3.10 target_include_directories(${test} PRIVATE ${CUNIT_INCLUDE_DIR}) 3.11 add_test(NAME ${test} COMMAND ${test} WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
4.1 --- a/test/test_allocator.c Sun Sep 26 14:41:49 2021 +0200 4.2 +++ b/test/test_allocator.c Sun Sep 26 14:45:51 2021 +0200 4.3 @@ -28,6 +28,7 @@ 4.4 4.5 #include "cx/allocator.h" 4.6 #include "test_config.h" 4.7 +#include <errno.h> 4.8 4.9 void test_default_allocator_available(void) { 4.10 cx_allocator_class *clazz = cxDefaultAllocator->cl; 4.11 @@ -42,7 +43,7 @@ 4.12 4.13 void test_default_realloc(void) { 4.14 void *test = calloc(8, 1); 4.15 - memcpy(test, "Test", 4); 4.16 + memcpy(test, "Test", 5); 4.17 test = cxRealloc(cxDefaultAllocator, test, 16); 4.18 CU_ASSERT_PTR_NOT_NULL(test) 4.19 CU_ASSERT_STRING_EQUAL("Test", test) 4.20 @@ -51,7 +52,7 @@ 4.21 4.22 void test_default_reallocate(void) { 4.23 void *test = calloc(8, 1); 4.24 - memcpy(test, "Test", 4); 4.25 + memcpy(test, "Test", 5); 4.26 int rval = cxReallocate(cxDefaultAllocator, &test, 16); 4.27 CU_ASSERT_EQUAL(rval, 0); 4.28 CU_ASSERT_PTR_NOT_NULL(test) 4.29 @@ -61,7 +62,8 @@ 4.30 4.31 void test_reallocate_null(void) { 4.32 int rval = cxReallocate(cxDefaultAllocator, NULL, 16); 4.33 - CU_ASSERT_EQUAL(rval, EINVAL); 4.34 + CU_ASSERT_NOT_EQUAL(rval, 0); 4.35 + CU_ASSERT_EQUAL(errno, EINVAL); 4.36 } 4.37 4.38 void test_default_calloc(void) { 4.39 @@ -94,7 +96,7 @@ 4.40 !CU_add_test(suite, "test of malloc()", test_default_malloc)|| 4.41 !CU_add_test(suite, "test of realloc()", test_default_realloc) || 4.42 !CU_add_test(suite, "test of realloc() via cxReallocate", test_default_reallocate) || 4.43 - !CU_add_test(suite, "test of cxReallocate with NULL", test_default_reallocate) || 4.44 + !CU_add_test(suite, "test of cxReallocate with NULL", test_reallocate_null) || 4.45 !CU_add_test(suite, "test of calloc()", test_default_calloc) || 4.46 !CU_add_test(suite, "test of free()", test_default_free) 4.47 ) {
5.1 --- a/test/test_list.c Sun Sep 26 14:41:49 2021 +0200 5.2 +++ b/test/test_list.c Sun Sep 26 14:45:51 2021 +0200 5.3 @@ -28,6 +28,7 @@ 5.4 5.5 #include "cx/linked_list.h" 5.6 #include "test_config.h" 5.7 +#include "util_allocator.h" 5.8 5.9 int cmp_int(int const *l, int const *r) { 5.10 int left = *l, right = *r; 5.11 @@ -35,27 +36,32 @@ 5.12 } 5.13 5.14 void test_linked_list_create() { 5.15 - CxList list = cxLinkedListCreate(cxDefaultAllocator, (CxListComparator) cmp_int, sizeof(int)); 5.16 + cxTestingAllocatorReset(); 5.17 + 5.18 + CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); 5.19 5.20 CU_ASSERT_EQUAL(list->data.size, 0) 5.21 CU_ASSERT_EQUAL(list->data.capacity, (size_t) -1) 5.22 - CU_ASSERT_PTR_EQUAL(list->data.allocator, cxDefaultAllocator) 5.23 + CU_ASSERT_PTR_EQUAL(list->data.allocator, cxTestingAllocator) 5.24 CU_ASSERT_EQUAL(list->data.itemsize, sizeof(int)) 5.25 CU_ASSERT_PTR_EQUAL(list->data.cmpfunc, cmp_int) 5.26 5.27 struct node { 5.28 - void* begin; void* end; ptrdiff_t ploc; ptrdiff_t nloc; 5.29 + void *begin; 5.30 + void *end; 5.31 + ptrdiff_t ploc; 5.32 + ptrdiff_t nloc; 5.33 }; 5.34 5.35 - struct node* actual = (struct node*) list->data.listdata; 5.36 + struct node *actual = (struct node *) list->data.listdata; 5.37 CU_ASSERT_PTR_NULL(actual->begin) 5.38 CU_ASSERT_PTR_NULL(actual->end) 5.39 CU_ASSERT_EQUAL(0, actual->ploc) 5.40 - CU_ASSERT_EQUAL(sizeof(void*), actual->nloc) 5.41 + CU_ASSERT_EQUAL(sizeof(void *), actual->nloc) 5.42 5.43 cxLinkedListDestroy(list); 5.44 5.45 - // TODO: use allocator that keeps track of the freed memory 5.46 + CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 5.47 } 5.48 5.49 int main() {
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/test/util_allocator.c Sun Sep 26 14:45:51 2021 +0200 6.3 @@ -0,0 +1,126 @@ 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 "util_allocator.h" 6.33 +#include <string.h> 6.34 + 6.35 +void cx_testing_allocator_add(cx_testing_allocator_s *data, void *ptr) { 6.36 + data->tracked[data->live] = ptr; 6.37 + data->live++; 6.38 +} 6.39 + 6.40 +int cx_testing_allocator_remove(cx_testing_allocator_s *data, void *ptr) { 6.41 + for (int i = 0; i < data->live; i++) { 6.42 + if (data->tracked[i] == ptr) { 6.43 + data->tracked[i] = data->tracked[data->live - 1]; 6.44 + data->live--; 6.45 + return 0; 6.46 + } 6.47 + } 6.48 + return 1; 6.49 +} 6.50 + 6.51 +void *cx_malloc_testing(void *d, size_t n) { 6.52 + cx_testing_allocator_s *data = d; 6.53 + void *ptr = malloc(n); 6.54 + data->alloc_total++; 6.55 + if (ptr == NULL) { 6.56 + data->alloc_failed++; 6.57 + } else { 6.58 + cx_testing_allocator_add(data, ptr); 6.59 + } 6.60 + return ptr; 6.61 +} 6.62 + 6.63 +void *cx_realloc_testing(void *d, void *mem, size_t n) { 6.64 + cx_testing_allocator_s *data = d; 6.65 + void *ptr = realloc(mem, n); 6.66 + if (ptr == mem) { 6.67 + return ptr; 6.68 + } else { 6.69 + data->alloc_total++; 6.70 + if (ptr == NULL) { 6.71 + data->alloc_failed++; 6.72 + } else { 6.73 + data->free_total++; 6.74 + if (cx_testing_allocator_remove(data, mem)) { 6.75 + data->free_failed++; 6.76 + } 6.77 + cx_testing_allocator_add(data, ptr); 6.78 + } 6.79 + return ptr; 6.80 + } 6.81 +} 6.82 + 6.83 +void *cx_calloc_testing(void *d, size_t nelem, size_t n) { 6.84 + cx_testing_allocator_s *data = d; 6.85 + void *ptr = calloc(nelem, n); 6.86 + data->alloc_total++; 6.87 + if (ptr == NULL) { 6.88 + data->alloc_failed++; 6.89 + } else { 6.90 + cx_testing_allocator_add(data, ptr); 6.91 + } 6.92 + return ptr; 6.93 +} 6.94 + 6.95 +void cx_free_testing(void *d, void *mem) { 6.96 + cx_testing_allocator_s *data = d; 6.97 + data->free_total++; 6.98 + if (cx_testing_allocator_remove(data, mem)) { 6.99 + data->free_failed++; 6.100 + // do not even attempt to free mem, because it is likely to segfault 6.101 + } else { 6.102 + free(mem); 6.103 + } 6.104 +} 6.105 + 6.106 +cx_allocator_class cx_testing_allocator_class = { 6.107 + cx_malloc_testing, 6.108 + cx_realloc_testing, 6.109 + cx_calloc_testing, 6.110 + cx_free_testing 6.111 +}; 6.112 + 6.113 +cx_testing_allocator_s cx_testing_allocator_data; 6.114 + 6.115 +struct cx_allocator_s cx_testing_allocator = { 6.116 + &cx_testing_allocator_class, 6.117 + &cx_testing_allocator_data 6.118 +}; 6.119 +CxAllocator cxTestingAllocator = &cx_testing_allocator; 6.120 + 6.121 +void cxTestingAllocatorReset(void) { 6.122 + memset(&cx_testing_allocator_data, 0, sizeof(cx_testing_allocator_s)); 6.123 +} 6.124 + 6.125 +int cxTestingAllocatorVerify(void) { 6.126 + return cx_testing_allocator_data.live == 0 6.127 + && cx_testing_allocator_data.alloc_failed == 0 && cx_testing_allocator_data.free_failed == 0 6.128 + && cx_testing_allocator_data.alloc_total == cx_testing_allocator_data.free_total; 6.129 +}
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/test/util_allocator.h Sun Sep 26 14:45:51 2021 +0200 7.3 @@ -0,0 +1,90 @@ 7.4 +/* 7.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 7.6 + * 7.7 + * Copyright 2021 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_UTIL_ALLOCATOR_H 7.33 +#define UCX_UTIL_ALLOCATOR_H 7.34 + 7.35 +#include "cx/allocator.h" 7.36 + 7.37 +#ifdef __cplusplus 7.38 +extern "C" { 7.39 +#endif 7.40 + 7.41 +#define CX_TESTING_ALLOCATOR_MAX_LIVE 1024 7.42 + 7.43 +typedef struct { 7.44 + /** 7.45 + * Total number of all allocations (malloc, calloc, realloc). 7.46 + * A realloc() does only count when the memory has to be moved. 7.47 + */ 7.48 + int alloc_total; 7.49 + /** 7.50 + * Number of failed allocations (malloc, calloc, realloc). 7.51 + */ 7.52 + int alloc_failed; 7.53 + /** 7.54 + * Total number of freed pointers. 7.55 + * A reallocation also counts as a free when the memory has to be moved. 7.56 + */ 7.57 + int free_total; 7.58 + /** 7.59 + * Number of failed free invocations. 7.60 + * A free() is considered failed, if it has not been performed on tracked memory. 7.61 + */ 7.62 + int free_failed; 7.63 + /** 7.64 + * Number of memory blocks that are currently live (and tracked). 7.65 + * The maximum number of tracked blocks is defined in #CX_TESTING_ALLOCATOR_MAX_LIVE. 7.66 + */ 7.67 + int live; 7.68 + /** 7.69 + * The array of tracked memory blocks. 7.70 + */ 7.71 + void *tracked[CX_TESTING_ALLOCATOR_MAX_LIVE]; 7.72 +} cx_testing_allocator_s; 7.73 + 7.74 +extern CxAllocator cxTestingAllocator; 7.75 + 7.76 +/** 7.77 + * Resets the testing allocator information. 7.78 + * This function SHOULD be called prior to any use of this allocator. 7.79 + */ 7.80 +void cxTestingAllocatorReset(void); 7.81 + 7.82 +/** 7.83 + * Checks whether all allocated memory is properly freed and no failed (de)allocations happened. 7.84 + * 7.85 + * @return true on success, false if there was any problem 7.86 + */ 7.87 +int cxTestingAllocatorVerify(void); 7.88 + 7.89 +#ifdef __cplusplus 7.90 +}; /* extern "C" */ 7.91 +#endif 7.92 + 7.93 +#endif /* UCX_UTIL_ALLOCATOR_H */