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 <locale.h>
17 #include <stdlib.h>
18 #include "functionalext.h"
19
20 static const int lcMarkArry[] = {
21 LC_PAPER_MASK,
22 LC_NAME_MASK,
23 LC_ADDRESS_MASK,
24 LC_TELEPHONE_MASK,
25 LC_MEASUREMENT_MASK,
26 LC_IDENTIFICATION_MASK,
27 LC_ALL_MASK
28 };
29
30 /**
31 * @tc.name : newlocale_0100
32 * @tc.desc : en-US is a invalid locale. The newlocale should return null
33 * @tc.level : Level 0
34 */
newlocale_0100(void)35 void newlocale_0100(void)
36 {
37 char *lo = setlocale(LC_ALL, "C");
38 if (!lo) {
39 EXPECT_PTRNE("newlocale_0100", lo, NULL);
40 return;
41 }
42 locale_t newloc = newlocale(LC_ALL_MASK, "en_US", NULL);
43 EXPECT_PTRNE("newlocale_0100", newloc, NULL);
44
45 if (newloc) {
46 freelocale(newloc);
47 newloc = NULL;
48 }
49 }
50
51 /**
52 * @tc.name : newlocale_0200
53 * @tc.desc : Check whether the LC_ALL type is passed to newlocale to create a custom locale environment
54 * @tc.level : Level 0
55 */
newlocale_0200(void)56 void newlocale_0200(void)
57 {
58 char *lo = setlocale(LC_ALL, "C");
59 if (!lo) {
60 EXPECT_PTRNE("newlocale_0200", lo, NULL);
61 return;
62 }
63 locale_t newloc = newlocale(LC_ALL_MASK, "C", NULL);
64 EXPECT_PTRNE("newlocale_0200", newloc, NULL);
65
66 if (newloc) {
67 freelocale(newloc);
68 newloc = NULL;
69 }
70 }
71
72 /**
73 * @tc.name : newlocale_0300
74 * @tc.desc : Determines whether the custom locale environment is created successfully
75 * by passing different LC data types to newlocale
76 * tips: en-US is a invalid locale. The newlocale should return null
77 * @tc.level : Level 0
78 */
newlocale_0300(void)79 void newlocale_0300(void)
80 {
81 char *lo = setlocale(LC_ALL, "C");
82 if (!lo) {
83 EXPECT_PTRNE("newlocale_0300", lo, NULL);
84 return;
85 }
86 for (int i = 0; i < sizeof(lcMarkArry) / sizeof(lcMarkArry[0]); i++) {
87 locale_t newloc = newlocale(lcMarkArry[i], "en_US", NULL);
88 EXPECT_PTRNE("newlocale_0300", newloc, NULL);
89
90 if (newloc) {
91 freelocale(newloc);
92 newloc = NULL;
93 }
94 }
95 }
96
97 /**
98 * @tc.name : newlocale_0400
99 * @tc.desc : Determines whether the custom locale environment is created successfully
100 * by passing different LC data types to newlocale
101 * tips: en-US is a invalid locale. The newlocale should return null
102 * @tc.level : Level 0
103 */
newlocale_0400(void)104 void newlocale_0400(void)
105 {
106 for (int i = 0; i < sizeof(lcMarkArry) / sizeof(lcMarkArry[0]); i++) {
107 locale_t newloc = newlocale(lcMarkArry[i], "C", NULL);
108 EXPECT_PTRNE("newlocale_0400", newloc, NULL);
109
110 if (newloc) {
111 freelocale(newloc);
112 newloc = NULL;
113 }
114
115 newloc = newlocale(lcMarkArry[i], "C.UTF-8", NULL);
116 EXPECT_PTRNE("newlocale_0400", newloc, NULL);
117
118 if (newloc) {
119 freelocale(newloc);
120 newloc = NULL;
121 }
122 }
123 }
124
125 /**
126 * @tc.name : newlocale_0500
127 * @tc.desc : Determines whether the custom locale environment is created successfully
128 * by passing different LC data types to newlocale zh_CN and zh_CN.UTF-8
129 * @tc.level : Level 0
130 */
newlocale_0500(void)131 void newlocale_0500(void)
132 {
133 for (int i = 0; i < sizeof(lcMarkArry) / sizeof(lcMarkArry[0]); i++) {
134 locale_t newloc = newlocale(lcMarkArry[i], "zh_CN", NULL);
135 EXPECT_PTRNE("newlocale_0500", newloc, NULL);
136
137 if (newloc) {
138 freelocale(newloc);
139 newloc = NULL;
140 }
141
142 newloc = newlocale(lcMarkArry[i], "zh_CN.UTF-8", NULL);
143 EXPECT_PTRNE("newlocale_0500", newloc, NULL);
144
145 if (newloc) {
146 freelocale(newloc);
147 newloc = NULL;
148 }
149 }
150 }
151
152
153 /**
154 * @tc.name : newlocale_0600
155 * @tc.desc : Set de_DE to newlocale, which is not supported and should return NULL
156 * @tc.level : Level 0
157 */
newlocale_0600(void)158 void newlocale_0600(void)
159 {
160 locale_t newloc = newlocale(LC_ALL_MASK, "de_DE", NULL);
161 EXPECT_PTREQ("newlocale_0600", newloc, NULL);
162
163 if (newloc) {
164 freelocale(newloc);
165 newloc = NULL;
166 }
167 }
168
169
main(void)170 int main(void)
171 {
172 newlocale_0100();
173 newlocale_0200();
174 newlocale_0300();
175 newlocale_0400();
176 newlocale_0500();
177 newlocale_0600();
178
179 return t_status;
180 }