Merge pull request #1 from ceticamarco/bignum_experimental
Adding BigInt support
This commit is contained in:
381
tests/test_bigint.c
Normal file
381
tests/test_bigint.c
Normal file
@@ -0,0 +1,381 @@
|
||||
/*
|
||||
* Unit tests for BigInt data type
|
||||
*/
|
||||
|
||||
#define TEST(NAME) do { \
|
||||
printf("Running test_%s...", #NAME); \
|
||||
test_##NAME(); \
|
||||
printf(" PASSED\n"); \
|
||||
} while(0)
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "../src/bigint.h"
|
||||
|
||||
static void bigint_eq(const bigint_t *number, const char *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);
|
||||
|
||||
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) {
|
||||
bigint_result_t res = bigint_from_int(0);
|
||||
|
||||
assert(res.status == BIGINT_OK);
|
||||
bigint_eq(res.value.number, "0");
|
||||
bigint_destroy(res.value.number);
|
||||
|
||||
res = bigint_from_int(10);
|
||||
assert(res.status == BIGINT_OK);
|
||||
bigint_eq(res.value.number, "10");
|
||||
bigint_destroy(res.value.number);
|
||||
|
||||
res = bigint_from_int(-12345678900LL);
|
||||
assert(res.status == BIGINT_OK);
|
||||
bigint_eq(res.value.number, "-12345678900");
|
||||
bigint_destroy(res.value.number);
|
||||
}
|
||||
|
||||
// Test creating big integers 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;
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
Binary file not shown.
@@ -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);
|
||||
@@ -134,7 +134,9 @@ void test_vector_sort_int_asc() {
|
||||
assert(sort_res.status == VECTOR_OK);
|
||||
|
||||
const int expected[] = { -7, 1, 4, 6, 12, 25, 25, 71 };
|
||||
for (size_t idx = 0; idx < vector_size(v); idx++) {
|
||||
|
||||
const size_t sz = vector_size(v);
|
||||
for (size_t idx = 0; idx < sz; idx++) {
|
||||
int *val = (int*)vector_get(v, idx).value.element;
|
||||
assert(*val == expected[idx]);
|
||||
}
|
||||
@@ -142,7 +144,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);
|
||||
@@ -157,7 +159,9 @@ void test_vector_sort_int_desc() {
|
||||
assert(sort_res.status == VECTOR_OK);
|
||||
|
||||
const int expected[] = { 71, 25, 25, 12, 6, 4, 1, -7 };
|
||||
for (size_t idx = 0; idx < vector_size(v); idx++) {
|
||||
|
||||
const size_t sz = vector_size(v);
|
||||
for (size_t idx = 0; idx < sz; idx++) {
|
||||
int *val = (int*)vector_get(v, idx).value.element;
|
||||
assert(*val == expected[idx]);
|
||||
}
|
||||
@@ -183,7 +187,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);
|
||||
@@ -198,7 +202,9 @@ void test_vector_sort_string() {
|
||||
assert(sort_res.status == VECTOR_OK);
|
||||
|
||||
const char *expected[] = { "world!", "system-programming", "hello", "foo", "embedded", "bar"};
|
||||
for (size_t idx = 0; idx < vector_size(v); idx++) {
|
||||
|
||||
const size_t sz = vector_size(v);
|
||||
for (size_t idx = 0; idx < sz; idx++) {
|
||||
const char *val = *(const char**)vector_get(v, idx).value.element;
|
||||
assert(!strcmp(val, expected[idx]));
|
||||
}
|
||||
@@ -234,7 +240,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);
|
||||
@@ -257,7 +263,8 @@ void test_vector_sort_struct_by_age() {
|
||||
{ .name = "Bob", .age = 45 }
|
||||
};
|
||||
|
||||
for (size_t idx = 0; idx < vector_size(people); idx++) {
|
||||
const size_t sz = sizeof(expected) / sizeof(expected[0]);
|
||||
for (size_t idx = 0; idx < sz; idx++) {
|
||||
Person *p = (Person*)vector_get(people, idx).value.element;
|
||||
assert(!strcmp(p->name, expected[idx].name));
|
||||
assert(p->age == expected[idx].age);
|
||||
@@ -266,7 +273,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);
|
||||
@@ -295,7 +302,8 @@ void test_vector_sort_struct_by_name() {
|
||||
{ .name = "Sophia", .age = 45 }
|
||||
};
|
||||
|
||||
for (size_t idx = 0; idx < vector_size(people); idx++) {
|
||||
const size_t sz = vector_size(people);
|
||||
for (size_t idx = 0; idx < sz; idx++) {
|
||||
Person *p = (Person*)vector_get(people, idx).value.element;
|
||||
assert(!strcmp(p->name, expected[idx].name));
|
||||
assert(p->age == expected[idx].age);
|
||||
@@ -305,7 +313,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 +332,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 +348,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 +369,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 +382,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 +404,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 +434,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 +457,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);
|
||||
|
||||
Reference in New Issue
Block a user