init
This commit is contained in:
132
tests/error.c
Normal file
132
tests/error.c
Normal file
@@ -0,0 +1,132 @@
|
||||
// Copyright 2022 Darwin Schuppan <darwin@nobrain.org>
|
||||
// SPDX license identifier: MIT
|
||||
|
||||
#include <ds/error.h>
|
||||
#include <ds/fmt.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
static int iq;
|
||||
static char iqstr[1024];
|
||||
|
||||
static Error no_error() {
|
||||
return OK();
|
||||
}
|
||||
|
||||
static Error no_brain() {
|
||||
return ERROR_STRING("You do not have sufficient neural functionality to pass this test");
|
||||
}
|
||||
|
||||
static Error no_brain_heap() {
|
||||
char *s = malloc(128);
|
||||
assert(s != NULL);
|
||||
snprintf(s, 128, "Insufficient IQ: %d (required: 55)", iq);
|
||||
return ERROR_HEAP_STRING(s);
|
||||
}
|
||||
|
||||
static Error start_javascript_engine() {
|
||||
return ERROR_OUT_OF_MEMORY();
|
||||
}
|
||||
|
||||
static Error nested() {
|
||||
return ERROR_NESTED(ERROR_STRING("Human error"), ERROR_NESTED(no_brain(), no_brain_heap()));
|
||||
}
|
||||
|
||||
static Error idiot_takes_iq_test() {
|
||||
return nested();
|
||||
}
|
||||
|
||||
static Error dennis_takes_iq_test() {
|
||||
return OK();
|
||||
}
|
||||
|
||||
static Error try_to_take_the_iq_test(bool *failed) {
|
||||
*failed = false;
|
||||
TRY(idiot_takes_iq_test(), *failed = true);
|
||||
return OK();
|
||||
}
|
||||
|
||||
static Error force_dennis_to_take_the_iq_test(bool *failed) {
|
||||
*failed = false;
|
||||
TRY(dennis_takes_iq_test(), *failed = true);
|
||||
return OK();
|
||||
}
|
||||
|
||||
int main() {
|
||||
srand(time(NULL));
|
||||
iq = rand() % 55;
|
||||
snprintf(iqstr, 1024, "Insufficient IQ: %d (required: 55)", iq);
|
||||
|
||||
fmt_init();
|
||||
error_init();
|
||||
|
||||
Error e;
|
||||
char *s = malloc(2048);
|
||||
assert(s != NULL);
|
||||
|
||||
e = no_error();
|
||||
assert(e.kind == ErrorNone);
|
||||
assert(error_to_string(s, 2048, e, true) == strlen("Success"));
|
||||
assert(strcmp(s, "Success") == 0);
|
||||
|
||||
e = no_brain();
|
||||
assert(e.kind == ErrorString);
|
||||
assert(!e.str_on_heap);
|
||||
assert(error_to_string(s, 2048, e, true) == strlen("You do not have sufficient neural functionality to pass this test"));
|
||||
assert(strcmp(s, "You do not have sufficient neural functionality to pass this test") == 0);
|
||||
|
||||
e = no_brain_heap();
|
||||
assert(e.kind == ErrorString);
|
||||
assert(e.str_on_heap);
|
||||
assert(error_to_string(s, 2048, e, true) == strlen(iqstr));
|
||||
assert(strcmp(s, iqstr) == 0);
|
||||
|
||||
e = start_javascript_engine();
|
||||
assert(e.kind == ErrorOutOfMemory);
|
||||
assert(error_to_string(s, 2048, e, true) == strlen("Out of memory"));
|
||||
assert(strcmp(s, "Out of memory") == 0);
|
||||
|
||||
snprintf(iqstr, 1024, "Human error: You do not have sufficient neural functionality to pass this test: Insufficient IQ: %d (required: 55)", iq);
|
||||
e = nested();
|
||||
assert(e.kind == ErrorString);
|
||||
assert(e.has_annex);
|
||||
assert(error_to_string(NULL, 0, e, false) == strlen(iqstr)); /* should still give us the right size, also shouldn't free/destroy anything */
|
||||
assert(error_to_string(s, 2048, e, true) == strlen(iqstr));
|
||||
assert(strcmp(s, iqstr) == 0);
|
||||
|
||||
bool failed;
|
||||
e = try_to_take_the_iq_test(&failed);
|
||||
assert(e.kind == ErrorString);
|
||||
assert(failed);
|
||||
assert(error_to_string(s, 2048, e, true) == strlen(iqstr));
|
||||
assert(strcmp(s, iqstr) == 0);
|
||||
|
||||
e = ERROR_HEREIFY(e);
|
||||
assert(e.kind == ErrorString);
|
||||
assert(e.has_location);
|
||||
assert(e.file != NULL);
|
||||
assert(e.line != 0);
|
||||
|
||||
e = force_dennis_to_take_the_iq_test(&failed);
|
||||
assert(e.kind == ErrorNone);
|
||||
assert(!failed);
|
||||
assert(error_to_string(s, 2048, e, true) == strlen("Success"));
|
||||
assert(strcmp(s, "Success") == 0);
|
||||
|
||||
e = nested();
|
||||
fmts(s, 2048, "%{Error}", e);
|
||||
assert(strcmp(s, iqstr) == 0);
|
||||
fmts(NULL, 0, "%{Error:destroy}", e);
|
||||
|
||||
fmts(s, 2048, "%{Error:destroy}", nested());
|
||||
assert(strcmp(s, iqstr) == 0);
|
||||
|
||||
free(s);
|
||||
|
||||
error_term();
|
||||
fmt_term();
|
||||
}
|
||||
18
tests/fmt.c
Normal file
18
tests/fmt.c
Normal file
@@ -0,0 +1,18 @@
|
||||
// Copyright 2022 Darwin Schuppan <darwin@nobrain.org>
|
||||
// SPDX license identifier: MIT
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <ds/fmt.h>
|
||||
|
||||
int main() {
|
||||
fmt_init();
|
||||
char buf[128];
|
||||
size_t size = fmts(buf, 128, "%hhu, %s %c%c %{uint:X,p=1024,c='.'}", ' ', "abc123", 'A', 65, 99999999);
|
||||
assert(size == 1038);
|
||||
assert(strcmp(buf, "32, abc123 AA .................................................................................................................") == 0);
|
||||
size = fmts(NULL, 0, "%%");
|
||||
assert(size == 1);
|
||||
fmt_term();
|
||||
}
|
||||
76
tests/generic/map.c
Normal file
76
tests/generic/map.c
Normal file
@@ -0,0 +1,76 @@
|
||||
// Copyright 2022 Darwin Schuppan <darwin@nobrain.org>
|
||||
// SPDX license identifier: MIT
|
||||
|
||||
#define GENERIC_IMPL_STATIC
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <ds/fmt.h>
|
||||
|
||||
static bool malloc_fail = false;
|
||||
static void *custom_malloc(size_t size) {
|
||||
return malloc_fail ? NULL : malloc(size);
|
||||
}
|
||||
#define malloc(size) custom_malloc(size)
|
||||
|
||||
#define GENERIC_KEY_TYPE int
|
||||
#define GENERIC_VALUE_TYPE int
|
||||
#define GENERIC_NAME IntIntMap
|
||||
#define GENERIC_PREFIX int_int_map
|
||||
#include <ds/generic/map.h>
|
||||
|
||||
#define GENERIC_KEY_TYPE int
|
||||
#define GENERIC_VALUE_TYPE float
|
||||
#define GENERIC_NAME IntFloatMap
|
||||
#define GENERIC_PREFIX int_float_map
|
||||
#include <ds/generic/map.h>
|
||||
|
||||
int main() {
|
||||
fmt_init();
|
||||
|
||||
IntIntMap m = int_int_map();
|
||||
int_int_map_rehash(&m, 10);
|
||||
// Insert
|
||||
for (size_t i = 10; i < 80; i++)
|
||||
ERROR_ASSERT(int_int_map_set(&m, i, i + 20));
|
||||
assert(int_int_map_del(m, 15));
|
||||
assert(!int_int_map_del(m, 15));
|
||||
// Get
|
||||
for (size_t i = 10; i < 80; i++) {
|
||||
int *p = int_int_map_get(m, i);
|
||||
if (i == 15) {
|
||||
assert(p == NULL);
|
||||
} else {
|
||||
assert(p != NULL);
|
||||
assert(*p == i + 20);
|
||||
}
|
||||
}
|
||||
// Invalid rehash
|
||||
int_int_map_rehash(&m, 2);
|
||||
// Replace
|
||||
for (size_t i = 10; i < 80; i++)
|
||||
ERROR_ASSERT(int_int_map_set(&m, i, i + 20));
|
||||
// Use iterators to access every item
|
||||
IntIntMapItem *i = NULL;
|
||||
while (int_int_map_it_next(m, &i)) {
|
||||
assert(i->key != 0);
|
||||
assert(i->val != 0);
|
||||
}
|
||||
// Print using fmt
|
||||
int_int_map_fmt_register("%d", "%d");
|
||||
char buf[2048];
|
||||
fmts(buf, 2048, "%{IntIntMap}", m);
|
||||
assert(strcmp(buf, "{69: 89, 52: 72, 39: 59, 22: 42, 77: 97, 60: 80, 47: 67, 30: 50, 68: 88, 55: 75, 38: 58, 17: 37, 76: 96, 63: 83, 46: 66, 25: 45, 71: 91, 54: 74, 33: 53, 16: 36, 79: 99, 62: 82, 41: 61, 24: 44, 11: 31, 70: 90, 49: 69, 32: 52, 19: 39, 78: 98, 57: 77, 40: 60, 27: 47, 10: 30, 65: 85, 48: 68, 35: 55, 18: 38, 13: 33, 73: 93, 56: 76, 43: 63, 26: 46, 21: 41, 64: 84, 51: 71, 34: 54, 29: 49, 12: 32, 72: 92, 59: 79, 42: 62, 37: 57, 20: 40, 67: 87, 50: 70, 45: 65, 28: 48, 15: 35, 75: 95, 58: 78, 53: 73, 36: 56, 23: 43, 66: 86, 61: 81, 44: 64, 31: 51, 14: 34, 74: 94}") == 0);
|
||||
int_int_map_term(m);
|
||||
// Error recovery
|
||||
malloc_fail = true;
|
||||
IntFloatMap fm = int_float_map();
|
||||
Error err = int_float_map_set(&fm, 10, 10.f);
|
||||
assert(err.kind == ErrorOutOfMemory);
|
||||
assert(fm.len == 0);
|
||||
assert(fm.cap == 0);
|
||||
int_float_map_term(fm);
|
||||
|
||||
fmt_term();
|
||||
}
|
||||
108
tests/generic/smap.c
Normal file
108
tests/generic/smap.c
Normal file
@@ -0,0 +1,108 @@
|
||||
// Copyright 2022 Darwin Schuppan <darwin@nobrain.org>
|
||||
// SPDX license identifier: MIT
|
||||
|
||||
#define GENERIC_IMPL_STATIC
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <ds/fmt.h>
|
||||
|
||||
static bool malloc_fail = false;
|
||||
static void *custom_malloc(size_t size) {
|
||||
return malloc_fail ? NULL : malloc(size);
|
||||
}
|
||||
#define malloc(size) custom_malloc(size)
|
||||
|
||||
static bool strdup_fail = false;
|
||||
static void *custom_strdup(const char *str) {
|
||||
return strdup_fail ? NULL : strdup(str);
|
||||
}
|
||||
#define strdup(str) custom_strdup(str)
|
||||
|
||||
#define GENERIC_TYPE int
|
||||
#define GENERIC_NAME IntMap
|
||||
#define GENERIC_PREFIX int_map
|
||||
#include <ds/generic/smap.h>
|
||||
|
||||
#define GENERIC_TYPE size_t
|
||||
#define GENERIC_NAME SizeTMap
|
||||
#define GENERIC_PREFIX size_t_map
|
||||
#include <ds/generic/smap.h>
|
||||
|
||||
typedef struct Test {
|
||||
size_t n;
|
||||
const char *s;
|
||||
double d;
|
||||
} Test;
|
||||
|
||||
#define GENERIC_TYPE Test
|
||||
#define GENERIC_NAME TestMap
|
||||
#define GENERIC_PREFIX test_map
|
||||
#include <ds/generic/smap.h>
|
||||
|
||||
int main() {
|
||||
fmt_init();
|
||||
|
||||
IntMap m = int_map();
|
||||
// Insert
|
||||
for (size_t i = 10; i < 80; i++) {
|
||||
char buf[64];
|
||||
snprintf(buf, 64, "number: %d", (int)i);
|
||||
ERROR_ASSERT(int_map_set(&m, buf, (int)i));
|
||||
}
|
||||
assert(int_map_del(m, "number: 15"));
|
||||
assert(!int_map_del(m, "number: 15"));
|
||||
// Get
|
||||
for (size_t i = 10; i < 80; i++) {
|
||||
char buf[64];
|
||||
snprintf(buf, 64, "number: %d", (int)i);
|
||||
int *p = int_map_get(m, buf);
|
||||
if (i != 15) {
|
||||
assert(p);
|
||||
assert(*p == (int)i);
|
||||
}
|
||||
}
|
||||
// Replace
|
||||
for (size_t i = 10; i < 80; i++) {
|
||||
char buf[64];
|
||||
snprintf(buf, 64, "number: %d", (int)i);
|
||||
ERROR_ASSERT(int_map_set(&m, buf, (int)i));
|
||||
}
|
||||
// Use iterators get every item
|
||||
IntMapItem *i = NULL;
|
||||
while (int_map_it_next(m, &i)) {
|
||||
assert(i->key != 0);
|
||||
assert(i->val != 0);
|
||||
}
|
||||
// Print using fmt
|
||||
int_map_fmt_register("%d");
|
||||
char buf[2048];
|
||||
fmts(buf, 2048, "%{IntMap}", m);
|
||||
assert(strcmp(buf, "{\"number: 64\": 64, \"number: 29\": 29, \"number: 46\": 46, \"number: 63\": 63, \"number: 20\": 20, \"number: 55\": 55, \"number: 74\": 74, \"number: 27\": 27, \"number: 52\": 52, \"number: 14\": 14, \"number: 30\": 30, \"number: 65\": 65, \"number: 47\": 47, \"number: 21\": 21, \"number: 54\": 54, \"number: 73\": 73, \"number: 17\": 17, \"number: 37\": 37, \"number: 66\": 66, \"number: 44\": 44, \"number: 22\": 22, \"number: 57\": 57, \"number: 11\": 11, \"number: 72\": 72, \"number: 16\": 16, \"number: 36\": 36, \"number: 42\": 42, \"number: 67\": 67, \"number: 59\": 59, \"number: 45\": 45, \"number: 23\": 23, \"number: 56\": 56, \"number: 10\": 10, \"number: 71\": 71, \"number: 48\": 48, \"number: 19\": 19, \"number: 35\": 35, \"number: 43\": 43, \"number: 60\": 60, \"number: 58\": 58, \"number: 77\": 77, \"number: 24\": 24, \"number: 51\": 51, \"number: 13\": 13, \"number: 70\": 70, \"number: 33\": 33, \"number: 49\": 49, \"number: 79\": 79, \"number: 18\": 18, \"number: 34\": 34, \"number: 40\": 40, \"number: 61\": 61, \"number: 76\": 76, \"number: 68\": 68, \"number: 39\": 39, \"number: 25\": 25, \"number: 50\": 50, \"number: 12\": 12, \"number: 32\": 32, \"number: 78\": 78, \"number: 28\": 28, \"number: 41\": 41, \"number: 62\": 62, \"number: 75\": 75, \"number: 69\": 69, \"number: 38\": 38, \"number: 26\": 26, \"number: 53\": 53, \"number: 15\": 15, \"number: 31\": 31}") == 0);
|
||||
int_map_term(m);
|
||||
|
||||
SizeTMap sm = size_t_map();
|
||||
size_t_map_term(sm);
|
||||
|
||||
TestMap tm = test_map();
|
||||
// Error recovery (malloc fail)
|
||||
malloc_fail = true;
|
||||
Error err = test_map_set(&tm, "a", (Test){ .n = 22 });
|
||||
assert(err.kind == ErrorOutOfMemory);
|
||||
assert(tm.len == 0);
|
||||
assert(tm.cap == 0);
|
||||
// Error recovery (strdup fail)
|
||||
malloc_fail = false;
|
||||
strdup_fail = true;
|
||||
err = test_map_set(&tm, "a", (Test){ .n = 22 });
|
||||
assert(err.kind == ErrorOutOfMemory);
|
||||
assert(tm.len == 0);
|
||||
assert(tm.cap == 8);
|
||||
|
||||
test_map_term(tm);
|
||||
|
||||
fmt_term();
|
||||
}
|
||||
62
tests/generic/vec.c
Normal file
62
tests/generic/vec.c
Normal file
@@ -0,0 +1,62 @@
|
||||
// Copyright 2022 Darwin Schuppan <darwin@nobrain.org>
|
||||
// SPDX license identifier: MIT
|
||||
|
||||
#define GENERIC_IMPL_STATIC
|
||||
|
||||
#define GENERIC_TYPE int
|
||||
#define GENERIC_NAME IntVec
|
||||
#define GENERIC_PREFIX int_vec
|
||||
#include <ds/generic/vec.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
int main() {
|
||||
fmt_init();
|
||||
|
||||
IntVec v = int_vec();
|
||||
|
||||
assert(vec_len(v) == 0);
|
||||
assert(vec_cap(v) == 0);
|
||||
assert(int_vec_len(v) == 0);
|
||||
assert(int_vec_cap(v) == 0);
|
||||
|
||||
// Push
|
||||
for (size_t i = 0; i < 512; i++)
|
||||
int_vec_push(&v, i + 1);
|
||||
assert(vec_len(v) == 512);
|
||||
assert(vec_cap(v) == 512);
|
||||
assert(vec_len(v) == 512);
|
||||
assert(vec_cap(v) == 512);
|
||||
// Retrieve
|
||||
for (size_t i = 0; i < 512; i++)
|
||||
assert(v[i] == i + 1);
|
||||
// Delete
|
||||
int_vec_del(v, 256);
|
||||
assert(vec_len(v) == 511);
|
||||
assert(vec_cap(v) == 512);
|
||||
for (size_t i = 0; i < 256; i++)
|
||||
assert(v[i] == i + 1);
|
||||
for (size_t i = 256; i < 511; i++)
|
||||
assert(v[i] == i + 2);
|
||||
// Retrieve via pop() and back()
|
||||
assert(int_vec_pop(v) == 512);
|
||||
assert(*int_vec_back(v) == 511);
|
||||
// Insert
|
||||
int_vec_insert(&v, 1, 999);
|
||||
assert(vec_len(v) == 511);
|
||||
assert(vec_cap(v) == 512);
|
||||
assert(v[0] == 1);
|
||||
assert(v[1] == 999);
|
||||
for (size_t i = 2; i < 256; i++)
|
||||
assert(v[i] == i);
|
||||
// Print via fmt
|
||||
int_vec_fmt_register("%d");
|
||||
char buf[4096];
|
||||
fmts(buf, 4096, "%{IntVec}", v);
|
||||
assert(strcmp(buf, "{1, 999, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480, 481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511}") == 0);
|
||||
|
||||
int_vec_term(v);
|
||||
|
||||
fmt_term();
|
||||
}
|
||||
Reference in New Issue
Block a user