Skip to content

Commit

Permalink
Stack & queue tests (#21)
Browse files Browse the repository at this point in the history
* impl stack tests

* impl queue tests

* fix random str regression
  • Loading branch information
a-lafrance authored Apr 8, 2022
1 parent 9946e6b commit 86fb0f1
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 8 deletions.
12 changes: 6 additions & 6 deletions include/tests/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ if (!(condition)) { \
#define assert(condition) assert_or(condition, #condition)
#define assert_false(condition) assert_or(!(condition), #condition "is true, but should be false")

#define assert_eq(lhs, rhs) assert(lhs == rhs)
#define assert_ne(lhs, rhs) assert(lhs != rhs)
#define assert_eq(lhs, rhs) assert((lhs) == (rhs))
#define assert_ne(lhs, rhs) assert((lhs) != (rhs))

#define assert_gt(lhs, rhs) assert(lhs > rhs)
#define assert_lt(lhs, rhs) assert(lhs < rhs)
#define assert_gt(lhs, rhs) assert((lhs) > (rhs))
#define assert_lt(lhs, rhs) assert((lhs) < (rhs))

#define assert_ge(lhs, rhs) assert(lhs >= rhs)
#define assert_le(lhs, rhs) assert(lhs <= rhs)
#define assert_ge(lhs, rhs) assert((lhs) >= (rhs))
#define assert_le(lhs, rhs) assert((lhs) <= (rhs))

#endif
Binary file removed src/lfc/collections/.str.c.swp
Binary file not shown.
2 changes: 1 addition & 1 deletion src/lfc/collections/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void* queue_pop(queue_t* q) {
}

void* queue_peek(queue_t* q) {
return ll_last(&q->base);
return ll_first(&q->base);
}

size_t queue_len(queue_t* q) {
Expand Down
Binary file removed src/lfc/collections/tests/.map_tests.c.swp
Binary file not shown.
90 changes: 90 additions & 0 deletions src/lfc/collections/tests/queue_tests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "lfc/collections/queue.h"

#include "tests/assert.h"
#include "tests/setup.h"
#include "tests/utils.h"

void test_queue_init_correctly() {
start_test();

queue_t q;
queue_init(&q);

assert_eq(queue_len(&q), 0);
assert_eq(queue_peek(&q), NULL);
assert(queue_is_empty(&q));

queue_free(&q, NULL);

end_test();
}

void test_queue_push_pop_on_empty() {
start_test();

int i = 5;
queue_t q;
queue_init(&q);
queue_push(&q, &i);

assert_eq(queue_len(&q), 1);
assert_eq(*(int*)queue_peek(&q), i);
assert_false(queue_is_empty(&q));

assert_eq(*(int*)queue_pop(&q), i);
assert_eq(queue_len(&q), 0);
assert_eq(queue_peek(&q), NULL);
assert(queue_is_empty(&q));

queue_free(&q, NULL);

end_test();
}

void test_queue_push_pop_many() {
start_test();

int len = 3;
int v[] = {8, 15, 17};
queue_t q;
queue_init(&q);

for (unsigned int i = 0; i < len; i++) {
queue_push(&q, v + i);

assert_eq(queue_len(&q), i + 1);
assert_eq(*(int*)queue_peek(&q), v[0]);
assert_false(queue_is_empty(&q));
}

assert_eq(queue_len(&q), len);
assert_eq(*(int*)queue_peek(&q), v[0]);
assert_false(queue_is_empty(&q));

for (int i = 0; i < len; i++) {
assert_eq(*(int*)queue_pop(&q), v[i]);
assert_eq(queue_len(&q), len - 1 - i);

if (i < len - 1) {
assert_eq(*(int*)queue_peek(&q), v[i + 1]);
} else {
assert_eq(queue_peek(&q), NULL);
}

assert_eq(queue_is_empty(&q), i == len - 1);
}

queue_free(&q, NULL);

end_test();
}

void run_queue_tests() {
start_suite();

test_queue_init_correctly();
test_queue_push_pop_on_empty();
test_queue_push_pop_many();

end_suite();
}
96 changes: 96 additions & 0 deletions src/lfc/collections/tests/stack_tests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "lfc/collections/stack.h"

#include "tests/assert.h"
#include "tests/setup.h"
#include "tests/utils.h"

void test_stack_init_correctly() {
start_test();

vstack_t stack;
stack_init(&stack, sizeof(int));

assert_eq(stack_len(&stack), 0);
assert_eq(stack_peek(&stack), NULL);
assert(stack_is_empty(&stack));

stack_free(&stack, NULL);

end_test();
}

void test_stack_push_pop_on_empty() {
start_test();

int i = 5;
vstack_t stack;
stack_init(&stack, sizeof(int));
stack_push(&stack, &i);

assert_eq(stack_len(&stack), 1);
assert_eq(*(int*)stack_peek(&stack), i);
assert_false(stack_is_empty(&stack));

int n;
stack_pop(&stack, &n);

assert_eq(n, i);
assert_eq(stack_len(&stack), 0);
assert_eq(stack_peek(&stack), NULL);
assert(stack_is_empty(&stack));

stack_free(&stack, NULL);

end_test();
}

void test_stack_push_pop_many() {
start_test();

int len = 3;
int v[] = {8, 15, 17};
vstack_t stack;
stack_init(&stack, sizeof(int));

for (unsigned int i = 0; i < len; i++) {
stack_push(&stack, v + i);

assert_eq(stack_len(&stack), i + 1);
assert_eq(*(int*)stack_peek(&stack), v[i]);
assert_false(stack_is_empty(&stack));
}

assert_eq(stack_len(&stack), len);
assert_eq(*(int*)stack_peek(&stack), v[len - 1]);
assert_false(stack_is_empty(&stack));

for (int i = len - 1; i >= 0; i--) {
int n;
stack_pop(&stack, &n);

assert_eq(n, v[i]);
assert_eq(stack_len(&stack), i);

if (i > 0) {
assert_eq(*(int*)stack_peek(&stack), v[i - 1]);
} else {
assert_eq(stack_peek(&stack), NULL);
}

assert_eq(stack_is_empty(&stack), i == 0);
}

stack_free(&stack, NULL);

end_test();
}

void run_stack_tests() {
start_suite();

test_stack_init_correctly();
test_stack_push_pop_on_empty();
test_stack_push_pop_many();

end_suite();
}
2 changes: 1 addition & 1 deletion src/lfc/collections/tests/str_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void test_str_pushed_chars_correctly() {
str_push(&str, c);

assert_eq(str.len, i + 1);
assert_eq(str.capacity, i > STR_DEFAULT_CAPACITY ? 2 * STR_DEFAULT_CAPACITY : STR_DEFAULT_CAPACITY);
assert_eq(str.capacity, i == STR_DEFAULT_CAPACITY ? 2 * STR_DEFAULT_CAPACITY : STR_DEFAULT_CAPACITY);
assert_false(str_is_empty(&str));
assert_eq(str_get(&str, i), c);
}
Expand Down
8 changes: 8 additions & 0 deletions src/tests/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
void run_array_tests();
void run_linkedlist_tests();
void run_map_tests();
void run_queue_tests();
void run_set_tests();
void run_stack_tests();
void run_str_tests();
void run_vector_tests();

Expand All @@ -20,7 +22,9 @@ int main(int argc, char** argv) {
run_array_tests();
run_linkedlist_tests();
run_map_tests();
run_queue_tests();
run_set_tests();
run_stack_tests();
run_str_tests();
run_vector_tests();

Expand All @@ -40,8 +44,12 @@ int main(int argc, char** argv) {
run_linkedlist_tests();
} else if (strcmp(suite, "map") == 0) {
run_map_tests();
} else if (strcmp(suite, "queue") == 0) {
run_queue_tests();
} else if (strcmp(suite, "set") == 0) {
run_set_tests();
} else if (strcmp(suite, "stack") == 0) {
run_stack_tests();
} else if (strcmp(suite, "str") == 0) {
run_str_tests();
} else if (strcmp(suite, "vector") == 0) {
Expand Down

0 comments on commit 86fb0f1

Please sign in to comment.