Skip to content

Commit

Permalink
added new stuff to expect.h
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGAzed committed Jun 25, 2024
1 parent 87ff335 commit 15076a2
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 73 deletions.
65 changes: 34 additions & 31 deletions program/include/instance/expect.h
Original file line number Diff line number Diff line change
@@ -1,48 +1,51 @@
#ifndef INSTANCE_EXPECT_H
#define INSTANCE_EXPECT_H

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

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 */
#endif /* ERROR_LOG_FILE_PATH */

#endif /* INSTANCE_EXPECT_H */
4 changes: 2 additions & 2 deletions program/include/structures/concrete/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
10 changes: 7 additions & 3 deletions program/source/algorithms/arc_consistency.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>

#include <algorithms/arc_consistency.h>
#include <structures/concrete/board.h>
#include <instance/settings.h>
#include <instance/statistics.h>
#include <instance/expect.h>

#define STACK_DATA_TYPE ksize_t
#include <structures/abstract/stack.h>
Expand All @@ -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 };
Expand Down
9 changes: 6 additions & 3 deletions program/source/algorithms/backtrack.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#include <structures/concrete/state.h>
#include <algorithms/backtrack.h>
#include <instance/settings.h>
#include <instance/statistics.h>
#include <instance/expect.h>

bool _backtrack_row_sum(Kakuro board, SArray current_state, size_t index);
bool _backtrack_col_sum(Kakuro board, SArray current_state, size_t index);
Expand All @@ -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 };
Expand Down Expand Up @@ -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 };

Expand Down
39 changes: 9 additions & 30 deletions program/source/algorithms/forward_checking.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
#include <assert.h>
#include <instance/expect.h>

#include <algorithms/forward_checking.h>
#include <instance/settings.h>
#include <instance/statistics.h>
#include <instance/expect.h>

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++;

Expand All @@ -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;

Expand All @@ -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;

Expand Down
16 changes: 12 additions & 4 deletions program/source/structures/concrete/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
#include <stdio.h>

#include <structures/concrete/state.h>
#include <instance/expect.h>

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

Expand All @@ -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;
Expand Down

0 comments on commit 15076a2

Please sign in to comment.