aboutsummaryrefslogtreecommitdiff
path: root/tools/test_api.c
blob: 18f68b6696e5776f5fd3f4ee0db280b0f4cdbcc0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "langdet.h"

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef USE_OPENBLAS
#include <cblas.h>
#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;
}