• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 **********************************************************************
5 * Copyright (c) 2003-2009, International Business Machines
6 * Corporation and others.  All Rights Reserved.
7 **********************************************************************
8 * Author: Alan Liu
9 * Created: March 20 2003
10 * Since: ICU 2.6
11 **********************************************************************
12 */
13 #include "cintltst.h"
14 #include "cmemory.h"
15 #include "cstring.h"
16 #include "unicode/ucat.h"
17 #include "unicode/ustring.h"
18 
19 /**********************************************************************/
20 /* Prototypes */
21 
22 void TestMessageCatalog(void);
23 
24 /**********************************************************************/
25 /* Add our tests into the hierarchy */
26 
27 void addPosixTest(TestNode** root);
28 
addPosixTest(TestNode ** root)29 void addPosixTest(TestNode** root)
30 {
31 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
32     addTest(root, &TestMessageCatalog, "tsutil/cposxtst/TestMessageCatalog");
33 #endif
34 }
35 
36 /**********************************************************************/
37 /* Test basic ucat.h API */
38 
TestMessageCatalog(void)39 void TestMessageCatalog(void) {
40 
41     UErrorCode ec = U_ZERO_ERROR;
42     u_nl_catd catd;
43     const char* DATA[] = {
44         /* set_num, msg_num, expected string result, expected error code */
45         "1", "4", "Good morning.", "U_ZERO_ERROR",
46         "1", "5", "Good afternoon.", "U_ZERO_ERROR",
47         "1", "6", "FAIL", "U_MISSING_RESOURCE_ERROR",
48         "1", "7", "Good evening.", "U_ZERO_ERROR",
49         "1", "8", "Good night.", "U_ZERO_ERROR",
50         "1", "9", "FAIL", "U_MISSING_RESOURCE_ERROR",
51         "3", "1", "FAIL", "U_MISSING_RESOURCE_ERROR",
52         "4", "14", "Please ", "U_ZERO_ERROR",
53         "4", "15", "FAIL", "U_MISSING_RESOURCE_ERROR",
54         "4", "19", "Thank you.", "U_ZERO_ERROR",
55         "4", "20", "Sincerely,", "U_ZERO_ERROR",
56         NULL
57     };
58     const UChar FAIL[] = {0x46, 0x41, 0x49, 0x4C, 0x00}; /* "FAIL" */
59     int32_t i;
60     const char *path = loadTestData(&ec);
61 
62     if (U_FAILURE(ec)) {
63         log_data_err("FAIL: loadTestData => %s\n", u_errorName(ec));
64         return;
65     }
66 
67     catd = u_catopen(path, "mc", &ec);
68     if (U_FAILURE(ec)) {
69         log_data_err("FAIL: u_catopen => %s\n", u_errorName(ec));
70         return;
71     }
72 
73     for (i=0; DATA[i]!=NULL; i+=4) {
74         int32_t set_num = T_CString_stringToInteger(DATA[i], 10);
75         int32_t msg_num = T_CString_stringToInteger(DATA[i+1], 10);
76         UChar exp[128];
77         int32_t len = -1;
78         const char* err;
79         char str[128];
80         const UChar* ustr;
81 
82         u_uastrcpy(exp, DATA[i+2]);
83 
84         ec = U_ZERO_ERROR;
85         ustr = u_catgets(catd, set_num, msg_num, FAIL, &len, &ec);
86         u_austrcpy(str, ustr);
87         err = u_errorName(ec);
88 
89         log_verbose("u_catgets(%d, %d) => \"%s\", len=%d, %s\n",
90                     set_num, msg_num, str, len, err);
91 
92         if (u_strcmp(ustr, exp) != 0) {
93             log_err("FAIL: u_catgets => \"%s\", exp. \"%s\"\n",
94                     str, DATA[i+2]);
95         }
96 
97         if (len != (int32_t) uprv_strlen(DATA[i+2])) {
98             log_err("FAIL: u_catgets => len=%d, exp. %d\n",
99                     len, uprv_strlen(DATA[i+2]));
100         }
101 
102         if (uprv_strcmp(err, DATA[i+3]) != 0) {
103             log_err("FAIL: u_catgets => %s, exp. %s\n",
104                     err, DATA[i+3]);
105         }
106     }
107 
108     u_catclose(catd);
109 }
110 
111 /*eof*/
112