boot.dev lesson answers for the course: Learn Memory Management in C
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

88 lines
2.3 KiB

#include "bootlib.h"
#include "munit.h"
#include "snekstack.h"
munit_case(RUN, pop_stack, {
my_stack_t *s = stack_new(2);
assert_ptr_not_null(s, "Must allocate a new stack");
assert_int(s->capacity, ==, 2, "Sets capacity to 2");
assert_int(s->count, ==, 0, "No elements in the stack yet");
assert_ptr_not_null(s->data, "Allocates the stack data");
int one = 1;
int two = 2;
int three = 3;
stack_push(s, &one);
stack_push(s, &two);
assert_int(s->capacity, ==, 2, "Sets capacity to 2");
assert_int(s->count, ==, 2, "2 elements in the stack");
stack_push(s, &three);
assert_int(s->capacity, ==, 4, "Capacity is doubled");
assert_int(s->count, ==, 3, "3 elements in the stack");
int *popped = stack_pop(s);
assert_int(*popped, ==, three, "Should pop the last element");
popped = stack_pop(s);
assert_int(*popped, ==, two, "Should pop the last element");
popped = stack_pop(s);
assert_int(*popped, ==, one, "Should pop the only remaining element");
popped = stack_pop(s);
assert_null(popped, "No remaining elements");
stack_free(s);
assert(boot_all_freed());
});
munit_case(RUN, push_stack, {
my_stack_t *s = stack_new(2);
assert_ptr_not_null(s, "Must allocate a new stack");
assert_int(s->capacity, ==, 2, "Sets capacity to 2");
assert_int(s->count, ==, 0, "No elements in the stack yet");
assert_ptr_not_null(s->data, "Allocates the stack data");
int a = 1;
stack_push(s, &a);
stack_push(s, &a);
assert_int(s->capacity, ==, 2, "Sets capacity to 2");
assert_int(s->count, ==, 2, "2 elements in the stack");
stack_push(s, &a);
assert_int(s->capacity, ==, 4, "Capacity is doubled");
assert_int(s->count, ==, 3, "3 elements in the stack");
stack_free(s);
assert(boot_all_freed());
});
munit_case(RUN, create_stack, {
my_stack_t *s = stack_new(10);
assert_int(s->capacity, ==, 10, "Sets capacity to 10");
assert_int(s->count, ==, 0, "No elements in the stack yet");
assert_ptr_not_null(s->data, "Allocates the stack data");
stack_free(s);
assert(boot_all_freed());
});
int main() {
MunitTest tests[] = {
munit_test("/create_stack", create_stack),
munit_test("/push_stack", push_stack),
munit_test("/pop_stack", pop_stack),
munit_null_test,
};
MunitSuite suite = munit_suite("snekstack", tests);
return munit_suite_main(&suite, NULL, 0, NULL);
}