From 6d830283ef141b935b0706c3b6a7bbf614e2230f Mon Sep 17 00:00:00 2001 From: Matej Dedina Date: Mon, 8 Jul 2024 20:53:42 +0200 Subject: [PATCH] minor cosmetic changes --- .github/workflows/cmake-single-platform.yml | 2 +- program/CMakeLists.txt | 13 +- program/include/algorithms/arc_consistency.h | 2 +- program/include/algorithms/backtrack.h | 2 +- .../include/algorithms/depth_first_search.h | 2 +- program/include/algorithms/forward_checking.h | 2 +- program/include/algorithms/reduce.h | 2 +- program/include/instance/argument.h | 4 - program/include/instance/expect.h | 16 +-- program/include/instance/settings.h | 2 - program/include/instance/statistics.h | 1 - program/include/structures/abstract/queue.h | 69 +++++----- program/include/structures/abstract/stack.h | 55 ++++---- program/include/structures/concrete/state.h | 64 +++++---- program/main.c | 2 +- program/source/algorithms/arc_consistency.c | 124 +++++++++--------- program/source/algorithms/backtrack.c | 38 +++--- .../source/algorithms/depth_first_search.c | 16 +-- program/source/algorithms/forward_checking.c | 10 +- program/source/algorithms/reduce.c | 32 ++--- program/source/instance/argument.c | 4 +- program/source/instance/expect.c | 18 +++ program/source/structures/concrete/state.c | 48 +++---- test/blackbox/CMakeLists.txt | 31 ++--- test/blackbox/main.c | 30 ++--- .../{easy_arc_consistency.c => easy_ac.c} | 12 +- ...c_consistency_backtrack.c => easy_ac_bt.c} | 12 +- ...track_forward_check.c => easy_ac_bt_fch.c} | 12 +- ...sistency_forward_check.c => easy_ac_fch.c} | 12 +- .../easy/{easy_backtrack.c => easy_bt.c} | 12 +- ...acktrack_forward_check.c => easy_bt_fch.c} | 12 +- .../easy/{easy_forward_check.c => easy_fch.c} | 12 +- test/blackbox/suites/easy/easy_unset.c | 10 +- ..._arc_consistency_backtrack_forward_check.c | 10 +- .../medium/medium_backtrack_forward_check.c | 10 +- test/unit/CMakeLists.txt | 19 +-- test/unit/structures/abstract/stack_test.c | 38 +++--- 37 files changed, 373 insertions(+), 387 deletions(-) create mode 100644 program/source/instance/expect.c rename test/blackbox/suites/easy/{easy_arc_consistency.c => easy_ac.c} (94%) rename test/blackbox/suites/easy/{easy_arc_consistency_backtrack.c => easy_ac_bt.c} (94%) rename test/blackbox/suites/easy/{easy_arc_consistency_backtrack_forward_check.c => easy_ac_bt_fch.c} (94%) rename test/blackbox/suites/easy/{easy_arc_consistency_forward_check.c => easy_ac_fch.c} (94%) rename test/blackbox/suites/easy/{easy_backtrack.c => easy_bt.c} (94%) rename test/blackbox/suites/easy/{easy_backtrack_forward_check.c => easy_bt_fch.c} (94%) rename test/blackbox/suites/easy/{easy_forward_check.c => easy_fch.c} (94%) diff --git a/.github/workflows/cmake-single-platform.yml b/.github/workflows/cmake-single-platform.yml index 903f52d..2a9d1be 100644 --- a/.github/workflows/cmake-single-platform.yml +++ b/.github/workflows/cmake-single-platform.yml @@ -20,7 +20,7 @@ jobs: runs-on: ubuntu-20.04 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 with: submodules: true diff --git a/program/CMakeLists.txt b/program/CMakeLists.txt index 2f5a23a..1bd3c41 100644 --- a/program/CMakeLists.txt +++ b/program/CMakeLists.txt @@ -4,9 +4,9 @@ find_package(GLEW REQUIRED) add_executable(${PROJECT_NAME} main.c) -add_library(PROGRAM_LIBRARY "") +add_library(program "") -target_sources(PROGRAM_LIBRARY +target_sources(program PRIVATE source/gui/graphics.c source/gui/input.c @@ -22,6 +22,7 @@ target_sources(PROGRAM_LIBRARY source/instance/argument.c source/instance/settings.c source/instance/statistics.c + source/instance/expect.c source/structures/concrete/board.c source/structures/concrete/state.c @@ -29,7 +30,9 @@ target_sources(PROGRAM_LIBRARY source/slnoslav.c ) -target_include_directories(PROGRAM_LIBRARY PUBLIC include) -target_link_libraries(PROGRAM_LIBRARY PRIVATE glfw GLEW::GLEW OpenGL::GL m nuklear) +target_include_directories(program PUBLIC include) -target_link_libraries(${PROJECT_NAME} PRIVATE PROGRAM_LIBRARY) +target_link_libraries(program PRIVATE glfw GLEW::GLEW OpenGL::GL m nuklear) +target_link_libraries(${PROJECT_NAME} PRIVATE program) + +target_compile_definitions(${PROJECT_NAME} PRIVATE ERROR_LOG_FILE_PATH=\"${CMAKE_BINARY_DIR}/slnoslav-debug.txt\") diff --git a/program/include/algorithms/arc_consistency.h b/program/include/algorithms/arc_consistency.h index b266989..01535ed 100644 --- a/program/include/algorithms/arc_consistency.h +++ b/program/include/algorithms/arc_consistency.h @@ -6,6 +6,6 @@ #include #include -bool look_ahead(board_s board, SArray * current_state); +bool look_ahead(const board_s board, state_array_s * current_state); #endif /* ALGORITHMS_ARC_CONSISTENCY_H */ diff --git a/program/include/algorithms/backtrack.h b/program/include/algorithms/backtrack.h index 312a89c..d6eba76 100644 --- a/program/include/algorithms/backtrack.h +++ b/program/include/algorithms/backtrack.h @@ -6,6 +6,6 @@ #include #include -bool backtrack(board_s board, SArray current_state); +bool backtrack(board_s board, state_array_s current_state); #endif /* ALGORITHMS_BACKTRACK_H */ diff --git a/program/include/algorithms/depth_first_search.h b/program/include/algorithms/depth_first_search.h index 76517f1..996e873 100644 --- a/program/include/algorithms/depth_first_search.h +++ b/program/include/algorithms/depth_first_search.h @@ -4,6 +4,6 @@ #include #include -SArray depth_first_search(board_s board); +state_array_s depth_first_search(board_s board); #endif /* ALGORITHMS_DEPTH_FIRST_SEARCH_H */ diff --git a/program/include/algorithms/forward_checking.h b/program/include/algorithms/forward_checking.h index 054f9b0..dcb57f4 100644 --- a/program/include/algorithms/forward_checking.h +++ b/program/include/algorithms/forward_checking.h @@ -6,6 +6,6 @@ #include #include -bool forward_checking(board_s board, SArray * current_state, ulookup_t index); +bool forward_checking(board_s board, state_array_s * current_state, ulookup_t index); #endif /* ALGORITHMS_FORWARD_CHECKING_H */ diff --git a/program/include/algorithms/reduce.h b/program/include/algorithms/reduce.h index 9790010..2e873c2 100644 --- a/program/include/algorithms/reduce.h +++ b/program/include/algorithms/reduce.h @@ -4,6 +4,6 @@ #include #include -void reduce(board_s board, SArray * initial_state); +void reduce(board_s board, state_array_s * initial_state); #endif /* ALGORITHMS_REDUCE_H */ diff --git a/program/include/instance/argument.h b/program/include/instance/argument.h index 63d18b8..9b33773 100644 --- a/program/include/instance/argument.h +++ b/program/include/instance/argument.h @@ -1,10 +1,6 @@ #ifndef INSTANCE_ARGUMENT_H #define INSTANCE_ARGUMENT_H -#include -#include -#include - #define FILEPATH_FLAG_STRING_SHORT "-fp" #define FILEPATH_FLAG_STRING_LONG "--filepath" diff --git a/program/include/instance/expect.h b/program/include/instance/expect.h index c0d2174..64bf4ba 100644 --- a/program/include/instance/expect.h +++ b/program/include/instance/expect.h @@ -2,29 +2,25 @@ #define INSTANCE_EXPECT_H #include -#include #include +#include typedef enum error_mode_type { DEFAULT_E, ABORT_E, ASSERT_E, EXIT_E, } error_mode_e; -static error_mode_e error_mode = DEFAULT_E; -static FILE * error_log = NULL; +extern error_mode_e error_mode; +extern FILE * error_log; #define NO_ACTION (void)(0) #ifdef ERROR_LOG_FILE_PATH -static FILE * error_log = fopen(ERROR_LOG_FILE_PATH, "a"); - #define expect(assertion, error_action, ...) \ { \ if (!(assertion)) { \ - assert(error_log); \ fprintf(error_log, __VA_ARGS__); \ fprintf(error_log, "\n"); \ - fclose(error_log) \ switch (error_mode) { \ case ABORT_E : { error_action; abort(); break; } \ case ASSERT_E : { error_action; assert(0 && (assertion)); break; } \ @@ -33,14 +29,8 @@ static FILE * error_log = fopen(ERROR_LOG_FILE_PATH, "a"); } \ } -__attribute__((destructor)) static void close_error_log(void) { - fclose(error_log); -} - #else -static FILE * error_log; - #define expect(assertion, error_action, ...) \ { \ if (!(assertion)) { \ diff --git a/program/include/instance/settings.h b/program/include/instance/settings.h index cf1cc79..8ce0b47 100644 --- a/program/include/instance/settings.h +++ b/program/include/instance/settings.h @@ -1,9 +1,7 @@ #ifndef INSTANCE_SETTINGS_H #define INSTANCE_SETTINGS_H -#include #include -#include #include typedef enum playstate { diff --git a/program/include/instance/statistics.h b/program/include/instance/statistics.h index 14d7297..75d36a1 100644 --- a/program/include/instance/statistics.h +++ b/program/include/instance/statistics.h @@ -1,7 +1,6 @@ #ifndef INSTANCE_DATA_H #define INSTANCE_DATA_H -#include #include typedef struct statistics { diff --git a/program/include/structures/abstract/queue.h b/program/include/structures/abstract/queue.h index 470f898..548bac9 100644 --- a/program/include/structures/abstract/queue.h +++ b/program/include/structures/abstract/queue.h @@ -6,9 +6,6 @@ #include #include -//#define FINITE_QUEUE -//#undef FINITE_QUEUE - #ifndef QUEUE_DATA_TYPE #define QUEUE_DATA_TYPE void * @@ -22,7 +19,7 @@ typedef struct queue_list_array { QUEUE_DATA_TYPE elements[QUEUE_LIST_ARRAY_SIZE]; struct queue_list_array * next; -} QLArray; +} qlarray_s; #endif /* FINITE_QUEUE */ @@ -33,61 +30,61 @@ typedef struct queue { size_t max; QUEUE_DATA_TYPE * elements; #else - QLArray * head; - QLArray * tail; + qlarray_s * head; + qlarray_s * tail; #endif /* FINITE_QUEUE */ -} Queue; +} queue_s; typedef enum queue_index_position { QI_POSITION_CURRENT = 1, QI_POSITION_NEXT = 0, -} QIPosition; +} qip_e; #ifdef FINITE_QUEUE -static Queue create_queue(size_t max); -static bool is_full_queue(Queue queue); +static queue_s create_queue(size_t max); +static bool is_full_queue(queue_s queue); #else -static Queue create_queue(void); +static queue_s create_queue(void); #endif /* FINITE_QUEUE */ -static bool is_empty_queue(Queue queue); -static void enqueue(Queue * queue, QUEUE_DATA_TYPE element); -static QUEUE_DATA_TYPE dequeue(Queue * queue); -static QUEUE_DATA_TYPE peek_queue(Queue queue); -static Queue copy_queue(Queue queue, QUEUE_DATA_TYPE (*copy_element)(QUEUE_DATA_TYPE)); -static void destroy_queue(Queue * queue, void (*free_element)(QUEUE_DATA_TYPE *)); -static size_t _get_index_queue(Queue queue, QIPosition type); +static bool is_empty_queue(queue_s queue); +static void enqueue(queue_s * queue, QUEUE_DATA_TYPE element); +static QUEUE_DATA_TYPE dequeue(queue_s * queue); +static QUEUE_DATA_TYPE peek_queue(queue_s queue); +static queue_s copy_queue(queue_s queue, QUEUE_DATA_TYPE (*copy_element)(QUEUE_DATA_TYPE)); +static void destroy_queue(queue_s * queue, void (*free_element)(QUEUE_DATA_TYPE *)); +static size_t _get_index_queue(queue_s queue, qip_e type); #ifdef FINITE_QUEUE -static inline Queue create_queue(size_t max) { - Queue queue = { .elements = malloc(sizeof(QUEUE_DATA_TYPE) * max), .max = max, .size = 0, .current = 0 }; +static inline queue_s create_queue(const size_t max) { + queue_s queue = { .elements = malloc(sizeof(QUEUE_DATA_TYPE) * max), .max = max, .size = 0, .current = 0 }; assert(queue.elements && "MEMORY ALLOCATION FAILED"); return queue; } -static inline bool is_full_queue(Queue queue) { +static inline bool is_full_queue(const queue_s queue) { return queue.size == queue.max; } #else -static inline Queue create_queue(void) { - return (Queue) { 0 }; +static inline queue_s create_queue(void) { + return (queue_s) { 0 }; } #endif /* FINITE_QUEUE */ -static inline bool is_empty_queue(Queue queue) { +static inline bool is_empty_queue(const queue_s queue) { return !queue.size; } -static inline void enqueue(Queue * queue, QUEUE_DATA_TYPE element) { +static inline void enqueue(queue_s * queue, QUEUE_DATA_TYPE element) { assert(queue && "QUEUE POINTER IS NULL"); #ifdef FINITE_QUEUE @@ -100,7 +97,7 @@ static inline void enqueue(Queue * queue, QUEUE_DATA_TYPE element) { queue->elements[idx] = element; #else if (!idx) { - QLArray * temp = malloc(sizeof(QLArray)); + qlarray_s * temp = malloc(sizeof(qlarray_s)); assert(temp && "MEMORY ALLOCATION FAILED"); temp->next = NULL; @@ -113,7 +110,7 @@ static inline void enqueue(Queue * queue, QUEUE_DATA_TYPE element) { queue->size++; } -static inline QUEUE_DATA_TYPE dequeue(Queue * queue) { +static inline QUEUE_DATA_TYPE dequeue(queue_s * queue) { assert(queue && "QUEUE POINTER IS NULL"); assert(!is_empty_queue(*queue) && "CAN'T POP EMPTY QUEUE"); @@ -125,7 +122,7 @@ static inline QUEUE_DATA_TYPE dequeue(Queue * queue) { size_t idx = _get_index_queue(*queue, QI_POSITION_NEXT); if (idx == (QUEUE_LIST_ARRAY_SIZE - 1)) { - QLArray * temp = queue->head; + qlarray_s * temp = queue->head; queue->head = queue->head->next; free(temp); @@ -145,7 +142,7 @@ static inline QUEUE_DATA_TYPE dequeue(Queue * queue) { return e; } -static inline QUEUE_DATA_TYPE peek_queue(Queue queue) { +static inline QUEUE_DATA_TYPE peek_queue(queue_s queue) { assert(!is_empty_queue(queue) && "CAN'T PEEK EMPTY QUEUE"); size_t idx = _get_index_queue(queue, QI_POSITION_CURRENT); @@ -157,9 +154,9 @@ static inline QUEUE_DATA_TYPE peek_queue(Queue queue) { } -static inline Queue copy_queue(Queue queue, QUEUE_DATA_TYPE (*copy_element)(QUEUE_DATA_TYPE)) { +static inline queue_s copy_queue(queue_s queue, QUEUE_DATA_TYPE (*copy_element)(QUEUE_DATA_TYPE)) { #ifdef FINITE_QUEUE - Queue copy = queue; + queue_s copy = queue; assert((copy.elements = malloc(sizeof(QUEUE_DATA_TYPE) * copy.max)) && "MEMORY ALLOCATION FAILED"); size_t current_index = _get_index_queue(queue, QI_POSITION_CURRENT); @@ -168,12 +165,12 @@ static inline Queue copy_queue(Queue queue, QUEUE_DATA_TYPE (*copy_element)(QUEU copy.elements[index] = copy_element ? copy_element(queue.elements[index]) : queue.elements[index]; } #else - Queue copy = create_queue(); + queue_s copy = create_queue(); copy.current = queue.current; - QLArray * current_array = queue.head; + qlarray_s * current_array = queue.head; for (size_t i = _get_index_queue(queue, QI_POSITION_CURRENT); i < queue.size && current_array;) { - QLArray * temp = malloc(sizeof(QLArray)); + qlarray_s * temp = malloc(sizeof(qlarray_s)); assert(temp && "MEMORY ALLOCATION FAILED"); temp->next = NULL; @@ -192,7 +189,7 @@ static inline Queue copy_queue(Queue queue, QUEUE_DATA_TYPE (*copy_element)(QUEU return copy; } -static inline void destroy_queue(Queue * queue, void (*free_element)(QUEUE_DATA_TYPE *)) { +static inline void destroy_queue(queue_s * queue, void (*free_element)(QUEUE_DATA_TYPE *)) { while (!is_empty_queue(*queue)) { QUEUE_DATA_TYPE e = dequeue(queue); if (free_element) free_element(&e); @@ -206,7 +203,7 @@ static inline void destroy_queue(Queue * queue, void (*free_element)(QUEUE_DATA_ queue->current = 0; } -static inline size_t _get_index_queue(Queue queue, QIPosition type) { +static inline size_t _get_index_queue(const queue_s queue, const qip_e type) { assert((!is_empty_queue(queue) || type != QI_POSITION_CURRENT) && "CAN'T GET CURRENT INDEX IF EMPTY QUEUE"); if (type == QI_POSITION_CURRENT) return queue.current; diff --git a/program/include/structures/abstract/stack.h b/program/include/structures/abstract/stack.h index 91f4116..9688a7f 100644 --- a/program/include/structures/abstract/stack.h +++ b/program/include/structures/abstract/stack.h @@ -5,9 +5,6 @@ #include #include -//#define FINITE_QUEUE -//#undef FINITE_QUEUE - #ifndef STACK_DATA_TYPE #define STACK_DATA_TYPE void * @@ -35,57 +32,57 @@ typedef struct stack { SLArray * head; #endif /* FINITE_STACK */ -} Stack; +} stack_s; typedef enum stack_index_position { SI_POSITION_CURRENT = 1, SI_POSITION_NEXT = 0, -} SIPosition; +} sip_e; #ifdef FINITE_STACK -static Stack create_stack(size_t max); -static bool is_full_stack(Stack stack); +static stack_s create_stack(size_t max); +static bool is_full_stack(stack_s stack); #else -static Stack create_stack(void); +static stack_s create_stack(void); #endif /* FINITE_STACK */ -static bool is_empty_stack(Stack stack); -static void push_stack(Stack * stack, STACK_DATA_TYPE element); -static STACK_DATA_TYPE pop_stack(Stack * stack); -static STACK_DATA_TYPE peek_stack(Stack stack); -static Stack copy_stack(Stack stack, STACK_DATA_TYPE (*copy_element)(STACK_DATA_TYPE)); -static void destroy_stack(Stack * stack, void (*free_element)(STACK_DATA_TYPE *)); -static size_t _get_index_stack(Stack stack, SIPosition type); +static bool is_empty_stack(stack_s stack); +static void push_stack(stack_s * stack, STACK_DATA_TYPE element); +static STACK_DATA_TYPE pop_stack(stack_s * stack); +static STACK_DATA_TYPE peek_stack(stack_s stack); +static stack_s copy_stack(stack_s stack, STACK_DATA_TYPE (*copy_element)(STACK_DATA_TYPE)); +static void destroy_stack(stack_s * stack, void (*free_element)(STACK_DATA_TYPE *)); +static size_t _get_index_stack(stack_s stack, sip_e type); #ifdef FINITE_STACK -static inline Stack create_stack(size_t max) { - Stack stack = { .elements = malloc(sizeof(STACK_DATA_TYPE) * max), .max = max, 0, }; +static inline stack_s create_stack(const size_t max) { + stack_s stack = { .elements = malloc(sizeof(STACK_DATA_TYPE) * max), .max = max, 0, }; assert(stack.elements && "MEMORY ALLOCATION FAILED"); return stack; } -static inline bool is_full_stack(Stack stack) { +static inline bool is_full_stack(const stack_s stack) { return stack.size == stack.max; } #else -static inline Stack create_stack(void) { - return (Stack) { 0 }; +static inline stack_s create_stack(void) { + return (stack_s) { 0 }; } #endif /* FINITE_STACK */ -static inline bool is_empty_stack(Stack stack) { +static inline bool is_empty_stack(const stack_s stack) { return !stack.size; } -static inline void push_stack(Stack * stack, STACK_DATA_TYPE element) { +static inline void push_stack(stack_s * stack, STACK_DATA_TYPE element) { assert(stack && "QUEUE POINTER IS NULL"); #ifdef FINITE_STACK @@ -112,7 +109,7 @@ static inline void push_stack(Stack * stack, STACK_DATA_TYPE element) { stack->size++; } -static inline STACK_DATA_TYPE pop_stack(Stack * stack) { +static inline STACK_DATA_TYPE pop_stack(stack_s * stack) { assert(stack && "QUEUE POINTER IS NULL"); assert(!is_empty_stack(*stack) && "CAN'T POP EMPTY QUEUE"); @@ -132,7 +129,7 @@ static inline STACK_DATA_TYPE pop_stack(Stack * stack) { return e; } -static inline STACK_DATA_TYPE peek_stack(Stack stack) { +static inline STACK_DATA_TYPE peek_stack(stack_s stack) { assert(!is_empty_stack(stack) && "CAN'T PEEK EMPTY STACK"); size_t idx = _get_index_stack(stack, SI_POSITION_CURRENT); @@ -144,17 +141,17 @@ static inline STACK_DATA_TYPE peek_stack(Stack stack) { } -static inline Stack copy_stack(Stack stack, STACK_DATA_TYPE (*copy_element)(STACK_DATA_TYPE)) { +static inline stack_s copy_stack(stack_s stack, STACK_DATA_TYPE (*copy_element)(STACK_DATA_TYPE)) { #ifdef FINITE_STACK - Stack copy = stack; + stack_s copy = stack; assert(copy.elements = malloc(sizeof(STACK_DATA_TYPE) * stack.max) && "MEMORY ALLOCATION FAILED"); for (size_t i = 0; i < stack.size; i++) { copy.elements[i] = copy_element ? copy_element(stack.elements[i]) : stack.elements[i]; } #else - Stack copy = create_stack(); + stack_s copy = create_stack(); SLArray * current_array = stack.head; for (size_t i = stack.size - 1; current_array;) { @@ -178,7 +175,7 @@ static inline Stack copy_stack(Stack stack, STACK_DATA_TYPE (*copy_element)(STAC return copy; } -static inline void destroy_stack(Stack * stack, void (*free_element)(STACK_DATA_TYPE *)) { +static inline void destroy_stack(stack_s * stack, void (*free_element)(STACK_DATA_TYPE *)) { while (!is_empty_stack(*stack)) { STACK_DATA_TYPE e = pop_stack(stack); if (free_element) free_element(&e); @@ -191,7 +188,7 @@ static inline void destroy_stack(Stack * stack, void (*free_element)(STACK_DATA_ } -static inline size_t _get_index_stack(Stack stack, SIPosition type) { +static inline size_t _get_index_stack(stack_s stack, sip_e type) { assert((!is_empty_stack(stack) || type != SI_POSITION_CURRENT) && "CAN'T GET CURRENT INDEX IF EMPTY STACK"); #ifdef FINITE_STACK diff --git a/program/include/structures/concrete/state.h b/program/include/structures/concrete/state.h index 8172792..3af9fef 100644 --- a/program/include/structures/concrete/state.h +++ b/program/include/structures/concrete/state.h @@ -3,11 +3,9 @@ #define STRUCTURES_CONCRETE_STATE_H #include -#include #include typedef uint8_t ulookup_t; -#define KAKURO_SIZE_MAX (sizeof(ksize_t) << 8) #define MAX_BLOCK_VALUES 9 @@ -19,44 +17,44 @@ typedef uint16_t state_t; typedef struct state_array { state_t * elements; ulookup_t size; -} SArray; +} state_array_s; -typedef struct state_array_array { - SArray elements[MAX_BLOCK_VALUES]; +typedef struct state_matrix { + state_array_s elements[MAX_BLOCK_VALUES]; ulookup_t size; -} SMatrix; +} state_matrix_s; typedef enum edge_type { - UPPER_EDGE = 9, - LOWER_EDGE = 1 -} EType; - -SArray create_state_array (ulookup_t size); -void set_full_state_array(SArray * array); -void destroy_state_array (SArray * array); - -SArray copy_state_array (SArray array); -bool compare_states (SArray array_a, SArray array_b); -bool valid_states (SArray array); -bool is_end_state (SArray array); -SArray split_state (state_t state); -state_t merge_state_array(SArray array); - -bool is_one_value (state_t state); -ulookup_t get_sums (ulookup_t start, EType type); -state_t get_edge_state(ulookup_t count, EType type); -ulookup_t get_one_value (state_t state); + UPPER_EDGE_E = 9, + LOWER_EDGE_E = 1, +} edge_type_e; + +state_array_s create_state_array (ulookup_t size); +void set_full_state_array(state_array_s * array); +void destroy_state_array (state_array_s * array); + +state_array_s copy_state_array (state_array_s array); +bool compare_states (state_array_s array_a, state_array_s array_b); +bool valid_states (state_array_s array); +bool is_end_state (state_array_s array); +state_array_s split_state (state_t state); +state_t merge_state_array(state_array_s array); + +bool is_one_value (state_t state); +ulookup_t get_sums (ulookup_t start, edge_type_e type); +state_t get_edge_state(ulookup_t count, edge_type_e type); +ulookup_t get_one_value (state_t state); ulookup_t state_to_sums (state_t state); -state_t get_one_state (ulookup_t value); -ulookup_t shortest_multi_index(SArray array); -ulookup_t state_count(state_t state); +state_t get_one_state (ulookup_t value); +ulookup_t shortest_multi_index(state_array_s array); +ulookup_t state_count (state_t state); -SMatrix generate_neighbor(SArray array, ulookup_t index); -void destroy_state_matrix(SMatrix * matrix); +state_matrix_s create_neighbors_state_matrix(state_array_s array, ulookup_t index); +void destroy_state_matrix (state_matrix_s * matrix); void print_state(state_t s); -void print_state_array(SArray s); -void print_solution(SArray solved); +void print_state_array(state_array_s s); +void print_solution(state_array_s solved); -#endif /* STRUCTURES_CONCRETE_STATE_H */ +#endif diff --git a/program/main.c b/program/main.c index ed1c810..9124a28 100644 --- a/program/main.c +++ b/program/main.c @@ -1,6 +1,6 @@ #include -int main(int argc, char ** argv) { +int main(const int argc, char ** argv) { setup_program(argc, argv); run_program(); return 0; diff --git a/program/source/algorithms/arc_consistency.c b/program/source/algorithms/arc_consistency.c index f549775..efc704b 100644 --- a/program/source/algorithms/arc_consistency.c +++ b/program/source/algorithms/arc_consistency.c @@ -8,34 +8,36 @@ #include #include -#define STACK_DATA_TYPE ksize_t +#define STACK_DATA_TYPE ulookup_t #include -typedef struct reduce_combinations { - SMatrix e_matrix; +typedef struct valid { + state_matrix_s e_matrix; ulookup_t e_valid[MAX_BLOCK_VALUES][MAX_BLOCK_VALUES]; -} Valid; +} valid_s; -void _reduce_one_values(board_s board, SArray * current_state, check_e * checks); -void _reduce_row_one_values(board_s board, Stack * ones, SArray * current_state, ulookup_t index, check_e * checks); -void _reduce_col_one_values(board_s board, Stack * ones, SArray * current_state, ulookup_t index, check_e * checks); +void _reduce_one_values(board_s board, state_array_s * current_state, check_e * checks); +void _reduce_row_one_values(board_s board, stack_s * ones, state_array_s * current_state, ulookup_t index, check_e * checks); +void _reduce_col_one_values(board_s board, stack_s * ones, state_array_s * current_state, ulookup_t index, check_e * checks); -Valid _create_valid(board_s board, SArray * current_state, ulookup_t index, KGSizes type); -void _destroy_valid(Valid * combinations); -bool _reduce_no_combination(board_s board, SArray * current_state, check_e * checks); -bool _reduce_no_col_combination(board_s board, SArray * current_state, ulookup_t index); -bool _reduce_no_row_combination(board_s board, SArray * current_state, ulookup_t index); +valid_s _create_valid(board_s board, state_array_s * current_state, ulookup_t index, KGSizes type); +void _destroy_valid(valid_s * combinations); +bool _reduce_no_combination(board_s board, state_array_s * current_state, check_e * checks); +bool _reduce_no_col_combination(board_s board, state_array_s * current_state, ulookup_t index); +bool _reduce_no_row_combination(board_s board, state_array_s * current_state, ulookup_t index); -bool look_ahead(board_s board, SArray * current_state) { +bool look_ahead(const board_s board, state_array_s * current_state) { error_mode = ASSERT_E; - expect(current_state, NO_ACTION, "current state parameter is NULL (%p)", (void*)current_state); + expect(current_state, NO_ACTION, "ERROR: current state parameter is NULL (%p)", (void*)current_state); error_mode = DEFAULT_E; expect(get_settings_singleton()->is_arc_consistency, return true, "WARNING: arc consistency is off"); get_stat_singleton()->look_ahead_call_count++; - check_e checks = calloc(board.game.empty_count, sizeof(check_e)); + check_e * checks = calloc(board.game.empty_count, sizeof(check_e)); + error_mode = ASSERT_E; + expect(checks, NO_ACTION, "ERROR: 'checks' variable allocation failed (%p)", (void*)checks); do { _reduce_one_values(board, current_state, checks); } while (valid_states(*current_state) && _reduce_no_combination(board, current_state, checks)); @@ -44,8 +46,8 @@ bool look_ahead(board_s board, SArray * current_state) { return !invalid_state_look_ahead_stat(!valid_states(*current_state)); } -void _reduce_one_values(board_s board, SArray * current_state, check_e * checks) { - Stack ones = create_stack(); +void _reduce_one_values(board_s board, state_array_s * current_state, check_e * checks) { + stack_s ones = create_stack(); for (ulookup_t i = 0; i < board.game.empty_count; i++) { if (is_one_value(current_state->elements[i])) push_stack(&ones, i); @@ -61,34 +63,34 @@ void _reduce_one_values(board_s board, SArray * current_state, check_e * checks) destroy_stack(&ones, NULL); } -void _reduce_row_one_values(board_s board, Stack * ones, SArray * current_state, ulookup_t index, check_e * checks) { +void _reduce_row_one_values(board_s board, stack_s * ones, state_array_s * current_state, ulookup_t index, check_e * checks) { ulookup_t row = board.coords[ROW_E][index], col = board.coords[COLUMN_E][index]; bool is_row_changed = false; for (size_t c = col - 1; !is_wall_hit(board, row, c); c--) { - state_t * s = &(current_state->elements[board.grid[row][c]]); + state_t * state = &(current_state->elements[board.grid[row][c]]); - if (!(*s & current_state->elements[index])) continue; + if (!(*state & current_state->elements[index])) continue; else is_row_changed = true; - *s &= ~(current_state->elements[index]); - if (is_one_value(*s)) push_stack(ones, (ulookup_t)board.grid[row][c]); + *state &= ~(current_state->elements[index]); + if (is_one_value(*state)) push_stack(ones, (ulookup_t)board.grid[row][c]); } for (size_t c = col + 1; !is_wall_hit(board, row, c); c++) { - state_t * s = &(current_state->elements[board.grid[row][c]]); + state_t * state = &(current_state->elements[board.grid[row][c]]); - if (!(*s & current_state->elements[index])) continue; + if (!(*state & current_state->elements[index])) continue; else is_row_changed = true; - *s &= ~(current_state->elements[index]); - if (is_one_value(*s)) push_stack(ones, (ulookup_t)board.grid[row][c]); + *state &= ~(current_state->elements[index]); + if (is_one_value(*state)) push_stack(ones, (ulookup_t)board.grid[row][c]); } if (is_row_changed) sub_row_check(board, checks, index); } -void _reduce_col_one_values(board_s board, Stack * ones, SArray * current_state, ulookup_t index, check_e * checks) { +void _reduce_col_one_values(board_s board, stack_s * ones, state_array_s * current_state, ulookup_t index, check_e * checks) { ulookup_t row = board.coords[ROW_E][index], col = board.coords[COLUMN_E][index]; bool is_col_changed = false; @@ -115,55 +117,55 @@ void _reduce_col_one_values(board_s board, Stack * ones, SArray * current_state, if (is_col_changed) sub_col_check(board, checks, index); } -Valid _create_valid(board_s board, SArray * current_state, ulookup_t index, KGSizes type) { - Valid comb = { +valid_s _create_valid(board_s board, state_array_s * current_state, ulookup_t index, KGSizes type) { + valid_s combinations = { .e_matrix = { .size = board.blocks[type][index], } }; ulookup_t row = board.coords[ROW_E][index], col = board.coords[COLUMN_E][index]; uint32_t comb_count = 1; for (ulookup_t i = 0; i < board.blocks[type][index]; i++) { - state_t s = type == ROW_E ? - current_state->elements[board.grid[row][col + i]] : + state_t s = type == ROW_E ? + current_state->elements[board.grid[row][col + i]] : current_state->elements[board.grid[row + i][col]]; - + ulookup_t s_count = state_count(s); comb_count *= s_count; - comb.e_matrix.elements[i] = split_state(s); + combinations.e_matrix.elements[i] = split_state(s); } - ulookup_t c_lookup[MAX_BLOCK_VALUES] = { 0 }; - for (uint32_t i = 0; i < comb_count; i++) { - state_t s = { 0 }; + ulookup_t comb_index[MAX_BLOCK_VALUES] = { 0 }; + for (uint32_t c = 0; c < comb_count; c++) { + state_t state = { 0 }; - for (ulookup_t j = 0; j < comb.e_matrix.size; j++) { - ulookup_t a = c_lookup[j]; - s |= comb.e_matrix.elements[j].elements[a]; + for (ulookup_t s = 0; s < combinations.e_matrix.size; s++) { + ulookup_t idx = comb_index[s]; + state |= combinations.e_matrix.elements[s].elements[idx]; } - for (ulookup_t j = 0; j < comb.e_matrix.size; j++) { - ulookup_t a = c_lookup[j]; - comb.e_valid[j][a] += - state_count(s) == board.blocks[type][index] && - state_to_sums(s) == board.sums[type][index]; + for (ulookup_t s = 0; s < combinations.e_matrix.size; s++) { + ulookup_t idx = comb_index[s]; + combinations.e_valid[s][idx] += + state_count(state) == board.blocks[type][index] && + state_to_sums(state) == board.sums[type][index]; } - - c_lookup[0]++; - for (ulookup_t j = 0; c_lookup[j] / comb.e_matrix.elements[j].size && j < comb.e_matrix.size - 1; j++) { - c_lookup[j] = 0; - c_lookup[j + 1]++; + + comb_index[0]++; + for (ulookup_t s = 0; comb_index[s] / combinations.e_matrix.elements[s].size && s < combinations.e_matrix.size - 1; s++) { + comb_index[s] = 0; + comb_index[s + 1]++; } } - return comb; + return combinations; } -void _destroy_valid(Valid * combinations) { +void _destroy_valid(valid_s * combinations) { destroy_state_matrix(&(combinations->e_matrix)); } -bool _reduce_no_combination(board_s board, SArray * current_state, check_e * checks) { +bool _reduce_no_combination(board_s board, state_array_s * current_state, check_e * checks) { bool is_reduced = false; for (size_t i = 0; i < board.game.empty_count; i++) { if (!(checks[i] & ROWCHECK)) is_reduced |= _reduce_no_row_combination(board, current_state, i); @@ -174,36 +176,36 @@ bool _reduce_no_combination(board_s board, SArray * current_state, check_e * che return is_reduced; } -bool _reduce_no_row_combination(board_s board, SArray * current_state, ulookup_t index) { +bool _reduce_no_row_combination(board_s board, state_array_s * current_state, ulookup_t index) { ulookup_t row = board.coords[ROW_E][index], col = board.coords[COLUMN_E][index]; for (ulookup_t i = 0; i < board.blocks[ROW_E][index]; i++) { if (!current_state->elements[board.grid[row][col + i]]) return false; } - Valid comb = _create_valid(board, current_state, index, ROW_E); + valid_s combinations = _create_valid(board, current_state, index, ROW_E); bool is_reduced = false; - for (ulookup_t i = 0; i < comb.e_matrix.size; i++) { - for (ulookup_t j = 0; j < comb.e_matrix.elements[i].size; j++) { - if (comb.e_valid[i][j]) continue; + for (ulookup_t i = 0; i < combinations.e_matrix.size; i++) { + for (ulookup_t j = 0; j < combinations.e_matrix.elements[i].size; j++) { + if (combinations.e_valid[i][j]) continue; - current_state->elements[board.grid[row][col + i]] &= ~comb.e_matrix.elements[i].elements[j]; + current_state->elements[board.grid[row][col + i]] &= ~combinations.e_matrix.elements[i].elements[j]; is_reduced = true; } } - _destroy_valid(&comb); + _destroy_valid(&combinations); return is_reduced; } -bool _reduce_no_col_combination(board_s board, SArray * current_state, ulookup_t index) { +bool _reduce_no_col_combination(board_s board, state_array_s * current_state, ulookup_t index) { ulookup_t row = board.coords[ROW_E][index], col = board.coords[COLUMN_E][index]; for (ulookup_t i = 0; i < board.blocks[COLUMN_E][index]; i++) { if (current_state->elements[board.grid[row + i][col]] == INVALID_STATE) return false; } - Valid comb = _create_valid(board, current_state, index, COLUMN_E); + valid_s comb = _create_valid(board, current_state, index, COLUMN_E); bool is_reduced = false; for (ulookup_t i = 0; i < comb.e_matrix.size; i++) { diff --git a/program/source/algorithms/backtrack.c b/program/source/algorithms/backtrack.c index 66998f2..438eacd 100644 --- a/program/source/algorithms/backtrack.c +++ b/program/source/algorithms/backtrack.c @@ -7,17 +7,17 @@ #include #include -bool _backtrack_row_sum(board_s board, SArray current_state, size_t index); -bool _backtrack_col_sum(board_s board, SArray current_state, size_t index); +bool _backtrack_row_sum(board_s board, state_array_s current_state, size_t index); +bool _backtrack_col_sum(board_s board, state_array_s current_state, size_t index); -bool _backtrack_valid_sums(board_s board, SArray current_state); -bool _backtrack_valid_row_sums(board_s board, SArray current_state, size_t index); -bool _backtrack_valid_col_sums(board_s board, SArray current_state, size_t index); +bool _backtrack_valid_sums(board_s board, state_array_s current_state); +bool _backtrack_valid_row_sums(board_s board, state_array_s current_state, size_t index); +bool _backtrack_valid_col_sums(board_s board, state_array_s current_state, size_t index); -bool _backtrack_row_repeat(board_s board, SArray current_state, size_t index); -bool _backtrack_col_repeat(board_s board, SArray current_state, size_t index); +bool _backtrack_row_repeat(board_s board, state_array_s current_state, size_t index); +bool _backtrack_col_repeat(board_s board, state_array_s current_state, size_t index); -bool backtrack(board_s board, SArray current_state) { +bool backtrack(board_s board, state_array_s current_state) { error_mode = DEFAULT_E; expect(get_settings_singleton()->is_backtrack, return _backtrack_valid_sums(board, current_state), "WARNING: backtracking is off"); @@ -41,7 +41,7 @@ bool backtrack(board_s board, SArray current_state) { return invalid_state_backtrack_stat(is_backtrack); } -bool _backtrack_row_sum(board_s board, SArray current_state, size_t index) { +bool _backtrack_row_sum(board_s board, state_array_s current_state, size_t index) { ulookup_t row = board.coords[ROW_E][index], col = board.coords[COLUMN_E][index]; ulookup_t filled_sums = 0, sums = board.sums[ROW_E][index]; ulookup_t filled_blocks = 0, blocks = board.blocks[ROW_E][index]; @@ -54,12 +54,12 @@ bool _backtrack_row_sum(board_s board, SArray current_state, size_t index) { return (filled_blocks == blocks) ? filled_sums != sums : false || (filled_sums > sums) || - (blocks - filled_blocks) ? get_sums(blocks - filled_blocks, UPPER_EDGE) < (sums - filled_sums) : false || - (blocks - filled_blocks) ? get_sums(blocks - filled_blocks, LOWER_EDGE) > (sums - filled_sums) : false || + (blocks - filled_blocks) ? get_sums(blocks - filled_blocks, UPPER_EDGE_E) < (sums - filled_sums) : false || + (blocks - filled_blocks) ? get_sums(blocks - filled_blocks, LOWER_EDGE_E) > (sums - filled_sums) : false || _backtrack_row_repeat(board, current_state, index); } -bool _backtrack_col_sum(board_s board, SArray current_state, size_t index) { +bool _backtrack_col_sum(board_s board, state_array_s current_state, size_t index) { ulookup_t row = board.coords[ROW_E][index], col = board.coords[COLUMN_E][index]; ulookup_t filled_sums = 0, sums = board.sums[COLUMN_E][index]; ulookup_t filled_blocks = 0, blocks = board.blocks[COLUMN_E][index]; @@ -72,12 +72,12 @@ bool _backtrack_col_sum(board_s board, SArray current_state, size_t index) { return (blocks == filled_blocks) ? filled_sums != sums : false || (filled_sums > sums) || - (blocks - filled_blocks) ? get_sums(blocks - filled_blocks, UPPER_EDGE) < (sums - filled_sums) : false || - (blocks - filled_blocks) ? get_sums(blocks - filled_blocks, LOWER_EDGE) > (sums - filled_sums) : false || + (blocks - filled_blocks) ? get_sums(blocks - filled_blocks, UPPER_EDGE_E) < (sums - filled_sums) : false || + (blocks - filled_blocks) ? get_sums(blocks - filled_blocks, LOWER_EDGE_E) > (sums - filled_sums) : false || _backtrack_col_repeat(board, current_state, index); } -bool _backtrack_valid_sums(board_s board, SArray current_state) { +bool _backtrack_valid_sums(board_s board, state_array_s current_state) { error_mode = DEFAULT_E; expect(is_end_state(current_state), return false, "current state is not an end state"); @@ -99,7 +99,7 @@ bool _backtrack_valid_sums(board_s board, SArray current_state) { return is_backtrack; } -bool _backtrack_valid_row_sums(board_s board, SArray current_state, size_t index) { +bool _backtrack_valid_row_sums(board_s board, state_array_s current_state, size_t index) { ulookup_t row = board.coords[ROW_E][index], col = board.coords[COLUMN_E][index]; ulookup_t filled_sums = 0, sums = board.sums[ROW_E][index]; @@ -111,7 +111,7 @@ bool _backtrack_valid_row_sums(board_s board, SArray current_state, size_t index return filled_sums != sums; } -bool _backtrack_valid_col_sums(board_s board, SArray current_state, size_t index) { +bool _backtrack_valid_col_sums(board_s board, state_array_s current_state, size_t index) { ulookup_t row = board.coords[ROW_E][index], col = board.coords[COLUMN_E][index]; ulookup_t filled_sums = 0, sums = board.sums[COLUMN_E][index]; @@ -123,7 +123,7 @@ bool _backtrack_valid_col_sums(board_s board, SArray current_state, size_t index return filled_sums != sums; } -bool _backtrack_row_repeat(board_s board, SArray current_state, size_t index) { +bool _backtrack_row_repeat(board_s board, state_array_s current_state, size_t index) { ulookup_t row = board.coords[ROW_E][index], col = board.coords[COLUMN_E][index]; state_t state = INVALID_STATE; @@ -139,7 +139,7 @@ bool _backtrack_row_repeat(board_s board, SArray current_state, size_t index) { return false; } -bool _backtrack_col_repeat(board_s board, SArray current_state, size_t index) { +bool _backtrack_col_repeat(board_s board, state_array_s current_state, size_t index) { ulookup_t row = board.coords[ROW_E][index], col = board.coords[COLUMN_E][index]; state_t state = INVALID_STATE; diff --git a/program/source/algorithms/depth_first_search.c b/program/source/algorithms/depth_first_search.c index d059e83..13adbec 100644 --- a/program/source/algorithms/depth_first_search.c +++ b/program/source/algorithms/depth_first_search.c @@ -1,6 +1,4 @@ #include -#include -#include #include #include @@ -10,22 +8,22 @@ #include -#define STACK_DATA_TYPE SArray +#define STACK_DATA_TYPE state_array_s #include -SArray depth_first_search(board_s board) { - Stack stack = create_stack(); +state_array_s depth_first_search(board_s board) { + stack_s stack = create_stack(); - SArray initial = create_state_array(board.game.empty_count); + state_array_s initial = create_state_array(board.game.empty_count); set_full_state_array(&initial); reduce(board, &initial); push_stack(&stack, initial); - SArray solution = { 0 }; + state_array_s solution = { 0 }; while (!is_empty_stack(stack)) { get_stat_singleton()->dfs_iteration_count++; - SArray guess = pop_stack(&stack); + state_array_s guess = pop_stack(&stack); if (!look_ahead(board, &guess) || backtrack(board, guess)) { destroy_state_array(&guess); @@ -40,7 +38,7 @@ SArray depth_first_search(board_s board) { ulookup_t index = shortest_multi_index(guess); - SMatrix next = generate_neighbor(guess, index); + state_matrix_s next = create_neighbors_state_matrix(guess, index); for (size_t i = 0; i < next.size; i++) { if (forward_checking(board, &next.elements[i], index)) push_stack(&stack, next.elements[i]); else destroy_state_array(&next.elements[i]); diff --git a/program/source/algorithms/forward_checking.c b/program/source/algorithms/forward_checking.c index 1d253cd..6437ad6 100644 --- a/program/source/algorithms/forward_checking.c +++ b/program/source/algorithms/forward_checking.c @@ -5,10 +5,10 @@ #include #include -bool _row_forward_check(board_s board, SArray * current_state, ulookup_t index); -bool _col_forward_check(board_s board, SArray * current_state, ulookup_t index); +bool _row_forward_check(board_s board, state_array_s * current_state, ulookup_t index); +bool _col_forward_check(board_s board, state_array_s * current_state, ulookup_t index); -bool forward_checking(board_s board, SArray * current_state, ulookup_t index) { +bool forward_checking(board_s board, state_array_s * current_state, ulookup_t index) { error_mode = ASSERT_E; expect(current_state, NO_ACTION, "current state parameter is NULL (%p)", (void*)current_state); expect(is_one_value(current_state->elements[index]), NO_ACTION, "current state element at index %u is not a one value", index); @@ -25,7 +25,7 @@ bool forward_checking(board_s board, SArray * current_state, ulookup_t index) { )); } -bool _row_forward_check(board_s board, SArray * current_state, ulookup_t index) { +bool _row_forward_check(board_s board, state_array_s * current_state, ulookup_t index) { state_t s = current_state->elements[index]; ulookup_t row = board.coords[ROW_E][index], col = board.coords[COLUMN_E][index], c; @@ -44,7 +44,7 @@ bool _row_forward_check(board_s board, SArray * current_state, ulookup_t index) return true; } -bool _col_forward_check(board_s board, SArray * current_state, ulookup_t index) { +bool _col_forward_check(board_s board, state_array_s * current_state, ulookup_t index) { state_t s = current_state->elements[index]; ulookup_t row = board.coords[ROW_E][index], col = board.coords[COLUMN_E][index], r; diff --git a/program/source/algorithms/reduce.c b/program/source/algorithms/reduce.c index 1ac990e..d953f3e 100644 --- a/program/source/algorithms/reduce.c +++ b/program/source/algorithms/reduce.c @@ -1,7 +1,7 @@ #include #include -#define STACK_DATA_TYPE ksize_t +#define STACK_DATA_TYPE ulookup_t #include #define MAGIC_TABLE_LENGTH 7 @@ -24,11 +24,11 @@ state_t _check_one_blocks(ulookup_t blocks, ulookup_t sum); state_t _check_even_two_blocks(ulookup_t block, ulookup_t sum); state_t _check_high_end(ulookup_t block, ulookup_t sum); state_t _check_low_end(ulookup_t block, ulookup_t sum); -void _reduce_multi_values(board_s board, SArray * initial_state); -void _reduce_row_multi_values(board_s board, SArray * initial_state, ulookup_t index); -void _reduce_col_multi_values(board_s board, SArray * initial_state, ulookup_t index); +void _reduce_multi_values(board_s board, state_array_s * initial_state); +void _reduce_row_multi_values(board_s board, state_array_s * initial_state, ulookup_t index); +void _reduce_col_multi_values(board_s board, state_array_s * initial_state, ulookup_t index); -void reduce(board_s board, SArray * initial_state) { +void reduce(board_s board, state_array_s * initial_state) { assert(initial_state && "INITIAL STATE ARRAY IS NULL"); if (!(get_settings_singleton()->is_reduce)) return; @@ -72,32 +72,34 @@ state_t _check_even_two_blocks(ulookup_t block, ulookup_t sum) { } state_t _check_high_end(ulookup_t block, ulookup_t sum) { - ulookup_t block_sum = get_sums(block - 1, UPPER_EDGE); - state_t s = (LOWER_EDGE + block_sum < sum) ? get_edge_state((MAX_BLOCK_VALUES + 1) - (sum - block_sum), UPPER_EDGE) : FULL_STATE; + ulookup_t block_sum = get_sums(block - 1, UPPER_EDGE_E); + state_t s = (LOWER_EDGE_E + block_sum < sum) ? get_edge_state((MAX_BLOCK_VALUES + 1) - (sum - block_sum), UPPER_EDGE_E) : FULL_STATE; return s; } state_t _check_low_end(ulookup_t block, ulookup_t sum) { - ulookup_t block_sum = get_sums(block - 1, LOWER_EDGE); - state_t s = (UPPER_EDGE + block_sum > sum) ? get_edge_state(sum - block_sum, LOWER_EDGE) : FULL_STATE; + ulookup_t block_sum = get_sums(block - 1, LOWER_EDGE_E); + state_t s = (UPPER_EDGE_E + block_sum > sum) ? get_edge_state(sum - block_sum, LOWER_EDGE_E) : FULL_STATE; return s; } -void _reduce_multi_values(board_s board, SArray * current_state) { - check_e checks[KAKURO_SIZE_MAX] = { 0 }; +void _reduce_multi_values(board_s board, state_array_s * current_state) { + check_e * checks = calloc(board.game.empty_count, sizeof(check_e)); for (size_t i = 0; i < board.game.empty_count; i++) { if (!(checks[i] & ROWCHECK)) _reduce_row_multi_values(board, current_state, i); if (!(checks[i] & COLCHECK)) _reduce_col_multi_values(board, current_state, i); add_check(board, checks, i); } + + free(checks); } -void _reduce_row_multi_values(board_s board, SArray * current_state, ulookup_t index) { +void _reduce_row_multi_values(board_s board, state_array_s * current_state, ulookup_t index) { ulookup_t row = board.coords[ROW_E][index], col = board.coords[COLUMN_E][index]; ulookup_t block = board.blocks[ROW_E][index], sums = board.sums[ROW_E][index]; ulookup_t empty_blocks = block, empty_sums = sums; - Stack multi = create_stack(); + stack_s multi = create_stack(); for (size_t i = 0; i < block; i++) { state_t s = current_state->elements[board.grid[row][col + i]]; @@ -118,11 +120,11 @@ void _reduce_row_multi_values(board_s board, SArray * current_state, ulookup_t i destroy_stack(&multi, NULL); } -void _reduce_col_multi_values(board_s board, SArray * current_state, ulookup_t index) { +void _reduce_col_multi_values(board_s board, state_array_s * current_state, ulookup_t index) { ulookup_t row = board.coords[ROW_E][index], col = board.coords[COLUMN_E][index]; ulookup_t block = board.blocks[COLUMN_E][index], sums = board.sums[COLUMN_E][index]; ulookup_t empty_blocks = block, empty_sums = sums; - Stack multi = create_stack(); + stack_s multi = create_stack(); for (size_t i = 0; i < block; i++) { state_t s = current_state->elements[board.grid[row + i][col]]; diff --git a/program/source/instance/argument.c b/program/source/instance/argument.c index 27215e5..17b5c90 100644 --- a/program/source/instance/argument.c +++ b/program/source/instance/argument.c @@ -2,6 +2,8 @@ #include #include #include +#include +#include #include #include @@ -162,7 +164,7 @@ void _setup_settings(Argument argument); void setup_program(int argc, char **argv) { // argument queue to torn arguemnt value access like a queue (first-in-first-out). - Queue arg_queue = { .elements = argv, .max = argc, .size = argc, .current = 0 }; + queue_s arg_queue = { .elements = argv, .max = argc, .size = argc, .current = 0 }; dequeue(&arg_queue); // remove invocation name from queue while (!is_empty_queue(arg_queue)) { diff --git a/program/source/instance/expect.c b/program/source/instance/expect.c new file mode 100644 index 0000000..2f05320 --- /dev/null +++ b/program/source/instance/expect.c @@ -0,0 +1,18 @@ +#include + +FILE * error_log = NULL; +error_mode_e error_mode = DEFAULT_E; + +#ifdef ERROR_LOG_FILE_PATH + +__attribute__((constructor)) static void open_error_log(void) { + remove(ERROR_LOG_FILE_PATH); + error_log = fopen(ERROR_LOG_FILE_PATH, "a"); + assert(error_log && "Failed to open error_log"); +} + +__attribute__((destructor)) static void close_error_log(void) { + fclose(error_log); +} + +#endif diff --git a/program/source/structures/concrete/state.c b/program/source/structures/concrete/state.c index 3a9d110..853448d 100644 --- a/program/source/structures/concrete/state.c +++ b/program/source/structures/concrete/state.c @@ -5,26 +5,26 @@ #include #include -SArray create_state_array(ulookup_t size) { +state_array_s create_state_array(ulookup_t size) { error_mode = ASSERT_E; expect(size != 0, NO_ACTION, "state array size can't be zero (%u)", size); - SArray array = { .elements = calloc(size, sizeof(state_t)), .size = size, }; + state_array_s array = { .elements = calloc(size, sizeof(state_t)), .size = size, }; expect(array.elements, NO_ACTION, "memory allocation for array failed/array element is NULL (%p)", (void*)array.elements); return array; } -void set_full_state_array(SArray * array) { +void set_full_state_array(state_array_s * array) { error_mode = ASSERT_E; expect(array, NO_ACTION, "state array pointer is NULL (%p)", (void*)array); for (ulookup_t i = 0; i < array->size; i++) array->elements[i] = FULL_STATE; } -SArray split_state(state_t state) { +state_array_s split_state(state_t state) { state_t copy = state; - SArray sub = create_state_array(state_count(copy)); + state_array_s sub = create_state_array(state_count(copy)); for (ulookup_t i = 0; i < sub.size; i++) { sub.elements[i] = copy & ~(copy - 1); @@ -34,7 +34,7 @@ SArray split_state(state_t state) { return sub; } -state_t merge_state_array(SArray array) { +state_t merge_state_array(state_array_s array) { state_t merge = { 0 }; for (ulookup_t i = 0; i < array.size; i++) { @@ -44,7 +44,7 @@ state_t merge_state_array(SArray array) { return merge; } -void destroy_state_array(SArray * array) { +void destroy_state_array(state_array_s * array) { error_mode = ASSERT_E; expect(array, NO_ACTION, "state array pointer is NULL (%p)", (void*)array); @@ -53,8 +53,8 @@ void destroy_state_array(SArray * array) { array->size = 0; } -SArray copy_state_array(SArray array) { - SArray sa = { .elements = malloc(sizeof(state_t) * array.size), .size = array.size }; +state_array_s copy_state_array(state_array_s array) { + state_array_s sa = { .elements = malloc(sizeof(state_t) * array.size), .size = array.size }; assert(sa.elements && "MEMORY ALLOCATION FAILED"); for (ulookup_t i = 0; i < array.size; i++) sa.elements[i] = array.elements[i]; @@ -62,7 +62,7 @@ SArray copy_state_array(SArray array) { return sa; } -bool compare_states(SArray array_a, SArray array_b) { +bool compare_states(state_array_s array_a, state_array_s array_b) { assert(array_a.size == array_b.size && "NOT EQUAL LENGTHS"); for (ulookup_t i = 0; i < array_a.size; i++) { if (array_a.elements[i] != array_b.elements[i]) return false; @@ -70,34 +70,34 @@ bool compare_states(SArray array_a, SArray array_b) { return true; } -bool valid_states(SArray array) { +bool valid_states(state_array_s array) { for (ulookup_t i = 0; i < array.size; i++) { if (array.elements[i] == INVALID_STATE) return false; } return true; } -bool is_end_state(SArray array) { +bool is_end_state(state_array_s array) { for (ulookup_t i = 0; i < array.size; i++) { if (!is_one_value(array.elements[i])) return false; } return true; } -ulookup_t get_sums(ulookup_t start, EType type) { +ulookup_t get_sums(ulookup_t start, edge_type_e type) { assert(start <= MAX_BLOCK_VALUES && "VALUE IS TOO HIGH"); ulookup_t sums = 0; - if (type == LOWER_EDGE) for (ulookup_t i = 1; i <= start; i++) sums += i; + if (type == LOWER_EDGE_E) for (ulookup_t i = 1; i <= start; i++) sums += i; else for (ulookup_t i = MAX_BLOCK_VALUES; i > MAX_BLOCK_VALUES - start; i--) sums += i; return sums; } -state_t get_edge_state(ulookup_t count, EType type) { +state_t get_edge_state(ulookup_t count, edge_type_e type) { assert(count <= MAX_BLOCK_VALUES && "VALUE IS TOO HIGH"); - return (type == LOWER_EDGE) ? (FULL_STATE >> (MAX_BLOCK_VALUES - count)) : ((FULL_STATE << (MAX_BLOCK_VALUES - count)) & FULL_STATE); + return (type == LOWER_EDGE_E) ? (FULL_STATE >> (MAX_BLOCK_VALUES - count)) : ((FULL_STATE << (MAX_BLOCK_VALUES - count)) & FULL_STATE); } bool is_one_value(state_t state) { @@ -122,11 +122,11 @@ ulookup_t state_to_sums(state_t state) { } state_t get_one_state(ulookup_t value) { - assert(value >= LOWER_EDGE && value <= UPPER_EDGE && "CAN'T TURN INTO MULTI VALUE STATE"); + assert(value >= LOWER_EDGE_E && value <= UPPER_EDGE_E && "CAN'T TURN INTO MULTI VALUE STATE"); return 1 << (value - 1); } -ulookup_t shortest_multi_index(SArray array) { +ulookup_t shortest_multi_index(state_array_s array) { int16_t index = -1; for (ulookup_t i = 0; i < array.size; i++) { @@ -144,10 +144,10 @@ ulookup_t state_count(state_t state) { return __builtin_popcount(state); } -SMatrix generate_neighbor(SArray array, ulookup_t index) { +state_matrix_s create_neighbors_state_matrix(state_array_s array, ulookup_t index) { assert(array.size > index && "INVALID INDEX"); - SMatrix sm = { + state_matrix_s sm = { .size = state_count(array.elements[index]), }; @@ -155,7 +155,7 @@ SMatrix generate_neighbor(SArray array, ulookup_t index) { for (ulookup_t i = 0; i < MAX_BLOCK_VALUES; i++) { if (!(array.elements[index] & (1 << i))) continue; - SArray next_state = copy_state_array(array); + state_array_s next_state = copy_state_array(array); next_state.elements[index] &= (1 << i); sm.elements[idx++] = next_state; @@ -163,7 +163,7 @@ SMatrix generate_neighbor(SArray array, ulookup_t index) { return sm; } -void destroy_state_matrix(SMatrix * matrix) { +void destroy_state_matrix(state_matrix_s * matrix) { for (ulookup_t i = 0; i < matrix->size; i++) destroy_state_array(&matrix->elements[i]); matrix->size = 0; } @@ -179,7 +179,7 @@ void print_state(state_t s) { putchar('\n'); } -void print_state_array(SArray s) { +void print_state_array(state_array_s s) { for (ulookup_t i = 0; i < s.size; i++) { printf("[ %3u ] : ", i); print_state(s.elements[i]); @@ -188,7 +188,7 @@ void print_state_array(SArray s) { fflush(stdout); } -void print_solution(SArray solved) { +void print_solution(state_array_s solved) { assert(is_end_state(solved) && "CAN ONLY PRINT END STATE"); printf("[ "); for (ulookup_t i = 0; i < solved.size; i++) printf("%u ", __builtin_ctz(solved.elements[i]) + 1); diff --git a/test/blackbox/CMakeLists.txt b/test/blackbox/CMakeLists.txt index 1a2b53d..8f9edf7 100644 --- a/test/blackbox/CMakeLists.txt +++ b/test/blackbox/CMakeLists.txt @@ -1,38 +1,31 @@ add_executable(BLACKBOX main.c) -add_library(black_box "") - -target_link_libraries(black_box - PRIVATE - PROGRAM_LIBRARY - greatest -) - -target_sources(black_box +target_sources(BLACKBOX PRIVATE suites/easy/easy_unset.c - suites/easy/easy_forward_check.c - suites/easy/easy_backtrack.c - suites/easy/easy_backtrack_forward_check.c - suites/easy/easy_arc_consistency.c - suites/easy/easy_arc_consistency_forward_check.c - suites/easy/easy_arc_consistency_backtrack.c - suites/easy/easy_arc_consistency_backtrack_forward_check.c + suites/easy/easy_fch.c + suites/easy/easy_bt.c + suites/easy/easy_bt_fch.c + suites/easy/easy_ac.c + suites/easy/easy_ac_fch.c + suites/easy/easy_ac_bt.c + suites/easy/easy_ac_bt_fch.c suites/medium/medium_backtrack_forward_check.c suites/medium/medium_arc_consistency_backtrack_forward_check.c ) -target_include_directories(black_box +target_include_directories(BLACKBOX PUBLIC ${CMAKE_CURRENT_LIST_DIR} ) target_link_libraries(BLACKBOX PRIVATE - black_box greatest - PROGRAM_LIBRARY + program ) +target_compile_definitions(program PRIVATE ERROR_LOG_FILE_PATH=\"${CMAKE_BINARY_DIR}/blackbox-debug.txt\") + add_test(NAME BLACKBOX_TEST COMMAND BLACKBOX WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}") diff --git a/test/blackbox/main.c b/test/blackbox/main.c index ab601b1..ac9b629 100644 --- a/test/blackbox/main.c +++ b/test/blackbox/main.c @@ -1,30 +1,30 @@ #include extern SUITE(easy_unset); -extern SUITE(easy_forward_check); -extern SUITE(easy_backtrack); -extern SUITE(easy_backtrack_forward_check); -extern SUITE(easy_arc_consistency); -extern SUITE(easy_arc_consistency_forward_check); -extern SUITE(easy_arc_consistency_backtrack); -extern SUITE(easy_arc_consistency_backtrack_forward_check); +extern SUITE(easy_fch); +extern SUITE(easy_bt); +extern SUITE(easy_bt_fch); +extern SUITE(easy_ac); +extern SUITE(easy_ac_fch); +extern SUITE(easy_ac_bt); +extern SUITE(easy_ac_bt_fch); extern SUITE(medium_backtrack_forward_check); extern SUITE(medium_arc_consistency_backtrack_forward_check); GREATEST_MAIN_DEFS(); -int main(int argc, char **argv) { +int main(const int argc, char **argv) { GREATEST_MAIN_BEGIN(); RUN_SUITE(easy_unset); - RUN_SUITE(easy_forward_check); - RUN_SUITE(easy_backtrack); - RUN_SUITE(easy_backtrack_forward_check); - RUN_SUITE(easy_arc_consistency); - RUN_SUITE(easy_arc_consistency_forward_check); - RUN_SUITE(easy_arc_consistency_backtrack); - RUN_SUITE(easy_arc_consistency_backtrack_forward_check); + RUN_SUITE(easy_fch); + RUN_SUITE(easy_bt); + RUN_SUITE(easy_bt_fch); + RUN_SUITE(easy_ac); + RUN_SUITE(easy_ac_fch); + RUN_SUITE(easy_ac_bt); + RUN_SUITE(easy_ac_bt_fch); RUN_SUITE(medium_backtrack_forward_check); RUN_SUITE(medium_arc_consistency_backtrack_forward_check); diff --git a/test/blackbox/suites/easy/easy_arc_consistency.c b/test/blackbox/suites/easy/easy_ac.c similarity index 94% rename from test/blackbox/suites/easy/easy_arc_consistency.c rename to test/blackbox/suites/easy/easy_ac.c index 305c486..977de6c 100644 --- a/test/blackbox/suites/easy/easy_arc_consistency.c +++ b/test/blackbox/suites/easy/easy_ac.c @@ -19,7 +19,7 @@ TEST easy_arc_consistency_one(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -48,7 +48,7 @@ TEST easy_arc_consistency_two(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -77,7 +77,7 @@ TEST easy_arc_consistency_three(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -106,7 +106,7 @@ TEST easy_arc_consistency_four(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -135,7 +135,7 @@ TEST easy_arc_consistency_five(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -150,7 +150,7 @@ TEST easy_arc_consistency_five(void) { PASS(); } -SUITE (easy_arc_consistency) { +SUITE (easy_ac) { // 4x4 RUN_TEST(easy_arc_consistency_one); RUN_TEST(easy_arc_consistency_two); diff --git a/test/blackbox/suites/easy/easy_arc_consistency_backtrack.c b/test/blackbox/suites/easy/easy_ac_bt.c similarity index 94% rename from test/blackbox/suites/easy/easy_arc_consistency_backtrack.c rename to test/blackbox/suites/easy/easy_ac_bt.c index c6aaf24..87461bb 100644 --- a/test/blackbox/suites/easy/easy_arc_consistency_backtrack.c +++ b/test/blackbox/suites/easy/easy_ac_bt.c @@ -19,7 +19,7 @@ TEST easy_arc_consistency_backtrack_one(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -48,7 +48,7 @@ TEST easy_arc_consistency_backtrack_two(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -77,7 +77,7 @@ TEST easy_arc_consistency_backtrack_three(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -106,7 +106,7 @@ TEST easy_arc_consistency_backtrack_four(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -135,7 +135,7 @@ TEST easy_arc_consistency_backtrack_five(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -150,7 +150,7 @@ TEST easy_arc_consistency_backtrack_five(void) { PASS(); } -SUITE (easy_arc_consistency_backtrack) { +SUITE (easy_ac_bt) { // 4x4 RUN_TEST(easy_arc_consistency_backtrack_one); RUN_TEST(easy_arc_consistency_backtrack_two); diff --git a/test/blackbox/suites/easy/easy_arc_consistency_backtrack_forward_check.c b/test/blackbox/suites/easy/easy_ac_bt_fch.c similarity index 94% rename from test/blackbox/suites/easy/easy_arc_consistency_backtrack_forward_check.c rename to test/blackbox/suites/easy/easy_ac_bt_fch.c index e7ee8b8..c21e178 100644 --- a/test/blackbox/suites/easy/easy_arc_consistency_backtrack_forward_check.c +++ b/test/blackbox/suites/easy/easy_ac_bt_fch.c @@ -19,7 +19,7 @@ TEST easy_arc_consistency_backtrack_forward_check_one(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -48,7 +48,7 @@ TEST easy_arc_consistency_backtrack_forward_check_two(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -77,7 +77,7 @@ TEST easy_arc_consistency_backtrack_forward_check_three(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -106,7 +106,7 @@ TEST easy_arc_consistency_backtrack_forward_check_four(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -135,7 +135,7 @@ TEST easy_arc_consistency_backtrack_forward_check_five(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -150,7 +150,7 @@ TEST easy_arc_consistency_backtrack_forward_check_five(void) { PASS(); } -SUITE (easy_arc_consistency_backtrack_forward_check) { +SUITE (easy_ac_bt_fch) { // 4x4 RUN_TEST(easy_arc_consistency_backtrack_forward_check_one); RUN_TEST(easy_arc_consistency_backtrack_forward_check_two); diff --git a/test/blackbox/suites/easy/easy_arc_consistency_forward_check.c b/test/blackbox/suites/easy/easy_ac_fch.c similarity index 94% rename from test/blackbox/suites/easy/easy_arc_consistency_forward_check.c rename to test/blackbox/suites/easy/easy_ac_fch.c index d5c67d3..1673118 100644 --- a/test/blackbox/suites/easy/easy_arc_consistency_forward_check.c +++ b/test/blackbox/suites/easy/easy_ac_fch.c @@ -19,7 +19,7 @@ TEST easy_arc_consistency_forward_check_one(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -48,7 +48,7 @@ TEST easy_arc_consistency_forward_check_two(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -77,7 +77,7 @@ TEST easy_arc_consistency_forward_check_three(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -106,7 +106,7 @@ TEST easy_arc_consistency_forward_check_four(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -135,7 +135,7 @@ TEST easy_arc_consistency_forward_check_five(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -150,7 +150,7 @@ TEST easy_arc_consistency_forward_check_five(void) { PASS(); } -SUITE (easy_arc_consistency_forward_check) { +SUITE (easy_ac_fch) { // 4x4 RUN_TEST(easy_arc_consistency_forward_check_one); RUN_TEST(easy_arc_consistency_forward_check_two); diff --git a/test/blackbox/suites/easy/easy_backtrack.c b/test/blackbox/suites/easy/easy_bt.c similarity index 94% rename from test/blackbox/suites/easy/easy_backtrack.c rename to test/blackbox/suites/easy/easy_bt.c index 68c3b04..95c4f50 100644 --- a/test/blackbox/suites/easy/easy_backtrack.c +++ b/test/blackbox/suites/easy/easy_bt.c @@ -19,7 +19,7 @@ TEST easy_backtrack_one(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -48,7 +48,7 @@ TEST easy_backtrack_two(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -77,7 +77,7 @@ TEST easy_backtrack_three(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -106,7 +106,7 @@ TEST easy_backtrack_four(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -135,7 +135,7 @@ TEST easy_backtrack_five(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -150,7 +150,7 @@ TEST easy_backtrack_five(void) { PASS(); } -SUITE (easy_backtrack) { +SUITE (easy_bt) { // 4x4 RUN_TEST(easy_backtrack_one); RUN_TEST(easy_backtrack_two); diff --git a/test/blackbox/suites/easy/easy_backtrack_forward_check.c b/test/blackbox/suites/easy/easy_bt_fch.c similarity index 94% rename from test/blackbox/suites/easy/easy_backtrack_forward_check.c rename to test/blackbox/suites/easy/easy_bt_fch.c index 86b64cc..22cce00 100644 --- a/test/blackbox/suites/easy/easy_backtrack_forward_check.c +++ b/test/blackbox/suites/easy/easy_bt_fch.c @@ -19,7 +19,7 @@ TEST easy_backtrack_forward_check_one(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -48,7 +48,7 @@ TEST easy_backtrack_forward_check_two(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -77,7 +77,7 @@ TEST easy_backtrack_forward_check_three(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -106,7 +106,7 @@ TEST easy_backtrack_forward_check_four(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -135,7 +135,7 @@ TEST easy_backtrack_forward_check_five(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -150,7 +150,7 @@ TEST easy_backtrack_forward_check_five(void) { PASS(); } -SUITE (easy_backtrack_forward_check) { +SUITE (easy_bt_fch) { // 4x4 RUN_TEST(easy_backtrack_forward_check_one); RUN_TEST(easy_backtrack_forward_check_two); diff --git a/test/blackbox/suites/easy/easy_forward_check.c b/test/blackbox/suites/easy/easy_fch.c similarity index 94% rename from test/blackbox/suites/easy/easy_forward_check.c rename to test/blackbox/suites/easy/easy_fch.c index 800be4c..9430aef 100644 --- a/test/blackbox/suites/easy/easy_forward_check.c +++ b/test/blackbox/suites/easy/easy_fch.c @@ -19,7 +19,7 @@ TEST easy_forward_check_one(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -48,7 +48,7 @@ TEST easy_forward_check_two(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -77,7 +77,7 @@ TEST easy_forward_check_three(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -106,7 +106,7 @@ TEST easy_forward_check_four(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -135,7 +135,7 @@ TEST easy_forward_check_five(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -150,7 +150,7 @@ TEST easy_forward_check_five(void) { PASS(); } -SUITE (easy_forward_check) { +SUITE (easy_fch) { // 4x4 RUN_TEST(easy_forward_check_one); RUN_TEST(easy_forward_check_two); diff --git a/test/blackbox/suites/easy/easy_unset.c b/test/blackbox/suites/easy/easy_unset.c index e998134..e38c82e 100644 --- a/test/blackbox/suites/easy/easy_unset.c +++ b/test/blackbox/suites/easy/easy_unset.c @@ -19,7 +19,7 @@ TEST easy_unset_one(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -48,7 +48,7 @@ TEST easy_unset_two(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -77,7 +77,7 @@ TEST easy_unset_three(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -106,7 +106,7 @@ TEST easy_unset_four(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -135,7 +135,7 @@ TEST easy_unset_five(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); diff --git a/test/blackbox/suites/medium/medium_arc_consistency_backtrack_forward_check.c b/test/blackbox/suites/medium/medium_arc_consistency_backtrack_forward_check.c index a5398f1..a6368ed 100644 --- a/test/blackbox/suites/medium/medium_arc_consistency_backtrack_forward_check.c +++ b/test/blackbox/suites/medium/medium_arc_consistency_backtrack_forward_check.c @@ -29,7 +29,7 @@ TEST medium_arc_consistency_backtrack_forward_check_one(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -68,7 +68,7 @@ TEST medium_arc_consistency_backtrack_forward_check_two(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -107,7 +107,7 @@ TEST medium_arc_consistency_backtrack_forward_check_three(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -146,7 +146,7 @@ TEST medium_arc_consistency_backtrack_forward_check_four(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -185,7 +185,7 @@ TEST medium_arc_consistency_backtrack_forward_check_five(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); diff --git a/test/blackbox/suites/medium/medium_backtrack_forward_check.c b/test/blackbox/suites/medium/medium_backtrack_forward_check.c index 64b0259..2106905 100644 --- a/test/blackbox/suites/medium/medium_backtrack_forward_check.c +++ b/test/blackbox/suites/medium/medium_backtrack_forward_check.c @@ -29,7 +29,7 @@ TEST medium_backtrack_forward_check_one(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -68,7 +68,7 @@ TEST medium_backtrack_forward_check_two(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -107,7 +107,7 @@ TEST medium_backtrack_forward_check_three(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -146,7 +146,7 @@ TEST medium_backtrack_forward_check_four(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); @@ -185,7 +185,7 @@ TEST medium_backtrack_forward_check_five(void) { board_s board = create_board(fp); fclose(fp); - SArray solution = depth_first_search(board); + state_array_s solution = depth_first_search(board); destroy_board(&board); ASSERTm("NO SOLUTION FOUND", solution.size); diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index 8de9ff7..6cf75e5 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -1,28 +1,21 @@ add_executable(UNITTESTS main.c) -add_library(unit_tests "") - -target_link_libraries(unit_tests - PRIVATE - PROGRAM_LIBRARY - greatest -) - -target_sources(unit_tests +target_sources(UNITTESTS PRIVATE structures/abstract/stack_test.c ) -target_include_directories(unit_tests +target_include_directories(UNITTESTS PUBLIC ${CMAKE_CURRENT_LIST_DIR} ) target_link_libraries(UNITTESTS PRIVATE - unit_tests greatest - PROGRAM_LIBRARY + program ) -add_test(UNIT_TEST UNITTESTS) +target_compile_definitions(program PRIVATE ERROR_LOG_FILE_PATH=\"${CMAKE_BINARY_DIR}/blackbox-debug.txt\") + +add_test(NAME UNIT_TEST COMMAND UNITTESTS WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}") diff --git a/test/unit/structures/abstract/stack_test.c b/test/unit/structures/abstract/stack_test.c index f64e770..dd7b359 100644 --- a/test/unit/structures/abstract/stack_test.c +++ b/test/unit/structures/abstract/stack_test.c @@ -6,11 +6,11 @@ #include -#define STACK_DATA_TYPE ksize_t +#define STACK_DATA_TYPE ulookup_t #include TEST init_stack_is_empty(void) { - Stack stack = create_stack(); + stack_s stack = create_stack(); ASSERTm( "EXPECTED STACK WITH ZERO ELEMENTS/EMPTY", is_empty_stack(stack) @@ -19,7 +19,7 @@ TEST init_stack_is_empty(void) { } TEST init_stack_top_array_pointer_null(void) { - Stack stack = create_stack(); + stack_s stack = create_stack(); ASSERT_FALSEm( "EXPECTED TOP ARRAY POINTER IN STACK TO BE NULL", stack.head @@ -28,7 +28,7 @@ TEST init_stack_top_array_pointer_null(void) { } TEST push_one_to_empty_stack_expected_count_one(void) { - Stack stack = create_stack(); + stack_s stack = create_stack(); push_stack(&stack, 42); ASSERT_EQm( "EXPECTED COUNT TO BE ONE", @@ -40,7 +40,7 @@ TEST push_one_to_empty_stack_expected_count_one(void) { } TEST push_one_to_stack_is_empty_false(void) { - Stack stack = create_stack(); + stack_s stack = create_stack(); push_stack(&stack, 42); ASSERT_FALSEm( "EXPECTED STACK TO NOT BE EMPTY", @@ -52,7 +52,7 @@ TEST push_one_to_stack_is_empty_false(void) { } TEST push_one_to_stack_top_array_not_null(void) { - Stack stack = create_stack(); + stack_s stack = create_stack(); push_stack(&stack, 42); ASSERTm( "EXPECTED TOP ARRAY POINTER TO NOT BE NULL", @@ -64,7 +64,7 @@ TEST push_one_to_stack_top_array_not_null(void) { } TEST push_and_pop_one_to_empty_stack_expected_count_zero(void) { - Stack stack = create_stack(); + stack_s stack = create_stack(); push_stack(&stack, 42); pop_stack(&stack); ASSERT_EQm( @@ -77,7 +77,7 @@ TEST push_and_pop_one_to_empty_stack_expected_count_zero(void) { } TEST push_and_pop_one_to_empty_stack_expected_value_42(void) { - Stack stack = create_stack(); + stack_s stack = create_stack(); ulookup_t expected = 42; push_stack(&stack, expected); ulookup_t actual = pop_stack(&stack); @@ -92,7 +92,7 @@ TEST push_and_pop_one_to_empty_stack_expected_value_42(void) { } TEST push_and_pop_one_from_stack_expect_top_null(void) { - Stack stack = create_stack(); + stack_s stack = create_stack(); push_stack(&stack, 42); pop_stack(&stack); @@ -106,7 +106,7 @@ TEST push_and_pop_one_from_stack_expect_top_null(void) { } TEST push_42_and_0_and_pop_from_stack_to_get_0(void) { - Stack stack = create_stack(); + stack_s stack = create_stack(); ulookup_t expected = 0; push_stack(&stack, 42); @@ -123,7 +123,7 @@ TEST push_42_and_0_and_pop_from_stack_to_get_0(void) { } TEST push_until_max_array_list_size_bottom_is_null(void) { - Stack stack = create_stack(); + stack_s stack = create_stack(); for (size_t i = 0; i < STACK_LIST_ARRAY_SIZE; i++) push_stack(&stack, 42); ASSERT_FALSEm( @@ -136,7 +136,7 @@ TEST push_until_max_array_list_size_bottom_is_null(void) { } TEST push_until_max_array_list_size_plus_one_bottom_is_not_null(void) { - Stack stack = create_stack(); + stack_s stack = create_stack(); for (size_t i = 0; i < STACK_LIST_ARRAY_SIZE + 1; i++) push_stack(&stack, 42); ASSERTm( @@ -149,7 +149,7 @@ TEST push_until_max_array_list_size_plus_one_bottom_is_not_null(void) { } TEST push_max_array_list_size_plus_one_then_pop_one_bottom_is_null(void) { - Stack stack = create_stack(); + stack_s stack = create_stack(); for (size_t i = 0; i < STACK_LIST_ARRAY_SIZE + 1; i++) push_stack(&stack, 42); pop_stack(&stack); @@ -164,7 +164,7 @@ TEST push_max_array_list_size_plus_one_then_pop_one_bottom_is_null(void) { } TEST push_max_array_list_size_plus_one_then_pop_all_top_is_null(void) { - Stack stack = create_stack(); + stack_s stack = create_stack(); for (size_t i = 0; i < STACK_LIST_ARRAY_SIZE + 1; i++) push_stack(&stack, 42); for (size_t i = 0; i < STACK_LIST_ARRAY_SIZE + 1; i++) pop_stack(&stack); @@ -179,7 +179,7 @@ TEST push_max_array_list_size_plus_one_then_pop_all_top_is_null(void) { } TEST push_one_and_free_stack_top_is_null(void) { - Stack stack = create_stack(); + stack_s stack = create_stack(); push_stack(&stack, 42); ASSERTm( "EXPECTED TOP TO NOT BE NULL", @@ -195,7 +195,7 @@ TEST push_one_and_free_stack_top_is_null(void) { } TEST push_max_arrayes_and_free_stack_top_is_null(void) { - Stack stack = create_stack(); + stack_s stack = create_stack(); for (size_t i = 0; i < STACK_LIST_ARRAY_SIZE; i++) push_stack(&stack, 42); ASSERTm( @@ -212,7 +212,7 @@ TEST push_max_arrayes_and_free_stack_top_is_null(void) { } TEST push_max_array_plus_onees_and_free_stack_top_is_null(void) { - Stack stack = create_stack(); + stack_s stack = create_stack(); for (size_t i = 0; i < STACK_LIST_ARRAY_SIZE + 1; i++) push_stack(&stack, 42); ASSERTm( @@ -229,7 +229,7 @@ TEST push_max_array_plus_onees_and_free_stack_top_is_null(void) { } TEST push_max_array_with_1_plus_one_42_and_pop_back_42(void) { - Stack stack = create_stack(); + stack_s stack = create_stack(); for (size_t i = 0; i < STACK_LIST_ARRAY_SIZE; i++) push_stack(&stack, 1); ulookup_t expected = 42, actual; push_stack(&stack, 42); @@ -244,7 +244,7 @@ TEST push_max_array_with_1_plus_one_42_and_pop_back_42(void) { } TEST push_max_array_minus_one_with_1_plus_one_42_and_pop_back_42(void) { - Stack stack = create_stack(); + stack_s stack = create_stack(); for (size_t i = 0; i < STACK_LIST_ARRAY_SIZE - 1; i++) push_stack(&stack, 1); ulookup_t expected = 42, actual; push_stack(&stack, 42);