• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     setlocale(LC_MESSAGES, "zh_CN.UTF-8");
89     char *ptr = nl_langinfo(MESSAGES_ERROR_INFO);
90     EXPECT_STREQ("nl_langinfo_0400", ptr, "");
91 }
92 
93 /**
94  * @tc.name      : nl_langinfo_0500
95  * @tc.desc      : Assert whether the return value result is UTF-8 or ASCII
96  *                 when the function nl_langinfo passes in the CODESET parameter
97  * @tc.level     : Level 2
98  */
nl_langinfo_0500()99 void nl_langinfo_0500()
100 {
101     char *lo = setlocale(LC_CTYPE, "");
102     if (!lo) {
103         EXPECT_PTRNE("nl_langinfo_0500", lo, NULL);
104         return;
105     }
106     char *ptr = nl_langinfo(CODESET);
107     EXPECT_STREQ("nl_langinfo_0500", ptr, "UTF-8");
108 
109     lo = setlocale(LC_TIME, "");
110     if (!lo) {
111         EXPECT_PTRNE("nl_langinfo_0500", lo, NULL);
112         return;
113     }
114     ptr = nl_langinfo(CODESET);
115     EXPECT_STREQ("nl_langinfo_0500", ptr, "UTF-8");
116 }
117 
main(void)118 int main(void)
119 {
120     langinfo_0100();
121     nl_langinfo_0200();
122     nl_langinfo_0300();
123     nl_langinfo_0400();
124     nl_langinfo_0500();
125     return t_status;
126 }