91 lines
2.5 KiB
C
91 lines
2.5 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include "guf_cstr.h"
|
|
|
|
#define GUF_DBUF_INITIAL_CAP 128
|
|
#define GUF_CNT_NAME dbuf_int
|
|
#define GUF_CNT_T int
|
|
#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;
|
|
|
|
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);
|
|
|
|
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, "First mut!");
|
|
dbuf_heap_cstr_push_val_cpy(&mut_strings, "Second mut!");
|
|
|
|
char *move_me_pls = calloc(128, sizeof(char));
|
|
strcpy(move_me_pls, "Third mut");
|
|
dbuf_heap_cstr_push(&mut_strings, &move_me_pls, GUF_CPY_MOVE);
|
|
GUF_ASSERT_RELEASE(move_me_pls == NULL);
|
|
|
|
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;
|
|
} |