1570af302Sopenharmony_ci/**
2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4570af302Sopenharmony_ci * you may not use this file except in compliance with the License.
5570af302Sopenharmony_ci * You may obtain a copy of the License at
6570af302Sopenharmony_ci *
7570af302Sopenharmony_ci *   http://www.apache.org/licenses/LICENSE-2.0
8570af302Sopenharmony_ci *
9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12570af302Sopenharmony_ci * See the License for the specific language governing permissions and
13570af302Sopenharmony_ci * limitations under the License.
14570af302Sopenharmony_ci */
15570af302Sopenharmony_ci
16570af302Sopenharmony_ci#include <locale.h>
17570af302Sopenharmony_ci#include <stdlib.h>
18570af302Sopenharmony_ci#include <stdio.h>
19570af302Sopenharmony_ci#include "functionalext.h"
20570af302Sopenharmony_ci
21570af302Sopenharmony_ci#define TEST_LC_COUNT 7
22570af302Sopenharmony_ci#define TEST_LC_LENGTH 18
23570af302Sopenharmony_ci#define TEST_LC_OFFSET 6
24570af302Sopenharmony_ci#define PARAM_ERROR_VALUE_1 13
25570af302Sopenharmony_ci#define PARAM_ERROR_VALUE_2 (-1)
26570af302Sopenharmony_ci
27570af302Sopenharmony_cistatic const int LcArry[TEST_LC_COUNT] = {
28570af302Sopenharmony_ci    LC_PAPER, LC_NAME, LC_ADDRESS, LC_TELEPHONE, LC_MEASUREMENT, LC_IDENTIFICATION, LC_ALL};
29570af302Sopenharmony_ci
30570af302Sopenharmony_cistatic const char envforlocale[][TEST_LC_LENGTH] = {
31570af302Sopenharmony_ci    "LC_PAPER",
32570af302Sopenharmony_ci    "LC_NAME",
33570af302Sopenharmony_ci    "LC_ADDRESS",
34570af302Sopenharmony_ci    "LC_TELEPHONE",
35570af302Sopenharmony_ci    "LC_MEASUREMENT",
36570af302Sopenharmony_ci    "LC_IDENTIFICATION",
37570af302Sopenharmony_ci};
38570af302Sopenharmony_ci
39570af302Sopenharmony_ci/**
40570af302Sopenharmony_ci * @tc.name      : setlocaletest_0100
41570af302Sopenharmony_ci * @tc.desc      : Determines whether setlocale returns the default value C
42570af302Sopenharmony_ci *                 when the character set passed in for different data types is NULL
43570af302Sopenharmony_ci * @tc.level     : Level 0
44570af302Sopenharmony_ci */
45570af302Sopenharmony_civoid setlocale_0100(void)
46570af302Sopenharmony_ci{
47570af302Sopenharmony_ci    const int num = sizeof(LcArry) / sizeof(LcArry[0]);
48570af302Sopenharmony_ci    for (int i = 0; i < num; i++) {
49570af302Sopenharmony_ci        const char *locale = setlocale(LcArry[i], NULL);
50570af302Sopenharmony_ci        if (!locale) {
51570af302Sopenharmony_ci            t_error("[%s] failed\n", "setlocale_0100");
52570af302Sopenharmony_ci            return;
53570af302Sopenharmony_ci        }
54570af302Sopenharmony_ci        EXPECT_EQ("SetlocaleTest_0100", strcmp(locale, "C"), 0);
55570af302Sopenharmony_ci    }
56570af302Sopenharmony_ci}
57570af302Sopenharmony_ci
58570af302Sopenharmony_ci/**
59570af302Sopenharmony_ci * @tc.name      : setlocaletest_0200
60570af302Sopenharmony_ci * @tc.desc      : Determines whether setlocale returns the default value "C"
61570af302Sopenharmony_ci *                 when the default value "C" is passed in for different LC data types
62570af302Sopenharmony_ci * @tc.level     : Level 0
63570af302Sopenharmony_ci */
64570af302Sopenharmony_civoid setlocale_0200(void)
65570af302Sopenharmony_ci{
66570af302Sopenharmony_ci    const int num = sizeof(LcArry) / sizeof(LcArry[0]);
67570af302Sopenharmony_ci    for (int i = 0; i < num; i++) {
68570af302Sopenharmony_ci        setenv(envforlocale[i], "en-US", 1);
69570af302Sopenharmony_ci        const char *locale = setlocale(LcArry[i], "C");
70570af302Sopenharmony_ci        if (!locale) {
71570af302Sopenharmony_ci            t_error("[%s] failed\n", "setlocale_0200");
72570af302Sopenharmony_ci            return;
73570af302Sopenharmony_ci        }
74570af302Sopenharmony_ci        EXPECT_STREQ("SetlocaleTest_0200", locale, "C");
75570af302Sopenharmony_ci    }
76570af302Sopenharmony_ci}
77570af302Sopenharmony_ci
78570af302Sopenharmony_ci/**
79570af302Sopenharmony_ci * @tc.name      : setlocaletest_0300
80570af302Sopenharmony_ci * @tc.desc      : Asserts whether the result returned is null when an exception LC data type is passed in
81570af302Sopenharmony_ci * @tc.level     : Level 2
82570af302Sopenharmony_ci */
83570af302Sopenharmony_civoid setlocale_0300(void)
84570af302Sopenharmony_ci{
85570af302Sopenharmony_ci    const char *locale1 = setlocale(PARAM_ERROR_VALUE_1, NULL);
86570af302Sopenharmony_ci    if (locale1) {
87570af302Sopenharmony_ci        t_error("[%s] failed\n", "SetlocaleTest_0300");
88570af302Sopenharmony_ci    }
89570af302Sopenharmony_ci
90570af302Sopenharmony_ci    const char *locale2 = setlocale(PARAM_ERROR_VALUE_2, NULL);
91570af302Sopenharmony_ci    if (locale2) {
92570af302Sopenharmony_ci        t_error("[%s] failed\n", "SetlocaleTest_0300");
93570af302Sopenharmony_ci    }
94570af302Sopenharmony_ci}
95570af302Sopenharmony_ci
96570af302Sopenharmony_ci/**
97570af302Sopenharmony_ci * @tc.name      : setlocaletest_0400
98570af302Sopenharmony_ci * @tc.desc      : Determines whether setlocale returns da_DK
99570af302Sopenharmony_ci *                 when the environment variable is set to da_DK for different LC data types
100570af302Sopenharmony_ci * @tc.level     : Level 0
101570af302Sopenharmony_ci */
102570af302Sopenharmony_civoid setlocale_0400(void)
103570af302Sopenharmony_ci{
104570af302Sopenharmony_ci    for (unsigned int i = 0; i < sizeof(envforlocale) / sizeof(envforlocale[0]); i++) {
105570af302Sopenharmony_ci        setenv(envforlocale[i], "da_DK", 1);
106570af302Sopenharmony_ci        const char *locale = setlocale(LcArry[i], "");
107570af302Sopenharmony_ci        if (locale) {
108570af302Sopenharmony_ci            t_error("[%s] failed\n", "setlocale_0400");
109570af302Sopenharmony_ci            return;
110570af302Sopenharmony_ci        }
111570af302Sopenharmony_ci    }
112570af302Sopenharmony_ci}
113570af302Sopenharmony_ci
114570af302Sopenharmony_ci/**
115570af302Sopenharmony_ci * @tc.name      : setlocaletest_0500
116570af302Sopenharmony_ci * @tc.desc      : Determines whether setlocale returns en_ZA
117570af302Sopenharmony_ci *                 when the character set passed in for different LC data types is set to en_ZA
118570af302Sopenharmony_ci * @tc.level     : Level 0
119570af302Sopenharmony_ci */
120570af302Sopenharmony_civoid setlocale_0500(void)
121570af302Sopenharmony_ci{
122570af302Sopenharmony_ci    char *rev = setlocale(LC_ALL, "C");
123570af302Sopenharmony_ci    if (!rev) {
124570af302Sopenharmony_ci        t_error("[%s] failed\n", "setlocale_0500");
125570af302Sopenharmony_ci        return;
126570af302Sopenharmony_ci    }
127570af302Sopenharmony_ci    for (unsigned int i = 0; i < sizeof(LcArry) / sizeof(LcArry[0]); i++) {
128570af302Sopenharmony_ci        const char *locale = setlocale(LcArry[i], "en_ZA");
129570af302Sopenharmony_ci        if (locale) {
130570af302Sopenharmony_ci            t_error("[%s] failed\n", "setlocale_0500");
131570af302Sopenharmony_ci            return;
132570af302Sopenharmony_ci        }
133570af302Sopenharmony_ci    }
134570af302Sopenharmony_ci}
135570af302Sopenharmony_ci
136570af302Sopenharmony_ci/**
137570af302Sopenharmony_ci * @tc.name      : setlocaletest_0600
138570af302Sopenharmony_ci * @tc.desc      : Verify that the environment variable of different LC data types is set to ar_QA. When the setlocale
139570af302Sopenharmony_ci *                 function is used to pass LC_ALL, the corresponding field returned by the function is ar_QA
140570af302Sopenharmony_ci * @tc.level     : Level 0
141570af302Sopenharmony_ci */
142570af302Sopenharmony_civoid setlocale_0600(void)
143570af302Sopenharmony_ci{
144570af302Sopenharmony_ci    for (unsigned int i = 0; i < sizeof(envforlocale) / sizeof(envforlocale[0]); i++) {
145570af302Sopenharmony_ci        int count = 0;
146570af302Sopenharmony_ci        char *vec[LC_ALL];
147570af302Sopenharmony_ci        const char *flag = ";";
148570af302Sopenharmony_ci
149570af302Sopenharmony_ci        setenv(envforlocale[i], "ar_QA", 1);
150570af302Sopenharmony_ci        char *locale = setlocale(LC_ALL, "");
151570af302Sopenharmony_ci        if (locale) {
152570af302Sopenharmony_ci            t_error("[%s] failed\n", "setlocale_0600");
153570af302Sopenharmony_ci            return;
154570af302Sopenharmony_ci        }
155570af302Sopenharmony_ci    }
156570af302Sopenharmony_ci}
157570af302Sopenharmony_ci
158570af302Sopenharmony_ciint main(void)
159570af302Sopenharmony_ci{
160570af302Sopenharmony_ci    setlocale_0100();
161570af302Sopenharmony_ci    setlocale_0200();
162570af302Sopenharmony_ci    setlocale_0300();
163570af302Sopenharmony_ci    setlocale_0400();
164570af302Sopenharmony_ci    setlocale_0500();
165570af302Sopenharmony_ci    setlocale_0600();
166570af302Sopenharmony_ci
167570af302Sopenharmony_ci    return t_status;
168570af302Sopenharmony_ci}