root/lang/objective-cplusplus/i3/trunk/src/tests/TestNLS.cc @ 37684

Revision 37684, 6.2 kB (checked in by saturday06, 3 years ago)

まじ人生終わってるよ.....

Line 
1#include <mil/PrecompiledHeaders.h>
2#include <algorithm>
3#include <boost/range.hpp>
4
5#include <mil/MscCrt.h>
6#include "Common.h"
7#include "Mediator.h"
8#include <quicktest/quicktest.h>
9
10#ifdef ENABLE_NLS
11#include <iconv.h>
12#endif
13
14using namespace std;
15using namespace boost;
16using namespace i3;
17
18namespace test_nls {
19
20#ifdef UNICODE
21QT_TEST(iconv_ascii_to_utf16le) {
22    iconv_t cd = iconv_open("UTF-16LE", "ASCII");
23    if (cd == (iconv_t)-1) {
24        QT_CHECK(0 && (int)"iconv_open() failed");
25        return;
26    }
27
28    char ascii[] = "ABC+++";
29    size_t ascii_bytes = sizeof(ascii) - 1;
30    char* ascii_pointer = ascii;
31
32    char utf16le[30] = {};
33    size_t utf16le_bytes = sizeof(utf16le) - 2;
34    char* utf16le_pointer = utf16le;
35
36    iconv(cd,
37          (ICONV_CONST char**)&ascii_pointer, &ascii_bytes,
38          (char**)&utf16le_pointer, &utf16le_bytes);
39
40    QT_CHECK(utf16le[0] == 0x41);
41    QT_CHECK(utf16le[1] == 0);
42    QT_CHECK(utf16le[2] == 0x42);
43    QT_CHECK(utf16le[3] == 0);
44    QT_CHECK(utf16le[4] == 0x43);
45    QT_CHECK(utf16le[5] == 0);
46
47    iconv_close(cd);
48}
49QT_TEST(wgettext_ascii7bit_to_utf16le) {
50    const char* buf = (const char*)wgettext("DEF+++");
51    QT_CHECK(buf[0] == 0x44);
52    QT_CHECK(buf[1] == 0);
53    QT_CHECK(buf[2] == 0x45);
54    QT_CHECK(buf[3] == 0);
55    QT_CHECK(buf[4] == 0x46);
56    QT_CHECK(buf[5] == 0);
57}
58#endif
59
60#ifdef ENABLE_NLS
61void dump_string(const TCHAR* str) {
62    int bytes = _tcslen(str) * sizeof(TCHAR);
63    if (bytes >= 50) {
64        bytes = 50;
65    }
66    printf("\"");
67    for (int i = 0; i < bytes; i++) {
68        unsigned char c = ((unsigned char*)str)[i];
69        //if (isprint(c)) {
70        if (isalnum(c)) {
71            printf("%c", c);
72        } else {
73            printf(" ");
74        }
75    }       
76    printf("\" {");
77    for (int i = 0; i < bytes; i++) {
78        if (i != 0) {
79            printf(",");
80        }
81        unsigned char c = ((unsigned char*)str)[i];
82        printf("%02x", c);
83    }
84    printf("}");
85}
86
87#define check_translation(key, expected) check_translation_(key, _(key), expected);
88
89void check_translation_(const char* key, const TCHAR* got, const TCHAR* expected) {
90    const int max_bytes = _tcslen(expected) * sizeof(TCHAR);
91    bool is_translated = memcmp(got, expected, max_bytes) == 0; // XXX danger !
92   
93    QT_CHECK(is_translated);
94    if (is_translated) {
95        return;
96    }
97
98    printf("    expected = ");
99    dump_string(expected);
100    printf("\n");
101    printf("    got      = ");
102    dump_string(got);
103    printf("\n");
104}
105
106/** Cygwin distcheck ok
107const char* LOCALE_ENV_LIST[] = {
108        "LC_ALL",
109#if MIL_OS_UNIX
110        "LANG",
111        "LANGUAGE",
112#endif
113#if 0
114        "LC_ADDRESS",
115        "LC_COLLATE",
116        "LC_CTYPE",
117        "LC_IDENTIFICATION",
118        "LC_MEASUREMENT",
119        "LC_MESSAGES",
120        "LC_MONETARY",
121        "LC_NAME",
122        "LC_NUMERIC",
123        "LC_PAPER",
124        "LC_TELEPHONE",
125        "LC_TIME",
126#endif
127        0
128    };
129int set_locale(const char* locale) {
130    for (unsigned int i = 0; LOCALE_ENV_LIST[i]; i++) {
131        my_setenv(LOCALE_ENV_LIST[i], locale);
132    }
133
134#ifdef MIL_OS_UNIX
135    setlocale(LC_ALL, locale);
136    textdomain(PACKAGE_NAME);
137#endif
138
139    return 0;
140}
141*/
142
143/** Cygwin distcheck ok 2
144const char* LOCALE_ENV_LIST[] = {
145        "LC_ALL",
146        "LANG",
147        "LANGUAGE",
148        0
149    };
150int set_locale(const char* locale) {
151    for (unsigned int i = 0; LOCALE_ENV_LIST[i]; i++) {
152        my_setenv(LOCALE_ENV_LIST[i], locale);
153    }
154    return 0;
155}
156*/
157
158const char* LOCALE_ENV_LIST[] = {
159        "LC_ALL",
160        "LANG",
161        "LANGUAGE",
162        0
163    };
164
165int set_locale(const char* locale) {
166    for (unsigned int i = 0; LOCALE_ENV_LIST[i]; i++) {
167        my_setenv(LOCALE_ENV_LIST[i], locale);
168    }
169    //if (setlocale(LC_ALL, locale) == -1) {
170    //    return -1;
171    //}
172    setlocale(LC_ALL, "");
173    //textdomain(PACKAGE_NAME);
174    return 0;
175}
176
177void print_locale_env() {
178    for (unsigned int i = 0; LOCALE_ENV_LIST[i]; i++) {
179        printf("%s = %s\n", LOCALE_ENV_LIST[i], getenv(LOCALE_ENV_LIST[i]));
180    }
181}
182
183QT_TEST(translation) {
184    using namespace std;
185
186    bool result = false;
187#if UNICODE
188    // UTF-16LE
189    WCHAR hello_world_ja1[] = {
190        0x4E16, 0x754C, 0x3088, 0x002C,
191        0x0020, 0x3053, 0x3093, 0x306B,
192        0x3061, 0x306F, 0x0021, 0
193    };
194    char hello_world_ja2[] = {
195        0x16, 0x4e, 0x4c, 0x75, 0x88, 0x30, 0x01, 0x30,
196        0x53, 0x30, 0x93, 0x30, 0x6b, 0x30, 0x61, 0x30,
197        0x8f, 0x30, 0x28, 0x00, 0x60, 0x00, 0xfb, 0x30,
198        0xc9, 0x03, 0xfb, 0x30, 0xb4, 0x00, 0x29, 0x00,
199        0, 0
200    };
201    WCHAR* hello_world_ja = (WCHAR*)hello_world_ja2;
202#else
203    // UTF-8
204    char hello_world_ja[] = {
205        0xe4, 0xb8, 0x96, 0xe7, 0x95, 0x8c, 0xe3, 0x82,
206        0x88, 0xe3, 0x80, 0x81, 0xe3, 0x81, 0x93, 0xe3,
207        0x82, 0x93, 0xe3, 0x81, 0xab, 0xe3, 0x81, 0xa1,
208        0xe3, 0x82, 0x8f, 0x28, 0x60, 0xe3, 0x83, 0xbb,
209        0xcf, 0x89, 0xe3, 0x83, 0xbb, 0xc2, 0xb4, 0x29, 0
210    };
211#endif
212
213    int argc = 0;
214    char** argv = NULL;
215
216    QT_CHECK(!init_common_global_data(argc, argv));
217    QT_CHECK(!init_os_global_data(argc, argv));
218    QT_CHECK(!init_gui_global_data(argc, argv));
219
220#ifdef MIL_OS_UNIX
221    {
222        char cwd[mil::STACKABLE_MAX_PATH] = "";
223        if (getcwd(cwd, _countof(cwd)) == NULL) {
224            cerr << "getcwd() failed" << endl;
225            QT_CHECK(false);
226        }
227        std::string test_localedir = cwd;
228        test_localedir += "/locale";
229        struct stat s;
230        if (stat(test_localedir.c_str(), &s) == 0) {
231            bindtextdomain(PACKAGE_NAME, test_localedir.c_str());
232        }
233    }
234#endif
235
236    QT_CHECK_NOT_EQUAL(set_locale("da_DK"), -1);
237    check_translation("Hello, world!", _T("Hej verden!"));
238
239#if MIL_OS_UNIX || defined(UNICODE)
240    QT_CHECK_NOT_EQUAL(set_locale("ja_JP"), -1);
241    check_translation("Hello, world!", hello_world_ja);
242#endif
243
244    QT_CHECK_NOT_EQUAL(set_locale("lv_LV"), -1);
245    check_translation("Hello, world!", _T("Sveika, pasaule!"));
246}
247
248#endif
249
250}
Note: See TracBrowser for help on using the browser.