File: utf82locale.c

package info (click to toggle)
libisds 0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,348 kB
  • ctags: 1,659
  • sloc: ansic: 24,898; sh: 11,772; makefile: 393; xml: 375; sed: 16
file content (54 lines) | stat: -rw-r--r-- 1,463 bytes parent folder | download
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
#include "../test.h"
#include "utils.h"
#include <locale.h>

static int prepare_locale(_Bool utf8) {
    char *old_locale;

    if (utf8) {
        old_locale = setlocale(LC_ALL, "en_US.UTF-8");
        if (old_locale != NULL) return 0;

        old_locale = setlocale(LC_ALL, "cs_CZ.UTF-8");
        if (old_locale != NULL) return 0;
    } else {
        old_locale = setlocale(LC_ALL, "C");
        if (old_locale != NULL) return 0;
    }

    return -1;
}

static int test_utf82locale(const void *input, const void *correct) {
    void *output = NULL;

    output = _isds_utf82locale(input);
    TEST_DESTRUCTOR(free, output);

    TEST_STRING_DUPLICITY(correct, output);

    PASS_TEST;
}


int main(int argc, char **argv) {
    INIT_TEST("utf8locale");

    if (prepare_locale(1)) 
        ABORT_UNIT("Could not set any UTF-8 locale");

    TEST("NULL input", test_utf82locale, NULL, NULL);
    TEST("Empty string", test_utf82locale, "", "");
    TEST("ASCII text", test_utf82locale, "lazy fox", "lazy fox");
    TEST("non-ASCII text in UTF-8 locale", test_utf82locale, "Šíleně žluťoučký",
            "Šíleně žluťoučký");
    TEST("OTP message in UTF-8 locale", test_utf82locale,
            "Jednorázový kód odeslán.", "Jednorázový kód odeslán.");

    if (prepare_locale(0)) 
        ABORT_UNIT("Could not set C locale");

    TEST("non-ASCII text in C locale", test_utf82locale, "Šíleně žluťoučký", NULL);

    SUM_TEST();
}