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 <stdio.h>
19 #include "functionalext.h"
20
21 #define TEST_LC_COUNT 7
22 #define TEST_LC_LENGTH 18
23 #define TEST_LC_OFFSET 6
24 #define PARAM_ERROR_VALUE_1 13
25 #define PARAM_ERROR_VALUE_2 (-1)
26
27 static const int LcArry[TEST_LC_COUNT] = {
28 LC_PAPER,
29 LC_NAME,
30 LC_ADDRESS,
31 LC_TELEPHONE,
32 LC_MEASUREMENT,
33 LC_IDENTIFICATION,
34 LC_ALL
35 };
36
37 static const char envforlocale[][TEST_LC_LENGTH] = {
38 "LC_PAPER",
39 "LC_NAME",
40 "LC_ADDRESS",
41 "LC_TELEPHONE",
42 "LC_MEASUREMENT",
43 "LC_IDENTIFICATION",
44 };
45
46 /**
47 * @tc.name : setlocaletest_0100
48 * @tc.desc : Determines whether setlocale returns the default value C
49 * when the character set passed in for different data types is NULL
50 * @tc.level : Level 0
51 */
setlocale_0100(void)52 void setlocale_0100(void)
53 {
54 const int num = sizeof(LcArry) / sizeof(LcArry[0]);
55 for (int i = 0; i < num; i++) {
56 const char *locale = setlocale(LcArry[i], NULL);
57 if (!locale) {
58 t_error("[%s] failed\n", "setlocale_0100");
59 return;
60 }
61 EXPECT_EQ("SetlocaleTest_0100", strcmp(locale, "C"), 0);
62 }
63 }
64
65 /**
66 * @tc.name : setlocaletest_0200
67 * @tc.desc : Determines whether setlocale returns the default value "C"
68 * when the default value "C" is passed in for different LC data types
69 * @tc.level : Level 0
70 */
setlocale_0200(void)71 void setlocale_0200(void)
72 {
73 const int num = sizeof(LcArry) / sizeof(LcArry[0]);
74 for (int i = 0; i < num; i++) {
75 setenv(envforlocale[i], "en-US", 1);
76 const char *locale = setlocale(LcArry[i], "C");
77 if (!locale) {
78 t_error("[%s] failed\n", "setlocale_0200");
79 return;
80 }
81 EXPECT_STREQ("SetlocaleTest_0200", locale, "C");
82 }
83 }
84
85 /**
86 * @tc.name : setlocaletest_0300
87 * @tc.desc : Asserts whether the result returned is null when an exception LC data type is passed in
88 * @tc.level : Level 2
89 */
setlocale_0300(void)90 void setlocale_0300(void)
91 {
92 const char *locale1 = setlocale(PARAM_ERROR_VALUE_1, NULL);
93 if (locale1) {
94 t_error("[%s] failed\n", "SetlocaleTest_0300");
95 }
96
97 const char *locale2 = setlocale(PARAM_ERROR_VALUE_2, NULL);
98 if (locale2) {
99 t_error("[%s] failed\n", "SetlocaleTest_0300");
100 }
101 }
102
103 /**
104 * @tc.name : setlocaletest_0400
105 * @tc.desc : Determines whether setlocale returns da_DK
106 * when the environment variable is set to da_DK for different LC data types
107 * @tc.level : Level 0
108 */
setlocale_0400(void)109 void setlocale_0400(void)
110 {
111 for (unsigned int i = 0; i < sizeof(envforlocale) / sizeof(envforlocale[0]); i++) {
112 setenv(envforlocale[i], "da_DK", 1);
113 const char *locale = setlocale(LcArry[i], "");
114 if (!locale) {
115 t_error("[%s] failed\n", "setlocale_0400");
116 return;
117 }
118 EXPECT_STREQ("SetlocaleTest_0400", locale, "da_DK");
119 }
120 }
121
122 /**
123 * @tc.name : setlocaletest_0500
124 * @tc.desc : Determines whether setlocale returns en_ZA
125 * when the character set passed in for different LC data types is set to en_ZA
126 * @tc.level : Level 0
127 */
setlocale_0500(void)128 void setlocale_0500(void)
129 {
130 char *rev = setlocale(LC_ALL, "C");
131 if (!rev) {
132 t_error("[%s] failed\n", "setlocale_0500");
133 return;
134 }
135 for (unsigned int i = 0; i < sizeof(LcArry) / sizeof(LcArry[0]); i++) {
136 const char *locale = setlocale(LcArry[i], "en_ZA");
137 if (locale == NULL) {
138 t_error("[%s] failed\n", "setlocale_0500");
139 return;
140 }
141 EXPECT_STREQ("SetlocaleTest_0600", locale, "en_ZA");
142 }
143 }
144
145 /**
146 * @tc.name : setlocaletest_0600
147 * @tc.desc : Verify that the environment variable of different LC data types is set to ar_QA.
148 * When the setlocale function is used to pass LC_ALL, the corresponding field returned by the function is ar_QA
149 * @tc.level : Level 0
150 */
setlocale_0600(void)151 void setlocale_0600(void)
152 {
153 for (unsigned int i = 0; i < sizeof(envforlocale) / sizeof(envforlocale[0]); i++) {
154 int count = 0;
155 char *vec[LC_ALL];
156 const char *flag = ";";
157
158 setenv(envforlocale[i], "ar_QA", 1);
159 char *locale = setlocale(LC_ALL, "");
160 if (locale == NULL) {
161 t_error("[%s] failed\n", "setlocale_0600");
162 return;
163 }
164
165 char *token = strtok(locale, flag);
166 while (token != NULL && count < LC_ALL) {
167 vec[count] = token;
168 token = strtok(NULL, flag);
169 count++;
170 }
171
172 EXPECT_NE("setlocale_0600", count, 0);
173
174 int expectPos = i + TEST_LC_OFFSET;
175 EXPECT_STREQ("setlocale_0600", "ar_QA", vec[expectPos]);
176 }
177 }
178
main(void)179 int main(void)
180 {
181 setlocale_0100();
182 setlocale_0200();
183 setlocale_0300();
184 setlocale_0400();
185 setlocale_0500();
186 setlocale_0600();
187
188 return t_status;
189 }