Initial Commit

This commit is contained in:
Marc
2025-09-13 14:40:16 +02:00
commit ded01301c2
383 changed files with 71046 additions and 0 deletions

204
OU1/int_stack_test.c Normal file
View File

@@ -0,0 +1,204 @@
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h> // for EXIT_FAILURE
/*#include <int_list.h>*/
#include <int_stack.h>
/*
* test program for the stack implementation in stack.h.
*
* Author: Marc Meunier (mame0264@student.umu.se).
*
* Version information:
* 2025-04-22: v1.0. First version.
* 2025-05-01: v2.0. fixed comments from labres, see changes.txt for details.
*/
#define VERSION "v2.0"
#define VERSION_DATE "2025-05-01"
//
// Function to compare the values stored in the list.
bool value_equal(int v1, int v2)
{
return v1 == v2;
}
// test that the stack_empty() function creates an empty stack.
// no input parameters
// returns nothing
void isempty_return_true(void)
{
fprintf(stderr, "Starting isempty_return_true()...\n");
// initialize stack
stack s = stack_empty();
// checks false positive
if (!stack_is_empty(s))
{
fprintf(stderr, "\nStack is empty but \"stack_is_empty\" reports non empty\n");
exit(1);
}
// adds values to stack
s = stack_push(s, 10);
// test if stack prints false with values inside
if (stack_is_empty(s))
{
fprintf(stderr, "\nStack is non empty but \"stack_is_empty\" reports empty.\n");
}
fprintf(stderr, "done.\n");
}
// tests if stack returns top value in stack.
// no input parameters
// returns nothing
void push_stack_on_top(void)
{
fprintf(stderr, "Starting push_stack_on_top()...\n");
// initialize stack
stack s = stack_empty();
// adds values to stack
for (int i = 1; i <= 10; i++)
{
s = stack_push(s, i);
}
int a = 0;
// check if correct stack has correct order
for (int i = 0; i <= 9; i++)
{
a = 10 - i;
// check if value is same as the top value in stack
if (!value_equal(stack_top(s), a))
{
fprintf(stderr, "FAIL: Expected <%d>, got <%d>.\n", a, stack_top(s));
exit(1);
}
// removes one value from the stack
s = stack_pop(s);
}
// Check if stack is empty after running test.
if (!stack_is_empty(s))
{
fprintf(stderr, "\nStack is non empty but should be empty.\n");
exit(1);
}
fprintf(stderr, "done.\n");
}
// tests if the top reads wrong value.
// no input parameters
// returns nothing
void top_read_wrong(void)
{
fprintf(stderr, "Starting top_read_wrong()...\n");
// initialize stack
stack s = stack_empty();
// loops over values to add to the stack.
for (int i = 0; i <= 3; i++)
{
// adds value to stack
s = stack_push(s,i);
// check if value is not same as inserted value and prinnt error message
if (!value_equal(stack_top(s), i))
{
fprintf(stderr, "FAIL: Expected <%d>, got <%d>.\n", i, stack_top(s));
exit(1);
}
}
// removes values from the stack
for (int i = 0; i <= 3; i++)
{
s = stack_pop(s);
}
// Check if stack is empty after running test.
if (!stack_is_empty(s))
{
fprintf(stderr, "\nStack is non empty but should be empty.\n");
exit(1);
}
fprintf(stderr,"done.\n");
}
// tests if the pop function removes the wrong value or not.
// no input parameters
// returns nothing
void pop_remove_wrong(void)
{
fprintf(stderr, "Starting pop_remove_wrong()...\n");
// initialize stack
stack s = stack_empty();
// how many values to add to stack
int iter = 3;
// adds values to stack
for (int i = 0; i <= iter; i++)
{
s = stack_push(s,i);
}
// check if values are correct order
for (int i = 0; i <= iter; i++)
{
if (!value_equal(stack_top(s), 3-i))
{
fprintf(stderr, "FAIL: Expected <%d>, got <%d>.\n", i, stack_top(s));
exit(1);
}
// removes one value from the stack
s = stack_pop(s);
}
// Check if stack is empty after running test.
if (!stack_is_empty(s))
{
fprintf(stderr, "\nStack is non empty but should be empty.\n");
exit(1);
}
fprintf(stderr,"done.\n");
}
// main function to run the tests-
int main(void)
{
printf("%s, %s %s: Test program for the typed int_stack datatype.\n",
__FILE__, VERSION, VERSION_DATE);
// runs tests
isempty_return_true();
push_stack_on_top();
top_read_wrong();
pop_remove_wrong();
// test sucessful
fprintf(stderr, "\nSUCCESS: Implementation passed all tests. Normal exit.\n");
return 0;
}