diff --git a/program/include/instance/expect.h b/program/include/instance/expect.h index ad64268..594bb4d 100644 --- a/program/include/instance/expect.h +++ b/program/include/instance/expect.h @@ -1,48 +1,51 @@ +#ifndef INSTANCE_EXPECT_H +#define INSTANCE_EXPECT_H + #include #include #include -typedef enum error_mode { - DEFAULT_E, - ABORT_E, - ASSERT_E, +typedef enum error_mode_type { + DEFAULT_E, ABORT_E, ASSERT_E, } error_mode_e; -error_mode_e mode = DEFAULT_E; -FILE * error_log; +static error_mode_e error_mode = DEFAULT_E; +static FILE * error_log = NULL; #define NO_ACTION (void)(0) #ifdef ERROR_LOG_FILE_PATH -#define expect(assertion, error_action, ...) \ -{ \ - if (assertion) { \ - assert(error_log = fopen(ERROR_LOG_FILE_PATH, "a")); \ - fprintf(error_log, __VA_ARGS__); \ - fprintf(error_log, "\n"); \ - fclose(error_log) \ - switch (mode) { \ - case ABORT_E : { abort(); } \ - case ASSERT_E : { assert(assertion); } \ - default : { error_action; } \ - } \ - } \ +#define expect(assertion, error_action, ...) \ +{ \ + if (!(assertion)) { \ + assert(error_log = fopen(ERROR_LOG_FILE_PATH, "a")); \ + fprintf(error_log, __VA_ARGS__); \ + fprintf(error_log, "\n"); \ + fclose(error_log) \ + switch (error_mode) { \ + case ABORT_E : { error_action; abort(); } \ + case ASSERT_E : { error_action; assert(0 && assertion); } \ + default : { error_action; } \ + } \ + } \ } #else -#define expect(assertion, error_action, ...) \ -{ \ - if (assertion) { \ - fprintf(error_log ? error_log : stderr, __VA_ARGS__); \ - fprintf(error_log ? error_log : stderr, "\n"); \ - switch (mode) { \ - case ABORT_E : { abort(); } \ - case ASSERT_E : { assert(assertion); } \ - default : { error_action; } \ - } \ - } \ +#define expect(assertion, error_action, ...) \ +{ \ + if (!(assertion)) { \ + fprintf(error_log ? error_log : stderr, __VA_ARGS__); \ + fprintf(error_log ? error_log : stderr, "\n"); \ + switch (error_mode) { \ + case ABORT_E : { error_action; abort(); } \ + case ASSERT_E : { error_action; assert(0 && assertion); } \ + default : { error_action; } \ + } \ + } \ } -#endif /* ERROR_LOG_FILE_PATH */ \ No newline at end of file +#endif /* ERROR_LOG_FILE_PATH */ + +#endif /* INSTANCE_EXPECT_H */ \ No newline at end of file diff --git a/program/include/structures/concrete/state.h b/program/include/structures/concrete/state.h index 3756378..b554d08 100644 --- a/program/include/structures/concrete/state.h +++ b/program/include/structures/concrete/state.h @@ -11,8 +11,8 @@ typedef uint8_t ksize_t; #define MAX_BLOCK_VALUES 9 -#define FULL_STATE 0b111111111 -#define INVALID_STATE 0b000000000 +#define FULL_STATE 0x1FF // 0b111111111 +#define INVALID_STATE 0x000 // 0b000000000 typedef uint16_t state_t; diff --git a/program/source/algorithms/arc_consistency.c b/program/source/algorithms/arc_consistency.c index aba8eb6..90ea3b0 100644 --- a/program/source/algorithms/arc_consistency.c +++ b/program/source/algorithms/arc_consistency.c @@ -1,5 +1,4 @@ #include -#include #include #include @@ -7,6 +6,7 @@ #include #include #include +#include #define STACK_DATA_TYPE ksize_t #include @@ -27,8 +27,12 @@ bool _reduce_no_col_combination(Kakuro board, SArray * current_state, ksize_t i bool _reduce_no_row_combination(Kakuro board, SArray * current_state, ksize_t index); bool look_ahead(Kakuro board, SArray * current_state) { - assert(current_state && "CURRENT STATE ARRAY IS NULL"); - if (!(get_settings_singleton()->is_arc_consistency)) return true; + error_mode = ASSERT_E; + expect(current_state, NO_ACTION, "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 checks[KAKURO_SIZE_MAX] = { 0 }; diff --git a/program/source/algorithms/backtrack.c b/program/source/algorithms/backtrack.c index d873cab..95f4099 100644 --- a/program/source/algorithms/backtrack.c +++ b/program/source/algorithms/backtrack.c @@ -1,11 +1,11 @@ #include -#include #include #include #include #include #include +#include bool _backtrack_row_sum(Kakuro board, SArray current_state, size_t index); bool _backtrack_col_sum(Kakuro board, SArray current_state, size_t index); @@ -18,7 +18,9 @@ bool _backtrack_row_repeat(Kakuro board, SArray current_state, size_t index); bool _backtrack_col_repeat(Kakuro board, SArray current_state, size_t index); bool backtrack(Kakuro board, SArray current_state) { - if (!(get_settings_singleton()->is_backtrack)) return _backtrack_valid_sums(board, current_state); + error_mode = DEFAULT_E; + expect(get_settings_singleton()->is_backtrack, return _backtrack_valid_sums(board, current_state), "WARNING: backtracking is off"); + get_stat_singleton()->backtrack_call_count++; Check checks[KAKURO_SIZE_MAX] = { 0 }; @@ -74,7 +76,8 @@ bool _backtrack_col_sum(Kakuro board, SArray current_state, size_t index) { } bool _backtrack_valid_sums(Kakuro board, SArray current_state) { - if (!is_end_state(current_state)) return false; + error_mode = DEFAULT_E; + expect(is_end_state(current_state), return false, "current state is not an end state"); Check checks[KAKURO_SIZE_MAX] = { 0 }; diff --git a/program/source/algorithms/forward_checking.c b/program/source/algorithms/forward_checking.c index f6e3d91..ecc8f88 100644 --- a/program/source/algorithms/forward_checking.c +++ b/program/source/algorithms/forward_checking.c @@ -1,24 +1,21 @@ #include -#include #include #include #include +#include bool _row_forward_check(Kakuro board, SArray * current_state, ksize_t index); bool _col_forward_check(Kakuro board, SArray * current_state, ksize_t index); bool forward_checking(Kakuro board, SArray * current_state, ksize_t index) { - expect( - current_state, - assert(current_state), - "current state parameter is NULL (%p)", current_state - ); - expect( - get_settings_singleton()->is_forward_check, - return true, - "" - ); + 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); + expect(index < current_state->size, NO_ACTION, "index '%u' is out of bounds of current state size '%u'", index, current_state->size); + + error_mode = DEFAULT_E; + expect(get_settings_singleton()->is_forward_check, return true, "WARNING: forward checking is off"); get_stat_singleton()->forward_check_call_count++; @@ -28,18 +25,7 @@ bool forward_checking(Kakuro board, SArray * current_state, ksize_t index) { )); } -bool _row_forward_check(Kakuro board, SArray * current_state, ksize_t index) { - mode = ASSERT_E; - expect( - index < current_state->size, NO_ACTION, - "index '%u' is out of bounds of current state size '%u'", index, current_state->size - ); - expect( - is_one_value(current_state->elements[index]), NO_ACTION, - "current state element at index %u is not a one value", index - ); - mode = DEFAULT_E; - +bool _row_forward_check(Kakuro board, SArray * current_state, ksize_t index) { state_t s = current_state->elements[index]; ksize_t row = board.coords[ROW][index], col = board.coords[COLUMN][index], c; @@ -59,13 +45,6 @@ bool _row_forward_check(Kakuro board, SArray * current_state, ksize_t index) { } bool _col_forward_check(Kakuro board, SArray * current_state, ksize_t index) { - expect(index < current_state->size, assert(index < current_state->size), - "index '%u' is out of bounds of current state size '%u'", index, current_state->size - ); - expect(is_one_value(current_state->elements[index]), assert(is_one_value(current_state->elements[index])), - "current state element at index '%u' is not a one value", index - ); - state_t s = current_state->elements[index]; ksize_t row = board.coords[ROW][index], col = board.coords[COLUMN][index], r; diff --git a/program/source/structures/concrete/state.c b/program/source/structures/concrete/state.c index 7cfce6d..6865499 100644 --- a/program/source/structures/concrete/state.c +++ b/program/source/structures/concrete/state.c @@ -3,17 +3,22 @@ #include #include +#include SArray create_state_array(ksize_t size) { - assert(size != 0 && "SIZE OF ARRAY CAN'T BE ZERO"); + error_mode = ASSERT_E; + expect(size != 0, NO_ACTION, "state array size can't be zero (%u)", size); - SArray sa = { .elements = malloc(sizeof(state_t) * size), .size = size, }; - assert(sa.elements && "MEMORY ALLOCATION FAILED"); + SArray 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 sa; + return array; } void set_full_state_array(SArray * array) { + error_mode = ASSERT_E; + expect(array, NO_ACTION, "state array pointer is NULL (%p)", (void*)array); + for (ksize_t i = 0; i < array->size; i++) array->elements[i] = FULL_STATE; } @@ -40,6 +45,9 @@ state_t merge_state_array(SArray array) { } void destroy_state_array(SArray * array) { + error_mode = ASSERT_E; + expect(array, NO_ACTION, "state array pointer is NULL (%p)", (void*)array); + free(array->elements); array->elements = NULL; array->size = 0;