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