1 /**
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <langinfo.h>
17 #include <locale.h>
18 #include <stdlib.h>
19 #include "functionalext.h"
20
21 #define TIME_ERROR_INFO 0x20034
22 #define MESSAGES_ERROR_INFO 0x50004
23
24 /**
25 * @tc.name : langinfo_0100
26 * @tc.desc : Asserts whether the nl_langinfo function succeeds in reading data
27 * from the MUSL_LOCPATH environment variable set to zh_CN. Utf-8
28 * @tc.level : Level 0
29 */
langinfo_0100(void)30 void langinfo_0100(void)
31 {
32 setenv("MUSL_LOCPATH", "/etc/locale", 1);
33 char *lo = setlocale(LC_TIME, "");
34 if (!lo) {
35 EXPECT_PTRNE("nl_langinfo_0100", lo, NULL);
36 return;
37 }
38 lo = setlocale(LC_TIME, "zh_CN.UTF-8");
39 char *ptr = nl_langinfo(DAY_2);
40 EXPECT_STREQ("nl_langinfo_0100", ptr, "Monday");
41 }
42
43 /**
44 * @tc.name : nl_langinfo_0200
45 * @tc.desc : Assert that the nl_langinfo function does not read data from the default zh_CN.UTF-8 file
46 * when the locale is reset to the default environment
47 * @tc.level : Level 0
48 */
nl_langinfo_0200()49 void nl_langinfo_0200()
50 {
51 char *lo = setlocale(LC_TIME, "");
52 if (!lo) {
53 EXPECT_PTRNE("nl_langinfo_0200", lo, NULL);
54 return;
55 }
56 char *ptr = nl_langinfo(DAY_2);
57 if (ptr) {
58 EXPECT_STRNE("nl_langinfo_0200", ptr, "lm星期一");
59 }
60 }
61
62 /**
63 * @tc.name : nl_langinfo_0300
64 * @tc.desc : Assert whether the LC_TIME data type is set to zh_CN.UTF-8 through setlocale, and whether the
65 * return value is empty when the abnormal time data is passed to the nl_langinfo function
66 * @tc.level : Level 2
67 */
nl_langinfo_0300()68 void nl_langinfo_0300()
69 {
70 char *lo = setlocale(LC_TIME, "");
71 if (!lo) {
72 EXPECT_PTRNE("nl_langinfo_0300", lo, NULL);
73 return;
74 }
75 lo = setlocale(LC_TIME, "zh_CN.UTF-8");
76 char *ptr = nl_langinfo(TIME_ERROR_INFO);
77 EXPECT_STREQ("nl_langinfo_0300", ptr, "");
78 }
79
80 /**
81 * @tc.name : nl_langinfo_0400
82 * @tc.desc : Assert whether the data type of LC_MESSAGES is set to zh_CN.UTF-8 through setlocale, and whether
83 * the return value is empty when the abnormal time data is passed to the nl_langinfo function
84 * @tc.level : Level 2
85 */
nl_langinfo_0400()86 void nl_langinfo_0400()
87 {
88 char *lo = setlocale(LC_MESSAGES, "zh_CN.UTF-8");
89 if (lo) {
90 t_error("nl_langinfo_0400 %s != NULL\n", lo);
91 return;
92 }
93 char *ptr = nl_langinfo(MESSAGES_ERROR_INFO);
94 EXPECT_STREQ("nl_langinfo_0400", ptr, "");
95 }
96
97 /**
98 * @tc.name : nl_langinfo_0500
99 * @tc.desc : Assert whether the return value result is UTF-8 or ASCII
100 * when the function nl_langinfo passes in the CODESET parameter
101 * @tc.level : Level 2
102 */
nl_langinfo_0500()103 void nl_langinfo_0500()
104 {
105 char *lo = setlocale(LC_CTYPE, "");
106 if (!lo) {
107 EXPECT_PTRNE("nl_langinfo_0500", lo, NULL);
108 return;
109 }
110 char *ptr = nl_langinfo(CODESET);
111 EXPECT_STREQ("nl_langinfo_0500", ptr, "UTF-8");
112
113 lo = setlocale(LC_TIME, "");
114 if (!lo) {
115 EXPECT_PTRNE("nl_langinfo_0500", lo, NULL);
116 return;
117 }
118 ptr = nl_langinfo(CODESET);
119 EXPECT_STREQ("nl_langinfo_0500", ptr, "UTF-8");
120 }
121
main(void)122 int main(void)
123 {
124 langinfo_0100();
125 nl_langinfo_0200();
126 nl_langinfo_0300();
127 nl_langinfo_0400();
128 nl_langinfo_0500();
129 return t_status;
130 }