132 lines
3.6 KiB
C
132 lines
3.6 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
|
|
#define GUF_IMPLEMENTATION
|
|
#include "guf_common.h"
|
|
#include "guf_cstr.h"
|
|
|
|
#define GUF_CNT_NAME dbuf_int
|
|
#define GUF_CNT_T int
|
|
#define GUF_CNT_T_OPS guf_int_ops
|
|
#include "guf_dbuf.h"
|
|
|
|
#define GUF_CNT_NAME dbuf_char
|
|
#define GUF_CNT_T char
|
|
#define GUF_CNT_T_OPS guf_char_ops
|
|
#include "guf_dbuf.h"
|
|
|
|
#define GUF_CNT_NAME dbuf_float
|
|
#define GUF_CNT_T float
|
|
#define GUF_CNT_T_OPS guf_float_ops
|
|
#include "guf_dbuf.h"
|
|
|
|
#define GUF_CNT_NAME dbuf_const_cstr
|
|
#define GUF_CNT_T guf_const_cstr
|
|
#define GUF_CNT_T_OPS guf_const_cstr_ops
|
|
#include "guf_dbuf.h"
|
|
|
|
#define GUF_CNT_NAME dbuf_heap_cstr
|
|
#define GUF_CNT_T guf_heap_cstr
|
|
#define GUF_CNT_T_OPS guf_heap_cstr_ops
|
|
#include "guf_dbuf.h"
|
|
|
|
// typedef struct guf_test {
|
|
// const char *name, *expected_output;
|
|
// char *output;
|
|
// void (*test_fn)(struct guf_test *test);
|
|
// uint64_t runtime_ms;
|
|
// bool passed;
|
|
// } guf_test;
|
|
|
|
int main(void)
|
|
{
|
|
bool success = true;
|
|
|
|
GUF_LIFETIME_BLOCK(dbuf_float floats = dbuf_float_new(), floats, dbuf_float_free, {
|
|
for (int i = 0; i < 10; ++i) {
|
|
dbuf_float_push_val(&floats, i % 2 ? i * 2.718f : i * -2.718f);
|
|
}
|
|
|
|
GUF_FOREACH(&floats, dbuf_float, it) {
|
|
printf("float: %f\n", *it.cur);
|
|
}
|
|
|
|
dbuf_float_sort(&floats, GUF_SORT_ASCENDING);
|
|
printf("Sorted.\n");
|
|
GUF_FOREACH(&floats, dbuf_float, it) {
|
|
printf("float: %f\n", *it.cur);
|
|
}
|
|
})
|
|
|
|
dbuf_char str = dbuf_char_new();
|
|
dbuf_char_push_val(&str, 'H');
|
|
dbuf_char_push_val(&str, 'e');
|
|
dbuf_char_push_val(&str, 'j');
|
|
dbuf_char_push_val(&str, '\0');
|
|
printf("str.data = %s\n", str.data);
|
|
dbuf_char_free(&str);
|
|
|
|
dbuf_int integers = dbuf_int_new();
|
|
dbuf_int_push_val(&integers, 420);
|
|
printf("initial cap %td\n", integers.capacity);
|
|
dbuf_int_push_val(&integers, 520);
|
|
dbuf_int_push_val(&integers, 620);
|
|
dbuf_int_push_val(&integers, 720);
|
|
|
|
printf("%d\n", *dbuf_int_at(&integers, 4));
|
|
|
|
int i = 0;
|
|
GUF_DBUF_FOREACH(integers, int, elem) {
|
|
printf("elem %d: %d\n", i, *elem);
|
|
++i;
|
|
}
|
|
|
|
GUF_FOREACH(&integers, dbuf_int, it) {
|
|
printf("it-elem: %d", *it.cur);
|
|
if (it.next(&it, 1).cur != it.end) {
|
|
printf(", it-next: %d", *it.next(&it, 1).cur);
|
|
}
|
|
if (it.next(&it, -1).cur != it.end) {
|
|
printf(", it-prev: %d", *it.next(&it, -1).cur);
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
for (dbuf_int_iter it = dbuf_int_begin(&integers); it.cur != it.end; it = it.next(&it, 2)) {
|
|
printf("every other: %d\n", *it.cur);
|
|
}
|
|
|
|
dbuf_const_cstr strings = dbuf_const_cstr_new();
|
|
dbuf_const_cstr_push_val(&strings, "First");
|
|
printf("initial cap %td\n", strings.capacity);
|
|
|
|
dbuf_const_cstr_push_val(&strings, "Second");
|
|
guf_const_cstr foo = "Hello, World!";
|
|
dbuf_const_cstr_push(&strings, &foo, GUF_CPY_VALUE);
|
|
|
|
GUF_FOREACH(&strings, dbuf_const_cstr, it) {
|
|
printf("str: %s\n", *it.cur);
|
|
}
|
|
|
|
dbuf_heap_cstr mut_strings = dbuf_heap_cstr_new();
|
|
dbuf_heap_cstr_push_val_cpy(&mut_strings, "1");
|
|
dbuf_heap_cstr_push_val_cpy(&mut_strings, "2");
|
|
|
|
char *move_me_pls = calloc(128, sizeof(char));
|
|
strcpy(move_me_pls, "3");
|
|
dbuf_heap_cstr_push(&mut_strings, &move_me_pls, GUF_CPY_MOVE);
|
|
GUF_ASSERT_RELEASE(move_me_pls == NULL);
|
|
|
|
dbuf_heap_cstr_sort(&mut_strings, GUF_SORT_DESCENDING);
|
|
|
|
GUF_FOREACH(&mut_strings, dbuf_heap_cstr, it) {
|
|
printf("str: %s\n", *it.cur);
|
|
}
|
|
|
|
dbuf_heap_cstr_free(&mut_strings);
|
|
dbuf_const_cstr_free(&strings);
|
|
dbuf_int_free(&integers);
|
|
|
|
return success ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
} |