Added unit tests for BigInt data type and updated docs
This commit is contained in:
19
.github/workflows/clang-build.yml
vendored
Normal file
19
.github/workflows/clang-build.yml
vendored
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
name: clang-build
|
||||||
|
on: [push,pull_request,workflow_dispatch]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
clang-build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Install Clang
|
||||||
|
run: sudo apt update && sudo apt install -y clang
|
||||||
|
|
||||||
|
- name: Build Datum
|
||||||
|
run: |
|
||||||
|
make clean all
|
||||||
|
|
||||||
|
- name: Run unit tests
|
||||||
|
run: |
|
||||||
|
./test_vector && ./test_map && ./test_bigint
|
||||||
@@ -1,17 +1,16 @@
|
|||||||
name: datum
|
name: gcc-build
|
||||||
on:
|
on: [push,pull_request,workflow_dispatch]
|
||||||
push:
|
|
||||||
branch: [master]
|
|
||||||
workflow_dispatch:
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
gcc-build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Build Datum
|
- name: Build Datum
|
||||||
run: |
|
run: |
|
||||||
make clean all
|
make clean all
|
||||||
|
|
||||||
- name: Run unit tests
|
- name: Run unit tests
|
||||||
run: |
|
run: |
|
||||||
./test_vector && ./test_map
|
./test_vector && ./test_map && ./test_bigint
|
||||||
19
README.md
19
README.md
@@ -2,7 +2,8 @@
|
|||||||
<h1>Datum</h1>
|
<h1>Datum</h1>
|
||||||
<h6><i>Collection of dynamic and generic data structures.</i></h6>
|
<h6><i>Collection of dynamic and generic data structures.</i></h6>
|
||||||
|
|
||||||
[](https://github.com/ceticamarco/datum/actions/workflows/datum.yml)
|
[](https://github.com/ceticamarco/datum/actions/workflows/gcc-build.yml)
|
||||||
|
[](https://github.com/ceticamarco/datum/actions/workflows/clang-build.yml)
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
Datum is a collection of dynamic and generic data structures implemented from scratch in C with no external dependencies beyond
|
Datum is a collection of dynamic and generic data structures implemented from scratch in C with no external dependencies beyond
|
||||||
@@ -98,19 +99,18 @@ int main(void) {
|
|||||||
|
|
||||||
### `BigInt` usage
|
### `BigInt` usage
|
||||||
```c
|
```c
|
||||||
#include <stdio.h>
|
|
||||||
#include "src/bigint.h"
|
#include "src/bigint.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Compile with: gcc main.c src/bigint.c
|
* Compile with: gcc -O3 main.c src/bigint.c src/vector.c
|
||||||
* Output: 20000! = 1819206320230345134827641...
|
* Output: 20000! = 1819206320230345134827641...
|
||||||
* Time: real 0m5.482s user 0m5.453s sys 0m0.017
|
* Time: 4.01s user 0.00s system 99% cpu 4.021 total
|
||||||
*/
|
*/
|
||||||
int main(void) {
|
int main(void) {
|
||||||
const int n = 20000;
|
const int n = 20000;
|
||||||
bigint_t *fact = bigint_from_int(1).value.number;
|
bigint_t *fact = bigint_from_int(1).value.number;
|
||||||
|
|
||||||
for (int idx = 2; idx<=n; idx++) {
|
for (int idx = 2; idx <= n; idx++) {
|
||||||
bigint_t *big_idx = bigint_from_int(idx).value.number;
|
bigint_t *big_idx = bigint_from_int(idx).value.number;
|
||||||
bigint_t *partial_fact = bigint_prod(fact, big_idx).value.number;
|
bigint_t *partial_fact = bigint_prod(fact, big_idx).value.number;
|
||||||
|
|
||||||
@@ -119,17 +119,13 @@ int main(void) {
|
|||||||
fact = partial_fact;
|
fact = partial_fact;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("%d! = ", n);
|
bigint_printf("%d! = %B\n", n, fact);
|
||||||
bigint_print(fact);
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
bigint_destroy(fact);
|
bigint_destroy(fact);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
For a more exhaustive example, refer to the `usage.c` file. There, you will find a program with proper error management
|
For a more exhaustive example, refer to the `usage.c` file. There, you will find a program with proper error management
|
||||||
and a sample usage for every available method. To run it, first issue the following command:
|
and a sample usage for every available method. To run it, first issue the following command:
|
||||||
|
|
||||||
@@ -144,12 +140,13 @@ For additional details about this library (internal design, memory
|
|||||||
management, data ownership, etc.) go to the [docs folder](/docs).
|
management, data ownership, etc.) go to the [docs folder](/docs).
|
||||||
|
|
||||||
## Unit tests
|
## Unit tests
|
||||||
Datum provides some unit tests for both the `Vector` and the `Map` data types. To run them, you can issue the following commands:
|
Datum provides some unit tests for `Vector`, `Map` and `BigInt`. To run them, you can issue the following commands:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ make clean all
|
$ make clean all
|
||||||
$ ./test_vector
|
$ ./test_vector
|
||||||
$ ./test_map
|
$ ./test_map
|
||||||
|
$ ./test_bigint
|
||||||
```
|
```
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ uint64_t hash_key(const char *key) {
|
|||||||
*
|
*
|
||||||
* Returns a map_result_t data type containing a new hash map
|
* Returns a map_result_t data type containing a new hash map
|
||||||
*/
|
*/
|
||||||
map_result_t map_new() {
|
map_result_t map_new(void) {
|
||||||
map_result_t result = {0};
|
map_result_t result = {0};
|
||||||
|
|
||||||
map_t *map = malloc(sizeof(map_t));
|
map_t *map = malloc(sizeof(map_t));
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ typedef struct {
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
map_result_t map_new();
|
map_result_t map_new(void);
|
||||||
map_result_t map_add(map_t *map, const char *key, void *value);
|
map_result_t map_add(map_t *map, const char *key, void *value);
|
||||||
map_result_t map_get(const map_t *map, const char *key);
|
map_result_t map_get(const map_t *map, const char *key);
|
||||||
map_result_t map_remove(map_t *map, const char *key);
|
map_result_t map_remove(map_t *map, const char *key);
|
||||||
|
|||||||
@@ -16,15 +16,21 @@
|
|||||||
#include "../src/bigint.h"
|
#include "../src/bigint.h"
|
||||||
|
|
||||||
static void bigint_eq(const bigint_t *number, const char *expected) {
|
static void bigint_eq(const bigint_t *number, const char *expected) {
|
||||||
bigint_result_t to_str = bigint_to_string(number);
|
bigint_result_t exp_num_res = bigint_from_string(expected);
|
||||||
assert(to_str.status == BIGINT_OK);
|
assert(exp_num_res.status == BIGINT_OK);
|
||||||
assert(!strcmp(to_str.value.string_num, expected));
|
bigint_t *exp_num = exp_num_res.value.number;
|
||||||
|
|
||||||
free(to_str.value.string_num);
|
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
|
// 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);
|
bigint_result_t res = bigint_from_int(0);
|
||||||
|
|
||||||
assert(res.status == BIGINT_OK);
|
assert(res.status == BIGINT_OK);
|
||||||
@@ -43,19 +49,331 @@ void test_bigint_from_int() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Test creating big integers from string
|
// 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");
|
bigint_result_t res = bigint_from_string("00000123");
|
||||||
|
|
||||||
assert(res.status == BIGINT_OK);
|
assert(res.status == BIGINT_OK);
|
||||||
bigint_eq(res.value.number, "123");
|
bigint_eq(res.value.number, "123");
|
||||||
bigint_destroy(res.value.number);
|
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) {
|
int main(void) {
|
||||||
printf("=== Running BigInt unit tests ===\n\n");
|
printf("=== Running BigInt unit tests ===\n\n");
|
||||||
|
|
||||||
TEST(bigint_from_int);
|
TEST(bigint_from_int);
|
||||||
TEST(bigint_from_string);
|
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");
|
printf("\n=== All tests passed ===\n");
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
#include "../src/map.h"
|
#include "../src/map.h"
|
||||||
|
|
||||||
// Create a new map
|
// Create a new map
|
||||||
void test_map_new() {
|
void test_map_new(void) {
|
||||||
map_result_t res = map_new();
|
map_result_t res = map_new();
|
||||||
|
|
||||||
assert(res.status == MAP_OK);
|
assert(res.status == MAP_OK);
|
||||||
@@ -27,7 +27,7 @@ void test_map_new() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add elements to map
|
// Add elements to map
|
||||||
void test_map_add() {
|
void test_map_add(void) {
|
||||||
map_result_t res = map_new();
|
map_result_t res = map_new();
|
||||||
|
|
||||||
assert(res.status == MAP_OK);
|
assert(res.status == MAP_OK);
|
||||||
@@ -47,7 +47,7 @@ void test_map_add() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add multiple elements to the map
|
// Add multiple elements to the map
|
||||||
void test_map_add_multiple() {
|
void test_map_add_multiple(void) {
|
||||||
map_result_t res = map_new();
|
map_result_t res = map_new();
|
||||||
|
|
||||||
assert(res.status == MAP_OK);
|
assert(res.status == MAP_OK);
|
||||||
@@ -68,7 +68,7 @@ void test_map_add_multiple() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get map element
|
// Get map element
|
||||||
void test_map_get() {
|
void test_map_get(void) {
|
||||||
map_result_t res = map_new();
|
map_result_t res = map_new();
|
||||||
|
|
||||||
assert(res.status == MAP_OK);
|
assert(res.status == MAP_OK);
|
||||||
@@ -85,7 +85,7 @@ void test_map_get() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get non-existing key from map
|
// Get non-existing key from map
|
||||||
void test_map_get_invalid() {
|
void test_map_get_invalid(void) {
|
||||||
map_result_t res = map_new();
|
map_result_t res = map_new();
|
||||||
|
|
||||||
assert(res.status == MAP_OK);
|
assert(res.status == MAP_OK);
|
||||||
@@ -98,7 +98,7 @@ void test_map_get_invalid() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Map with heterogeneous types
|
// Map with heterogeneous types
|
||||||
void test_map_mixed() {
|
void test_map_mixed(void) {
|
||||||
map_result_t res = map_new();
|
map_result_t res = map_new();
|
||||||
|
|
||||||
assert(res.status == MAP_OK);
|
assert(res.status == MAP_OK);
|
||||||
@@ -126,7 +126,7 @@ void test_map_mixed() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update existing map key
|
// Update existing map key
|
||||||
void test_map_update() {
|
void test_map_update(void) {
|
||||||
map_result_t res = map_new();
|
map_result_t res = map_new();
|
||||||
|
|
||||||
assert(res.status == MAP_OK);
|
assert(res.status == MAP_OK);
|
||||||
@@ -149,7 +149,7 @@ void test_map_update() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Remove an element from map
|
// Remove an element from map
|
||||||
void test_map_remove() {
|
void test_map_remove(void) {
|
||||||
map_result_t res = map_new();
|
map_result_t res = map_new();
|
||||||
|
|
||||||
assert(res.status == MAP_OK);
|
assert(res.status == MAP_OK);
|
||||||
@@ -177,7 +177,7 @@ void test_map_remove() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Remove non-existing key from map
|
// Remove non-existing key from map
|
||||||
void test_map_remove_invalid() {
|
void test_map_remove_invalid(void) {
|
||||||
map_result_t res = map_new();
|
map_result_t res = map_new();
|
||||||
|
|
||||||
assert(res.status == MAP_OK);
|
assert(res.status == MAP_OK);
|
||||||
@@ -190,7 +190,7 @@ void test_map_remove_invalid() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Clear the map
|
// Clear the map
|
||||||
void test_map_clear() {
|
void test_map_clear(void) {
|
||||||
map_result_t res = map_new();
|
map_result_t res = map_new();
|
||||||
|
|
||||||
assert(res.status == MAP_OK);
|
assert(res.status == MAP_OK);
|
||||||
@@ -212,7 +212,7 @@ void test_map_clear() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Clear empty map
|
// Clear empty map
|
||||||
void test_map_clear_empty() {
|
void test_map_clear_empty(void) {
|
||||||
map_result_t res = map_new();
|
map_result_t res = map_new();
|
||||||
|
|
||||||
assert(res.status == MAP_OK);
|
assert(res.status == MAP_OK);
|
||||||
@@ -226,7 +226,7 @@ void test_map_clear_empty() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Multiple operations in sequence (add, update, delete and clear)
|
// Multiple operations in sequence (add, update, delete and clear)
|
||||||
void test_map_sequence() {
|
void test_map_sequence(void) {
|
||||||
map_result_t res = map_new();
|
map_result_t res = map_new();
|
||||||
|
|
||||||
assert(res.status == MAP_OK);
|
assert(res.status == MAP_OK);
|
||||||
@@ -266,7 +266,7 @@ typedef struct {
|
|||||||
short age;
|
short age;
|
||||||
} Person;
|
} Person;
|
||||||
|
|
||||||
void test_map_struct() {
|
void test_map_struct(void) {
|
||||||
map_result_t res = map_new();
|
map_result_t res = map_new();
|
||||||
|
|
||||||
assert(res.status == MAP_OK);
|
assert(res.status == MAP_OK);
|
||||||
@@ -298,7 +298,7 @@ void test_map_struct() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Test map capacity tracking
|
// Test map capacity tracking
|
||||||
void test_map_cap() {
|
void test_map_cap(void) {
|
||||||
map_result_t res = map_new();
|
map_result_t res = map_new();
|
||||||
|
|
||||||
assert(res.status == MAP_OK);
|
assert(res.status == MAP_OK);
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
#include "../src/vector.h"
|
#include "../src/vector.h"
|
||||||
|
|
||||||
// Create a new vector
|
// Create a new vector
|
||||||
void test_vector_new() {
|
void test_vector_new(void) {
|
||||||
vector_result_t res = vector_new(5, sizeof(int));
|
vector_result_t res = vector_new(5, sizeof(int));
|
||||||
|
|
||||||
assert(res.status == VECTOR_OK);
|
assert(res.status == VECTOR_OK);
|
||||||
@@ -27,14 +27,14 @@ void test_vector_new() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create a vector with zero capacity
|
// 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));
|
vector_result_t res = vector_new(0, sizeof(int));
|
||||||
|
|
||||||
assert(res.status == VECTOR_ERR_ALLOCATE);
|
assert(res.status == VECTOR_ERR_ALLOCATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Push elements to vector
|
// Push elements to vector
|
||||||
void test_vector_push() {
|
void test_vector_push(void) {
|
||||||
vector_result_t res = vector_new(5, sizeof(int));
|
vector_result_t res = vector_new(5, sizeof(int));
|
||||||
|
|
||||||
assert(res.status == VECTOR_OK);
|
assert(res.status == VECTOR_OK);
|
||||||
@@ -54,7 +54,7 @@ void test_vector_push() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Trigger vector reallocation
|
// Trigger vector reallocation
|
||||||
void test_vector_push_realloc() {
|
void test_vector_push_realloc(void) {
|
||||||
vector_result_t res = vector_new(1, sizeof(int));
|
vector_result_t res = vector_new(1, sizeof(int));
|
||||||
|
|
||||||
assert(res.status == VECTOR_OK);
|
assert(res.status == VECTOR_OK);
|
||||||
@@ -72,7 +72,7 @@ void test_vector_push_realloc() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get vector elements
|
// Get vector elements
|
||||||
void test_vector_get() {
|
void test_vector_get(void) {
|
||||||
vector_result_t res = vector_new(5, sizeof(int));
|
vector_result_t res = vector_new(5, sizeof(int));
|
||||||
|
|
||||||
assert(res.status == VECTOR_OK);
|
assert(res.status == VECTOR_OK);
|
||||||
@@ -89,7 +89,7 @@ void test_vector_get() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Test out of bounds
|
// Test out of bounds
|
||||||
void test_vector_get_ofb() {
|
void test_vector_get_ofb(void) {
|
||||||
vector_result_t res = vector_new(5, sizeof(int));
|
vector_result_t res = vector_new(5, sizeof(int));
|
||||||
|
|
||||||
assert(res.status == VECTOR_OK);
|
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);
|
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));
|
vector_result_t res = vector_new(5, sizeof(int));
|
||||||
|
|
||||||
assert(res.status == VECTOR_OK);
|
assert(res.status == VECTOR_OK);
|
||||||
@@ -142,7 +142,7 @@ void test_vector_sort_int_asc() {
|
|||||||
vector_destroy(v);
|
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));
|
vector_result_t res = vector_new(5, sizeof(int));
|
||||||
|
|
||||||
assert(res.status == VECTOR_OK);
|
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;
|
return VECTOR_ORDER_EQ;
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_vector_sort_string() {
|
void test_vector_sort_string(void) {
|
||||||
vector_result_t res = vector_new(5, sizeof(char*));
|
vector_result_t res = vector_new(5, sizeof(char*));
|
||||||
|
|
||||||
assert(res.status == VECTOR_OK);
|
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;
|
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));
|
vector_result_t res = vector_new(5, sizeof(Person));
|
||||||
|
|
||||||
assert(res.status == VECTOR_OK);
|
assert(res.status == VECTOR_OK);
|
||||||
@@ -266,7 +266,7 @@ void test_vector_sort_struct_by_age() {
|
|||||||
vector_destroy(people);
|
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));
|
vector_result_t res = vector_new(5, sizeof(Person));
|
||||||
|
|
||||||
assert(res.status == VECTOR_OK);
|
assert(res.status == VECTOR_OK);
|
||||||
@@ -305,7 +305,7 @@ void test_vector_sort_struct_by_name() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set vector element
|
// Set vector element
|
||||||
void test_vector_set() {
|
void test_vector_set(void) {
|
||||||
vector_result_t res = vector_new(5, sizeof(int));
|
vector_result_t res = vector_new(5, sizeof(int));
|
||||||
|
|
||||||
assert(res.status == VECTOR_OK);
|
assert(res.status == VECTOR_OK);
|
||||||
@@ -324,7 +324,7 @@ void test_vector_set() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set vector element out of bounds
|
// 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));
|
vector_result_t res = vector_new(5, sizeof(int));
|
||||||
|
|
||||||
assert(res.status == VECTOR_OK);
|
assert(res.status == VECTOR_OK);
|
||||||
@@ -340,7 +340,7 @@ void test_vector_set_ofb() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Pop element from vector
|
// Pop element from vector
|
||||||
void test_vector_pop() {
|
void test_vector_pop(void) {
|
||||||
vector_result_t res = vector_new(5, sizeof(int));
|
vector_result_t res = vector_new(5, sizeof(int));
|
||||||
|
|
||||||
assert(res.status == VECTOR_OK);
|
assert(res.status == VECTOR_OK);
|
||||||
@@ -361,7 +361,7 @@ void test_vector_pop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Test pop element from empty vector
|
// 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));
|
vector_result_t res = vector_new(5, sizeof(int));
|
||||||
|
|
||||||
assert(res.status == VECTOR_OK);
|
assert(res.status == VECTOR_OK);
|
||||||
@@ -374,7 +374,7 @@ void test_vector_pop_empty() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Clear vector
|
// Clear vector
|
||||||
void test_vector_clear() {
|
void test_vector_clear(void) {
|
||||||
vector_result_t res = vector_new(5, sizeof(int));
|
vector_result_t res = vector_new(5, sizeof(int));
|
||||||
|
|
||||||
assert(res.status == VECTOR_OK);
|
assert(res.status == VECTOR_OK);
|
||||||
@@ -396,7 +396,7 @@ void test_vector_clear() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Multiple operations in sequence (push, set, pop and 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));
|
vector_result_t res = vector_new(2, sizeof(int));
|
||||||
|
|
||||||
assert(res.status == VECTOR_OK);
|
assert(res.status == VECTOR_OK);
|
||||||
@@ -426,7 +426,7 @@ void test_vector_sequence() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Vector with chars
|
// Vector with chars
|
||||||
void test_vector_char() {
|
void test_vector_char(void) {
|
||||||
vector_result_t res = vector_new(5, sizeof(char));
|
vector_result_t res = vector_new(5, sizeof(char));
|
||||||
|
|
||||||
assert(res.status == VECTOR_OK);
|
assert(res.status == VECTOR_OK);
|
||||||
@@ -449,7 +449,7 @@ typedef struct {
|
|||||||
int y;
|
int y;
|
||||||
} Point;
|
} Point;
|
||||||
|
|
||||||
void test_vector_struct() {
|
void test_vector_struct(void) {
|
||||||
vector_result_t res = vector_new(5, sizeof(Point));
|
vector_result_t res = vector_new(5, sizeof(Point));
|
||||||
|
|
||||||
assert(res.status == VECTOR_OK);
|
assert(res.status == VECTOR_OK);
|
||||||
|
|||||||
12
usage.c
12
usage.c
@@ -22,9 +22,9 @@
|
|||||||
#include "src/map.h"
|
#include "src/map.h"
|
||||||
#include "src/bigint.h"
|
#include "src/bigint.h"
|
||||||
|
|
||||||
static int vector_usage();
|
static int vector_usage(void);
|
||||||
static int map_usage();
|
static int map_usage(void);
|
||||||
static int bigint_usage();
|
static int bigint_usage(void);
|
||||||
|
|
||||||
static vector_order_t cmp_int_asc(const void *x, const void *y);
|
static vector_order_t cmp_int_asc(const void *x, const void *y);
|
||||||
static vector_order_t cmp_int_desc(const void *x, const void *y);
|
static vector_order_t cmp_int_desc(const void *x, const void *y);
|
||||||
@@ -62,7 +62,7 @@ vector_order_t cmp_int_desc(const void *x, const void *y) {
|
|||||||
return cmp_int_asc(y, x);
|
return cmp_int_asc(y, x);
|
||||||
}
|
}
|
||||||
|
|
||||||
int vector_usage() {
|
int vector_usage(void) {
|
||||||
// Create a vector of 3 integers
|
// Create a vector of 3 integers
|
||||||
vector_result_t res = vector_new(3, sizeof(int));
|
vector_result_t res = vector_new(3, sizeof(int));
|
||||||
if (res.status != VECTOR_OK) {
|
if (res.status != VECTOR_OK) {
|
||||||
@@ -203,7 +203,7 @@ int vector_usage() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int map_usage() {
|
int map_usage(void) {
|
||||||
// Create a new map
|
// Create a new map
|
||||||
map_result_t res = map_new();
|
map_result_t res = map_new();
|
||||||
if (res.status != MAP_OK) {
|
if (res.status != MAP_OK) {
|
||||||
@@ -304,7 +304,7 @@ int map_usage() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int bigint_usage() {
|
int bigint_usage(void) {
|
||||||
// Create two big integers
|
// Create two big integers
|
||||||
bigint_result_t x_res = bigint_from_string("123456789");
|
bigint_result_t x_res = bigint_from_string("123456789");
|
||||||
if (x_res.status != BIGINT_OK) {
|
if (x_res.status != BIGINT_OK) {
|
||||||
|
|||||||
Reference in New Issue
Block a user