206 lines
4.4 KiB
C
206 lines
4.4 KiB
C
|
|
#define NILEAKDETECTOR_VERBOSE true
|
||
|
|
#include "../../ni-leakdetector/nileakdetector.h"
|
||
|
|
|
||
|
|
#define NIARENA_IMPLEMENTATION
|
||
|
|
#define NILINKEDLIST_IMPLEMENTATION
|
||
|
|
|
||
|
|
#include "../../ni-arena/niarena.h"
|
||
|
|
#include "../nilinkedlist.h"
|
||
|
|
|
||
|
|
#include <assert.h>
|
||
|
|
#include <stdio.h>
|
||
|
|
|
||
|
|
NIArena *arena;
|
||
|
|
|
||
|
|
void
|
||
|
|
nilinkedlist_new_test()
|
||
|
|
{
|
||
|
|
NILinkedList *list = nilinkedlist_new(&arena);
|
||
|
|
assert(nilinkedlist_empty(&list));
|
||
|
|
}
|
||
|
|
|
||
|
|
void
|
||
|
|
nilinkedlist_stack_test()
|
||
|
|
{
|
||
|
|
NILinkedList *list = nilinkedlist_new(&arena);
|
||
|
|
|
||
|
|
// 0, 1, 2
|
||
|
|
for(int i = 0; i < 3; i++) {
|
||
|
|
int *n = (int *)niarena_alloc(&arena, sizeof(int));
|
||
|
|
*n = i;
|
||
|
|
nilinkedlist_push(&list, &arena, n);
|
||
|
|
}
|
||
|
|
assert(list->count == 3);
|
||
|
|
|
||
|
|
// 2, 1, 0
|
||
|
|
for(int i = 2; i >= 0; i--) {
|
||
|
|
int *n = (int *)nilinkedlist_pop(&list);
|
||
|
|
assert(*n == i);
|
||
|
|
}
|
||
|
|
|
||
|
|
assert(nilinkedlist_empty(&list));
|
||
|
|
}
|
||
|
|
|
||
|
|
void
|
||
|
|
nilinkedlist_queue_test()
|
||
|
|
{
|
||
|
|
NILinkedList *list = nilinkedlist_new(&arena);
|
||
|
|
|
||
|
|
// 0, 1, 2
|
||
|
|
for(int i = 0; i < 3; i++) {
|
||
|
|
int *n = (int *)niarena_alloc(&arena, sizeof(int));
|
||
|
|
*n = i;
|
||
|
|
nilinkedlist_emplace(&list, &arena, n, 0); // insert at the beginning
|
||
|
|
}
|
||
|
|
assert(list->count == 3);
|
||
|
|
|
||
|
|
// 0, 1, 2
|
||
|
|
for(int i = 0; i < 3; i++) {
|
||
|
|
int *n = (int *)nilinkedlist_pop(&list);
|
||
|
|
assert(*n == i);
|
||
|
|
}
|
||
|
|
|
||
|
|
assert(nilinkedlist_empty(&list));
|
||
|
|
}
|
||
|
|
|
||
|
|
void
|
||
|
|
nilinkedlist_emplace_test()
|
||
|
|
{
|
||
|
|
NILinkedList *list = nilinkedlist_new(&arena);
|
||
|
|
|
||
|
|
for(int i = 0; i < 3; i++) {
|
||
|
|
int *n = (int *)niarena_alloc(&arena, sizeof(int));
|
||
|
|
*n = i;
|
||
|
|
nilinkedlist_push(&list, &arena, n);
|
||
|
|
}
|
||
|
|
// 0, 1, 2
|
||
|
|
int *i_1 = (int *)niarena_alloc(&arena, sizeof(int));
|
||
|
|
*i_1 = 101;
|
||
|
|
nilinkedlist_emplace(&list, &arena, i_1, 1);
|
||
|
|
// 0, 101, 1, 2
|
||
|
|
int *i_2 = (int *)niarena_alloc(&arena, sizeof(int));
|
||
|
|
*i_2 = 102;
|
||
|
|
nilinkedlist_emplace(&list, &arena, i_2, 3);
|
||
|
|
// 0, 101, 1, 102, 2
|
||
|
|
int *i_3 = (int *)niarena_alloc(&arena, sizeof(int));
|
||
|
|
*i_3 = 103;
|
||
|
|
nilinkedlist_emplace(&list, &arena, i_3, 5);
|
||
|
|
// 0, 101, 1, 102, 2, 103
|
||
|
|
int r[6] = {0, 101, 1, 102, 2, 103};
|
||
|
|
for(int i = 5; i >= 0; i--) {
|
||
|
|
int *n = (int *)nilinkedlist_pop(&list);
|
||
|
|
assert(*n == r[i]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void
|
||
|
|
nilinkedlist_emplace_far_test()
|
||
|
|
{
|
||
|
|
NILinkedList *list = nilinkedlist_new(&arena);
|
||
|
|
|
||
|
|
int *i = (int *)niarena_alloc(&arena, sizeof(int));
|
||
|
|
*i = 1000;
|
||
|
|
nilinkedlist_emplace(&list, &arena, i, 10000);
|
||
|
|
int *j = (int *)niarena_alloc(&arena, sizeof(int));
|
||
|
|
*j = 2000;
|
||
|
|
nilinkedlist_emplace(&list, &arena, j, 20000);
|
||
|
|
|
||
|
|
int *k = (int *)nilinkedlist_pop(&list);
|
||
|
|
assert(*k == 2000);
|
||
|
|
int *l = (int *)nilinkedlist_pop(&list);
|
||
|
|
assert(*l == 1000);
|
||
|
|
}
|
||
|
|
|
||
|
|
void
|
||
|
|
nilinkedlist_int_printf_visitor(const void *content)
|
||
|
|
{
|
||
|
|
int *n = (int *)content;
|
||
|
|
printf("%d\n", *n);
|
||
|
|
}
|
||
|
|
|
||
|
|
void
|
||
|
|
nilinkedlist_traverse_test()
|
||
|
|
{
|
||
|
|
NILinkedList *list = nilinkedlist_new(&arena);
|
||
|
|
|
||
|
|
for(int i = 0; i < 5; i++) {
|
||
|
|
int *n = (int *)niarena_alloc(&arena, sizeof(int));
|
||
|
|
*n = i;
|
||
|
|
nilinkedlist_push(&list, &arena, n);
|
||
|
|
}
|
||
|
|
|
||
|
|
nilinkedlist_traverse(list, nilinkedlist_int_printf_visitor);
|
||
|
|
}
|
||
|
|
|
||
|
|
void *
|
||
|
|
nilinkedlist_int_duplicate(const void *p, NIArena **_arena)
|
||
|
|
{
|
||
|
|
int *n = (int *)p;
|
||
|
|
int *np = (int *)niarena_alloc(_arena, sizeof(int));
|
||
|
|
*np = *n * 2;
|
||
|
|
return (void *)np;
|
||
|
|
}
|
||
|
|
|
||
|
|
void
|
||
|
|
nilinkedlist_map_test()
|
||
|
|
{
|
||
|
|
NILinkedList *list = nilinkedlist_new(&arena);
|
||
|
|
for(int i = 0; i < 5; i++) {
|
||
|
|
int *n = (int *)niarena_alloc(&arena, sizeof(int));
|
||
|
|
*n = 100 + i;
|
||
|
|
nilinkedlist_push(&list, &arena, n);
|
||
|
|
}
|
||
|
|
NILinkedList *new_list = nilinkedlist_new(&arena);
|
||
|
|
nilinkedlist_map(list, &arena, nilinkedlist_int_duplicate, &new_list);
|
||
|
|
for(int i = 0; i < 5; i++) {
|
||
|
|
const int *j = (int *)nilinkedlist_peek(new_list, i);
|
||
|
|
assert(*j == 2 * (100 + i));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void
|
||
|
|
nilinkedlist_int_sum(void **sum_pp, const void *current)
|
||
|
|
{
|
||
|
|
int **current_sum_pp = (int **)sum_pp;
|
||
|
|
const int *current_int = (int *)current;
|
||
|
|
*(*current_sum_pp) += *current_int;
|
||
|
|
}
|
||
|
|
|
||
|
|
void
|
||
|
|
nilinkedlist_reduce_test()
|
||
|
|
{
|
||
|
|
NILinkedList *list = nilinkedlist_new(&arena);
|
||
|
|
int total = 0;
|
||
|
|
for(int i = 0; i < 5; i++) {
|
||
|
|
int *n = (int *)niarena_alloc(&arena, sizeof(int));
|
||
|
|
*n = 100 + i;
|
||
|
|
total += 100 + i;
|
||
|
|
nilinkedlist_push(&list, &arena, n);
|
||
|
|
}
|
||
|
|
int *result = niarena_alloc(&arena, sizeof(int));
|
||
|
|
nilinkedlist_reduce(list, nilinkedlist_int_sum, (void **)&result);
|
||
|
|
assert(*result == total);
|
||
|
|
}
|
||
|
|
|
||
|
|
int
|
||
|
|
main()
|
||
|
|
{
|
||
|
|
arena = niarena_new();
|
||
|
|
|
||
|
|
nilinkedlist_new_test();
|
||
|
|
nilinkedlist_stack_test();
|
||
|
|
nilinkedlist_queue_test();
|
||
|
|
nilinkedlist_emplace_test();
|
||
|
|
nilinkedlist_emplace_far_test();
|
||
|
|
|
||
|
|
nilinkedlist_traverse_test();
|
||
|
|
nilinkedlist_map_test();
|
||
|
|
nilinkedlist_reduce_test();
|
||
|
|
|
||
|
|
niarena_delete(arena);
|
||
|
|
nileakdetector_print_summary();
|
||
|
|
|
||
|
|
printf("== TESTS SUCCESSFUL ==\n");
|
||
|
|
return 0;
|
||
|
|
}
|