240 lines
8.0 KiB
C
240 lines
8.0 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include <time.h>
|
|
|
|
#include "guf_init.h" /* Must be included once */
|
|
|
|
#include "guf_alloc_libc.h"
|
|
#include "guf_cstr.h"
|
|
|
|
#define GUF_T float
|
|
#define GUF_SORT_IMPL_STATIC
|
|
#include "guf_sort.h"
|
|
|
|
#define GUF_T int
|
|
#define GUF_SORT_IMPL_STATIC
|
|
#include "guf_sort.h"
|
|
|
|
#define GUF_DBUF_NAME dbuf_int
|
|
#define GUF_T int
|
|
#define GUF_T_IS_INTEGRAL_TYPE
|
|
#define GUF_DBUF_IMPL_STATIC
|
|
#include "guf_dbuf.h"
|
|
|
|
#define GUF_DBUF_NAME dbuf_float
|
|
#define GUF_T float
|
|
#define GUF_T_IS_INTEGRAL_TYPE
|
|
#define GUF_DBUF_IMPL_STATIC
|
|
#include "guf_dbuf.h"
|
|
|
|
#define GUF_T guf_cstr_heap
|
|
#define GUF_DBUF_NAME dbuf_heap_cstr
|
|
#define GUF_T_COPY guf_cstr_heap_copy
|
|
#define GUF_T_MOVE guf_cstr_heap_move
|
|
#define GUF_T_FREE guf_cstr_heap_free
|
|
#define GUF_T_EQ guf_cstr_heap_eq
|
|
#define GUF_DBUF_IMPL_STATIC
|
|
// #define GUF_CNT_WITH_ELEM_CTX
|
|
#include "guf_dbuf.h"
|
|
|
|
#define GUF_T guf_cstr_const
|
|
#define GUF_DBUF_NAME dbuf_const_cstr
|
|
#define GUF_T_EQ guf_cstr_const_eq
|
|
#define GUF_DBUF_IMPL_STATIC
|
|
#include "guf_dbuf.h"
|
|
|
|
#define GUF_RAND_IMPL_STATIC
|
|
#include "guf_rand.h"
|
|
|
|
#include "guf_dict_impl.h"
|
|
|
|
int main(void)
|
|
{
|
|
printf("libguf test: compiled with C %ld\n", __STDC_VERSION__);
|
|
|
|
guf_allocator test_allocator = guf_allocator_libc;
|
|
guf_libc_alloc_ctx test_allocator_ctx = {.alloc_type_id = 0, .thread_id = 0, .zero_init = true};
|
|
test_allocator.ctx = &test_allocator_ctx;
|
|
|
|
dict_cstr_int ht;
|
|
dict_cstr_int_init(&ht, &test_allocator);
|
|
|
|
dict_cstr_int_insert_val_arg(&ht, "Hello", 42, GUF_CPY_VALUE, GUF_CPY_VALUE);
|
|
dict_cstr_int_insert_val_arg(&ht, "World", 64, GUF_CPY_VALUE, GUF_CPY_VALUE);
|
|
|
|
int kv_iter = 0;
|
|
GUF_CNT_FOREACH(&ht, dict_cstr_int, kv_it) {
|
|
printf("%d: %s -> %d\n", kv_iter++, kv_it.ptr->key, kv_it.ptr->val);
|
|
}
|
|
|
|
guf_cstr_const key = "World";
|
|
int *res = dict_cstr_int_at_val_arg(&ht, "World");
|
|
if (res) {
|
|
printf("%s: %d\n", key, *res);
|
|
} else {
|
|
printf("key '%s' not found\n", key);
|
|
}
|
|
|
|
GUF_ASSERT(dict_cstr_int_at_val_arg(&ht, "World"));
|
|
GUF_ASSERT(dict_cstr_int_at_val_arg(&ht, "Hello"));
|
|
GUF_ASSERT(dict_cstr_int_at_val_arg(&ht, "hello") == NULL);
|
|
GUF_ASSERT(dict_cstr_int_at_val_arg(&ht, "") == NULL);
|
|
|
|
GUF_ASSERT(dict_cstr_int_contains_val_arg(&ht, "World"));
|
|
GUF_ASSERT(dict_cstr_int_contains_val_arg(&ht, "Hello"));
|
|
|
|
const int ht_needle_val = 64;
|
|
const dict_cstr_int_iter ht_it = dict_cstr_int_find_val(&ht, dict_cstr_int_begin(&ht), dict_cstr_int_end(&ht), &ht_needle_val);
|
|
|
|
if (!dict_cstr_int_iter_is_end(&ht, ht_it)) {
|
|
printf("found value %d (key %s)\n", ht_needle_val, ht_it.ptr->key);
|
|
}
|
|
|
|
dict_cstr_int_free(&ht, NULL);
|
|
|
|
|
|
GUF_CNT_LIFETIME_BLOCK(dbuf_float, floats, {
|
|
floats = dbuf_float_new(&guf_allocator_libc);
|
|
|
|
for (int i = 0; i <= 16; ++i) {
|
|
dbuf_float_push_val(&floats, i % 2 ? i * -2.f : i * 2.f);
|
|
}
|
|
|
|
// float *tmp = test_allocator.alloc(floats.size * sizeof(float), &test_allocator_ctx);
|
|
// float *res = float_arr_merge_sort(floats.data, tmp, floats.size, GUF_SORT_ASCENDING, NULL);
|
|
// test_allocator.free(tmp, floats.size * sizeof(float), &test_allocator_ctx);
|
|
// GUF_ASSERT_RELEASE(res == floats.data);
|
|
|
|
float_arr_qsort(floats.data, floats.size, GUF_SORT_ASCENDING, NULL);
|
|
GUF_ASSERT_RELEASE(float_arr_is_sorted(floats.data, floats.size, GUF_SORT_ASCENDING, NULL));
|
|
|
|
GUF_CNT_FOREACH(&floats, dbuf_float, it) {
|
|
printf("float: %f\n", (double)*it.ptr);
|
|
}
|
|
})
|
|
|
|
dbuf_heap_cstr strings = dbuf_heap_cstr_new(&guf_allocator_libc);
|
|
dbuf_heap_cstr_push_val_cpy(&strings, "Foo 1");
|
|
dbuf_heap_cstr_push_val_cpy(&strings, "Bar 2");
|
|
char *move_me = strdup("Baz 3");
|
|
dbuf_heap_cstr_push(&strings, &move_me, GUF_CPY_MOVE);
|
|
GUF_ASSERT_RELEASE(move_me == NULL);
|
|
|
|
dbuf_heap_cstr_push_val_cpy(&strings, "Boz 4");
|
|
|
|
char *findme = "Baz 3";
|
|
dbuf_heap_cstr_iter beg = dbuf_heap_cstr_begin(&strings);
|
|
dbuf_heap_cstr_iter end = dbuf_heap_cstr_end(&strings);
|
|
dbuf_heap_cstr_iter fnd_it = dbuf_heap_cstr_find(&strings, beg, end, &findme);
|
|
if (!dbuf_heap_cstr_iter_is_end(&strings, fnd_it)) {
|
|
printf("%s found in range [%td, %td) at idx %td\n", findme, dbuf_heap_cstr_iter_to_idx(&strings, beg), dbuf_heap_cstr_iter_to_idx(&strings, end), dbuf_heap_cstr_iter_to_idx(&strings, fnd_it));
|
|
} else {
|
|
printf("%s not found in range [%td, %td) at idx %td\n", findme, dbuf_heap_cstr_iter_to_idx(&strings, beg), dbuf_heap_cstr_iter_to_idx(&strings, end), dbuf_heap_cstr_iter_to_idx(&strings, fnd_it));
|
|
}
|
|
|
|
if (dbuf_heap_cstr_contains_val(&strings, "Baz 3")) {
|
|
printf("contains\n");
|
|
} else {
|
|
printf("does not contain\n");
|
|
}
|
|
|
|
GUF_CNT_FOREACH(&strings, dbuf_heap_cstr, it) {
|
|
printf("%s\n", *it.ptr);
|
|
}
|
|
dbuf_heap_cstr_free(&strings, NULL);
|
|
|
|
dbuf_const_cstr const_strings = dbuf_const_cstr_new(&guf_allocator_libc);
|
|
dbuf_const_cstr_push_val(&const_strings, "Const 1");
|
|
dbuf_const_cstr_push_val(&const_strings, "Const 2");
|
|
const char *foo = "Const 3";
|
|
dbuf_const_cstr_push(&const_strings, &foo, GUF_CPY_VALUE);
|
|
|
|
dbuf_const_cstr_iter found_it = dbuf_const_cstr_find(&const_strings, dbuf_const_cstr_begin(&const_strings), dbuf_const_cstr_end(&const_strings), &foo);
|
|
if (found_it.ptr != dbuf_const_cstr_end(&const_strings).ptr) {
|
|
*found_it.ptr = "Found!";
|
|
}
|
|
|
|
GUF_CNT_FOREACH(&const_strings, dbuf_const_cstr, it) {
|
|
printf("%s\n", *it.ptr);
|
|
}
|
|
dbuf_const_cstr_free(&const_strings, NULL);
|
|
|
|
dbuf_int integers = dbuf_int_new(&guf_allocator_libc);
|
|
dbuf_int_push_val(&integers, 420);
|
|
dbuf_int_push_val(&integers, 520);
|
|
dbuf_int_push_val(&integers, 620);
|
|
dbuf_int_push_val(&integers, 720);
|
|
dbuf_int_push_val(&integers, 820);
|
|
|
|
guf_err err;
|
|
dbuf_int_try_at(&integers, 16, &err);
|
|
if (err) {
|
|
printf("%s %s\n", guf_err_to_str(err), GUF_ERR_MSG_EMPTY());
|
|
}
|
|
|
|
int i = 0;
|
|
GUF_DBUF_FOREACH(integers, int, elem) {
|
|
printf("elem %d: %d\n", i, *elem);
|
|
++i;
|
|
}
|
|
|
|
GUF_CNT_FOREACH(&integers, dbuf_int, it) {
|
|
printf("it-elem: %d", *it.ptr);
|
|
if (dbuf_int_iter_next(&integers, it, 1).ptr != dbuf_int_end(&integers).ptr) {
|
|
printf(", it-next: %d", *dbuf_int_iter_next(&integers, it, 1).ptr);
|
|
}
|
|
if (dbuf_int_iter_next(&integers, it, -1).ptr != dbuf_int_end(&integers).ptr) {
|
|
printf(", it-prev: %d", *dbuf_int_iter_next(&integers, it, -1).ptr);
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
for (dbuf_int_iter it = dbuf_int_begin(&integers); it.ptr != dbuf_int_end(&integers).ptr; it = dbuf_int_iter_next(&integers, it, 2)) {
|
|
printf("every other: %d\n", *it.ptr);
|
|
}
|
|
|
|
for (dbuf_int_iter it = dbuf_int_rbegin(&integers); it.ptr != dbuf_int_rend(&integers).ptr; it = dbuf_int_iter_next(&integers, it, 1)) {
|
|
printf("reverse: %d\n", *it.ptr);
|
|
}
|
|
|
|
for (dbuf_int_iter it = dbuf_int_rbegin(&integers); it.ptr != dbuf_int_rend(&integers).ptr; it = dbuf_int_iter_next(&integers, it, 2)) {
|
|
printf("every other reverse: %d (idx %td)\n", *it.ptr, dbuf_int_iter_to_idx(&integers, it));
|
|
}
|
|
|
|
dbuf_int_free(&integers, NULL);
|
|
printf("\n");
|
|
guf_randstate rng;
|
|
guf_randstate_init(&rng, time(NULL));
|
|
int heads = 0, tails = 0;
|
|
int throws = 10;
|
|
for (i = 0; i < throws; ++i) {
|
|
bool is_head = guf_rand_flip(&rng);
|
|
if (is_head) {
|
|
puts("head");
|
|
++heads;
|
|
} else {
|
|
puts("tail");
|
|
++tails;
|
|
}
|
|
}
|
|
printf("n: %d\nheads: %d\ntails: %d\n", throws, heads, tails);
|
|
|
|
int result[256];
|
|
memset(result, 0, sizeof result);
|
|
for (int n = 0; n < 32000; ++n) {
|
|
float r = roundf(guf_rand_normal_sample_one_f32(&rng, 100, 15));
|
|
r = guf_clamp_f32(r, 0, 255);
|
|
result[(int)r] += 1;
|
|
}
|
|
for (size_t n = 60; n <= 140; ++n) {
|
|
printf("%zu:\t", n);
|
|
for (int j = 0; j < result[n] / 8; ++j) {
|
|
putc('#', stdout);
|
|
}
|
|
puts("");
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|