Skip to content

Commit

Permalink
added queue.h implementation with some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGAzed committed Sep 19, 2024
1 parent b32fb98 commit c674520
Show file tree
Hide file tree
Showing 9 changed files with 1,702 additions and 98 deletions.
629 changes: 629 additions & 0 deletions headers/queue.h

Large diffs are not rendered by default.

120 changes: 52 additions & 68 deletions headers/stack.h

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
enable_testing()

add_subdirectory(stack)
add_subdirectory(stack)
add_subdirectory(queue)
35 changes: 35 additions & 0 deletions test/queue/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
add_executable(queue_test_010 main.c
create_queue_test.c
destroy_queue_test.c
)
target_compile_definitions(queue_test_010 PRIVATE TEST_QUEUE_MODE=010)
target_link_libraries(queue_test_010 PRIVATE greatest project_headers)
add_test(NAME QUEUE_TEST_01 COMMAND queue_test_010)
set_tests_properties(QUEUE_TEST_01 PROPERTIES LABELS "QUEUE_TEST")

add_executable(queue_test_020 main.c
create_queue_test.c
destroy_queue_test.c
)
target_compile_definitions(queue_test_020 PRIVATE TEST_QUEUE_MODE=020)
target_link_libraries(queue_test_020 PRIVATE greatest project_headers)
add_test(NAME QUEUE_TEST_02 COMMAND queue_test_020)
set_tests_properties(QUEUE_TEST_02 PROPERTIES LABELS "QUEUE_TEST")

add_executable(queue_test_030 main.c
create_queue_test.c
destroy_queue_test.c
)
target_compile_definitions(queue_test_030 PRIVATE TEST_QUEUE_MODE=030)
target_link_libraries(queue_test_030 PRIVATE greatest project_headers)
add_test(NAME QUEUE_TEST_03 COMMAND queue_test_030)
set_tests_properties(QUEUE_TEST_03 PROPERTIES LABELS "QUEUE_TEST")

add_executable(queue_test_040 main.c
create_queue_test.c
destroy_queue_test.c
)
target_compile_definitions(queue_test_040 PRIVATE TEST_QUEUE_MODE=040)
target_link_libraries(queue_test_040 PRIVATE greatest project_headers)
add_test(NAME QUEUE_TEST_04 COMMAND queue_test_040)
set_tests_properties(QUEUE_TEST_04 PROPERTIES LABELS "QUEUE_TEST")
182 changes: 182 additions & 0 deletions test/queue/create_queue_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
#include <greatest.h>

#ifndef TEST_QUEUE_MODE

#error No queue mode was defined before testing ('TEST_QUEUE_MODE' is not defined).

#else

#define QUEUE_MODE TEST_QUEUE_MODE

#endif

//#define QUEUE_MODE INFINITE_LIST_QUEUE
#define LIST_ARRAY_QUEUE_CHUNK 10
#define REALLOC_QUEUE_CHUNK 10
#define PREPROCESSOR_QUEUE_SIZE 10
#include "queue.h"

#if QUEUE_MODE == INFINITE_LIST_QUEUE

/// @brief Tests if size of queue is zero when creating it.
TEST test_010_01(void) {
queue_s test = create_queue();
ASSERT_EQm("[ERROR] Queue size must be zero.", 0, test.size);
destroy_queue(&test, NULL);

PASS();
}

/// @brief Tests if current index of queue is zero when creating it.
TEST test_010_02(void) {
queue_s test = create_queue();
ASSERT_EQm("[ERROR] Queue current index must be zero.", 0, test.current);
destroy_queue(&test, NULL);

PASS();
}

/// @brief Tests if head of queue is NULL when creating it.
TEST test_010_03(void) {
queue_s test = create_queue();
ASSERT_EQm("[ERROR] Queue head must be NULL.", NULL, test.head);
destroy_queue(&test, NULL);

PASS();
}

/// @brief Tests if tail of queue is NULL when creating it.
TEST test_010_04(void) {
queue_s test = create_queue();
ASSERT_EQm("[ERROR] Queue tail must be NULL.", NULL, test.tail);
destroy_queue(&test, NULL);

PASS();
}

SUITE (create_queue_test) {
RUN_TEST(test_010_01);
RUN_TEST(test_010_02);
RUN_TEST(test_010_03);
RUN_TEST(test_010_04);
}

#elif QUEUE_MODE == FINITE_ALLOCATED_QUEUE

/// @brief Tests if size of queue is zero when creating it.
TEST test_020_01(void) {
queue_s test = create_queue(10);
ASSERT_EQm("[ERROR] Queue size must be zero.", 0, test.size);
destroy_queue(&test, NULL);

PASS();
}

/// @brief Tests if current index of queue is zero when creating it.
TEST test_020_02(void) {
queue_s test = create_queue(10);
ASSERT_EQm("[ERROR] Queue current index must be zero.", 0, test.current);
destroy_queue(&test, NULL);

PASS();
}

/// @brief Tests if maximum size of queue is 10 when creating it.
TEST test_020_03(void) {
queue_s test = create_queue(10);
ASSERT_EQm("[ERROR] Queue current index must be zero.", 10, test.max);
destroy_queue(&test, NULL);

PASS();
}

/// @brief Tests if elements pointer of queue is initialized when creating it.
TEST test_020_04(void) {
queue_s test = create_queue(10);
ASSERT_NEQm("[ERROR] Queue elements pointer must not be NULL.", NULL, test.elements);
destroy_queue(&test, NULL);

PASS();
}

SUITE (create_queue_test) {
RUN_TEST(test_020_01);
RUN_TEST(test_020_02);
RUN_TEST(test_020_03);
RUN_TEST(test_020_04);
}

#elif QUEUE_MODE == INFINITE_REALLOC_QUEUE

/// @brief Tests if size of queue is zero when creating it.
TEST test_030_01(void) {
queue_s test = create_queue();
ASSERT_EQm("[ERROR] Queue size must be zero.", 0, test.size);
destroy_queue(&test, NULL);

PASS();
}

/// @brief Tests if current index of queue is zero when creating it.
TEST test_030_02(void) {
queue_s test = create_queue();
ASSERT_EQm("[ERROR] Queue current index must be zero.", 0, test.current);
destroy_queue(&test, NULL);

PASS();
}

/// @brief Tests if elements pointer of queue is NULL when creating it.
TEST test_030_03(void) {
queue_s test = create_queue();
ASSERT_EQm("[ERROR] Queue current index must be zero.", NULL, test.elements);
destroy_queue(&test, NULL);

PASS();
}

SUITE (create_queue_test) {
RUN_TEST(test_030_01);
RUN_TEST(test_030_02);
RUN_TEST(test_030_03);
}

#elif QUEUE_MODE == FINITE_PRERPOCESSOR_QUEUE

/// @brief Tests if size of queue is zero when creating it.
TEST test_040_01(void) {
queue_s test = create_queue();
ASSERT_EQm("[ERROR] Queue size must be zero.", 0, test.size);
destroy_queue(&test, NULL);

PASS();
}

/// @brief Tests if current index of queue is zero when creating it.
TEST test_040_02(void) {
queue_s test = create_queue();
ASSERT_EQm("[ERROR] Queue size must be zero.", 0, test.current);
destroy_queue(&test, NULL);

PASS();
}

/// @brief Tests if elements array of queue is correct size when creating it.
TEST test_040_03(void) {
queue_s test = create_queue();
ASSERT_EQm(
"[ERROR] Queue size must be zero.",
PREPROCESSOR_QUEUE_SIZE, sizeof(test.elements) / sizeof(QUEUE_DATA_TYPE)
);
destroy_queue(&test, NULL);

PASS();
}

SUITE (create_queue_test) {
RUN_TEST(test_040_01);
RUN_TEST(test_040_02);
RUN_TEST(test_040_03);
}

#endif
Loading

0 comments on commit c674520

Please sign in to comment.