2021-02-01 22:06:37 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2021-12-21 14:48:29 +00:00
|
|
|
#include "lflinkedlist.h"
|
|
|
|
#include "lfset.h"
|
|
|
|
#include "lfstack.h"
|
|
|
|
#include "lfbinarytree.h"
|
|
|
|
#include "lfmath.h"
|
2021-02-01 22:06:37 +00:00
|
|
|
|
2022-03-28 17:52:16 +00:00
|
|
|
void print_ll(List *list) {
|
|
|
|
LL_ITER(list) {
|
|
|
|
printf(" %d", *((int *) node->data));
|
2021-02-01 22:06:37 +00:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_ll() {
|
|
|
|
printf("\n--- LIST TEST ---\n");
|
2022-03-28 17:52:16 +00:00
|
|
|
List *list = malloc(sizeof(List));
|
2021-02-01 22:06:37 +00:00
|
|
|
ll_init(list, NULL);
|
|
|
|
|
|
|
|
int i = 1;
|
|
|
|
int j = 2;
|
|
|
|
int k = 4;
|
|
|
|
|
2022-03-28 17:52:16 +00:00
|
|
|
ll_ins_next(list, list->head, (void *) &i);
|
|
|
|
ll_ins_next(list, list->tail, (void *) &j);
|
|
|
|
ll_ins_next(list, list->tail, (void *) &k);
|
2021-02-01 22:06:37 +00:00
|
|
|
|
2021-02-01 22:27:01 +00:00
|
|
|
printf("List: ");
|
2021-02-01 22:06:37 +00:00
|
|
|
print_ll(list);
|
|
|
|
|
2022-03-28 17:52:16 +00:00
|
|
|
void *data;
|
2021-02-01 22:06:37 +00:00
|
|
|
ll_remove_next(list, list->head, &data);
|
|
|
|
|
2021-02-01 22:27:01 +00:00
|
|
|
printf("List: ");
|
2021-02-01 22:06:37 +00:00
|
|
|
print_ll(list);
|
2022-03-28 17:52:16 +00:00
|
|
|
printf("Removed: %d\n", *((int *) data));
|
2021-02-01 22:06:37 +00:00
|
|
|
|
|
|
|
ll_destroy(list);
|
|
|
|
free(list);
|
|
|
|
}
|
|
|
|
|
2022-03-28 17:52:16 +00:00
|
|
|
int int_match(const void *a, const void *b) {
|
|
|
|
return *((int *) a) == *((int *) b);
|
2021-02-01 22:06:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_set() {
|
|
|
|
printf("\n--- SET TEST ---\n");
|
2022-03-28 17:52:16 +00:00
|
|
|
Set *set = malloc(sizeof(Set));
|
2021-02-01 22:06:37 +00:00
|
|
|
set_init(set, int_match, NULL);
|
|
|
|
|
|
|
|
int i = 1;
|
|
|
|
int j = 2;
|
|
|
|
int k = 2;
|
|
|
|
|
2022-03-28 17:52:16 +00:00
|
|
|
set_insert(set, (void *) &i);
|
|
|
|
set_insert(set, (void *) &j);
|
|
|
|
set_insert(set, (void *) &k);
|
2021-02-01 22:06:37 +00:00
|
|
|
|
|
|
|
int i2 = 1;
|
|
|
|
int j2 = 4;
|
|
|
|
|
2022-03-28 17:52:16 +00:00
|
|
|
Set *set2 = malloc(sizeof(Set));
|
2021-02-01 22:06:37 +00:00
|
|
|
set_init(set2, int_match, NULL);
|
|
|
|
|
2022-03-28 17:52:16 +00:00
|
|
|
set_insert(set2, (void *) &i2);
|
|
|
|
set_insert(set2, (void *) &j2);
|
2021-02-01 22:06:37 +00:00
|
|
|
|
2021-02-01 22:27:01 +00:00
|
|
|
printf("Set 1:");
|
2021-02-01 22:06:37 +00:00
|
|
|
print_ll(set);
|
|
|
|
|
2021-02-01 22:27:01 +00:00
|
|
|
printf("Set 2:");
|
2021-02-01 22:06:37 +00:00
|
|
|
print_ll(set2);
|
|
|
|
printf("\n");
|
|
|
|
|
2022-03-28 17:52:16 +00:00
|
|
|
Set *set_u = malloc(sizeof(Set));
|
|
|
|
Set *set_i = malloc(sizeof(Set));
|
|
|
|
Set *set_d = malloc(sizeof(Set));
|
2021-02-01 22:06:37 +00:00
|
|
|
|
|
|
|
set_union(set_u, set, set2);
|
|
|
|
printf("Union:");
|
|
|
|
print_ll(set_u);
|
|
|
|
|
|
|
|
set_difference(set_d, set, set2);
|
|
|
|
printf("Difference:");
|
|
|
|
print_ll(set_d);
|
|
|
|
|
|
|
|
set_intersection(set_i, set, set2);
|
|
|
|
printf("Intersection:");
|
|
|
|
print_ll(set_i);
|
|
|
|
|
|
|
|
set_destroy(set);
|
|
|
|
set_destroy(set2);
|
|
|
|
set_destroy(set_u);
|
|
|
|
set_destroy(set_i);
|
|
|
|
set_destroy(set_d);
|
|
|
|
free(set);
|
|
|
|
free(set2);
|
|
|
|
free(set_u);
|
|
|
|
free(set_i);
|
|
|
|
free(set_d);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_stack() {
|
|
|
|
printf("\n--- STACK TEST ---\n");
|
2021-02-01 22:27:01 +00:00
|
|
|
Stack *stack = malloc(sizeof(Stack));
|
2021-02-01 22:06:37 +00:00
|
|
|
stack_init(stack, NULL);
|
|
|
|
|
|
|
|
int a = 1, b = 2;
|
|
|
|
stack_push(stack, &a);
|
|
|
|
stack_push(stack, &b);
|
2021-02-01 22:27:01 +00:00
|
|
|
printf("Stack size: %lu\n", stack->size);
|
2021-02-01 22:06:37 +00:00
|
|
|
|
|
|
|
int *p = NULL;
|
2022-03-28 17:52:16 +00:00
|
|
|
stack_pop(stack, (void **) &p);
|
2021-02-01 22:06:37 +00:00
|
|
|
printf("b = %d\n", *p);
|
|
|
|
|
2022-03-28 17:52:16 +00:00
|
|
|
stack_pop(stack, (void **) &p);
|
2021-02-01 22:06:37 +00:00
|
|
|
printf("a = %d\n", *p);
|
2021-02-01 22:27:01 +00:00
|
|
|
printf("Stack size: %lu\n", stack->size);
|
2021-02-01 22:06:37 +00:00
|
|
|
|
|
|
|
stack_destroy(stack);
|
2022-03-30 21:35:42 +00:00
|
|
|
free(stack);
|
|
|
|
stack = NULL;
|
2021-02-01 22:06:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_bintree() {
|
|
|
|
printf("\n--- BINARY TREE TEST ---\n");
|
2021-02-01 22:27:01 +00:00
|
|
|
BinTree *tree = malloc(sizeof(BinTree));
|
2021-02-01 22:06:37 +00:00
|
|
|
bintree_init(tree, NULL);
|
|
|
|
|
|
|
|
int root = 0;
|
|
|
|
int l1 = 1;
|
|
|
|
int l2 = 2;
|
|
|
|
int r1 = 12;
|
|
|
|
int r2 = 200;
|
|
|
|
|
|
|
|
bintree_ins_left(tree, NULL, &root);
|
|
|
|
bintree_ins_left(tree, tree->root, &l1);
|
|
|
|
bintree_ins_left(tree, tree->root->left, &l2);
|
|
|
|
bintree_ins_right(tree, tree->root->left, &r2);
|
|
|
|
bintree_ins_right(tree, tree->root, &r1);
|
|
|
|
bintree_ins_right(tree, tree->root->right, &r2);
|
|
|
|
bintree_ins_left(tree, tree->root->right, &l1);
|
|
|
|
|
|
|
|
bintree_debug_print(tree);
|
|
|
|
|
2022-03-30 21:35:42 +00:00
|
|
|
printf("Changing r2\n");
|
|
|
|
r2 = 100;
|
|
|
|
bintree_debug_print(tree);
|
|
|
|
|
2021-02-01 22:06:37 +00:00
|
|
|
bintree_destroy(tree);
|
2022-03-30 21:35:42 +00:00
|
|
|
free(tree);
|
|
|
|
tree = NULL;
|
2021-02-01 22:06:37 +00:00
|
|
|
}
|
|
|
|
|
2021-12-21 14:48:29 +00:00
|
|
|
void test_math() {
|
|
|
|
printf("\n--- MATH TEST ---\n");
|
|
|
|
int i = 1, j = 2;
|
|
|
|
printf("Between %d and %d, %d is larger\n", i, j, max_int(i, j));
|
|
|
|
printf("Between %d and %d, %d is smaller\n", i, j, min_int(i, j));
|
|
|
|
|
|
|
|
char *s = "1010110";
|
|
|
|
printf("Binary: %s\n", s);
|
|
|
|
printf("Decimal: %d\n", binstr_to_int(s));
|
2022-03-30 21:35:42 +00:00
|
|
|
|
|
|
|
printf("\nGenerate line from 0,0 to 2,5\n");
|
|
|
|
size_t sz = 0;
|
|
|
|
Point *line = bresenham(0, 0, 2, 5, &sz);
|
|
|
|
for (size_t idx = 0; idx < sz; idx++) {
|
|
|
|
printf("%d,%d ", line[idx].x, line[idx].y);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
free(line);
|
2021-12-21 14:48:29 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 22:06:37 +00:00
|
|
|
int main() {
|
|
|
|
test_ll();
|
|
|
|
test_set();
|
|
|
|
test_stack();
|
|
|
|
test_bintree();
|
2021-12-21 14:48:29 +00:00
|
|
|
test_math();
|
2021-02-01 22:06:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|