From 6f98fc8cf3f7cc4b9d523d16492a07d3e2aea8c9 Mon Sep 17 00:00:00 2001 From: Onur Gündüz Date: Tue, 29 Sep 2026 14:04:04 +0200 Subject: Add CPU reference comparison and repeatable API tests --- tools/test_api.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 tools/test_api.c (limited to 'tools/test_api.c') diff --git a/tools/test_api.c b/tools/test_api.c new file mode 100644 index 0000000..6923102 --- /dev/null +++ b/tools/test_api.c @@ -0,0 +1,91 @@ +#include "langdet.h" + +#include +#include +#include +#include + +#define CHECK(condition) do { \ + if (!(condition)) { \ + fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, #condition); \ + exit(1); \ + } \ +} while (0) + +static void check_scores(const langdet_result_t *result) { + CHECK(result->label); + float sum = 0; + int best = 0; + for (int i = 0; i < LANGDET_NUM_LABELS; i++) { + CHECK(result->labels[i]); + CHECK(isfinite(result->scores[i])); + CHECK(result->scores[i] >= 0 && result->scores[i] <= 1); + sum += result->scores[i]; + if (result->scores[i] > result->scores[best]) + best = i; + } + CHECK(fabsf(sum - 1) < 1e-5f); + CHECK(strcmp(result->label, result->labels[best]) == 0); + CHECK(result->score == result->scores[best]); +} + +int main(int argc, char **argv) { + CHECK(argc == 2); + const char *model_dir = argv[1]; + char error[256]; + CHECK(!langdet_open(NULL, 0, error, sizeof(error)) && error[0]); + CHECK(!langdet_open(model_dir, -1, error, sizeof(error)) && error[0]); + CHECK(!langdet_open(model_dir, 17, error, sizeof(error)) && error[0]); + langdet_free(NULL); + + langdet_t *first = langdet_open(model_dir, 1, error, sizeof(error)); + CHECK(first && !error[0]); + langdet_result_t result; + const char *english = "This is an English sentence."; + CHECK(langdet_predict(first, english, &result, error, sizeof(error)) == 0); + CHECK(!error[0] && strcmp(result.label, "en") == 0); + CHECK(!result.truncated); + check_scores(&result); + langdet_result_t saved = result; + + for (int i = 0; i < 3; i++) { + CHECK(langdet_predict(first, english, &result, NULL, 0) == 0); + CHECK(result.score == saved.score); + CHECK(result.tokens == saved.tokens); + CHECK(memcmp(result.scores, saved.scores, sizeof(result.scores)) == 0); + } + CHECK(langdet_predict(first, "Bugün hava çok güzel.", &result, error, sizeof(error)) == 0); + CHECK(strcmp(result.label, "tr") == 0 && strcmp(saved.label, "en") == 0); + check_scores(&saved); + + CHECK(langdet_predict(first, "", &result, error, sizeof(error)) == 0); + CHECK(result.tokens == 2 && !result.truncated); + check_scores(&result); + char long_text[3601]; + for (int i = 0; i < 600; i++) + memcpy(long_text + i * 6, "hello ", 6); + long_text[3600] = '\0'; + CHECK(langdet_predict(first, long_text, &result, error, sizeof(error)) == 0); + CHECK(result.tokens == LANGDET_MAX_TOKENS && result.truncated); + check_scores(&result); + + CHECK(langdet_predict(first, NULL, &result, error, sizeof(error)) == -1 && error[0]); + CHECK(!result.label && result.score == 0 && result.tokens == 0 && !result.truncated); + for (int i = 0; i < LANGDET_NUM_LABELS; i++) + CHECK(!result.labels[i] && result.scores[i] == 0); + CHECK(langdet_predict(first, english, NULL, error, sizeof(error)) == -1 && error[0]); + char tiny[2] = {'x', 'y'}; + CHECK(langdet_predict(NULL, english, &result, tiny, 1) == -1); + CHECK(tiny[0] == '\0' && tiny[1] == 'y'); + + langdet_t *second = langdet_open(model_dir, 4, error, sizeof(error)); + CHECK(second && !error[0]); + CHECK(langdet_predict(first, english, &result, error, sizeof(error)) == 0); + CHECK(result.score == saved.score); + langdet_free(first); + CHECK(langdet_predict(second, english, &result, error, sizeof(error)) == 0); + CHECK(result.score == saved.score); + langdet_free(second); + puts("PASS: public API, repeated predictions, result ownership, truncation, errors, and lifecycle."); + return 0; +} -- cgit v1.3.1