Skip to content

Commit

Permalink
added new stack mode and tests for peek_stack function
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGAzed committed Sep 11, 2024
1 parent 8935561 commit ec8af8b
Show file tree
Hide file tree
Showing 8 changed files with 687 additions and 120 deletions.
10 changes: 5 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
*
/*

!external/*
!structures/*
!test/*
!.github/*
!external/
!structures/
!test/
!github/

!.gitignore
!.gitmodule
Expand Down
276 changes: 207 additions & 69 deletions structures/stack.h

Large diffs are not rendered by default.

27 changes: 14 additions & 13 deletions test/stack/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,38 @@ add_executable(stack_test_01 main.c
create_stack_test.c
destroy_stack_test.c
is_full_stack_test.c
peek_stack_test.c
)
target_compile_definitions(stack_test_01 PRIVATE TEST_STACK_MODE=01)
target_link_libraries(stack_test_01 PRIVATE greatest project_headers)
add_test(NAME STACK_TEST_01 COMMAND stack_test_01)

add_executable(stack_test_02 main.c
create_stack_test.c
destroy_stack_test.c
is_full_stack_test.c
peek_stack_test.c
)
target_compile_definitions(stack_test_02 PRIVATE TEST_STACK_MODE=02)
target_link_libraries(stack_test_02 PRIVATE greatest project_headers)
add_test(NAME STACK_TEST_02 COMMAND stack_test_02)

add_executable(stack_test_03 main.c
create_stack_test.c
destroy_stack_test.c
is_full_stack_test.c
peek_stack_test.c
)
target_compile_definitions(stack_test_03 PRIVATE TEST_STACK_MODE=03)
target_link_libraries(stack_test_03 PRIVATE greatest project_headers)
add_test(NAME STACK_TEST_03 COMMAND stack_test_03)

add_test(
NAME STACK_TEST_01
COMMAND stack_test_01
)

add_test(
NAME STACK_TEST_02
COMMAND stack_test_02
add_executable(stack_test_04 main.c
create_stack_test.c
destroy_stack_test.c
is_full_stack_test.c
peek_stack_test.c
)

add_test(
NAME STACK_TEST_03
COMMAND stack_test_03
)
target_compile_definitions(stack_test_04 PRIVATE TEST_STACK_MODE=04)
target_link_libraries(stack_test_04 PRIVATE greatest project_headers)
add_test(NAME STACK_TEST_04 COMMAND stack_test_04)
35 changes: 30 additions & 5 deletions test/stack/create_stack_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include "stack.h"

#if STACK_MODE == INFINITE_STACK
#if STACK_MODE == INFINITE_LIST_STACK

/// @brief Tests if size of stack is zero when creating it.
TEST test_01_01(void) {
Expand All @@ -37,7 +37,7 @@ SUITE (create_stack_test) {
RUN_TEST(test_01_02);
}

#elif STACK_MODE == FINITE_STACK
#elif STACK_MODE == FINITE_ALLOCATED_STACK

/// @brief Tests if size of stack is zero when creating it.
TEST test_02_01(void) {
Expand Down Expand Up @@ -72,7 +72,7 @@ SUITE (create_stack_test) {
RUN_TEST(test_02_03);
}

#elif STACK_MODE == PREDEFINED_STACK
#elif STACK_MODE == INFINITE_REALLOC_STACK

/// @brief Tests if size of stack is zero when creating it.
TEST test_03_01(void) {
Expand All @@ -83,10 +83,10 @@ TEST test_03_01(void) {
PASS();
}

/// @brief Tests if elements array ha correct size when creating it.
/// @brief Tests if head of stack is NULL when creating it.
TEST test_03_02(void) {
stack_s test = create_stack();
ASSERTm("[ERROR] Stack array size is invalid", (sizeof(test.elements) / sizeof(STACK_DATA_TYPE)) == PREDEFINED_STACK_SIZE);
ASSERTm("[ERROR] Stack head must be NULL/have no allocated memory", test.elements == NULL);
destroy_stack(&test, NULL);

PASS();
Expand All @@ -97,4 +97,29 @@ SUITE (create_stack_test) {
RUN_TEST(test_03_02);
}

#elif STACK_MODE == FINITE_PRERPOCESSOR_STACK

/// @brief Tests if size of stack is zero when creating it.
TEST test_04_01(void) {
stack_s test = create_stack();
ASSERTm("[ERROR] Stack size must be zero", test.size == 0);
destroy_stack(&test, NULL);

PASS();
}

/// @brief Tests if elements array ha correct size when creating it.
TEST test_04_02(void) {
stack_s test = create_stack();
ASSERTm("[ERROR] Stack array size is invalid", (sizeof(test.elements) / sizeof(STACK_DATA_TYPE)) == PREPROCESSOR_STACK_SIZE);
destroy_stack(&test, NULL);

PASS();
}

SUITE (create_stack_test) {
RUN_TEST(test_04_01);
RUN_TEST(test_04_02);
}

#endif
74 changes: 64 additions & 10 deletions test/stack/destroy_stack_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@

#endif

#define STACK_DATA_TYPE char*
#define STACK_LIST_ARRAY_SIZE 10
#define STACK_DATA_TYPE char*
#define LIST_ARRAY_STACK_CHUNK 10
#define REALLOC_STACK_CHUNK 10
#include "stack.h"

void destroy_string(char ** string) {
free(*string);
}

#if STACK_MODE == INFINITE_STACK
#if STACK_MODE == INFINITE_LIST_STACK

/// @brief Tests if size of stack is zeroed when pushing and destroying it.
TEST test_01_01(void) {
Expand All @@ -41,10 +42,10 @@ TEST test_01_02(void) {
PASS();
}

/// @brief Tests if head of stack is NULLed when pushing 'STACK_LIST_ARRAY_SIZE' elements and destroying it.
/// @brief Tests if head of stack is NULLed when pushing 'LIST_ARRAY_STACK_CHUNK' elements and destroying it.
TEST test_01_03(void) {
stack_s test = create_stack();
for (size_t i = 0; i < STACK_LIST_ARRAY_SIZE; ++i) {
for (size_t i = 0; i < LIST_ARRAY_STACK_CHUNK; ++i) {
push_stack(&test, strdup("thegazed"));
}
destroy_stack(&test, destroy_string);
Expand All @@ -53,10 +54,10 @@ TEST test_01_03(void) {
PASS();
}

/// @brief Tests if head of stack is NULLed when pushing 'STACK_LIST_ARRAY_SIZE + 1' elements and destroying it.
/// @brief Tests if head of stack is NULLed when pushing 'LIST_ARRAY_STACK_CHUNK + 1' elements and destroying it.
TEST test_01_04(void) {
stack_s test = create_stack();
for (size_t i = 0; i < STACK_LIST_ARRAY_SIZE + 1; ++i) {
for (size_t i = 0; i < LIST_ARRAY_STACK_CHUNK + 1; ++i) {
push_stack(&test, strdup("thegazed"));
}
destroy_stack(&test, destroy_string);
Expand All @@ -72,7 +73,7 @@ SUITE (destroy_stack_test) {
RUN_TEST(test_01_04);
}

#elif STACK_MODE == FINITE_STACK
#elif STACK_MODE == FINITE_ALLOCATED_STACK

/// @brief Tests if size of stack is zeroed when pushing and destroying it.
TEST test_02_01(void) {
Expand Down Expand Up @@ -110,9 +111,9 @@ SUITE (destroy_stack_test) {
RUN_TEST(test_02_03);
}

#elif STACK_MODE == PREDEFINED_STACK
#elif STACK_MODE == INFINITE_REALLOC_STACK

/// @brief Tests if size of stack is zero when creating it.
/// @brief Tests if size of stack is zeroed when pushing and destroying it.
TEST test_03_01(void) {
stack_s test = create_stack();
push_stack(&test, strdup("thegazed"));
Expand All @@ -122,8 +123,61 @@ TEST test_03_01(void) {
PASS();
}

/// @brief Tests if head of stack is NULLed when pushing and destroying it.
TEST test_03_02(void) {
stack_s test = create_stack();
push_stack(&test, strdup("thegazed"));
destroy_stack(&test, destroy_string);
ASSERTm("[ERROR] Stack head must be NULL/have no allocated memory", test.elements == NULL);

PASS();
}

/// @brief Tests if head of stack is NULLed when pushing 'REALLOC_STACK_CHUNK' elements and destroying it.
TEST test_03_03(void) {
stack_s test = create_stack();
for (size_t i = 0; i < REALLOC_STACK_CHUNK; ++i) {
push_stack(&test, strdup("thegazed"));
}
destroy_stack(&test, destroy_string);
ASSERTm("[ERROR] Stack head must be NULL/have no allocated memory", test.elements == NULL);

PASS();
}

/// @brief Tests if head of stack is NULLed when pushing 'REALLOC_STACK_CHUNK + 1' elements and destroying it.
TEST test_03_04(void) {
stack_s test = create_stack();
for (size_t i = 0; i < REALLOC_STACK_CHUNK + 1; ++i) {
push_stack(&test, strdup("thegazed"));
}
destroy_stack(&test, destroy_string);
ASSERTm("[ERROR] Stack head must be NULL/have no allocated memory", test.elements == NULL);

PASS();
}

SUITE (destroy_stack_test) {
RUN_TEST(test_03_01);
RUN_TEST(test_03_02);
RUN_TEST(test_03_03);
RUN_TEST(test_03_04);
}

#elif STACK_MODE == FINITE_PRERPOCESSOR_STACK

/// @brief Tests if size of stack is zero when creating it.
TEST test_04_01(void) {
stack_s test = create_stack();
push_stack(&test, strdup("thegazed"));
destroy_stack(&test, destroy_string);
ASSERTm("[ERROR] Stack size must be zero", test.size == 0);

PASS();
}

SUITE (destroy_stack_test) {
RUN_TEST(test_04_01);
}

#endif
41 changes: 23 additions & 18 deletions test/stack/is_full_stack_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
#endif

#define STACK_DATA_TYPE int
#define PREDEFINED_STACK_SIZE 10
#define PREPROCESSOR_STACK_SIZE 10
#include "stack.h"

#if STACK_MODE == INFINITE_STACK
#if STACK_MODE == INFINITE_LIST_STACK

SUITE (is_full_stack_test) {
}

#elif STACK_MODE == FINITE_STACK
#elif STACK_MODE == FINITE_ALLOCATED_STACK

/// @brief Tests if stack of size 10 is full without pushing anything.
TEST test_02_01(void) {
stack_s test = create_stack(10);
ASSERTm("[ERROR] Stack cannot be full", !is_full_stack(test));
ASSERT_FALSEm("[ERROR] Stack cannot be full", is_full_stack(test));
destroy_stack(&test, NULL);

PASS();
Expand All @@ -34,7 +34,7 @@ TEST test_02_01(void) {
TEST test_02_02(void) {
stack_s test = create_stack(10);
push_stack(&test, 10);
ASSERTm("[ERROR] Stack cannot be full", !is_full_stack(test));
ASSERT_FALSEm("[ERROR] Stack cannot be full", is_full_stack(test));
destroy_stack(&test, NULL);

PASS();
Expand All @@ -58,7 +58,7 @@ TEST test_02_04(void) {
for(size_t i = 0; i < (10 - 1); i++) {
push_stack(&test, 10);
}
ASSERTm("[ERROR] Stack cannot be full", !is_full_stack(test));
ASSERT_FALSEm("[ERROR] Stack cannot be full", is_full_stack(test));
destroy_stack(&test, NULL);

PASS();
Expand All @@ -71,29 +71,34 @@ SUITE (is_full_stack_test) {
RUN_TEST(test_02_04);
}

#elif STACK_MODE == PREDEFINED_STACK
#elif STACK_MODE == INFINITE_REALLOC_STACK

SUITE (is_full_stack_test) {
}

#elif STACK_MODE == FINITE_PRERPOCESSOR_STACK

/// @brief Tests if stack of size 'PREDEFINED_STACK_SIZE' is full without pushing anything.
TEST test_03_01(void) {
TEST test_04_01(void) {
stack_s test = create_stack();
ASSERTm("[ERROR] Stack cannot be full", !is_full_stack(test));
ASSERT_FALSEm("[ERROR] Stack cannot be full", is_full_stack(test));
destroy_stack(&test, NULL);

PASS();
}

/// @brief Tests if stack of size 'PREDEFINED_STACK_SIZE' is full if 1 element is pushed.
TEST test_03_02(void) {
TEST test_04_02(void) {
stack_s test = create_stack();
push_stack(&test, 10);
ASSERTm("[ERROR] Stack cannot be full", !is_full_stack(test));
ASSERT_FALSEm("[ERROR] Stack cannot be full", is_full_stack(test));
destroy_stack(&test, NULL);

PASS();
}

/// @brief Tests if stack of size 'PREDEFINED_STACK_SIZE' is full if 'PREDEFINED_STACK_SIZE' elements are pushed.
TEST test_03_03(void) {
TEST test_04_03(void) {
stack_s test = create_stack();
for(size_t i = 0; i < 10; i++) {
push_stack(&test, 10);
Expand All @@ -105,22 +110,22 @@ TEST test_03_03(void) {
}

/// @brief Tests if stack of size 'PREDEFINED_STACK_SIZE' is full if ('PREDEFINED_STACK_SIZE' - 1) elements are pushed.
TEST test_03_04(void) {
TEST test_04_04(void) {
stack_s test = create_stack();
for(size_t i = 0; i < (10 - 1); i++) {
push_stack(&test, 10);
}
ASSERTm("[ERROR] Stack cannot be full", !is_full_stack(test));
ASSERT_FALSEm("[ERROR] Stack cannot be full", is_full_stack(test));
destroy_stack(&test, NULL);

PASS();
}

SUITE (is_full_stack_test) {
RUN_TEST(test_03_01);
RUN_TEST(test_03_02);
RUN_TEST(test_03_03);
RUN_TEST(test_03_04);
RUN_TEST(test_04_01);
RUN_TEST(test_04_02);
RUN_TEST(test_04_03);
RUN_TEST(test_04_04);
}

#endif
2 changes: 2 additions & 0 deletions test/stack/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
extern SUITE(create_stack_test);
extern SUITE(destroy_stack_test);
extern SUITE(is_full_stack_test);
extern SUITE(peek_stack_test);

GREATEST_MAIN_DEFS();

Expand All @@ -12,6 +13,7 @@ int main(const int argc, char **argv) {
RUN_SUITE(create_stack_test);
RUN_SUITE(destroy_stack_test);
RUN_SUITE(is_full_stack_test);
RUN_SUITE(peek_stack_test);

GREATEST_MAIN_END();
return 0;
Expand Down
Loading

0 comments on commit ec8af8b

Please sign in to comment.