#include "langdet.h" #include #include #include #include #ifdef USE_OPENBLAS #include #endif #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]); #ifdef USE_OPENBLAS CHECK(openblas_get_num_threads() == 1); #endif 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]); #ifdef USE_OPENBLAS CHECK(openblas_get_num_threads() == 4); #endif CHECK(langdet_predict(first, english, &result, error, sizeof(error)) == 0); #ifdef USE_OPENBLAS CHECK(openblas_get_num_threads() == 1); #endif CHECK(result.score == saved.score); langdet_free(first); CHECK(langdet_predict(second, english, &result, error, sizeof(error)) == 0); #ifdef USE_OPENBLAS CHECK(openblas_get_num_threads() == 4); #endif CHECK(result.score == saved.score); langdet_free(second); puts("PASS: public API, repeated predictions, result ownership, truncation, errors, and lifecycle."); return 0; }