-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* impl stack tests * impl queue tests * fix random str regression
- Loading branch information
1 parent
9946e6b
commit 86fb0f1
Showing
8 changed files
with
202 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters