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 /**
21 * @tc.name : duplocale_0100
22 * @tc.desc : Asserts whether duplocale returns a null value for the global locale
23 * @tc.level : Level 0
24 */
duplocale_0100(void)25 void duplocale_0100(void)
26 {
27 char *lo = setlocale(LC_ALL, "C");
28 if (!lo) {
29 EXPECT_PTRNE("duplocale_0100", lo, NULL);
30 return;
31 }
32
33 locale_t newlocale_ = duplocale(LC_GLOBAL_LOCALE);
34 EXPECT_TRUE("duplocale_0100", newlocale_);
35 if (newlocale_) {
36 freelocale(newlocale_);
37 newlocale_ = NULL;
38 }
39 }
40
41 /**
42 * @tc.name : duplocale_0200
43 * @tc.desc : Asserts that duplocale jumps first to the global locale and then to the custom locale,
44 * expecting that the results of the two calls are not equal
45 * @tc.level : Level 0
46 */
duplocale_0200(void)47 void duplocale_0200(void)
48 {
49 char *lo = setlocale(LC_ALL, "C");
50 if (!lo) {
51 EXPECT_PTRNE("duplocale_0200", lo, NULL);
52 return;
53 }
54
55 locale_t global = duplocale(LC_GLOBAL_LOCALE);
56 EXPECT_PTRNE("duplocale_0200", global, NULL);
57
58 locale_t newlocale_ = newlocale(LC_ALL_MASK, "en_US", NULL);
59 EXPECT_PTRNE("duplocale_0200", newlocale_, NULL);
60
61 locale_t clonelocale = duplocale(newlocale_);
62 EXPECT_PTRNE("duplocale_0200", clonelocale, NULL);
63 EXPECT_PTRNE("duplocale_0200", clonelocale, global);
64
65 if (clonelocale) {
66 freelocale(clonelocale);
67 clonelocale = NULL;
68 }
69 if (newlocale_) {
70 freelocale(newlocale_);
71 newlocale_ = NULL;
72 }
73 if (global) {
74 freelocale(global);
75 global = NULL;
76 }
77 }
78
main(void)79 int main(void)
80 {
81 duplocale_0100();
82 duplocale_0200();
83 return t_status;
84 }