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 }
112 }
113
114 /**
115 * @tc.name : setlocaletest_0500
116 * @tc.desc : Determines whether setlocale returns en_ZA
117 * when the character set passed in for different LC data types is set to en_ZA
118 * @tc.level : Level 0
119 */
setlocale_0500(void)120 void setlocale_0500(void)
121 {
122 char *rev = setlocale(LC_ALL, "C");
123 if (!rev) {
124 t_error("[%s] failed\n", "setlocale_0500");
125 return;
126 }
127 for (unsigned int i = 0; i < sizeof(LcArry) / sizeof(LcArry[0]); i++) {
128 const char *locale = setlocale(LcArry[i], "en_ZA");
129 if (locale) {
130 t_error("[%s] failed\n", "setlocale_0500");
131 return;
132 }
133 }
134 }
135
136 /**
137 * @tc.name : setlocaletest_0600
138 * @tc.desc : Verify that the environment variable of different LC data types is set to ar_QA. When the setlocale
139 * function is used to pass LC_ALL, the corresponding field returned by the function is ar_QA
140 * @tc.level : Level 0
141 */
setlocale_0600(void)142 void setlocale_0600(void)
143 {
144 for (unsigned int i = 0; i < sizeof(envforlocale) / sizeof(envforlocale[0]); i++) {
145 int count = 0;
146 char *vec[LC_ALL];
147 const char *flag = ";";
148
149 setenv(envforlocale[i], "ar_QA", 1);
150 char *locale = setlocale(LC_ALL, "");
151 if (locale) {
152 t_error("[%s] failed\n", "setlocale_0600");
153 return;
154 }
155 }
156 }
157
main(void)158 int main(void)
159 {
160 setlocale_0100();
161 setlocale_0200();
162 setlocale_0300();
163 setlocale_0400();
164 setlocale_0500();
165 setlocale_0600();
166
167 return t_status;
168 }