summaryrefslogtreecommitdiff
path: root/src/uci/state.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/uci/state.c')
-rw-r--r--src/uci/state.c156
1 files changed, 155 insertions, 1 deletions
diff --git a/src/uci/state.c b/src/uci/state.c
index 183f849..e108492 100644
--- a/src/uci/state.c
+++ b/src/uci/state.c
@@ -1,3 +1,4 @@
+#include <stdlib.h>
#include <string.h>
#include "uci/state.h"
@@ -180,7 +181,16 @@ void uci_state_init(uci_state_t *state) {
);
}
-void uci_state_deinit(uci_state_t *state) {}
+void uci_state_deinit(uci_state_t *state) {
+ int settings_count = sizeof(state->option_settings)/sizeof(option_setting_t);
+ for (int i = 0; i < settings_count; i++) {
+ option_setting_t *s = &state->option_settings[i];
+ if (s->type == OPTION_STRING && s->value.string && s->value.string->data) {
+ free(s->value.string->data);
+ s->value.string->data = NULL;
+ }
+ }
+}
void apply_option(uci_state_t *state, char *name, char *buffer) {
int settings_count = sizeof(state->option_settings)/sizeof(option_setting_t);
@@ -218,3 +228,147 @@ void apply_option(uci_state_t *state, char *name, char *buffer) {
}
}
+#ifdef TEST_MOD
+#include <stdbool.h>
+
+bool test_uci_state_init_defaults() {
+ uci_state_t state;
+ uci_state_init(&state);
+
+ bool ok = true;
+ ok &= state.signal == UCI_SIGNAL_NONE;
+ ok &= state.current_state == UCI_STATE_INITIAL;
+ ok &= state.threads == 1;
+ ok &= state.hash == 256;
+ ok &= state.clear_hash == false;
+ ok &= state.uci_showcurrline == false;
+ ok &= state.uci_showrefutations == false;
+ ok &= state.uci_limitstrength == false;
+ ok &= state.uci_elo == 800;
+ ok &= state.uci_analysemode == false;
+ ok &= strcmp(state.name, "Gacrux") == 0;
+ ok &= strcmp(state.author, "Aargh Rai <aarghrai.com>") == 0;
+
+ uci_state_deinit(&state);
+ return ok;
+}
+
+bool test_apply_option_spin_valid() {
+ uci_state_t state;
+ uci_state_init(&state);
+
+ apply_option(&state, "Threads", "4");
+ bool ok = state.threads == 4;
+
+ apply_option(&state, "Hash", "128");
+ ok &= state.hash == 128;
+
+ uci_state_deinit(&state);
+ return ok;
+}
+
+bool test_apply_option_spin_below_min() {
+ uci_state_t state;
+ uci_state_init(&state);
+
+ apply_option(&state, "Threads", "0");
+ bool ok = state.threads == 1;
+
+ uci_state_deinit(&state);
+ return ok;
+}
+
+bool test_apply_option_spin_above_max() {
+ uci_state_t state;
+ uci_state_init(&state);
+
+ apply_option(&state, "Threads", "100");
+ bool ok = state.threads == 1;
+
+ uci_state_deinit(&state);
+ return ok;
+}
+
+bool test_apply_option_check() {
+ uci_state_t state;
+ uci_state_init(&state);
+
+ apply_option(&state, "UCI_AnalyseMode", "true");
+ bool ok = state.uci_analysemode == true;
+
+ apply_option(&state, "UCI_AnalyseMode", "false");
+ ok &= state.uci_analysemode == false;
+
+ uci_state_deinit(&state);
+ return ok;
+}
+
+bool test_apply_option_button() {
+ uci_state_t state;
+ uci_state_init(&state);
+
+ apply_option(&state, "Clear Hash", "");
+ bool ok = state.clear_hash == true;
+
+ uci_state_deinit(&state);
+ return ok;
+}
+
+bool test_apply_option_string() {
+ uci_state_t state;
+ uci_state_init(&state);
+
+ apply_option(&state, "UCI_Opponent", "Carlsen");
+ bool ok = true;
+ ok &= state.uci_opponent.length == 7;
+ ok &= memcmp(state.uci_opponent.data, "Carlsen", 7) == 0;
+
+ uci_state_deinit(&state);
+ return ok;
+}
+
+bool test_apply_option_unknown_ignored() {
+ uci_state_t state;
+ uci_state_init(&state);
+ state.debug = false;
+
+ apply_option(&state, "Nonexistent", "42");
+ bool ok = true;
+
+ uci_state_deinit(&state);
+ return ok;
+}
+
+bool test_option_settings_count() {
+ uci_state_t state;
+ uci_state_init(&state);
+
+ int count = sizeof(state.option_settings) / sizeof(option_setting_t);
+ bool ok = count == 11;
+
+ uci_state_deinit(&state);
+ return ok;
+}
+
+bool test_option_settings_types() {
+ uci_state_t state;
+ uci_state_init(&state);
+
+ bool ok = true;
+ ok &= state.option_settings[0].type == OPTION_SPIN; // Threads
+ ok &= state.option_settings[1].type == OPTION_SPIN; // Hash
+ ok &= state.option_settings[2].type == OPTION_BUTTON; // Clear Hash
+ ok &= state.option_settings[3].type == OPTION_CHECK; // UCI_ShowCurrLine
+ ok &= state.option_settings[4].type == OPTION_CHECK; // UCI_ShowRefutations
+ ok &= state.option_settings[5].type == OPTION_CHECK; // UCI_LimitStrength
+ ok &= state.option_settings[6].type == OPTION_SPIN; // UCI_Elo
+ ok &= state.option_settings[7].type == OPTION_CHECK; // UCI_AnalyseMode
+ ok &= state.option_settings[8].type == OPTION_STRING; // UCI_Opponent
+ ok &= state.option_settings[9].type == OPTION_STRING; // UCI_EngineAbout
+ ok &= state.option_settings[10].type == OPTION_STRING;// UCI_SetPositionValue
+
+ uci_state_deinit(&state);
+ return ok;
+}
+#endif
+