improve coverage metrics

Sun, 29 Dec 2024 16:56:13 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 29 Dec 2024 16:56:13 +0100
changeset 1065
6eb7b54975ee
parent 1064
f3b04cd60776
child 1066
8610f87a6b14

improve coverage metrics

make/gcc.mk file | annotate | diff | comparison | revisions
src/allocator.c file | annotate | diff | comparison | revisions
src/array_list.c file | annotate | diff | comparison | revisions
src/buffer.c file | annotate | diff | comparison | revisions
src/hash_map.c file | annotate | diff | comparison | revisions
src/json.c file | annotate | diff | comparison | revisions
src/linked_list.c file | annotate | diff | comparison | revisions
src/list.c file | annotate | diff | comparison | revisions
src/mempool.c file | annotate | diff | comparison | revisions
src/printf.c file | annotate | diff | comparison | revisions
src/streams.c file | annotate | diff | comparison | revisions
tests/test_streams.c file | annotate | diff | comparison | revisions
--- a/make/gcc.mk	Sun Dec 29 15:24:20 2024 +0100
+++ b/make/gcc.mk	Sun Dec 29 16:56:13 2024 +0100
@@ -11,5 +11,5 @@
 SHLIB_CFLAGS = -fPIC
 SHLIB_LDFLAGS = -shared
 
-COVERAGE_CFLAGS = --coverage
+COVERAGE_CFLAGS = --coverage -DNDEBUG
 COVERAGE_LDFLAGS =  --coverage
--- a/src/allocator.c	Sun Dec 29 15:24:20 2024 +0100
+++ b/src/allocator.c	Sun Dec 29 16:56:13 2024 +0100
@@ -84,7 +84,7 @@
 ) {
     void *nmem = realloc(*mem, n);
     if (nmem == NULL) {
-        return 1;
+        return 1; // LCOV_EXCL_LINE
     } else {
         *mem = nmem;
         return 0;
@@ -104,7 +104,7 @@
     } else {
         void *nmem = realloc(*mem, n);
         if (nmem == NULL) {
-            return 1;
+            return 1; // LCOV_EXCL_LINE
         } else {
             *mem = nmem;
             return 0;
@@ -152,7 +152,7 @@
 ) {
     void *nmem = allocator->cl->realloc(allocator->data, *mem, n);
     if (nmem == NULL) {
-        return 1;
+        return 1; // LCOV_EXCL_LINE
     } else {
         *mem = nmem;
         return 0;
@@ -168,7 +168,7 @@
 ) {
     void *nmem = cxReallocArray(allocator, *mem, nmemb, size);
     if (nmem == NULL) {
-        return 1;
+        return 1; // LCOV_EXCL_LINE
     } else {
         *mem = nmem;
         return 0;
--- a/src/array_list.c	Sun Dec 29 15:24:20 2024 +0100
+++ b/src/array_list.c	Sun Dec 29 16:56:13 2024 +0100
@@ -179,7 +179,7 @@
                 *array, newcap, elem_size, reallocator
         );
         if (newmem == NULL) {
-            return 1;
+            return 1; // LCOV_EXCL_LINE
         }
 
         // store new pointer
@@ -365,7 +365,7 @@
         if (new_mem == NULL) {
             // give it up right away, there is no contract
             // that requires us to insert as much as we can
-            return 1;
+            return 1;  // LCOV_EXCL_LINE
         }
         *target = new_mem;
         *capacity = new_capacity;
@@ -779,7 +779,7 @@
     }
 
     // just move the elements to the left
-    int result = cx_array_copy(
+    cx_array_copy(
             &arl->data,
             &list->collection.size,
             &arl->capacity,
@@ -791,9 +791,6 @@
             &arl->reallocator
     );
 
-    // cx_array_copy cannot fail, array cannot grow
-    assert(result == 0);
-
     // decrease the size
     list->collection.size -= remove;
 
@@ -862,7 +859,8 @@
                 if (1 == cx_arl_remove(list, i, 1, NULL)) {
                     return i;
                 } else {
-                    return -1;
+                    // should be unreachable
+                    return -1;  // LCOV_EXCL_LINE
                 }
             } else {
                 return i;
@@ -1019,10 +1017,10 @@
 
     // allocate the array after the real elem_size is known
     list->data = cxCalloc(allocator, initial_capacity, elem_size);
-    if (list->data == NULL) {
+    if (list->data == NULL) { // LCOV_EXCL_START
         cxFree(allocator, list);
         return NULL;
-    }
+    } // LCOV_EXCL_STOP
 
     // configure the reallocator
     list->reallocator = cx_array_reallocator(allocator, NULL);
--- a/src/buffer.c	Sun Dec 29 15:24:20 2024 +0100
+++ b/src/buffer.c	Sun Dec 29 16:56:13 2024 +0100
@@ -61,7 +61,7 @@
     if (!space) {
         buffer->bytes = cxMalloc(allocator, capacity);
         if (buffer->bytes == NULL) {
-            return -1;
+            return -1; // LCOV_EXCL_LINE
         }
         buffer->flags |= CX_BUFFER_FREE_CONTENTS;
     } else {
@@ -101,8 +101,10 @@
     if (0 == cxBufferInit(buf, space, capacity, allocator, flags)) {
         return buf;
     } else {
+        // LCOV_EXCL_START
         cxFree(allocator, buf);
         return NULL;
+        // LCOV_EXCL_STOP
     }
 }
 
@@ -190,7 +192,7 @@
         buffer->capacity = newcap;
         return 0;
     } else {
-        return -1;
+        return -1; // LCOV_EXCL_LINE
     }
 }
 
@@ -264,7 +266,7 @@
                 perform_flush = true;
             } else {
                 if (cxBufferMinimumCapacity(buffer, required)) {
-                    return 0;
+                    return 0; // LCOV_EXCL_LINE
                 }
             }
         } else {
@@ -452,7 +454,7 @@
     if (buffer->capacity < req_capacity) {
         if (buffer->flags & CX_BUFFER_AUTO_EXTEND) {
             if (cxBufferMinimumCapacity(buffer, req_capacity)) {
-                return -1;
+                return -1; // LCOV_EXCL_LINE
             }
             movebytes = buffer->size;
         } else {
--- a/src/hash_map.c	Sun Dec 29 15:24:20 2024 +0100
+++ b/src/hash_map.c	Sun Dec 29 16:56:13 2024 +0100
@@ -115,9 +115,7 @@
                 allocator,
                 sizeof(struct cx_hash_map_element_s) + map->collection.elem_size
         );
-        if (e == NULL) {
-            return -1;
-        }
+        if (e == NULL) return -1;
 
         // write the value
         if (map->collection.store_pointer) {
@@ -128,9 +126,7 @@
 
         // copy the key
         void *kd = cxMalloc(allocator, key.len);
-        if (kd == NULL) {
-            return -1;
-        }
+        if (kd == NULL) return -1;
         memcpy(kd, key.data, key.len);
         e->key.data = kd;
         e->key.len = key.len;
@@ -363,7 +359,7 @@
             iter.base.current = cx_hash_map_iter_current_value;
             break;
         default:
-            assert(false);
+            assert(false); // LCOV_EXCL_LINE
     }
 
     iter.base.valid = cx_hash_map_iter_valid;
@@ -427,10 +423,10 @@
     map->bucket_count = buckets;
     map->buckets = cxCalloc(allocator, buckets,
                             sizeof(struct cx_hash_map_element_s *));
-    if (map->buckets == NULL) {
+    if (map->buckets == NULL) { // LCOV_EXCL_START
         cxFree(allocator, map);
         return NULL;
-    }
+    } // LCOV_EXCL_STOP
 
     // initialize base members
     map->base.cl = &cx_hash_map_class;
@@ -461,9 +457,7 @@
                 new_bucket_count, sizeof(struct cx_hash_map_element_s *)
         );
 
-        if (new_buckets == NULL) {
-            return 1;
-        }
+        if (new_buckets == NULL) return 1;
 
         // iterate through the elements and assign them to their new slots
         for (size_t slot = 0; slot < hash_map->bucket_count; slot++) {
--- a/src/json.c	Sun Dec 29 15:24:20 2024 +0100
+++ b/src/json.c	Sun Dec 29 16:56:13 2024 +0100
@@ -99,9 +99,9 @@
     if (json->uncompleted.tokentype != CX_JSON_NO_TOKEN) {
         allocated = true;
         str = cx_strcat_m(json->uncompleted.content, 1, str);
-        if (str.ptr == NULL) {
+        if (str.ptr == NULL) { // LCOV_EXCL_START
             return (CxJsonToken){CX_JSON_NO_TOKEN, false, {NULL, 0}};
-        }
+        } // LCOV_EXCL_STOP
     }
     json->uncompleted = (CxJsonToken){0};
     CxJsonTokenType ttype;
@@ -195,7 +195,7 @@
                 if (ctype != CX_JSON_NO_TOKEN) {
                     *result = token_create(json, false, token_start, i);
                     if (result->tokentype == CX_JSON_NO_TOKEN) {
-                        return CX_JSON_BUFFER_ALLOC_FAILED;
+                        return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE
                     }
                     if (result->tokentype == CX_JSON_TOKEN_ERROR) {
                         return CX_JSON_FORMAT_ERROR_NUMBER;
@@ -212,7 +212,7 @@
                 if (c == '"') {
                     *result = token_create(json, true, token_start, i + 1);
                     if (result->tokentype == CX_JSON_NO_TOKEN) {
-                        return CX_JSON_BUFFER_ALLOC_FAILED;
+                        return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE
                     }
                     json->buffer.pos = i + 1;
                     return CX_JSON_NO_ERROR;
@@ -234,7 +234,7 @@
                 cx_strdup(cx_strn(json->buffer.space + token_start, uncompleted_len))
             };
             if (uncompleted.content.ptr == NULL) {
-                return CX_JSON_BUFFER_ALLOC_FAILED;
+                return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE
             }
             json->uncompleted = uncompleted;
         } else {
@@ -244,7 +244,7 @@
             cxmutstr str = cx_strcat_m(json->uncompleted.content, 1,
                 cx_strn(json->buffer.space + token_start, uncompleted_len));
             if (str.ptr == NULL) {
-                return CX_JSON_BUFFER_ALLOC_FAILED;
+                return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE
             }
             json->uncompleted.content = str;
         }
@@ -261,9 +261,7 @@
     cxmutstr result;
     result.length = 0;
     result.ptr = cxMalloc(a, str.length - 1);
-    if (result.ptr == NULL) {
-        return result;
-    }
+    if (result.ptr == NULL) return result; // LCOV_EXCL_LINE
 
     bool u = false;
     for (size_t i = 1; i < str.length - 1; i++) {
@@ -291,23 +289,21 @@
 
 static CxJsonValue* create_json_value(CxJson *json, CxJsonValueType type) {
     CxJsonValue *v = cxMalloc(json->allocator, sizeof(CxJsonValue));
-    if (v == NULL) {
-        return NULL;
-    }
+    if (v == NULL) return NULL; // LCOV_EXCL_LINE
 
     // initialize the value
     if (type == CX_JSON_ARRAY) {
         cx_array_initialize_a(json->allocator, v->value.array.array, 16);
-        if (v->value.array.array == NULL) {
+        if (v->value.array.array == NULL) { // LCOV_EXCL_START
             cxFree(json->allocator, v);
             return NULL;
-        }
+        } // LCOV_EXCL_STOP
     } else if (type == CX_JSON_OBJECT) {
         cx_array_initialize_a(json->allocator, v->value.object.values, 16);
-        if (v->value.object.values == NULL) {
+        if (v->value.object.values == NULL) { // LCOV_EXCL_START
             cxFree(json->allocator, v);
             return NULL;
-        }
+        } // LCOV_EXCL_STOP
     } else {
         memset(v, 0, sizeof(CxJsonValue));
     }
@@ -325,7 +321,7 @@
             assert(parent->value.object.values[parent->value.object.values_size - 1].value == NULL);
             parent->value.object.values[parent->value.object.values_size - 1].value = v;
         } else {
-            assert(false);
+            assert(false); // LCOV_EXCL_LINE
         }
     }
 
@@ -333,9 +329,10 @@
     if (type == CX_JSON_ARRAY || type == CX_JSON_OBJECT) {
         CxArrayReallocator vbuf_realloc = cx_array_reallocator(NULL, json->vbuf_internal);
         if (cx_array_simple_add_a(&vbuf_realloc, json->vbuf, v)) {
+            // LCOV_EXCL_START
             cxFree(json->allocator, v);
             return NULL;
-        }
+        } // LCOV_EXCL_STOP
     }
 
     // if currently no value is parsed, this is now the value of interest
@@ -428,7 +425,7 @@
     // guarantee that at least two more states fit on the stack
     CxArrayReallocator state_realloc = cx_array_reallocator(NULL, json->states_internal);
     if (cx_array_simple_reserve_a(&state_realloc, json->states, 2)) {
-        return CX_JSON_BUFFER_ALLOC_FAILED;
+        return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE
     }
 
 
@@ -448,25 +445,25 @@
         switch (token.tokentype) {
             case CX_JSON_TOKEN_BEGIN_ARRAY: {
                 if (create_json_value(json, CX_JSON_ARRAY) == NULL) {
-                    return_rec(CX_JSON_VALUE_ALLOC_FAILED);
+                    return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
                 }
                 json_add_state(json, JP_STATE_VALUE_BEGIN_AR);
                 return_rec(CX_JSON_NO_ERROR);
             }
             case CX_JSON_TOKEN_BEGIN_OBJECT: {
                 if (create_json_value(json, CX_JSON_OBJECT) == NULL) {
-                    return_rec(CX_JSON_VALUE_ALLOC_FAILED);
+                    return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
                 }
                 json_add_state(json, JP_STATE_OBJ_NAME_OR_CLOSE);
                 return_rec(CX_JSON_NO_ERROR);
             }
             case CX_JSON_TOKEN_STRING: {
                 if ((vbuf = create_json_value(json, CX_JSON_STRING)) == NULL) {
-                    return_rec(CX_JSON_VALUE_ALLOC_FAILED);
+                    return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
                 }
                 cxmutstr str = unescape_string(json->allocator, token.content);
                 if (str.ptr == NULL) {
-                    return_rec(CX_JSON_VALUE_ALLOC_FAILED);
+                    return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
                 }
                 vbuf->value.string = str;
                 return_rec(CX_JSON_NO_ERROR);
@@ -475,7 +472,7 @@
             case CX_JSON_TOKEN_NUMBER: {
                 int type = token.tokentype == CX_JSON_TOKEN_INTEGER ? CX_JSON_INTEGER : CX_JSON_NUMBER;
                 if (NULL == (vbuf = create_json_value(json, type))) {
-                    return_rec(CX_JSON_VALUE_ALLOC_FAILED);
+                    return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
                 }
                 if (type == CX_JSON_INTEGER) {
                     if (cx_strtoi64(token.content, &vbuf->value.integer, 10)) {
@@ -490,7 +487,7 @@
             }
             case CX_JSON_TOKEN_LITERAL: {
                 if ((vbuf = create_json_value(json, CX_JSON_LITERAL)) == NULL) {
-                    return_rec(CX_JSON_VALUE_ALLOC_FAILED);
+                    return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
                 }
                 if (0 == cx_strcmp(cx_strcast(token.content), cx_str("true"))) {
                     vbuf->value.literal = CX_JSON_TRUE;
@@ -531,7 +528,7 @@
             // add new entry
             cxmutstr name = unescape_string(json->allocator, token.content);
             if (name.ptr == NULL) {
-                return_rec(CX_JSON_VALUE_ALLOC_FAILED);
+                return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
             }
             CxJsonObjValue kv = {name, NULL};
             assert(json->vbuf_size > 0);
@@ -540,7 +537,7 @@
             assert(parent->type == CX_JSON_OBJECT);
             CxArrayReallocator value_realloc = cx_array_reallocator(json->allocator, NULL);
             if (cx_array_simple_add_a(&value_realloc, parent->value.object.values, kv)) {
-                return_rec(CX_JSON_VALUE_ALLOC_FAILED);
+                return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE
             }
 
             // next state
@@ -706,6 +703,8 @@
     return v;
 }
 
+// LCOV_EXCL_START
+// never called as long as malloc() does not return NULL
 static void cx_json_arr_free_temp(CxJsonValue** values, size_t count) {
     for (size_t i = 0; i < count; i++) {
         if (values[i] == NULL) break;
@@ -713,6 +712,7 @@
     }
     free(values);
 }
+// LCOV_EXCL_STOP
 
 int cxJsonArrAddNumbers(CxJsonValue* arr, const double* num, size_t count) {
     CxJsonValue** values = calloc(count, sizeof(CxJsonValue*));
--- a/src/linked_list.c	Sun Dec 29 15:24:20 2024 +0100
+++ b/src/linked_list.c	Sun Dec 29 16:56:13 2024 +0100
@@ -656,9 +656,7 @@
     cx_linked_list_node *node = index == 0 ? NULL : cx_ll_node_at((cx_linked_list *) list, index - 1);
 
     // perform first insert
-    if (0 != cx_ll_insert_at(list, node, array)) {
-        return 1;
-    }
+    if (0 != cx_ll_insert_at(list, node, array)) return 1;
 
     // is there more?
     if (n == 1) return 1;
@@ -670,9 +668,7 @@
     const char *source = array;
     for (size_t i = 1; i < n; i++) {
         source += list->collection.elem_size;
-        if (0 != cx_ll_insert_at(list, node, source)) {
-            return i;
-        }
+        if (0 != cx_ll_insert_at(list, node, source)) return i;
         node = node->next;
     }
     return n;
--- a/src/list.c	Sun Dec 29 15:24:20 2024 +0100
+++ b/src/list.c	Sun Dec 29 16:56:13 2024 +0100
@@ -297,10 +297,9 @@
     const char *src = data;
     size_t i = 0;
     for (; i < n; i++) {
-        if (0 != invoke_list_func(insert_element,
-                                  list, index + i, src + (i * elem_size))) {
-            return i;
-        }
+        if (0 != invoke_list_func(
+            insert_element, list, index + i,
+            src + (i * elem_size))) return i;
     }
     return i;
 }
@@ -345,9 +344,8 @@
 
         // insert the elements at location si
         if (ins == 1) {
-            if (0 != invoke_list_func(insert_element,
-                                      list, di, src))
-                return inserted;
+            if (0 != invoke_list_func(
+                insert_element, list, di, src)) return inserted;
         } else {
             size_t r = invoke_list_func(insert_array, list, di, src, ins);
             if (r < ins) return inserted + r;
--- a/src/mempool.c	Sun Dec 29 15:24:20 2024 +0100
+++ b/src/mempool.c	Sun Dec 29 16:56:13 2024 +0100
@@ -53,17 +53,13 @@
             return NULL;
         }
         struct cx_mempool_memory_s **newdata = realloc(pool->data, newmsize);
-        if (newdata == NULL) {
-            return NULL;
-        }
+        if (newdata == NULL) return NULL;
         pool->data = newdata;
         pool->capacity = newcap;
     }
 
     struct cx_mempool_memory_s *mem = malloc(sizeof(cx_destructor_func) + n);
-    if (mem == NULL) {
-        return NULL;
-    }
+    if (mem == NULL) return NULL;
 
     mem->destructor = pool->auto_destr;
     pool->data[pool->size] = mem;
@@ -83,9 +79,7 @@
         return NULL;
     }
     void *ptr = cx_mempool_malloc(p, msz);
-    if (ptr == NULL) {
-        return NULL;
-    }
+    if (ptr == NULL) return NULL;
     memset(ptr, 0, nelem * elsize);
     return ptr;
 }
@@ -101,9 +95,7 @@
     mem = (struct cx_mempool_memory_s*)(((char *) ptr) - sizeof(cx_destructor_func));
     newm = realloc(mem, n + sizeof(cx_destructor_func));
 
-    if (newm == NULL) {
-        return NULL;
-    }
+    if (newm == NULL) return NULL;
     if (mem != newm) {
         for (size_t i = 0; i < pool->size; i++) {
             if (pool->data[i] == mem) {
@@ -111,7 +103,7 @@
                 return ((char*)newm) + sizeof(cx_destructor_func);
             }
         }
-        abort();
+        abort(); // LCOV_EXCL_LINE
     } else {
         return ptr;
     }
@@ -142,7 +134,7 @@
             return;
         }
     }
-    abort();
+    abort(); // LCOV_EXCL_LINE
 }
 
 void cxMempoolFree(CxMempool *pool) {
@@ -218,30 +210,28 @@
 
     struct cx_mempool_s *pool =
             malloc(sizeof(struct cx_mempool_s));
-    if (pool == NULL) {
-        return NULL;
-    }
+    if (pool == NULL) return NULL;
 
     CxAllocator *provided_allocator = malloc(sizeof(CxAllocator));
-    if (provided_allocator == NULL) {
+    if (provided_allocator == NULL) { // LCOV_EXCL_START
         free(pool);
         return NULL;
-    }
+    } // LCOV_EXCL_STOP
     provided_allocator->cl = &cx_mempool_allocator_class;
     provided_allocator->data = pool;
 
     pool->allocator = provided_allocator;
 
     pool->data = malloc(poolsize);
-    if (pool->data == NULL) {
+    if (pool->data == NULL) { // LCOV_EXCL_START
         free(provided_allocator);
         free(pool);
         return NULL;
-    }
+    } // LCOV_EXCL_STOP
 
     pool->size = 0;
     pool->capacity = capacity;
     pool->auto_destr = destr;
 
-    return (CxMempool *) pool;
+    return pool;
 }
--- a/src/printf.c	Sun Dec 29 15:24:20 2024 +0100
+++ b/src/printf.c	Sun Dec 29 16:56:13 2024 +0100
@@ -69,10 +69,10 @@
     } else {
         int len = ret + 1;
         char *newbuf = malloc(len);
-        if (!newbuf) {
+        if (!newbuf) { // LCOV_EXCL_START
             va_end(ap2);
             return -1;
-        }
+        } // LCOV_EXCL_STOP
 
         ret = vsnprintf(newbuf, len, fmt, ap2);
         va_end(ap2);
--- a/src/streams.c	Sun Dec 29 15:24:20 2024 +0100
+++ b/src/streams.c	Sun Dec 29 16:56:13 2024 +0100
@@ -58,9 +58,7 @@
     } else {
         if (bufsize == 0) bufsize = CX_STREAM_BCOPY_BUF_SIZE;
         lbuf = malloc(bufsize);
-        if (lbuf == NULL) {
-            return 0;
-        }
+        if (lbuf == NULL) return 0;
     }
 
     size_t r;
--- a/tests/test_streams.c	Sun Dec 29 15:24:20 2024 +0100
+++ b/tests/test_streams.c	Sun Dec 29 16:56:13 2024 +0100
@@ -45,6 +45,13 @@
         size_t result = cx_stream_bncopy(&source, &target,
                                          (cx_read_func) cxBufferRead,
                                          (cx_write_func) cxBufferWrite,
+                                         tmp, 4, 0);
+        CX_TEST_ASSERT(result == 0);
+        CX_TEST_ASSERT(target.size == 0);
+
+        result = cx_stream_bncopy(&source, &target,
+                                         (cx_read_func) cxBufferRead,
+                                         (cx_write_func) cxBufferWrite,
                                          tmp, 4, 20);
         CX_TEST_ASSERT(result == 20);
         CX_TEST_ASSERT(target.size == 20);

mercurial