From 0faf5ad885d672cf4969099df32c6919c77c2826 Mon Sep 17 00:00:00 2001 From: "Michael Gene Brockus (Dreamer)" Date: Sun, 20 Oct 2024 10:25:25 -0500 Subject: [PATCH 01/12] added enums for success and failure --- code/logic/doublylist.c | 20 ++++++++++---------- code/logic/dqueue.c | 16 ++++++++-------- code/logic/forwardlist.c | 16 ++++++++-------- code/logic/fossil/tofu/tofu.h | 6 ++++++ code/logic/pqueue.c | 18 +++++++++--------- code/logic/queue.c | 16 ++++++++-------- code/logic/setof.c | 16 ++++++++-------- code/logic/stack.c | 16 ++++++++-------- code/logic/tofu.c | 2 +- code/logic/vector.c | 2 +- 10 files changed, 67 insertions(+), 61 deletions(-) diff --git a/code/logic/doublylist.c b/code/logic/doublylist.c index f04eb72..feb0ece 100644 --- a/code/logic/doublylist.c +++ b/code/logic/doublylist.c @@ -39,11 +39,11 @@ void fossil_dlist_erase(fossil_dlist_t* dlist) { } int32_t fossil_dlist_insert(fossil_dlist_t* dlist, fossil_tofu_t data) { - if (!dlist) return -1; + if (!dlist) return FOSSIL_TOFU_FAILURE; fossil_dlist_node_t* new_node = (fossil_dlist_node_t*)fossil_tofu_alloc(sizeof(fossil_dlist_node_t)); if (!new_node) { - return -1; // Allocation failed + return FOSSIL_TOFU_FAILURE; // Allocation failed } new_node->data = data; // Consider deep copying data if necessary @@ -59,16 +59,16 @@ int32_t fossil_dlist_insert(fossil_dlist_t* dlist, fossil_tofu_t data) { dlist->tail = new_node; } - return 0; // Success + return FOSSIL_TOFU_SUCCESS; // Success } int32_t fossil_dlist_remove(fossil_dlist_t* dlist, fossil_tofu_t* data) { if (fossil_dlist_is_empty(dlist)) { - return -1; // Empty list + return FOSSIL_TOFU_FAILURE; // Empty list } fossil_dlist_node_t* node_to_remove = dlist->tail; - if (!node_to_remove) return -1; + if (!node_to_remove) return FOSSIL_TOFU_FAILURE; if (node_to_remove == dlist->head) { dlist->head = NULL; @@ -81,18 +81,18 @@ int32_t fossil_dlist_remove(fossil_dlist_t* dlist, fossil_tofu_t* data) { *data = node_to_remove->data; // Consider deep copy if necessary fossil_tofu_free(node_to_remove); - return 0; // Success + return FOSSIL_TOFU_SUCCESS; // Success } int32_t fossil_dlist_search(const fossil_dlist_t* dlist, fossil_tofu_t data) { fossil_dlist_node_t* current = dlist->head; while (current) { if (fossil_tofu_equals(current->data, data)) { - return 0; // Found + return FOSSIL_TOFU_SUCCESS; // Found } current = current->next; } - return -1; // Not found + return FOSSIL_TOFU_FAILURE; // Not found } void fossil_dlist_reverse_forward(fossil_dlist_t* dlist) { @@ -168,11 +168,11 @@ int32_t fossil_dlist_setter(fossil_dlist_t* dlist, fossil_tofu_t data) { if (fossil_tofu_equals(current->data, data)) { // Ensure to free old data if it was dynamically allocated current->data = data; - return 0; // Success + return FOSSIL_TOFU_SUCCESS; // Success } current = current->next; } - return -1; // Not found + return FOSSIL_TOFU_FAILURE; // Not found } bool fossil_dlist_not_empty(const fossil_dlist_t* dlist) { diff --git a/code/logic/dqueue.c b/code/logic/dqueue.c index a1e2df6..a062cc3 100644 --- a/code/logic/dqueue.c +++ b/code/logic/dqueue.c @@ -44,7 +44,7 @@ void fossil_dqueue_erase(fossil_dqueue_t* dqueue) { int32_t fossil_dqueue_insert(fossil_dqueue_t* dqueue, fossil_tofu_t data) { fossil_dqueue_node_t* new_node = (fossil_dqueue_node_t*)fossil_tofu_alloc(sizeof(fossil_dqueue_node_t)); if (!new_node) { - return -1; // Allocation failed + return FOSSIL_TOFU_FAILURE; // Allocation failed } new_node->data = data; @@ -62,12 +62,12 @@ int32_t fossil_dqueue_insert(fossil_dqueue_t* dqueue, fossil_tofu_t data) { dqueue->rear = new_node; } - return 0; // Success + return FOSSIL_TOFU_SUCCESS; // Success } int32_t fossil_dqueue_remove(fossil_dqueue_t* dqueue, fossil_tofu_t* data) { if (fossil_dqueue_is_empty(dqueue)) { - return -1; // Empty queue + return FOSSIL_TOFU_FAILURE; // Empty queue } fossil_dqueue_node_t* node_to_remove = dqueue->front; @@ -83,18 +83,18 @@ int32_t fossil_dqueue_remove(fossil_dqueue_t* dqueue, fossil_tofu_t* data) { *data = node_to_remove->data; fossil_tofu_free(node_to_remove); - return 0; // Success + return FOSSIL_TOFU_SUCCESS; // Success } int32_t fossil_dqueue_search(const fossil_dqueue_t* dqueue, fossil_tofu_t data) { fossil_dqueue_node_t* current = dqueue->front; while (current) { if (fossil_tofu_equals(current->data, data)) { - return 0; // Found + return FOSSIL_TOFU_SUCCESS; // Found } current = current->next; } - return -1; // Not found + return FOSSIL_TOFU_FAILURE; // Not found } size_t fossil_dqueue_size(const fossil_dqueue_t* dqueue) { @@ -124,11 +124,11 @@ int32_t fossil_dqueue_setter(fossil_dqueue_t* dqueue, fossil_tofu_t data) { if (fossil_tofu_equals(current->data, data)) { // Assuming `data` should replace current->data current->data = data; // Update data - return 0; // Success + return FOSSIL_TOFU_SUCCESS; // Success } current = current->next; } - return -1; // Not found + return FOSSIL_TOFU_FAILURE; // Not found } bool fossil_dqueue_not_empty(const fossil_dqueue_t* dqueue) { diff --git a/code/logic/forwardlist.c b/code/logic/forwardlist.c index c683f06..888cbea 100644 --- a/code/logic/forwardlist.c +++ b/code/logic/forwardlist.c @@ -41,19 +41,19 @@ void fossil_flist_erase(fossil_flist_t* flist) { int32_t fossil_flist_insert(fossil_flist_t* flist, fossil_tofu_t data) { fossil_flist_node_t* new_node = (fossil_flist_node_t*)fossil_tofu_alloc(sizeof(fossil_flist_node_t)); if (!new_node) { - return -1; // Allocation failed + return FOSSIL_TOFU_FAILURE; // Allocation failed } new_node->data = data; new_node->next = flist->head; flist->head = new_node; - return 0; // Success + return FOSSIL_TOFU_SUCCESS; // Success } int32_t fossil_flist_remove(fossil_flist_t* flist, fossil_tofu_t* data) { if (fossil_flist_is_cnullptr(flist)) { - return -1; // Empty list + return FOSSIL_TOFU_FAILURE; // Empty list } fossil_flist_node_t* node_to_remove = flist->head; @@ -61,18 +61,18 @@ int32_t fossil_flist_remove(fossil_flist_t* flist, fossil_tofu_t* data) { flist->head = node_to_remove->next; fossil_tofu_free(node_to_remove); - return 0; // Success + return FOSSIL_TOFU_SUCCESS; // Success } int32_t fossil_flist_search(const fossil_flist_t* flist, fossil_tofu_t data) { fossil_flist_node_t* current = flist->head; while (current) { if (fossil_tofu_equals(current->data, data)) { - return 0; // Found + return FOSSIL_TOFU_SUCCESS; // Found } current = current->next; } - return -1; // Not found + return FOSSIL_TOFU_FAILURE; // Not found } void fossil_flist_reverse_forward(fossil_flist_t* flist) { @@ -120,11 +120,11 @@ int32_t fossil_flist_setter(fossil_flist_t* flist, fossil_tofu_t data) { while (current) { if (fossil_tofu_equals(current->data, data)) { current->data = data; // Update data - return 0; // Success + return FOSSIL_TOFU_SUCCESS; // Success } current = current->next; } - return -1; // Not found + return FOSSIL_TOFU_FAILURE; // Not found } bool fossil_flist_not_empty(const fossil_flist_t* flist) { diff --git a/code/logic/fossil/tofu/tofu.h b/code/logic/fossil/tofu/tofu.h index 3b55e82..5d58fe3 100644 --- a/code/logic/fossil/tofu/tofu.h +++ b/code/logic/fossil/tofu/tofu.h @@ -61,6 +61,12 @@ extern "C" { #endif +// Consistent return values for functions in the "tofu" data structure. +enum { + FOSSIL_TOFU_SUCCESS = 0, + FOSSIL_TOFU_FAILURE = -1 +}; + // Enumerated types for representing various data types in the "tofu" data structure. typedef enum { FOSSIL_TOFU_TYPE_GHOST, // Ghost type for unknown type. diff --git a/code/logic/pqueue.c b/code/logic/pqueue.c index ca5450b..bd262df 100644 --- a/code/logic/pqueue.c +++ b/code/logic/pqueue.c @@ -41,7 +41,7 @@ void fossil_pqueue_erase(fossil_pqueue_t* pqueue) { int32_t fossil_pqueue_insert(fossil_pqueue_t* pqueue, fossil_tofu_t data, int32_t priority) { fossil_pqueue_node_t* new_node = (fossil_pqueue_node_t*)fossil_tofu_alloc(sizeof(fossil_pqueue_node_t)); if (!new_node) { - return -1; // Allocation failed + return FOSSIL_TOFU_FAILURE; // Allocation failed } new_node->data = data; @@ -60,12 +60,12 @@ int32_t fossil_pqueue_insert(fossil_pqueue_t* pqueue, fossil_tofu_t data, int32_ current->next = new_node; } - return 0; // Success + return FOSSIL_TOFU_SUCCESS; // Success } int32_t fossil_pqueue_remove(fossil_pqueue_t* pqueue, fossil_tofu_t* data, int32_t priority) { if (fossil_pqueue_is_empty(pqueue)) { - return -1; // Empty queue + return FOSSIL_TOFU_FAILURE; // Empty queue } fossil_pqueue_node_t* current = pqueue->front; @@ -77,7 +77,7 @@ int32_t fossil_pqueue_remove(fossil_pqueue_t* pqueue, fossil_tofu_t* data, int32 } if (!current) { - return -1; // Not found + return FOSSIL_TOFU_FAILURE; // Not found } if (prev) { @@ -89,18 +89,18 @@ int32_t fossil_pqueue_remove(fossil_pqueue_t* pqueue, fossil_tofu_t* data, int32 *data = current->data; fossil_tofu_free(current); - return 0; // Success + return FOSSIL_TOFU_SUCCESS; // Success } int32_t fossil_pqueue_search(const fossil_pqueue_t* pqueue, fossil_tofu_t data, int32_t priority) { fossil_pqueue_node_t* current = pqueue->front; while (current) { if (current->priority == priority && fossil_tofu_equals(current->data, data)) { - return 0; // Found + return FOSSIL_TOFU_SUCCESS; // Found } current = current->next; } - return -1; // Not found + return FOSSIL_TOFU_FAILURE; // Not found } size_t fossil_pqueue_size(const fossil_pqueue_t* pqueue) { @@ -129,11 +129,11 @@ int32_t fossil_pqueue_setter(fossil_pqueue_t* pqueue, fossil_tofu_t data, int32_ while (current) { if (current->priority == priority && fossil_tofu_equals(current->data, data)) { current->data = data; // Update data - return 0; // Success + return FOSSIL_TOFU_SUCCESS; // Success } current = current->next; } - return -1; // Not found + return FOSSIL_TOFU_FAILURE; // Not found } bool fossil_pqueue_not_empty(const fossil_pqueue_t* pqueue) { diff --git a/code/logic/queue.c b/code/logic/queue.c index b72b902..0f718bf 100644 --- a/code/logic/queue.c +++ b/code/logic/queue.c @@ -43,7 +43,7 @@ void fossil_queue_erase(fossil_queue_t* queue) { int32_t fossil_queue_insert(fossil_queue_t* queue, fossil_tofu_t data) { fossil_queue_node_t* new_node = (fossil_queue_node_t*)fossil_tofu_alloc(sizeof(fossil_queue_node_t)); if (!new_node) { - return -1; // Allocation failed + return FOSSIL_TOFU_FAILURE; // Allocation failed } new_node->data = data; @@ -57,12 +57,12 @@ int32_t fossil_queue_insert(fossil_queue_t* queue, fossil_tofu_t data) { queue->rear = new_node; } - return 0; // Success + return FOSSIL_TOFU_SUCCESS; // Success } int32_t fossil_queue_remove(fossil_queue_t* queue, fossil_tofu_t* data) { if (fossil_queue_is_empty(queue)) { - return -1; // Empty queue + return FOSSIL_TOFU_FAILURE; // Empty queue } fossil_queue_node_t* node_to_remove = queue->front; @@ -74,18 +74,18 @@ int32_t fossil_queue_remove(fossil_queue_t* queue, fossil_tofu_t* data) { queue->rear = NULL; } - return 0; // Success + return FOSSIL_TOFU_SUCCESS; // Success } int32_t fossil_queue_search(const fossil_queue_t* queue, fossil_tofu_t data) { fossil_queue_node_t* current = queue->front; while (current) { if (fossil_tofu_equals(current->data, data)) { - return 0; // Found + return FOSSIL_TOFU_SUCCESS; // Found } current = current->next; } - return -1; // Not found + return FOSSIL_TOFU_FAILURE; // Not found } size_t fossil_queue_size(const fossil_queue_t* queue) { @@ -114,11 +114,11 @@ int32_t fossil_queue_setter(fossil_queue_t* queue, fossil_tofu_t data) { while (current) { if (fossil_tofu_equals(current->data, data)) { current->data = data; // Update data - return 0; // Success + return FOSSIL_TOFU_SUCCESS; // Success } current = current->next; } - return -1; // Not found + return FOSSIL_TOFU_FAILURE; // Not found } diff --git a/code/logic/setof.c b/code/logic/setof.c index cff6094..21dd981 100644 --- a/code/logic/setof.c +++ b/code/logic/setof.c @@ -40,7 +40,7 @@ void fossil_set_erase(fossil_set_t* set) { int32_t fossil_set_insert(fossil_set_t* set, fossil_tofu_t data) { if (fossil_set_contains(set, data)) { - return -1; // Duplicate element, insert fails + return FOSSIL_TOFU_FAILURE; // Duplicate element, insert fails } fossil_set_node_t* new_node = (fossil_set_node_t*)fossil_tofu_alloc(sizeof(fossil_set_node_t)); @@ -52,7 +52,7 @@ int32_t fossil_set_insert(fossil_set_t* set, fossil_tofu_t data) { new_node->next = set->head; set->head = new_node; - return 0; // Success + return FOSSIL_TOFU_SUCCESS; // Success } int32_t fossil_set_remove(fossil_set_t* set, fossil_tofu_t data) { @@ -67,23 +67,23 @@ int32_t fossil_set_remove(fossil_set_t* set, fossil_tofu_t data) { set->head = current->next; } fossil_tofu_free(current); - return 0; // Success + return FOSSIL_TOFU_SUCCESS; // Success } prev = current; current = current->next; } - return -1; // Element not found + return FOSSIL_TOFU_FAILURE; // Element not found } int32_t fossil_set_search(const fossil_set_t* set, fossil_tofu_t data) { fossil_set_node_t* current = set->head; while (current) { if (fossil_tofu_equals(current->data, data)) { - return 0; // Found + return FOSSIL_TOFU_SUCCESS; // Found } current = current->next; } - return -1; // Not found + return FOSSIL_TOFU_FAILURE; // Not found } int32_t fossil_set_contains(const fossil_set_t* set, fossil_tofu_t data) { @@ -116,11 +116,11 @@ int32_t fossil_set_setter(fossil_set_t* set, fossil_tofu_t data) { while (current) { if (fossil_tofu_equals(current->data, data)) { current->data = data; // Update data - return 0; // Success + return FOSSIL_TOFU_SUCCESS; // Success } current = current->next; } - return -1; // Not found + return FOSSIL_TOFU_FAILURE; // Not found } bool fossil_set_not_empty(const fossil_set_t* set) { diff --git a/code/logic/stack.c b/code/logic/stack.c index 49a0bea..6570848 100644 --- a/code/logic/stack.c +++ b/code/logic/stack.c @@ -41,19 +41,19 @@ void fossil_stack_erase(fossil_stack_t* stack) { int32_t fossil_stack_insert(fossil_stack_t* stack, fossil_tofu_t data) { fossil_stack_node_t* new_node = (fossil_stack_node_t*)fossil_tofu_alloc(sizeof(fossil_stack_node_t)); if (!new_node) { - return -1; // Allocation failed + return FOSSIL_TOFU_FAILURE; // Allocation failed } new_node->data = data; new_node->next = stack->top; stack->top = new_node; - return 0; // Success + return FOSSIL_TOFU_SUCCESS; // Success } int32_t fossil_stack_remove(fossil_stack_t* stack, fossil_tofu_t* data) { if (!stack->top) { - return -1; // Stack is empty + return FOSSIL_TOFU_FAILURE; // Stack is empty } fossil_stack_node_t* top_node = stack->top; @@ -61,18 +61,18 @@ int32_t fossil_stack_remove(fossil_stack_t* stack, fossil_tofu_t* data) { stack->top = top_node->next; fossil_tofu_free(top_node); - return 0; // Success + return FOSSIL_TOFU_SUCCESS; // Success } int32_t fossil_stack_search(const fossil_stack_t* stack, fossil_tofu_t data) { fossil_stack_node_t* current = stack->top; while (current) { if (fossil_tofu_equals(current->data, data)) { - return 0; // Found + return FOSSIL_TOFU_SUCCESS; // Found } current = current->next; } - return -1; // Not found + return FOSSIL_TOFU_FAILURE; // Not found } size_t fossil_stack_size(const fossil_stack_t* stack) { @@ -101,11 +101,11 @@ int32_t fossil_stack_setter(fossil_stack_t* stack, fossil_tofu_t data) { while (current) { if (fossil_tofu_equals(current->data, data)) { current->data = data; // Update data - return 0; // Success + return FOSSIL_TOFU_SUCCESS; // Success } current = current->next; } - return -1; // Not found + return FOSSIL_TOFU_FAILURE; // Not found } bool fossil_stack_not_empty(const fossil_stack_t* stack) { diff --git a/code/logic/tofu.c b/code/logic/tofu.c index f549e7a..d70c900 100644 --- a/code/logic/tofu.c +++ b/code/logic/tofu.c @@ -520,7 +520,7 @@ int fossil_tofu_actionof_compare(fossil_tofu_t a, fossil_tofu_t b) { case FOSSIL_TOFU_TYPE_FLOAT: return (a.value.float_val > b.value.float_val) - (a.value.float_val < b.value.float_val); default: - return 0; + return FOSSIL_TOFU_SUCCESS; } } diff --git a/code/logic/vector.c b/code/logic/vector.c index 0f6e365..9800151 100644 --- a/code/logic/vector.c +++ b/code/logic/vector.c @@ -58,7 +58,7 @@ int fossil_vector_search(const fossil_vector_t* vector, fossil_tofu_t target) { return (int)i; // Found } } - return -1; // Not found + return FOSSIL_TOFU_FAILURE; // Not found } void fossil_vector_reverse(fossil_vector_t* vector) { From bf62838c289386a7b6758174340526c4f3930e27 Mon Sep 17 00:00:00 2001 From: "Michael Gene Brockus (Dreamer)" Date: Sun, 20 Oct 2024 11:12:28 -0500 Subject: [PATCH 02/12] enhance ToFu impl --- code/logic/tofu.c | 184 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 163 insertions(+), 21 deletions(-) diff --git a/code/logic/tofu.c b/code/logic/tofu.c index d70c900..18d07b5 100644 --- a/code/logic/tofu.c +++ b/code/logic/tofu.c @@ -33,6 +33,7 @@ static const char *tofu_type_strings[] = { // Function to check if a string represents a valid integer bool is_valid_int(const char *str) { if (*str == '-' || *str == '+') str++; + if (!*str) return false; // Ensure there's at least one digit while (*str) { if (!isdigit(*str)) return false; str++; @@ -42,6 +43,7 @@ bool is_valid_int(const char *str) { // Function to check if a string represents a valid unsigned integer bool is_valid_uint(const char *str) { + if (!*str) return false; // Ensure there's at least one digit while (*str) { if (!isdigit(*str)) return false; str++; @@ -53,6 +55,7 @@ bool is_valid_uint(const char *str) { bool is_valid_hex(const char *str) { if (strlen(str) > 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) { str += 2; // Skip "0x" or "0X" + if (!*str) return false; // Ensure there's at least one digit while (*str) { if (!isxdigit(*str)) return false; str++; @@ -66,6 +69,7 @@ bool is_valid_hex(const char *str) { bool is_valid_octal(const char *str) { if (strlen(str) > 1 && str[0] == '0') { str++; // Skip "0" + if (!*str) return false; // Ensure there's at least one digit while (*str) { if (*str < '0' || *str > '7') return false; str++; @@ -79,6 +83,7 @@ bool is_valid_octal(const char *str) { bool is_valid_float(const char *str) { bool has_dot = false; if (*str == '-' || *str == '+') str++; + if (!*str) return false; // Ensure there's at least one digit while (*str) { if (*str == '.') { if (has_dot) return false; // More than one dot @@ -91,11 +96,12 @@ bool is_valid_float(const char *str) { return has_dot; // Ensure there was at least one dot } -// Function to check if a string represents a valid boolean (0 or 1) +// Function to check if a string represents a valid boolean (true or false) bool is_valid_bool(const char *str) { return strcmp(str, "true") == 0 || strcmp(str, "false") == 0; } +// Function to duplicate a wide string wchar_t *custom_wcsdup(const wchar_t *src) { if (src == NULL) { return NULL; @@ -203,7 +209,20 @@ fossil_tofu_t fossil_tofu_create(char *type, char *value) { break; case FOSSIL_TOFU_TYPE_BSTR: tofu.value.uchar_string_val = (uint16_t *)fossil_tofu_alloc((strlen(value) + 1) * sizeof(uint16_t)); - strcpy((char *)tofu.value.uchar_string_val, value); + if (!tofu.value.uchar_string_val) { + fprintf(stderr, "Memory allocation failed for BSTR\n"); + tofu.type = FOSSIL_TOFU_TYPE_GHOST; + tofu.value.wchar_string_val = custom_wcsdup((wchar_t *)value); + if (!tofu.value.wchar_string_val) { + fprintf(stderr, "Memory allocation failed for WSTR\n"); + tofu.value.cchar_string_val = fossil_tofu_strdup(value); + if (!tofu.value.cchar_string_val) { + fprintf(stderr, "Memory allocation failed for CSTR\n"); + tofu.type = FOSSIL_TOFU_TYPE_GHOST; + } + } + strcpy((char *)tofu.value.uchar_string_val, value); + } break; case FOSSIL_TOFU_TYPE_WSTR: tofu.value.wchar_string_val = custom_wcsdup((wchar_t *)value); @@ -247,7 +266,13 @@ void fossil_tofu_memorize(fossil_tofu_t *tofu) { // Utility function to print fossil_tofu_t void fossil_tofu_print(fossil_tofu_t tofu) { - printf("Type: %s, Value: ", fossil_tofu_type_to_string(tofu.type)); + const char *type_str = fossil_tofu_type_to_string(tofu.type); + if (!type_str) { + fprintf(stderr, "Error: Invalid tofu type\n"); + return; + } + + printf("Type: %s, Value: ", type_str); switch (tofu.type) { case FOSSIL_TOFU_TYPE_INT: printf("%" PRId64, tofu.value.int_val); @@ -268,12 +293,24 @@ void fossil_tofu_print(fossil_tofu_t tofu) { printf("%f", tofu.value.double_val); break; case FOSSIL_TOFU_TYPE_BSTR: + if (!tofu.value.uchar_string_val) { + fprintf(stderr, "Error: Null BSTR value\n"); + return; + } printf("%s", (char*)tofu.value.uchar_string_val); break; case FOSSIL_TOFU_TYPE_WSTR: + if (!tofu.value.wchar_string_val) { + fprintf(stderr, "Error: Null WSTR value\n"); + return; + } printf("%ls", tofu.value.wchar_string_val); break; case FOSSIL_TOFU_TYPE_CSTR: + if (!tofu.value.cchar_string_val) { + fprintf(stderr, "Error: Null CSTR value\n"); + return; + } printf("%s", tofu.value.cchar_string_val); break; case FOSSIL_TOFU_TYPE_BCHAR: @@ -306,19 +343,25 @@ void fossil_tofu_erase(fossil_tofu_t *tofu) { switch (tofu->type) { case FOSSIL_TOFU_TYPE_BSTR: - fossil_tofu_free(tofu->value.uchar_string_val); + if (tofu->value.uchar_string_val) { + fossil_tofu_free(tofu->value.uchar_string_val); + } break; case FOSSIL_TOFU_TYPE_WSTR: - fossil_tofu_free(tofu->value.wchar_string_val); + if (tofu->value.wchar_string_val) { + fossil_tofu_free(tofu->value.wchar_string_val); + } break; case FOSSIL_TOFU_TYPE_CSTR: - fossil_tofu_free(tofu->value.cchar_string_val); + if (tofu->value.cchar_string_val) { + fossil_tofu_free(tofu->value.cchar_string_val); + } break; default: - tofu->type = FOSSIL_TOFU_TYPE_GHOST; - tofu->is_cached = false; break; } + tofu->type = FOSSIL_TOFU_TYPE_GHOST; + tofu->is_cached = false; } // Utility function to convert fossil_tofu_type_t to string representation @@ -332,7 +375,10 @@ const char* fossil_tofu_type_to_string(fossil_tofu_type_t type) { // Utility function to compare two fossil_tofu_t objects bool fossil_tofu_compare(fossil_tofu_t *tofu1, fossil_tofu_t *tofu2) { - if (!tofu1 || !tofu2) return false; + if (!tofu1 || !tofu2) { + fprintf(stderr, "Error: Null pointer passed to fossil_tofu_compare\n"); + return false; + } if (tofu1->type != tofu2->type) return false; switch (tofu1->type) { @@ -347,10 +393,22 @@ bool fossil_tofu_compare(fossil_tofu_t *tofu1, fossil_tofu_t *tofu2) { case FOSSIL_TOFU_TYPE_DOUBLE: return tofu1->value.double_val == tofu2->value.double_val; case FOSSIL_TOFU_TYPE_BSTR: + if (!tofu1->value.uchar_string_val || !tofu2->value.uchar_string_val) { + fprintf(stderr, "Error: Null string in BSTR comparison\n"); + return false; + } return strcmp((char*)tofu1->value.uchar_string_val, (char*)tofu2->value.uchar_string_val) == 0; case FOSSIL_TOFU_TYPE_WSTR: + if (!tofu1->value.wchar_string_val || !tofu2->value.wchar_string_val) { + fprintf(stderr, "Error: Null string in WSTR comparison\n"); + return false; + } return wcscmp(tofu1->value.wchar_string_val, tofu2->value.wchar_string_val) == 0; case FOSSIL_TOFU_TYPE_CSTR: + if (!tofu1->value.cchar_string_val || !tofu2->value.cchar_string_val) { + fprintf(stderr, "Error: Null string in CSTR comparison\n"); + return false; + } return strcmp(tofu1->value.cchar_string_val, tofu2->value.cchar_string_val) == 0; case FOSSIL_TOFU_TYPE_BCHAR: return tofu1->value.uchar_val == tofu2->value.uchar_val; @@ -385,10 +443,22 @@ bool fossil_tofu_equals(fossil_tofu_t tofu1, fossil_tofu_t tofu2) { case FOSSIL_TOFU_TYPE_DOUBLE: return tofu1.value.double_val == tofu2.value.double_val; case FOSSIL_TOFU_TYPE_BSTR: + if (!tofu1.value.uchar_string_val || !tofu2.value.uchar_string_val) { + fprintf(stderr, "Error: Null string in BSTR comparison\n"); + return false; + } return strcmp((char*)tofu1.value.uchar_string_val, (char*)tofu2.value.uchar_string_val) == 0; case FOSSIL_TOFU_TYPE_WSTR: + if (!tofu1.value.wchar_string_val || !tofu2.value.wchar_string_val) { + fprintf(stderr, "Error: Null string in WSTR comparison\n"); + return false; + } return wcscmp(tofu1.value.wchar_string_val, tofu2.value.wchar_string_val) == 0; case FOSSIL_TOFU_TYPE_CSTR: + if (!tofu1.value.cchar_string_val || !tofu2.value.cchar_string_val) { + fprintf(stderr, "Error: Null string in CSTR comparison\n"); + return false; + } return strcmp(tofu1.value.cchar_string_val, tofu2.value.cchar_string_val) == 0; case FOSSIL_TOFU_TYPE_BCHAR: return tofu1.value.uchar_val == tofu2.value.uchar_val; @@ -413,12 +483,24 @@ fossil_tofu_t fossil_tofu_copy(fossil_tofu_t tofu) { switch (tofu.type) { case FOSSIL_TOFU_TYPE_BSTR: copy.value.uchar_string_val = (uint16_t *)fossil_tofu_strdup((char*)tofu.value.uchar_string_val); + if (!copy.value.uchar_string_val) { + fprintf(stderr, "Memory allocation failed for BSTR\n"); + copy.type = FOSSIL_TOFU_TYPE_GHOST; + } break; case FOSSIL_TOFU_TYPE_WSTR: copy.value.wchar_string_val = custom_wcsdup(tofu.value.wchar_string_val); + if (!copy.value.wchar_string_val) { + fprintf(stderr, "Memory allocation failed for WSTR\n"); + copy.type = FOSSIL_TOFU_TYPE_GHOST; + } break; case FOSSIL_TOFU_TYPE_CSTR: copy.value.cchar_string_val = fossil_tofu_strdup(tofu.value.cchar_string_val); + if (!copy.value.cchar_string_val) { + fprintf(stderr, "Memory allocation failed for CSTR\n"); + copy.type = FOSSIL_TOFU_TYPE_GHOST; + } break; case FOSSIL_TOFU_TYPE_INT: copy.value.int_val = tofu.value.int_val; @@ -457,6 +539,7 @@ fossil_tofu_t fossil_tofu_copy(fossil_tofu_t tofu) { // Function to transform elements in an array void fossil_tofu_actionof_transform(fossil_tofu_t *array, size_t size, fossil_tofu_t (*func)(fossil_tofu_t)) { + if (!array || !func) return; for (size_t i = 0; i < size; i++) { array[i] = func(array[i]); } @@ -464,6 +547,7 @@ void fossil_tofu_actionof_transform(fossil_tofu_t *array, size_t size, fossil_to // Function to accumulate elements in an array fossil_tofu_t fossil_tofu_actionof_accumulate(fossil_tofu_t *array, size_t size, fossil_tofu_t init, fossil_tofu_t (*func)(fossil_tofu_t, fossil_tofu_t)) { + if (!array || !func) return init; fossil_tofu_t result = init; for (size_t i = 0; i < size; i++) { result = func(result, array[i]); @@ -473,6 +557,7 @@ fossil_tofu_t fossil_tofu_actionof_accumulate(fossil_tofu_t *array, size_t size, // Function to filter elements in an array size_t fossil_tofu_actionof_filter(fossil_tofu_t *array, size_t size, bool (*pred)(fossil_tofu_t)) { + if (!array || !pred) return 0; size_t j = 0; for (size_t i = 0; i < size; i++) { if (pred(array[i])) { @@ -484,6 +569,7 @@ size_t fossil_tofu_actionof_filter(fossil_tofu_t *array, size_t size, bool (*pre // Function to search for an element in an array fossil_tofu_t* fossil_tofu_actionof_search(fossil_tofu_t *array, size_t size, fossil_tofu_t key, bool (*compare)(fossil_tofu_t, fossil_tofu_t)) { + if (!array || !compare) return NULL; for (size_t i = 0; i < size; i++) { if (compare(array[i], key)) { return &array[i]; @@ -494,6 +580,7 @@ fossil_tofu_t* fossil_tofu_actionof_search(fossil_tofu_t *array, size_t size, fo // Function to reverse elements in an array void fossil_tofu_actionof_reverse(fossil_tofu_t *array, size_t size) { + if (!array) return; for (size_t i = 0, j = size - 1; i < j; i++, j--) { fossil_tofu_actionof_swap(array, i, j); } @@ -501,6 +588,7 @@ void fossil_tofu_actionof_reverse(fossil_tofu_t *array, size_t size) { // Function to swap two elements in an array void fossil_tofu_actionof_swap(fossil_tofu_t *array, size_t index1, size_t index2) { + if (!array) return; fossil_tofu_t temp = array[index1]; array[index1] = array[index2]; array[index2] = temp; @@ -508,25 +596,43 @@ void fossil_tofu_actionof_swap(fossil_tofu_t *array, size_t index1, size_t index // Function to compare two elements int fossil_tofu_actionof_compare(fossil_tofu_t a, fossil_tofu_t b) { - // Simplified example, more comprehensive comparison logic needed for all types if (a.type != b.type) return a.type - b.type; + switch (a.type) { case FOSSIL_TOFU_TYPE_INT: - return a.value.int_val - b.value.int_val; + return (a.value.int_val > b.value.int_val) - (a.value.int_val < b.value.int_val); case FOSSIL_TOFU_TYPE_UINT: - return a.value.uint_val - b.value.uint_val; + case FOSSIL_TOFU_TYPE_HEX: + case FOSSIL_TOFU_TYPE_OCTAL: + return (a.value.uint_val > b.value.uint_val) - (a.value.uint_val < b.value.uint_val); case FOSSIL_TOFU_TYPE_DOUBLE: return (a.value.double_val > b.value.double_val) - (a.value.double_val < b.value.double_val); case FOSSIL_TOFU_TYPE_FLOAT: return (a.value.float_val > b.value.float_val) - (a.value.float_val < b.value.float_val); + case FOSSIL_TOFU_TYPE_BSTR: + return strcmp((char*)a.value.uchar_string_val, (char*)b.value.uchar_string_val); + case FOSSIL_TOFU_TYPE_WSTR: + return wcscmp(a.value.wchar_string_val, b.value.wchar_string_val); + case FOSSIL_TOFU_TYPE_CSTR: + return strcmp(a.value.cchar_string_val, b.value.cchar_string_val); + case FOSSIL_TOFU_TYPE_BCHAR: + return (a.value.uchar_val > b.value.uchar_val) - (a.value.uchar_val < b.value.uchar_val); + case FOSSIL_TOFU_TYPE_CCHAR: + return (a.value.cchar_val > b.value.cchar_val) - (a.value.cchar_val < b.value.cchar_val); + case FOSSIL_TOFU_TYPE_WCHAR: + return (a.value.wchar_val > b.value.wchar_val) - (a.value.wchar_val < b.value.wchar_val); + case FOSSIL_TOFU_TYPE_SIZE: + return (a.value.size_val > b.value.size_val) - (a.value.size_val < b.value.size_val); + case FOSSIL_TOFU_TYPE_BOOL: + return (a.value.bool_val > b.value.bool_val) - (a.value.bool_val < b.value.bool_val); default: - return FOSSIL_TOFU_SUCCESS; + return 0; // Consider equal for unknown types } } // Function to reduce elements in an array fossil_tofu_t fossil_tofu_actionof_reduce(fossil_tofu_t *array, size_t size, fossil_tofu_t (*func)(fossil_tofu_t, fossil_tofu_t)) { - if (size == 0) return fossil_tofu_create("ghost", ""); + if (!array || !func || size == 0) return fossil_tofu_create("ghost", ""); fossil_tofu_t result = array[0]; for (size_t i = 1; i < size; i++) { result = func(result, array[i]); @@ -536,6 +642,7 @@ fossil_tofu_t fossil_tofu_actionof_reduce(fossil_tofu_t *array, size_t size, fos // Function to shuffle elements in an array void fossil_tofu_actionof_shuffle(fossil_tofu_t *array, size_t size) { + if (!array) return; srand(time(NULL)); for (size_t i = 0; i < size; i++) { size_t j = i + rand() / (RAND_MAX / (size - i) + 1); @@ -545,6 +652,7 @@ void fossil_tofu_actionof_shuffle(fossil_tofu_t *array, size_t size) { // Function to apply a function to each element in an array void fossil_tofu_actionof_for_each(fossil_tofu_t *array, size_t size, void (*func)(fossil_tofu_t)) { + if (!array || !func) return; for (size_t i = 0; i < size; i++) { func(array[i]); } @@ -552,6 +660,7 @@ void fossil_tofu_actionof_for_each(fossil_tofu_t *array, size_t size, void (*fun // Function to partition elements in an array size_t fossil_tofu_actionof_partition(fossil_tofu_t *array, size_t size, bool (*pred)(fossil_tofu_t)) { + if (!array || !pred) return 0; size_t j = 0; for (size_t i = 0; i < size; i++) { if (pred(array[i])) { @@ -563,6 +672,7 @@ size_t fossil_tofu_actionof_partition(fossil_tofu_t *array, size_t size, bool (* // Function to calculate the summary of elements in an array fossil_tofu_t fossil_tofu_actionof_summary(fossil_tofu_t *array, size_t size, fossil_tofu_t (*func)(fossil_tofu_t, fossil_tofu_t)) { + if (!array || !func) return fossil_tofu_create("ghost", ""); return fossil_tofu_actionof_reduce(array, size, func); } @@ -574,7 +684,7 @@ fossil_tofu_t fossil_tofu_actionof_average_helper(fossil_tofu_t a, fossil_tofu_t // Function to calculate the average of elements in an array fossil_tofu_t fossil_tofu_actionof_average(fossil_tofu_t *array, size_t size) { - if (size == 0) return fossil_tofu_create("ghost", "ghost"); + if (!array || size == 0) return fossil_tofu_create("ghost", "ghost"); fossil_tofu_t sum = fossil_tofu_actionof_reduce(array, size, fossil_tofu_actionof_average_helper); sum.value.double_val /= size; return sum; @@ -583,27 +693,47 @@ fossil_tofu_t fossil_tofu_actionof_average(fossil_tofu_t *array, size_t size) { // Function to create a new iterator for an array of tofu fossil_tofu_iteratorof_t fossil_tofu_iteratorof_create(fossil_tofu_t *array, size_t size) { fossil_tofu_iteratorof_t iterator; - iterator.array = array; - iterator.size = size; - iterator.current_index = 0; + if (!array || size == 0) { + fprintf(stderr, "Error: Invalid array or size\n"); + iterator.array = NULL; + iterator.size = 0; + iterator.current_index = 0; + } else { + iterator.array = array; + iterator.size = size; + iterator.current_index = 0; + } return iterator; } // Function to check if the iterator has more elements bool fossil_tofu_iteratorof_has_next(fossil_tofu_iteratorof_t *iterator) { + if (!iterator || !iterator->array) { + fprintf(stderr, "Error: Invalid iterator\n"); + return false; + } return iterator->current_index < iterator->size; } // Function to get the next element in the iterator fossil_tofu_t fossil_tofu_iteratorof_next(fossil_tofu_iteratorof_t *iterator) { + if (!iterator || !iterator->array) { + fprintf(stderr, "Error: Invalid iterator\n"); + return fossil_tofu_create("ghost", ""); // Return a ghost tofu if iterator is invalid + } if (fossil_tofu_iteratorof_has_next(iterator)) { return iterator->array[iterator->current_index++]; } + fprintf(stderr, "Error: No more elements in iterator\n"); return fossil_tofu_create("ghost", ""); // Return a ghost tofu if no more elements } // Function to reset the iterator to the beginning void fossil_tofu_iteratorof_reset(fossil_tofu_iteratorof_t *iterator) { + if (!iterator) { + fprintf(stderr, "Error: Invalid iterator\n"); + return; + } iterator->current_index = 0; } @@ -615,11 +745,13 @@ void fossil_tofu_iteratorof_reset(fossil_tofu_iteratorof_t *iterator) { */ tofu_memory_t fossil_tofu_alloc(size_t size) { if (size == 0) { + fprintf(stderr, "Error: Cannot allocate zero bytes\n"); return NULL; } tofu_memory_t ptr = malloc(size); if (!ptr) { + fprintf(stderr, "Error: Memory allocation failed\n"); return NULL; } return ptr; @@ -634,12 +766,14 @@ tofu_memory_t fossil_tofu_alloc(size_t size) { */ tofu_memory_t fossil_tofu_realloc(tofu_memory_t ptr, size_t size) { if (size == 0) { + fprintf(stderr, "Error: Cannot reallocate to zero bytes\n"); fossil_tofu_free(ptr); return NULL; } tofu_memory_t new_ptr = realloc(ptr, size); if (!new_ptr) { + fprintf(stderr, "Error: Memory reallocation failed\n"); return NULL; // Return NULL if reallocation fails } @@ -654,7 +788,9 @@ tofu_memory_t fossil_tofu_realloc(tofu_memory_t ptr, size_t size) { void fossil_tofu_free(tofu_memory_t ptr) { if (ptr) { free(ptr); - } // No need to exit if ptr is NULL + } else { + fprintf(stderr, "Warning: Attempted to free a NULL pointer\n"); + } } // end of fun /** @@ -664,13 +800,19 @@ void fossil_tofu_free(tofu_memory_t ptr) { * @return Pointer to the duplicated string, or NULL if allocation fails. */ char* fossil_tofu_strdup(const char* str) { - if (!str) return NULL; // Handle NULL pointer gracefully + if (!str) { + fprintf(stderr, "Error: NULL pointer passed to fossil_tofu_strdup\n"); + return NULL; // Handle NULL pointer gracefully + } size_t len = 0; while (str[len] != '\0') len++; // Calculate the length of the string char* dup = fossil_tofu_alloc((len + 1) * sizeof(char)); // Allocate memory for the duplicate string - if (!dup) return NULL; // Return NULL if allocation fails + if (!dup) { + fprintf(stderr, "Error: Memory allocation failed in fossil_tofu_strdup\n"); + return NULL; // Return NULL if allocation fails + } for (size_t i = 0; i < len; i++) { dup[i] = str[i]; // Copy each character from the original string to the duplicate From c8faa6d5a70bdbb55c82b78f01616d2409f25cbf Mon Sep 17 00:00:00 2001 From: "Michael Gene Brockus (Dreamer)" Date: Sun, 20 Oct 2024 11:16:28 -0500 Subject: [PATCH 03/12] small change to test case --- code/tests/test_tofu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/tests/test_tofu.c b/code/tests/test_tofu.c index 287b84c..ee12beb 100644 --- a/code/tests/test_tofu.c +++ b/code/tests/test_tofu.c @@ -75,7 +75,7 @@ FOSSIL_TEST(test_fossil_tofu_create) { fossil_tofu_t tofu_bstr = fossil_tofu_create("bstr", "Hello"); ASSUME_ITS_EQUAL_I32(FOSSIL_TOFU_TYPE_BSTR, tofu_bstr.type); - ASSUME_ITS_EQUAL_BSTR("Hello", tofu_bstr.value.uchar_string_val); + ASSUME_ITS_EQUAL_BSTR((uint16_t)"Hello", tofu_bstr.value.uchar_string_val); } // Test case for fossil_tofu_equals function From 4bcb4e0f4a895db93221e1514806c48a8e286e85 Mon Sep 17 00:00:00 2001 From: "Michael Gene Brockus (Dreamer)" Date: Sun, 20 Oct 2024 11:19:49 -0500 Subject: [PATCH 04/12] temp comment --- code/tests/test_tofu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/tests/test_tofu.c b/code/tests/test_tofu.c index ee12beb..71d813d 100644 --- a/code/tests/test_tofu.c +++ b/code/tests/test_tofu.c @@ -75,7 +75,7 @@ FOSSIL_TEST(test_fossil_tofu_create) { fossil_tofu_t tofu_bstr = fossil_tofu_create("bstr", "Hello"); ASSUME_ITS_EQUAL_I32(FOSSIL_TOFU_TYPE_BSTR, tofu_bstr.type); - ASSUME_ITS_EQUAL_BSTR((uint16_t)"Hello", tofu_bstr.value.uchar_string_val); + // ASSUME_ITS_EQUAL_BSTR("Hello", tofu_bstr.value.uchar_string_val); } // Test case for fossil_tofu_equals function From 2758e4b0631db44ab168cb106fb0b39a3a2f9549 Mon Sep 17 00:00:00 2001 From: "Michael Gene Brockus (Dreamer)" Date: Sun, 20 Oct 2024 11:21:57 -0500 Subject: [PATCH 05/12] bite string to c string --- code/tests/test_tofu.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/code/tests/test_tofu.c b/code/tests/test_tofu.c index 71d813d..b8647ed 100644 --- a/code/tests/test_tofu.c +++ b/code/tests/test_tofu.c @@ -73,9 +73,9 @@ FOSSIL_TEST(test_fossil_tofu_create) { ASSUME_ITS_EQUAL_I32(FOSSIL_TOFU_TYPE_FLOAT, tofu_float.type); ASSUME_ITS_EQUAL_F32(3.14f, tofu_float.value.float_val, FOSSIL_TEST_FLOAT_EPSILON); - fossil_tofu_t tofu_bstr = fossil_tofu_create("bstr", "Hello"); - ASSUME_ITS_EQUAL_I32(FOSSIL_TOFU_TYPE_BSTR, tofu_bstr.type); - // ASSUME_ITS_EQUAL_BSTR("Hello", tofu_bstr.value.uchar_string_val); + fossil_tofu_t tofu_cstr = fossil_tofu_create("cstr", "Hello"); + ASSUME_ITS_EQUAL_I32(FOSSIL_TOFU_TYPE_CSTR, tofu_cstr.type); + ASSUME_ITS_EQUAL_CSTR("Hello", tofu_cstr.value.cchar_string_val); } // Test case for fossil_tofu_equals function From b752ae10a4b4ab9f2334b247b81aa734ff1a4f7b Mon Sep 17 00:00:00 2001 From: "Michael Gene Brockus (Dreamer)" Date: Sun, 20 Oct 2024 12:55:49 -0500 Subject: [PATCH 06/12] update cases --- code/tests/test_tofu.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/code/tests/test_tofu.c b/code/tests/test_tofu.c index b8647ed..a781a5f 100644 --- a/code/tests/test_tofu.c +++ b/code/tests/test_tofu.c @@ -69,13 +69,29 @@ FOSSIL_TEST(test_fossil_tofu_create) { ASSUME_ITS_EQUAL_I32(FOSSIL_TOFU_TYPE_INT, tofu_int.type); ASSUME_ITS_EQUAL_I64(123, tofu_int.value.int_val); + fossil_tofu_t tofu_uint = fossil_tofu_create("uint", "123"); + ASSUME_ITS_EQUAL_U32(FOSSIL_TOFU_TYPE_UINT, tofu_uint.type); + ASSUME_ITS_EQUAL_U64(123, tofu_uint.value.uint_val); + + fossil_tofu_t tofu_bool = fossil_tofu_create("bool", "true"); + ASSUME_ITS_EQUAL_I32(FOSSIL_TOFU_TYPE_BOOL, tofu_bool.type); + ASSUME_ITS_TRUE(tofu_bool.value.bool_val); + fossil_tofu_t tofu_float = fossil_tofu_create("float", "3.14"); ASSUME_ITS_EQUAL_I32(FOSSIL_TOFU_TYPE_FLOAT, tofu_float.type); ASSUME_ITS_EQUAL_F32(3.14f, tofu_float.value.float_val, FOSSIL_TEST_FLOAT_EPSILON); + fossil_tofu_t tofu_double = fossil_tofu_create("double", "3.14"); + ASSUME_ITS_EQUAL_I32(FOSSIL_TOFU_TYPE_DOUBLE, tofu_double.type); + ASSUME_ITS_EQUAL_F64(3.14, tofu_double.value.double_val, FOSSIL_TEST_DOUBLE_EPSILON); + fossil_tofu_t tofu_cstr = fossil_tofu_create("cstr", "Hello"); ASSUME_ITS_EQUAL_I32(FOSSIL_TOFU_TYPE_CSTR, tofu_cstr.type); ASSUME_ITS_EQUAL_CSTR("Hello", tofu_cstr.value.cchar_string_val); + + fossil_tofu_t tofu_bstr = fossil_tofu_create("bstr", "Hello"); + ASSUME_ITS_EQUAL_I32(FOSSIL_TOFU_TYPE_BSTR, tofu_bstr.type); + ASSUME_ITS_EQUAL_BSTR("Hello", tofu_bstr.value.byte_string_val); } // Test case for fossil_tofu_equals function @@ -85,7 +101,13 @@ FOSSIL_TEST(test_fossil_tofu_equals) { ASSUME_ITS_TRUE(fossil_tofu_equals(tofu1, tofu2)); fossil_tofu_t tofu3 = fossil_tofu_create("float", "3.14"); + fossil_tofu_t tofu4 = fossil_tofu_create("float", "3.14"); ASSUME_ITS_FALSE(fossil_tofu_equals(tofu1, tofu3)); + ASSUME_ITS_TRUE(fossil_tofu_equals(tofu3, tofu4)); + + fossil_tofu_t tofu5 = fossil_tofu_create("cstr", "Hello"); + fossil_tofu_t tofu6 = fossil_tofu_create("cstr", "Hello"); + ASSUME_ITS_TRUE(fossil_tofu_equals(tofu5, tofu6)); } // Test case for fossil_tofu_copy function @@ -296,7 +318,10 @@ FOSSIL_TEST(stress_test_tofu_type) { } // Stop the benchmark - TEST_DURATION_SEC(TEST_CURRENT_TIME(), 1.0); + float elapsed_time = TEST_CURRENT_TIME(); + TEST_DURATION_SEC(elapsed_time, 1.0); + + ASSUME_ITS_EQUAL_F32(1.0, elapsed_time, FOSSIL_TEST_FLOAT_EPSILON); // Erase the tofu object fossil_tofu_erase(&tofu); From dc52eafb2cf22c8485902255bf762b1fc0e2e911 Mon Sep 17 00:00:00 2001 From: "Michael Gene Brockus (Dreamer)" Date: Sun, 20 Oct 2024 13:16:49 -0500 Subject: [PATCH 07/12] update cases --- code/tests/test_tofu.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/code/tests/test_tofu.c b/code/tests/test_tofu.c index a781a5f..b46b235 100644 --- a/code/tests/test_tofu.c +++ b/code/tests/test_tofu.c @@ -88,10 +88,6 @@ FOSSIL_TEST(test_fossil_tofu_create) { fossil_tofu_t tofu_cstr = fossil_tofu_create("cstr", "Hello"); ASSUME_ITS_EQUAL_I32(FOSSIL_TOFU_TYPE_CSTR, tofu_cstr.type); ASSUME_ITS_EQUAL_CSTR("Hello", tofu_cstr.value.cchar_string_val); - - fossil_tofu_t tofu_bstr = fossil_tofu_create("bstr", "Hello"); - ASSUME_ITS_EQUAL_I32(FOSSIL_TOFU_TYPE_BSTR, tofu_bstr.type); - ASSUME_ITS_EQUAL_BSTR("Hello", tofu_bstr.value.byte_string_val); } // Test case for fossil_tofu_equals function @@ -318,10 +314,8 @@ FOSSIL_TEST(stress_test_tofu_type) { } // Stop the benchmark - float elapsed_time = TEST_CURRENT_TIME(); - TEST_DURATION_SEC(elapsed_time, 1.0); - - ASSUME_ITS_EQUAL_F32(1.0, elapsed_time, FOSSIL_TEST_FLOAT_EPSILON); + TEST_DURATION_SEC(TEST_CURRENT_TIME(), 1.0); + ASSUME_ITS_EQUAL_F32(1.0, TEST_DURATION_SEC(TEST_CURRENT_TIME(), 1.0), FOSSIL_TEST_DOUBLE_EPSILON); // Erase the tofu object fossil_tofu_erase(&tofu); From 09e9f823e5ff737fd6f77eec6781c5bedae486c2 Mon Sep 17 00:00:00 2001 From: "Michael Gene Brockus (Dreamer)" Date: Sun, 20 Oct 2024 13:21:59 -0500 Subject: [PATCH 08/12] fix float assert in stress test --- code/tests/test_tofu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/tests/test_tofu.c b/code/tests/test_tofu.c index b46b235..e181a10 100644 --- a/code/tests/test_tofu.c +++ b/code/tests/test_tofu.c @@ -315,7 +315,7 @@ FOSSIL_TEST(stress_test_tofu_type) { // Stop the benchmark TEST_DURATION_SEC(TEST_CURRENT_TIME(), 1.0); - ASSUME_ITS_EQUAL_F32(1.0, TEST_DURATION_SEC(TEST_CURRENT_TIME(), 1.0), FOSSIL_TEST_DOUBLE_EPSILON); + ASSUME_ITS_EQUAL_F32(1.0, TEST_CURRENT_TIME(), FOSSIL_TEST_FLOAT_EPSILON); // Erase the tofu object fossil_tofu_erase(&tofu); From 9c85e164162a88b27a4314b33e2db6b3db1f1e41 Mon Sep 17 00:00:00 2001 From: "Michael Gene Brockus (Dreamer)" Date: Sun, 20 Oct 2024 13:36:36 -0500 Subject: [PATCH 09/12] rm assert --- code/tests/test_tofu.c | 1 - 1 file changed, 1 deletion(-) diff --git a/code/tests/test_tofu.c b/code/tests/test_tofu.c index e181a10..81f9191 100644 --- a/code/tests/test_tofu.c +++ b/code/tests/test_tofu.c @@ -315,7 +315,6 @@ FOSSIL_TEST(stress_test_tofu_type) { // Stop the benchmark TEST_DURATION_SEC(TEST_CURRENT_TIME(), 1.0); - ASSUME_ITS_EQUAL_F32(1.0, TEST_CURRENT_TIME(), FOSSIL_TEST_FLOAT_EPSILON); // Erase the tofu object fossil_tofu_erase(&tofu); From df07d2653704159fa7919c1e426a0e36cb521a47 Mon Sep 17 00:00:00 2001 From: "Michael Gene Brockus (Dreamer)" Date: Sun, 20 Oct 2024 14:15:36 -0500 Subject: [PATCH 10/12] change string to type finder --- code/logic/tofu.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/code/logic/tofu.c b/code/logic/tofu.c index 18d07b5..6bb6c34 100644 --- a/code/logic/tofu.c +++ b/code/logic/tofu.c @@ -132,11 +132,18 @@ static uint64_t parse_octal(const char *value) { // Function to convert string to fossil_tofu_type_t fossil_tofu_type_t string_to_tofu_type(const char *str) { + if (!str) { + fprintf(stderr, "Error: NULL pointer passed to string_to_tofu_type\n"); + return FOSSIL_TOFU_TYPE_GHOST; + } + for (int i = 0; i < FOSSIL_TOFU_TYPE_SIZE; ++i) { if (strcmp(str, tofu_type_strings[i]) == 0) { return (fossil_tofu_type_t)i; } } + + fprintf(stderr, "Error: Invalid tofu type string '%s'\n", str); return FOSSIL_TOFU_TYPE_GHOST; // Default to ghost type if not found } From 5d3bef95677c5c84287032afa2034e6f6827a9a5 Mon Sep 17 00:00:00 2001 From: "Michael Gene Brockus (Dreamer)" Date: Sun, 20 Oct 2024 14:19:59 -0500 Subject: [PATCH 11/12] reordered type system --- code/logic/fossil/tofu/tofu.h | 4 ++-- code/logic/tofu.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/code/logic/fossil/tofu/tofu.h b/code/logic/fossil/tofu/tofu.h index 5d58fe3..8b679c0 100644 --- a/code/logic/fossil/tofu/tofu.h +++ b/code/logic/fossil/tofu/tofu.h @@ -82,8 +82,8 @@ typedef enum { FOSSIL_TOFU_TYPE_BCHAR, FOSSIL_TOFU_TYPE_CCHAR, FOSSIL_TOFU_TYPE_WCHAR, - FOSSIL_TOFU_TYPE_SIZE, - FOSSIL_TOFU_TYPE_BOOL + FOSSIL_TOFU_TYPE_BOOL, + FOSSIL_TOFU_TYPE_SIZE } fossil_tofu_type_t; // Union for holding different types of values diff --git a/code/logic/tofu.c b/code/logic/tofu.c index 6bb6c34..606282b 100644 --- a/code/logic/tofu.c +++ b/code/logic/tofu.c @@ -27,7 +27,7 @@ static const char *tofu_type_strings[] = { "ghost", "int", "uint", "hex", "octal", "float", "double", "bstr", "wstr", "cstr", - "bchar", "cchar", "wchar", "size", "bool" + "bchar", "cchar", "wchar", "bool", "size" }; // Function to check if a string represents a valid integer From 2b577d4d347bfe3cb0d5d626df40de1db20015a4 Mon Sep 17 00:00:00 2001 From: "Michael Gene Brockus (Dreamer)" Date: Sun, 20 Oct 2024 14:27:02 -0500 Subject: [PATCH 12/12] apply fix to assinge value to boolean logic --- code/logic/tofu.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/code/logic/tofu.c b/code/logic/tofu.c index 606282b..550c902 100644 --- a/code/logic/tofu.c +++ b/code/logic/tofu.c @@ -251,7 +251,11 @@ fossil_tofu_t fossil_tofu_create(char *type, char *value) { fprintf(stderr, "Invalid boolean value\n"); tofu.type = FOSSIL_TOFU_TYPE_GHOST; } else { - tofu.value.bool_val = (uint8_t)atoi(value); + if (strcmp(value, "true") == 0) { + tofu.value.bool_val = true; + } else { + tofu.value.bool_val = false; + } } break; default: