Skip to content
Snippets Groups Projects
Commit 6895c2b8 authored by dario.genga's avatar dario.genga
Browse files

Add stack tests

Some of the tests need a verification for the asserts.
parent c5315913
Branches
No related tags found
No related merge requests found
...@@ -6,3 +6,28 @@ ...@@ -6,3 +6,28 @@
#include "stack.h" #include "stack.h"
#include <stdio.h> #include <stdio.h>
void stack_init(stack *s, int size) {
}
bool stack_is_empty(stack s) {
bool is_empty = true;
return is_empty;
}
void stack_push(stack *s, int val) {
}
void stack_pop(stack *s, int *val) {
}
void stack_peek(stack s, int *val) {
}
void stack_destroy(stack *s) {
}
...@@ -7,7 +7,110 @@ ...@@ -7,7 +7,110 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
void test_stack_init(stack *s, int size) {
printf("Testing stack_init()...\n");
stack_init(s, size);
assert(s != NULL);
assert(s->size == size);
printf("OK.\n");
}
void test_stack_destroy(stack *s) {
printf("Testing stack_destroy()...\n");
stack_destroy(s);
// TODO: Verify if s is already null or not
assert(s->data == NULL);
assert(s->top == -1);
printf("OK.\n");
}
void test_stack_is_empty(stack *s) {
printf("Testing stack_is_empty()...\n");
assert(stack_is_empty(*s) == true);
printf("OK.\n");
}
void test_stack_is_not_empty(stack *s) {
printf("Testing stack_is_not_empty()...\n");
assert(stack_is_empty(*s) == false);
printf("OK.\n");
}
void test_stack_peek(stack s, int *val) {
printf("Testing stack_peek()...\n");
stack_peek(s, val);
// TODO: Verify test
assert(val == &s.top);
printf("OK.\n");
}
void test_stack_push(stack *s, int val) {
printf("Testing stack_push()...\n");
stack_push(s, val);
// Testing push with remaining spaces
assert(s->top == val);
// Testing push on full stack
stack *stack_full;
int value_top_full_stack_value = 100;
stack_init(stack_full, 1);
stack_push(stack_full, value_top_full_stack_value);
stack_push(stack_full, val);
assert(stack_full->top == value_top_full_stack_value);
printf("OK.\n");
}
void test_stack_pop(stack *s, int *val) {
printf("Testing stack_pop()...\n");
int top_before_pop;
// Test with non-empty stack
if (s-> size == 0) {
// Add a value if the stack is empty
stack_push(s, 5);
}
top_before_pop = s->top;
stack_pop(s, val);
// TODO: Verify test
assert(val == &top_before_pop);
// Clear the stack
while(s->size != 0) {
stack_pop(s, val);
}
// Test pop when stack is empty
val = NULL;
stack_pop(s, val);
assert(val == NULL);
printf("OK.\n");
}
int main() { int main() {
stack *s, *non_empty_stack;
int size = 10, value_to_add = 7;
int *val;
printf("Starting the tests...\n");
// Test init and destroy
test_stack_init(s, size);
test_stack_destroy(s);
// Reinitialization
stack_init(s, size);
stack_init(non_empty_stack, 3);
non_empty_stack->data[0] = 1;
non_empty_stack->top = non_empty_stack->data[0];
// Test the other functions
test_stack_is_empty(s);
test_stack_is_not_empty(non_empty_stack);
test_stack_peek(*s, val);
test_stack_push(s, value_to_add);
test_stack_pop(s, val);
// Free the memory
stack_destroy(s);
stack_destroy(non_empty_stack);
// End the tests
printf("The tests are completed and were successful !\n"); printf("The tests are completed and were successful !\n");
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment