Skip to content

Commit

Permalink
added 'expect' assertion alternative
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGAzed committed Jun 24, 2024
1 parent 4025032 commit 87ff335
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 6 deletions.
48 changes: 48 additions & 0 deletions program/include/instance/expect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

typedef enum error_mode {
DEFAULT_E,
ABORT_E,
ASSERT_E,
} error_mode_e;

error_mode_e mode = DEFAULT_E;
FILE * error_log;

#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; } \
} \
} \
}

#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; } \
} \
} \
}

#endif /* ERROR_LOG_FILE_PATH */
1 change: 0 additions & 1 deletion program/source/algorithms/depth_first_search.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <string.h>
#include <sys/types.h>
#include <assert.h>
#include <stdlib.h>

#include <algorithms/depth_first_search.h>
Expand Down
33 changes: 28 additions & 5 deletions program/source/algorithms/forward_checking.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <assert.h>
#include <instance/expect.h>

#include <algorithms/forward_checking.h>
#include <instance/settings.h>
Expand All @@ -8,7 +9,17 @@ 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) {
if (!(get_settings_singleton()->is_forward_check)) return true;
expect(
current_state,
assert(current_state),
"current state parameter is NULL (%p)", current_state
);
expect(
get_settings_singleton()->is_forward_check,
return true,
""
);

get_stat_singleton()->forward_check_call_count++;

return !invalid_state_forward_check_stat(!(
Expand All @@ -18,8 +29,16 @@ bool forward_checking(Kakuro board, SArray * current_state, ksize_t index) {
}

bool _row_forward_check(Kakuro board, SArray * current_state, ksize_t index) {
assert(index < current_state->size);
assert(is_one_value(current_state->elements[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;

state_t s = current_state->elements[index];
ksize_t row = board.coords[ROW][index], col = board.coords[COLUMN][index], c;
Expand All @@ -40,8 +59,12 @@ bool _row_forward_check(Kakuro board, SArray * current_state, ksize_t index) {
}

bool _col_forward_check(Kakuro board, SArray * current_state, ksize_t index) {
assert(index < current_state->size);
assert(is_one_value(current_state->elements[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;
Expand Down

0 comments on commit 87ff335

Please sign in to comment.