Added unit tests for BigInt data type and updated docs

This commit is contained in:
2025-11-17 16:41:09 +01:00
parent f625862ad2
commit 9e419d09ac
11 changed files with 402 additions and 69 deletions

View File

@@ -16,15 +16,21 @@
#include "../src/bigint.h"
static void bigint_eq(const bigint_t *number, const char *expected) {
bigint_result_t to_str = bigint_to_string(number);
assert(to_str.status == BIGINT_OK);
assert(!strcmp(to_str.value.string_num, expected));
bigint_result_t exp_num_res = bigint_from_string(expected);
assert(exp_num_res.status == BIGINT_OK);
bigint_t *exp_num = exp_num_res.value.number;
bigint_result_t cmp_res = bigint_compare(number, exp_num);
assert(cmp_res.status == BIGINT_OK);
free(to_str.value.string_num);
const int8_t cmp = cmp_res.value.compare_status;
assert(cmp == 0);
bigint_destroy(exp_num);
}
// Test creating big integers from int
void test_bigint_from_int() {
void test_bigint_from_int(void) {
bigint_result_t res = bigint_from_int(0);
assert(res.status == BIGINT_OK);
@@ -43,21 +49,333 @@ void test_bigint_from_int() {
}
// Test creating big integers from string
void test_bigint_from_string() {
void test_bigint_from_string(void) {
bigint_result_t res = bigint_from_string("00000123");
assert(res.status == BIGINT_OK);
bigint_eq(res.value.number, "123");
bigint_destroy(res.value.number);
res = bigint_from_string("-00000456789");
assert(res.status == BIGINT_OK);
bigint_eq(res.value.number, "-456789");
bigint_destroy(res.value.number);
}
// Test sum between big integers
void test_bigint_add(void) {
bigint_result_t x = bigint_from_int(123);
bigint_result_t y = bigint_from_int(456);
assert(x.status == BIGINT_OK && y.status == BIGINT_OK);
bigint_result_t sum = bigint_add(x.value.number, y.value.number);
assert(sum.status == BIGINT_OK);
bigint_eq(sum.value.number, "579");
bigint_destroy(x.value.number);
bigint_destroy(y.value.number);
bigint_destroy(sum.value.number);
}
// Test difference between big numbers
void test_bigint_sub(void) {
bigint_result_t x = bigint_from_int(456);
bigint_result_t y = bigint_from_int(123);
assert(x.status == BIGINT_OK && y.status == BIGINT_OK);
bigint_result_t diff = bigint_sub(x.value.number, y.value.number);
assert(diff.status == BIGINT_OK);
bigint_eq(diff.value.number, "333");
bigint_destroy(x.value.number);
bigint_destroy(y.value.number);
bigint_destroy(diff.value.number);
}
// Test difference between big numbers with negative result
void test_bigint_sub_neg(void) {
bigint_result_t x = bigint_from_int(123);
bigint_result_t y = bigint_from_int(456);
assert(x.status == BIGINT_OK && y.status == BIGINT_OK);
bigint_result_t diff = bigint_sub(x.value.number, y.value.number);
assert(diff.status == BIGINT_OK);
bigint_eq(diff.value.number, "-333");
bigint_destroy(x.value.number);
bigint_destroy(y.value.number);
bigint_destroy(diff.value.number);
}
// Test difference between mixed big numbers
void test_bigint_sub_mixed(void) {
bigint_result_t x = bigint_from_int(456);
bigint_result_t y = bigint_from_int(-123);
assert(x.status == BIGINT_OK && y.status == BIGINT_OK);
bigint_result_t diff = bigint_sub(x.value.number, y.value.number);
assert(diff.status == BIGINT_OK);
bigint_eq(diff.value.number, "579");
bigint_destroy(x.value.number);
bigint_destroy(y.value.number);
bigint_destroy(diff.value.number);
}
// Test product between big numbers
void test_bigint_prod(void) {
bigint_result_t x = bigint_from_int(1234);
bigint_result_t y = bigint_from_int(56789);
assert(x.status == BIGINT_OK && y.status == BIGINT_OK);
bigint_result_t prod = bigint_prod(x.value.number, y.value.number);
assert(prod.status == BIGINT_OK);
bigint_eq(prod.value.number, "70077626");
bigint_destroy(x.value.number);
bigint_destroy(y.value.number);
bigint_destroy(prod.value.number);
}
// Test product between mixed negative big numbers
void test_bigint_prod_mixed(void) {
bigint_result_t x = bigint_from_int(-1234);
bigint_result_t y = bigint_from_int(56789);
assert(x.status == BIGINT_OK && y.status == BIGINT_OK);
bigint_result_t prod = bigint_prod(x.value.number, y.value.number);
assert(prod.status == BIGINT_OK);
bigint_eq(prod.value.number, "-70077626");
bigint_destroy(x.value.number);
bigint_destroy(y.value.number);
bigint_destroy(prod.value.number);
}
// Test product between negative big numbers
void test_bigint_prod_neg(void) {
bigint_result_t x = bigint_from_int(-1234);
bigint_result_t y = bigint_from_int(-56789);
assert(x.status == BIGINT_OK && y.status == BIGINT_OK);
bigint_result_t prod = bigint_prod(x.value.number, y.value.number);
assert(prod.status == BIGINT_OK);
bigint_eq(prod.value.number, "70077626");
bigint_destroy(x.value.number);
bigint_destroy(y.value.number);
bigint_destroy(prod.value.number);
}
// Test division between big numbers
void test_bigint_div(void) {
bigint_result_t x = bigint_from_int(100);
bigint_result_t y = bigint_from_int(2);
assert(x.status == BIGINT_OK && y.status == BIGINT_OK);
bigint_result_t div = bigint_divmod(x.value.number, y.value.number);
assert(div.status == BIGINT_OK);
bigint_t* const quotient = div.value.division.quotient;
bigint_t* const remainder = div.value.division.remainder;
bigint_eq(quotient, "50");
bigint_eq(remainder, "0");
bigint_destroy(quotient);
bigint_destroy(remainder);
bigint_destroy(x.value.number);
bigint_destroy(y.value.number);
}
// Test division between big numbers with negative dividend
// This library follows C-style divison such that sign(remainder) = sign(dividend)
void test_bigint_div_dividend(void) {
bigint_result_t x = bigint_from_int(-100);
bigint_result_t y = bigint_from_int(3);
assert(x.status == BIGINT_OK && y.status == BIGINT_OK);
bigint_result_t div = bigint_divmod(x.value.number, y.value.number);
assert(div.status == BIGINT_OK);
bigint_t* const quotient = div.value.division.quotient;
bigint_t* const remainder = div.value.division.remainder;
bigint_eq(quotient, "-33");
bigint_eq(remainder, "-1");
bigint_destroy(quotient);
bigint_destroy(remainder);
bigint_destroy(x.value.number);
bigint_destroy(y.value.number);
}
// Test division between big numbers with negative divisor
// This library follows C-style divison such that sign(remainder) = sign(dividend)
void test_bigint_div_divisor(void) {
bigint_result_t x = bigint_from_int(13);
bigint_result_t y = bigint_from_int(-4);
assert(x.status == BIGINT_OK && y.status == BIGINT_OK);
bigint_result_t div = bigint_divmod(x.value.number, y.value.number);
assert(div.status == BIGINT_OK);
bigint_t* const quotient = div.value.division.quotient;
bigint_t* const remainder = div.value.division.remainder;
bigint_eq(quotient, "-3");
bigint_eq(remainder, "1");
bigint_destroy(quotient);
bigint_destroy(remainder);
bigint_destroy(x.value.number);
bigint_destroy(y.value.number);
}
// Test division between big numbers with negative numbers
// This library follows C-style divison such that sign(remainder) = sign(dividend)
void test_bigint_div_neg(void) {
bigint_result_t x = bigint_from_int(-100);
bigint_result_t y = bigint_from_int(-3);
assert(x.status == BIGINT_OK && y.status == BIGINT_OK);
bigint_result_t div = bigint_divmod(x.value.number, y.value.number);
assert(div.status == BIGINT_OK);
bigint_t* const quotient = div.value.division.quotient;
bigint_t* const remainder = div.value.division.remainder;
bigint_eq(quotient, "33");
bigint_eq(remainder, "-1");
bigint_destroy(quotient);
bigint_destroy(remainder);
bigint_destroy(x.value.number);
bigint_destroy(y.value.number);
}
// Test division by zero
void test_bigint_div_by_zero(void) {
bigint_result_t x = bigint_from_int(-100);
bigint_result_t y = bigint_from_int(0);
assert(x.status == BIGINT_OK && y.status == BIGINT_OK);
bigint_result_t div = bigint_divmod(x.value.number, y.value.number);
assert(div.status == BIGINT_ERR_DIV_BY_ZERO);
bigint_destroy(x.value.number);
bigint_destroy(y.value.number);
}
// Test cloning of big numbers
void test_bigint_clone(void) {
bigint_result_t x = bigint_from_string("0010101010");
assert(x.status == BIGINT_OK);
bigint_result_t cloned = bigint_clone(x.value.number);
assert(cloned.status == BIGINT_OK);
bigint_eq(cloned.value.number, "10101010");
bigint_destroy(x.value.number);
bigint_destroy(cloned.value.number);
}
// Test comparison between equal numbers
void test_bigint_compare_eq(void) {
bigint_result_t x = bigint_from_int(123);
bigint_result_t y = bigint_from_int(123);
assert(x.status == BIGINT_OK);
assert(y.status == BIGINT_OK);
bigint_result_t cmp_res = bigint_compare(x.value.number, y.value.number);
assert(cmp_res.status == BIGINT_OK);
const int8_t cmp = cmp_res.value.compare_status;
assert(cmp == 0);
bigint_destroy(x.value.number);
bigint_destroy(y.value.number);
}
// Test comparison between numbers (less than)
void test_bigint_compare_lt(void) {
bigint_result_t x = bigint_from_int(-123);
bigint_result_t y = bigint_from_int(0);
assert(x.status == BIGINT_OK);
assert(y.status == BIGINT_OK);
bigint_result_t cmp_res = bigint_compare(x.value.number, y.value.number);
assert(cmp_res.status == BIGINT_OK);
const int8_t cmp = cmp_res.value.compare_status;
assert(cmp == -1);
bigint_destroy(x.value.number);
bigint_destroy(y.value.number);
}
// Test comparison between numbers (greater than)
void test_bigint_compare_gt(void) {
bigint_result_t x = bigint_from_int(123);
bigint_result_t y = bigint_from_int(-5);
assert(x.status == BIGINT_OK);
assert(y.status == BIGINT_OK);
bigint_result_t cmp_res = bigint_compare(x.value.number, y.value.number);
assert(cmp_res.status == BIGINT_OK);
const int8_t cmp = cmp_res.value.compare_status;
assert(cmp == 1);
bigint_destroy(x.value.number);
bigint_destroy(y.value.number);
}
int main(void) {
printf("=== Running BigInt unit tests ===\n\n");
TEST(bigint_from_int);
TEST(bigint_from_string);
TEST(bigint_add);
TEST(bigint_sub);
TEST(bigint_sub_neg);
TEST(bigint_sub_mixed);
TEST(bigint_prod);
TEST(bigint_prod_mixed);
TEST(bigint_prod_neg);
TEST(bigint_div);
TEST(bigint_div_dividend);
TEST(bigint_div_divisor);
TEST(bigint_div_neg);
TEST(bigint_div_by_zero);
TEST(bigint_clone);
TEST(bigint_compare_eq);
TEST(bigint_compare_lt);
TEST(bigint_compare_gt);
printf("\n=== All tests passed ===\n");
return 0;
}
}

View File

@@ -15,7 +15,7 @@
#include "../src/map.h"
// Create a new map
void test_map_new() {
void test_map_new(void) {
map_result_t res = map_new();
assert(res.status == MAP_OK);
@@ -27,7 +27,7 @@ void test_map_new() {
}
// Add elements to map
void test_map_add() {
void test_map_add(void) {
map_result_t res = map_new();
assert(res.status == MAP_OK);
@@ -47,7 +47,7 @@ void test_map_add() {
}
// Add multiple elements to the map
void test_map_add_multiple() {
void test_map_add_multiple(void) {
map_result_t res = map_new();
assert(res.status == MAP_OK);
@@ -68,7 +68,7 @@ void test_map_add_multiple() {
}
// Get map element
void test_map_get() {
void test_map_get(void) {
map_result_t res = map_new();
assert(res.status == MAP_OK);
@@ -85,7 +85,7 @@ void test_map_get() {
}
// Get non-existing key from map
void test_map_get_invalid() {
void test_map_get_invalid(void) {
map_result_t res = map_new();
assert(res.status == MAP_OK);
@@ -98,7 +98,7 @@ void test_map_get_invalid() {
}
// Map with heterogeneous types
void test_map_mixed() {
void test_map_mixed(void) {
map_result_t res = map_new();
assert(res.status == MAP_OK);
@@ -126,7 +126,7 @@ void test_map_mixed() {
}
// Update existing map key
void test_map_update() {
void test_map_update(void) {
map_result_t res = map_new();
assert(res.status == MAP_OK);
@@ -149,7 +149,7 @@ void test_map_update() {
}
// Remove an element from map
void test_map_remove() {
void test_map_remove(void) {
map_result_t res = map_new();
assert(res.status == MAP_OK);
@@ -177,7 +177,7 @@ void test_map_remove() {
}
// Remove non-existing key from map
void test_map_remove_invalid() {
void test_map_remove_invalid(void) {
map_result_t res = map_new();
assert(res.status == MAP_OK);
@@ -190,7 +190,7 @@ void test_map_remove_invalid() {
}
// Clear the map
void test_map_clear() {
void test_map_clear(void) {
map_result_t res = map_new();
assert(res.status == MAP_OK);
@@ -212,7 +212,7 @@ void test_map_clear() {
}
// Clear empty map
void test_map_clear_empty() {
void test_map_clear_empty(void) {
map_result_t res = map_new();
assert(res.status == MAP_OK);
@@ -226,7 +226,7 @@ void test_map_clear_empty() {
}
// Multiple operations in sequence (add, update, delete and clear)
void test_map_sequence() {
void test_map_sequence(void) {
map_result_t res = map_new();
assert(res.status == MAP_OK);
@@ -266,7 +266,7 @@ typedef struct {
short age;
} Person;
void test_map_struct() {
void test_map_struct(void) {
map_result_t res = map_new();
assert(res.status == MAP_OK);
@@ -298,7 +298,7 @@ void test_map_struct() {
}
// Test map capacity tracking
void test_map_cap() {
void test_map_cap(void) {
map_result_t res = map_new();
assert(res.status == MAP_OK);

View File

@@ -15,7 +15,7 @@
#include "../src/vector.h"
// Create a new vector
void test_vector_new() {
void test_vector_new(void) {
vector_result_t res = vector_new(5, sizeof(int));
assert(res.status == VECTOR_OK);
@@ -27,14 +27,14 @@ void test_vector_new() {
}
// Create a vector with zero capacity
void test_vector_new_zcap() {
void test_vector_new_zcap(void) {
vector_result_t res = vector_new(0, sizeof(int));
assert(res.status == VECTOR_ERR_ALLOCATE);
}
// Push elements to vector
void test_vector_push() {
void test_vector_push(void) {
vector_result_t res = vector_new(5, sizeof(int));
assert(res.status == VECTOR_OK);
@@ -54,7 +54,7 @@ void test_vector_push() {
}
// Trigger vector reallocation
void test_vector_push_realloc() {
void test_vector_push_realloc(void) {
vector_result_t res = vector_new(1, sizeof(int));
assert(res.status == VECTOR_OK);
@@ -72,7 +72,7 @@ void test_vector_push_realloc() {
}
// Get vector elements
void test_vector_get() {
void test_vector_get(void) {
vector_result_t res = vector_new(5, sizeof(int));
assert(res.status == VECTOR_OK);
@@ -89,7 +89,7 @@ void test_vector_get() {
}
// Test out of bounds
void test_vector_get_ofb() {
void test_vector_get_ofb(void) {
vector_result_t res = vector_new(5, sizeof(int));
assert(res.status == VECTOR_OK);
@@ -119,7 +119,7 @@ vector_order_t cmp_int_desc(const void *x, const void *y) {
return cmp_int_asc(y, x);
}
void test_vector_sort_int_asc() {
void test_vector_sort_int_asc(void) {
vector_result_t res = vector_new(5, sizeof(int));
assert(res.status == VECTOR_OK);
@@ -142,7 +142,7 @@ void test_vector_sort_int_asc() {
vector_destroy(v);
}
void test_vector_sort_int_desc() {
void test_vector_sort_int_desc(void) {
vector_result_t res = vector_new(5, sizeof(int));
assert(res.status == VECTOR_OK);
@@ -183,7 +183,7 @@ vector_order_t cmp_string_desc(const void *x, const void *y) {
return VECTOR_ORDER_EQ;
}
void test_vector_sort_string() {
void test_vector_sort_string(void) {
vector_result_t res = vector_new(5, sizeof(char*));
assert(res.status == VECTOR_OK);
@@ -234,7 +234,7 @@ vector_order_t cmp_person_by_name(const void *x, const void *y) {
return VECTOR_ORDER_EQ;
}
void test_vector_sort_struct_by_age() {
void test_vector_sort_struct_by_age(void) {
vector_result_t res = vector_new(5, sizeof(Person));
assert(res.status == VECTOR_OK);
@@ -266,7 +266,7 @@ void test_vector_sort_struct_by_age() {
vector_destroy(people);
}
void test_vector_sort_struct_by_name() {
void test_vector_sort_struct_by_name(void) {
vector_result_t res = vector_new(5, sizeof(Person));
assert(res.status == VECTOR_OK);
@@ -305,7 +305,7 @@ void test_vector_sort_struct_by_name() {
}
// Set vector element
void test_vector_set() {
void test_vector_set(void) {
vector_result_t res = vector_new(5, sizeof(int));
assert(res.status == VECTOR_OK);
@@ -324,7 +324,7 @@ void test_vector_set() {
}
// Set vector element out of bounds
void test_vector_set_ofb() {
void test_vector_set_ofb(void) {
vector_result_t res = vector_new(5, sizeof(int));
assert(res.status == VECTOR_OK);
@@ -340,7 +340,7 @@ void test_vector_set_ofb() {
}
// Pop element from vector
void test_vector_pop() {
void test_vector_pop(void) {
vector_result_t res = vector_new(5, sizeof(int));
assert(res.status == VECTOR_OK);
@@ -361,7 +361,7 @@ void test_vector_pop() {
}
// Test pop element from empty vector
void test_vector_pop_empty() {
void test_vector_pop_empty(void) {
vector_result_t res = vector_new(5, sizeof(int));
assert(res.status == VECTOR_OK);
@@ -374,7 +374,7 @@ void test_vector_pop_empty() {
}
// Clear vector
void test_vector_clear() {
void test_vector_clear(void) {
vector_result_t res = vector_new(5, sizeof(int));
assert(res.status == VECTOR_OK);
@@ -396,7 +396,7 @@ void test_vector_clear() {
}
// Multiple operations in sequence (push, set, pop and clear)
void test_vector_sequence() {
void test_vector_sequence(void) {
vector_result_t res = vector_new(2, sizeof(int));
assert(res.status == VECTOR_OK);
@@ -426,7 +426,7 @@ void test_vector_sequence() {
}
// Vector with chars
void test_vector_char() {
void test_vector_char(void) {
vector_result_t res = vector_new(5, sizeof(char));
assert(res.status == VECTOR_OK);
@@ -449,7 +449,7 @@ typedef struct {
int y;
} Point;
void test_vector_struct() {
void test_vector_struct(void) {
vector_result_t res = vector_new(5, sizeof(Point));
assert(res.status == VECTOR_OK);