From 7e227b0aa00c66d5d407751cac715e61e0c1c373 Mon Sep 17 00:00:00 2001 From: cyan Date: Tue, 10 Mar 2026 20:23:19 +0100 Subject: Cleanup and fixes (#180) --- tests/c/src/main.c | 12 ++++++++++ tests/c/src/utils.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/c/src/utils.h | 8 +++++++ 3 files changed, 85 insertions(+) create mode 100644 tests/c/src/main.c create mode 100644 tests/c/src/utils.c create mode 100644 tests/c/src/utils.h (limited to 'tests/c/src') diff --git a/tests/c/src/main.c b/tests/c/src/main.c new file mode 100644 index 0000000..0ae418f --- /dev/null +++ b/tests/c/src/main.c @@ -0,0 +1,12 @@ +#include +#include +#include +#include "../tests/all.h" + + +int main() { + monero_test_creation(); + monero_test_restore(); + printf("All tests passed\n"); + return 0; +} diff --git a/tests/c/src/utils.c b/tests/c/src/utils.c new file mode 100644 index 0000000..9816075 --- /dev/null +++ b/tests/c/src/utils.c @@ -0,0 +1,65 @@ +#include "utils.h" +#include +#include +#include +#include + +char* u_mktemp() { + char pattern[] = "/tmp/monero_c_test_XXXXXXXXX"; + + char *template = malloc(strlen(pattern) + 1); + if (!template) { + perror("malloc failed"); + exit(1); + } + + strcpy(template, pattern); + + if (mkdtemp(template) == NULL) { + perror("mkdtemp failed"); + free(template); + exit(1); + } + + return template; +} + +char* u_cat(const char* a, const char* b) { + size_t len_a = strlen(a); + size_t len_b = strlen(b); + + char* result = malloc(len_a + len_b + 1); + if (!result) { + perror("malloc failed"); + exit(1); + } + + memcpy(result, a, len_a); + memcpy(result + len_a, b, len_b + 1); // include null terminator + + return result; +} + +int u_wordcount(const char* str) { + if (!str) return 0; + + int count = 0; + int in_word = 0; + + for (size_t i = 0; str[i]; i++) { + // Japan smh ( = 0xE3 0x80 0x80) + int is_space = (str[i] == ' ') || + ((unsigned char)str[i] == 0xE3 && + (unsigned char)str[i+1] == 0x80 && + (unsigned char)str[i+2] == 0x80); + + if (is_space) { + in_word = 0; + } else if (!in_word) { + in_word = 1; + count++; + } + } + + return count; +} diff --git a/tests/c/src/utils.h b/tests/c/src/utils.h new file mode 100644 index 0000000..dd99556 --- /dev/null +++ b/tests/c/src/utils.h @@ -0,0 +1,8 @@ +#ifndef UTILS_H +#define UTILS_H + +char* u_mktemp(); +char* u_cat(const char* a, const char* b); +int u_wordcount(const char* str); + +#endif // UTILS_H -- cgit v1.2.3