summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAargh Rai <aargh.rai+git@gmail.com>2026-07-10 16:01:57 +0530
committerAargh Rai <aargh.rai+git@gmail.com>2026-07-10 16:01:57 +0530
commita14da2fe3864712dab4e7c4cfb5c323026d668de (patch)
tree418c8c8ff44a611cbe1c1269b10926527046e232 /tests
parent2761f8a533e2b025f209222a1e4ec70b91c7e8ee (diff)
moving header files to include directory & moving resources in it's own directory
i know currently there is race condition, but the code is getting too messy, i will continue to this after i make a vis tool to analyse how i should split up files & stuff the code rn needs intense restructing for it to make any more progress
Diffstat (limited to 'tests')
-rw-r--r--tests/find_tests.c218
-rw-r--r--tests/generated.c25
-rw-r--r--tests/main.c2
3 files changed, 244 insertions, 1 deletions
diff --git a/tests/find_tests.c b/tests/find_tests.c
new file mode 100644
index 0000000..a0b9f63
--- /dev/null
+++ b/tests/find_tests.c
@@ -0,0 +1,218 @@
+#include <dirent.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#define MAX_NUMBER_OF_TESTS_PER_FILE 100
+#define MAX_LENGTH_OF_TEST_NAME 100
+
+typedef enum {
+ path_type_unknown,
+ path_type_directory,
+ path_type_file
+} path_type;
+
+int check_path_type(const char* path) {
+ struct stat path_stat;
+ if (stat(path, &path_stat) != 0) return path_type_unknown;
+ if (S_ISDIR(path_stat.st_mode)) return path_type_directory;
+ return path_type_file;
+}
+
+void handle_input_file(
+ const char* input_path,
+ int path_strip_index,
+ void (*handle)(const char*, int, FILE*)
+) {
+ FILE* input_fd = fopen(input_path, "rb");
+ handle(input_path, path_strip_index, input_fd);
+ fclose(input_fd);
+}
+
+void push(char* directories, int cursor, const char* to_add) {
+ int i = 0;
+ for (; to_add[i] != 0; i++) {
+ directories[cursor * PATH_MAX + i] = to_add[i];
+ }
+ while (i < PATH_MAX) directories[cursor * PATH_MAX + i++] = 0;
+}
+
+void handle_input_directory(
+ const char* input_path,
+ void (*handle)(const char*, int, FILE*)
+) {
+ int cursor = 0;
+ char* directories = malloc(PATH_MAX * PATH_MAX * sizeof(*directories));
+ push(directories, cursor, input_path);
+ int input_shift = strlen(input_path) + 1;
+ while (cursor != -1) {
+ char* path = &directories[(cursor--) * PATH_MAX];
+ DIR* dir_ptr = opendir(path);
+ if (dir_ptr == NULL) {
+ fprintf(stderr, "Couldn't open the directory: %s\n", path);
+ continue;
+ }
+
+ struct dirent* entry_ptr;
+ char full_path[PATH_MAX];
+ int i = 0;
+ for (; path[i] != 0; i++) {
+ full_path[i] = path[i];
+ }
+ full_path[i++] = '/';
+ while ((entry_ptr = readdir(dir_ptr)) != NULL) {
+ if (
+ strcmp(entry_ptr->d_name, ".") == 0 ||
+ strcmp(entry_ptr->d_name, "..") == 0
+ ) continue;
+ int j = 0;
+ for (; entry_ptr->d_name[j] != 0; j++) {
+ full_path[i + j] = entry_ptr->d_name[j];
+ }
+ full_path[i + j] = 0;
+
+ if (entry_ptr->d_type == DT_REG) {
+ handle_input_file(full_path, input_shift, handle);
+ continue;
+ }
+ push(directories, ++cursor, full_path);
+ }
+ closedir(dir_ptr);
+ }
+ free(directories);
+}
+
+void handle_input(
+ const char* input_path,
+ int input_type,
+ void (*handle)(const char*, int, FILE*)
+) {
+ if (input_type == path_type_file) {
+ handle_input_file(input_path, 0, handle);
+ } else {
+ handle_input_directory(input_path, handle);
+ return;
+ }
+}
+
+struct file_tests {
+ char path[PATH_MAX];
+ int test_count;
+ char test_names[MAX_NUMBER_OF_TESTS_PER_FILE][MAX_LENGTH_OF_TEST_NAME];
+};
+struct tests {
+ int count;
+ int capacity;
+ struct file_tests* items;
+};
+
+struct tests tests;
+
+int max(int a, int b) {
+ if (a > b) return a;
+ return b;
+}
+
+void append_tests(struct file_tests file_tests) {
+ if (tests.count + 1 > tests.capacity) {
+ tests.capacity = max(32, tests.count) + tests.count;
+ tests.items = realloc(tests.items, tests.capacity * sizeof(*tests.items));
+ }
+
+ tests.items[tests.count++] = file_tests;
+}
+
+void handle_fn(const char* input_path, int path_strip_index, FILE* file) {
+ if (input_path[strlen(input_path) - 1] != 'c') return;
+ char ch;
+ char match[] = "\nbool test_";
+ int match_len = strlen(match);
+ int i = 0;
+ char output[MAX_NUMBER_OF_TESTS_PER_FILE][MAX_LENGTH_OF_TEST_NAME];
+ int k = 0; int kk = 0;
+ enum { search_mode, read_test_name };
+ int mode = search_mode;
+ while ((ch = fgetc(file)) != EOF) {
+ if (mode == search_mode) {
+ if (match[i] == ch) i++;
+ else i = match[0] == ch;
+
+ if (i == match_len) {
+ mode = read_test_name;
+ kk = 0;
+ }
+ } else if (mode == read_test_name) {
+ if (ch == '(') {
+ mode = search_mode;
+ k++;
+ continue;
+ }
+
+ output[k][kk++] = ch;
+ output[k][kk] = 0;
+ }
+ }
+ struct file_tests tests = { .test_count = k };
+ memcpy(tests.path, input_path, strlen(input_path) + 1);
+ memcpy(tests.test_names, output, sizeof(output));
+ append_tests(tests);
+}
+
+int main() {
+ if (fork() == 0) {
+ char* args[] = {"mkdir", "-p", "tests/generated", NULL};
+ execvp(args[0], args);
+ }
+ wait(NULL);
+
+ tests.items = malloc(sizeof(*tests.items) * 100);
+ tests.capacity = 100;
+ tests.count = 0;
+
+ handle_input("src", path_type_directory, handle_fn);
+
+ FILE* f = fopen("tests/generated.c", "w");
+
+ int total_test_count = 0;
+ for (int i = 0; i < tests.count; i++) {
+ struct file_tests item = tests.items[i];
+ if (item.test_count == 0) continue;
+ total_test_count += item.test_count;
+ fprintf(f, "#include \"../%s\"\n", item.path);
+ }
+ fprintf(
+ f,
+ "\nint total_test_count = %d;\nbool (*tests[%d])(void) = {\n",
+ total_test_count,
+ total_test_count
+ );
+ int max_test_name_size = 0;
+ for (int i = 0; i < tests.count; i++) {
+ struct file_tests item = tests.items[i];
+ printf("%s\n", item.path);
+ if (item.test_count == 0) continue;
+ for (int j = 0; j < item.test_count; j++) {
+ printf("\t%s\n", item.test_names[j]);
+ fprintf(f, " test_%s,\n", item.test_names[j]);
+ int test_name_size = strlen(item.test_names[j]);
+ if (max_test_name_size < test_name_size) {
+ max_test_name_size = test_name_size;
+ }
+ }
+ }
+ fprintf(f, "};\n");
+ fprintf(f, "\nint max_test_name_size = %d;", max_test_name_size);
+ fprintf(f, "\nchar test_names[%d][100] = {\n", total_test_count);
+ for (int i = 0; i < tests.count; i++) {
+ struct file_tests item = tests.items[i];
+ if (item.test_count == 0) continue;
+ for (int j = 0; j < item.test_count; j++) {
+ fprintf(f, " \"%s\",\n", item.test_names[j]);
+ }
+ }
+ fprintf(f, "};\n");
+ fclose(f);
+}
diff --git a/tests/generated.c b/tests/generated.c
new file mode 100644
index 0000000..777f8b1
--- /dev/null
+++ b/tests/generated.c
@@ -0,0 +1,25 @@
+#include "../src/engine/fen.c"
+#include "../src/engine/moves/king.c"
+#include "../src/uci/command.c"
+
+int total_test_count = 7;
+bool (*tests[7])(void) = {
+ test_fen_no_passant,
+ test_fen_passant,
+ test_starting_position,
+ test_white_king_corners,
+ test_black_king_corners,
+ test_ucicmd,
+ test_invalid_cmd_ucicmd,
+};
+
+int max_test_name_size = 18;
+char test_names[7][100] = {
+ "fen_no_passant",
+ "fen_passant",
+ "starting_position",
+ "white_king_corners",
+ "black_king_corners",
+ "ucicmd",
+ "invalid_cmd_ucicmd",
+};
diff --git a/tests/main.c b/tests/main.c
index df802c2..191f0dd 100644
--- a/tests/main.c
+++ b/tests/main.c
@@ -3,7 +3,7 @@
#include <time.h>
#define TEST_MOD
-#include "../generated/tests.c"
+#include "generated.c"
double diff_time(struct timespec a, struct timespec b) {
double a_time = (double)a.tv_sec + a.tv_nsec * 1e-9;