libflint/tests/tests.c

431 lines
9.5 KiB
C
Raw Normal View History

2021-02-01 22:06:37 +00:00
#include <stdio.h>
#include <stdlib.h>
2023-10-25 14:56:41 +00:00
#include <assert.h>
2023-12-28 20:28:13 +00:00
#include <unistd.h>
2021-02-01 22:06:37 +00:00
2023-11-29 21:40:41 +00:00
#include "lflinkedlist.h"
2024-07-08 02:50:28 +00:00
#include "lfnetwork.h"
2023-11-29 21:40:41 +00:00
#include "lfset.h"
#include "lfstack.h"
#include "lfbinarytree.h"
#include "lfvector.h"
#include "lfmath.h"
#include "lfstring.h"
2024-03-28 22:19:05 +00:00
#include "lfcrypto.h"
2024-04-05 18:13:08 +00:00
#include "lfparsing.h"
2021-02-01 22:06:37 +00:00
2023-12-28 20:28:13 +00:00
#if defined(__APPLE__) || defined(__MACH__)
#include "lfmacos.h"
#endif /* defined(__APPLE__) || defined(__MACH__) */
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);
assert(*(int*)data == 2);
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);
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);
printf("Changing r2\n");
r2 = 100;
bintree_debug_print(tree);
2021-02-01 22:06:37 +00:00
bintree_destroy(tree);
free(tree);
tree = NULL;
2021-02-01 22:06:37 +00:00
}
void test_math() {
printf("\n--- MATH TEST ---\n");
int i = 1, j = 2;
2023-10-25 14:56:41 +00:00
assert(max_int(i, j) == j);
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 = "10101101";
printf("Binary: %s\n", s);
printf("Decimal: %d\n", binstr_to_int(s));
char *s2 = "1010_1101";
printf("Binary: %s\n", s2);
printf("Decimal: %d\n", binstr_to_int(s2));
2024-01-12 15:49:53 +00:00
i = 10;
i = clamp_int(i, 2, 5);
assert(i == 5);
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);
}
2023-10-25 14:56:41 +00:00
void print_vector(Vector *vec) {
for (size_t i = 0; i < vec->length; ++i) {
int t = *(int*)vec_at(vec, i);
printf("%d ", t);
}
printf("\n");
}
void test_vector() {
printf("\n--- VECTOR TEST ---\n");
Vector *v = malloc(sizeof(Vector));
vec_init(v, NULL);
int e0 = 0;
int e1 = 1;
int e2 = 2;
int e3 = 3;
int e4 = 4;
vec_push(v, &e0);
assert(v->length == 1);
int *t = (int*)vec_at(v, 0);
assert(*t == 0);
vec_push(v, &e1);
vec_push(v, &e2);
assert(v->length == 3);
// test access outside bounds
t = (int*)vec_safe_at(v, 3);
assert(t == NULL);
printf("Before insert: ");
print_vector(v);
vec_push(v, &e4);
vec_insert(v, &e3, 3);
printf("After insert: ");
print_vector(v);
t = (int*)vec_at(v, 3);
assert(*t == e3);
t = (int*)vec_at(v, 4);
assert(*t == e4);
2023-11-29 22:39:59 +00:00
const int *min = vec_min(v, vec_cmp_int);
const int *max = vec_max(v, vec_cmp_int);
printf("min: %d\n", *min);
printf("max: %d\n", *max);
assert(*min == e0);
assert(*max == e4);
2023-10-25 14:56:41 +00:00
t = (int*)vec_remove(v, 1);
assert(t != NULL);
assert(*t == 1);
printf("After removal: ");
print_vector(v);
t = (int*)vec_remove(v, 10);
assert(t == NULL);
2023-10-25 15:42:38 +00:00
printf("\ncap before shrink: %zu\n", vec_cap(v));
vec_shrink(v);
assert(vec_len(v) == vec_cap(v));
printf("cap after shrink: %zu\n", vec_cap(v));
2023-10-25 14:56:41 +00:00
vec_destroy(v);
free(v);
2023-10-25 14:56:41 +00:00
}
void test_string() {
printf("\n--- STRING TEST ---\n");
const char* haystack = "Test one two one and also maybe two but not Gabe's least favorite number, which is not one.";
const char* needles[] = {
"one",
"two",
"Gabe"
};
size_t sub_sz = 0;
2023-12-04 01:42:18 +00:00
size_t *subs = NULL;
find_substrings(haystack, needles[0], &sub_sz, &subs);
assert(sub_sz == 3);
assert(subs[0] == 5);
assert(subs[1] == 13);
assert(subs[2] == 87);
const char *s = substr(haystack, subs[0], strlen(needles[0]));
assert(strcmp(s, needles[0]) == 0);
free(s);
free(subs);
2023-12-04 01:42:18 +00:00
subs = NULL;
2023-12-04 01:42:18 +00:00
find_substrings(haystack, needles[1], &sub_sz, &subs);
assert(sub_sz == 2);
assert(subs[0] == 9);
2023-12-04 01:42:18 +00:00
free(subs);
subs = NULL;
find_substrings("test one two", "nope", &sub_sz, &subs);
2023-12-04 01:52:55 +00:00
assert(sub_sz == 0);
assert(subs == NULL);
free(subs);
subs = NULL;
find_substrings("123", "nopes", &sub_sz, &subs);
assert(sub_sz == 0);
assert(subs == NULL);
2023-12-04 01:42:18 +00:00
free(subs);
subs = NULL;
printf("Passes all string tests\n");
}
2023-10-25 14:56:41 +00:00
2024-03-28 22:19:05 +00:00
void test_crypto() {
2024-04-05 18:13:08 +00:00
printf("\n--- CRYPTO TEST ---\n");
2024-03-28 22:19:05 +00:00
char *in = "BUTT";
2024-03-29 01:30:27 +00:00
unsigned char *s = b64_encode(in, strlen(in));
2024-03-28 22:19:05 +00:00
assert(strcmp(s, "QlVUVA==") == 0);
free(s);
char *in2 = "a longer base64 test, apparently";
s = b64_encode(in2, strlen(in2));
assert(strcmp(s, "YSBsb25nZXIgYmFzZTY0IHRlc3QsIGFwcGFyZW50bHk=") == 0);
free(s);
char *out2 = "YSBsb25nZXIgYmFzZTY0IHRlc3QsIGFwcGFyZW50bHk=";
size_t s_sz = 0;
s = (char *)b64_decode(out2, strlen(out2), &s_sz);
assert(strcmp(s, "a longer base64 test, apparently") == 0);
assert(strlen(s) == s_sz);
free(s);
2024-03-29 01:30:27 +00:00
s = hex_decode("DEADBEEF", &s_sz);
unsigned char h[4] = {
0xDE, 0xAD, 0xBE, 0xEF
};
for (size_t i = 0; i < 4; ++i) {
assert(s[i] == h[i]);
}
free(s);
2024-03-29 01:32:59 +00:00
// Odd number of characters
s = hex_decode("f00f5", &s_sz);
unsigned char h2[4] = {
0x0F, 0x00, 0xF5
};
for (size_t i = 0; i < 3; ++i) {
assert(s[i] == h2[i]);
}
free(s);
2024-03-29 02:09:39 +00:00
2024-04-11 14:17:42 +00:00
// leading 0x
s = hex_decode("0xf00f5", &s_sz);
for (size_t i = 0; i < 3; ++i) {
assert(s[i] == h2[i]);
}
free(s);
2024-03-29 02:09:39 +00:00
s = hex_encode(h, 4);
2024-04-09 21:24:16 +00:00
assert(strcmp(s, "deadbeef") == 0);
2024-03-29 02:09:39 +00:00
free(s);
2024-03-29 14:18:01 +00:00
// "Sup?"
unsigned char hexsup[4] = {
0x53, 0x75, 0x70, 0x3F
};
s = hex_to_str(hexsup, 4);
assert(strcmp(s, "Sup?") == 0);
free(s);
2024-04-05 18:13:08 +00:00
2024-04-09 17:47:34 +00:00
s = repeating_key_xor_s("TEST", "HI");
const char *enc = hex_encode(s, 4);
2024-04-09 21:24:16 +00:00
assert(strcmp(enc, "1c0c1b1d") == 0);
2024-04-09 17:47:34 +00:00
free(enc);
free(s);
2024-05-06 00:05:12 +00:00
unsigned char ua[2] = { 0x2, 0xF };
unsigned char ub[2] = { 0x4, 0xE };
unsigned int hamming = hamming_distance(ua, ub, 2);
assert(hamming == 3);
hamming = hamming_distance_s("this is a test", "wokka wokka!!!");
2024-05-04 18:35:54 +00:00
assert(hamming == 37);
2024-04-05 18:13:08 +00:00
printf("Passes all crypto tests\n");
}
void test_parsing() {
printf("\n--- PARSING TEST ---\n");
char *nonsense = "8d82jI|dms~<>s2d";
char *english = "This is an English sentence!";
assert(simple_english_scoring(english) > simple_english_scoring(nonsense));
printf("Passes all parsing tests\n");
2024-03-28 22:19:05 +00:00
}
2023-12-28 20:31:14 +00:00
#if defined(__APPLE__) || defined(__MACH__)
2023-12-28 20:28:13 +00:00
void test_macos() {
printf("\n--- macOS TEST ---\n");
pid_t pid = getpid();
ProcessData *pd = new_ProcessData();
for (int i = 0; i < 4; i++) {
update_process(pid, pd);
printf("CPU: %.2f\n", pd->percent_cpu);
sleep(1);
}
free(pd);
}
2023-12-28 20:31:14 +00:00
#endif
2023-12-28 20:28:13 +00:00
2021-02-01 22:06:37 +00:00
int main() {
test_ll();
test_set();
test_stack();
test_bintree();
test_math();
2023-10-25 14:56:41 +00:00
test_vector();
test_string();
2024-03-28 22:19:05 +00:00
test_crypto();
2024-04-05 18:13:08 +00:00
test_parsing();
2023-12-28 20:28:13 +00:00
#if defined(__APPLE__) || defined(__MACH__)
test_macos();
#endif
2021-02-01 22:06:37 +00:00
return 0;
2024-07-08 02:50:28 +00:00
}