1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <gtest/gtest.h>
18
19 #include <errno.h>
20 #include <limits.h>
21 #include <locale.h>
22
23 #include "utils.h"
24
TEST(locale,localeconv)25 TEST(locale, localeconv) {
26 EXPECT_STREQ(".", localeconv()->decimal_point);
27 EXPECT_STREQ("", localeconv()->thousands_sep);
28 EXPECT_STREQ("", localeconv()->grouping);
29 EXPECT_STREQ("", localeconv()->int_curr_symbol);
30 EXPECT_STREQ("", localeconv()->currency_symbol);
31 EXPECT_STREQ("", localeconv()->mon_decimal_point);
32 EXPECT_STREQ("", localeconv()->mon_thousands_sep);
33 EXPECT_STREQ("", localeconv()->mon_grouping);
34 EXPECT_STREQ("", localeconv()->positive_sign);
35 EXPECT_STREQ("", localeconv()->negative_sign);
36 EXPECT_EQ(CHAR_MAX, localeconv()->int_frac_digits);
37 EXPECT_EQ(CHAR_MAX, localeconv()->frac_digits);
38 EXPECT_EQ(CHAR_MAX, localeconv()->p_cs_precedes);
39 EXPECT_EQ(CHAR_MAX, localeconv()->p_sep_by_space);
40 EXPECT_EQ(CHAR_MAX, localeconv()->n_cs_precedes);
41 EXPECT_EQ(CHAR_MAX, localeconv()->n_sep_by_space);
42 EXPECT_EQ(CHAR_MAX, localeconv()->p_sign_posn);
43 EXPECT_EQ(CHAR_MAX, localeconv()->n_sign_posn);
44 EXPECT_EQ(CHAR_MAX, localeconv()->int_p_cs_precedes);
45 EXPECT_EQ(CHAR_MAX, localeconv()->int_p_sep_by_space);
46 EXPECT_EQ(CHAR_MAX, localeconv()->int_n_cs_precedes);
47 EXPECT_EQ(CHAR_MAX, localeconv()->int_n_sep_by_space);
48 EXPECT_EQ(CHAR_MAX, localeconv()->int_p_sign_posn);
49 EXPECT_EQ(CHAR_MAX, localeconv()->int_n_sign_posn);
50 }
51
TEST(locale,setlocale)52 TEST(locale, setlocale) {
53 EXPECT_STREQ("C.UTF-8", setlocale(LC_ALL, nullptr));
54 EXPECT_STREQ("C.UTF-8", setlocale(LC_CTYPE, nullptr));
55
56 errno = 0;
57 EXPECT_EQ(nullptr, setlocale(-1, nullptr));
58 EXPECT_ERRNO(EINVAL);
59 errno = 0;
60 EXPECT_EQ(nullptr, setlocale(13, nullptr));
61 EXPECT_ERRNO(EINVAL);
62
63 #if defined(__BIONIC__)
64 // The "" locale is implementation-defined. For bionic, it's the C.UTF-8 locale, which is
65 // pretty much all we support anyway.
66 // glibc will give us something like "en_US.UTF-8", depending on the user's configuration.
67 EXPECT_STREQ("C.UTF-8", setlocale(LC_ALL, ""));
68 #endif
69 EXPECT_STREQ("C", setlocale(LC_ALL, "C"));
70 EXPECT_STREQ("C", setlocale(LC_ALL, "POSIX"));
71
72 errno = 0;
73 EXPECT_EQ(nullptr, setlocale(LC_ALL, "this-is-not-a-locale"));
74 EXPECT_ERRNO(ENOENT); // POSIX specified, not an implementation detail!
75 }
76
TEST(locale,newlocale_invalid_category_mask)77 TEST(locale, newlocale_invalid_category_mask) {
78 errno = 0;
79 EXPECT_EQ(nullptr, newlocale(1 << 20, "C", nullptr));
80 EXPECT_ERRNO(EINVAL);
81 }
82
TEST(locale,newlocale_NULL_locale_name)83 TEST(locale, newlocale_NULL_locale_name) {
84 #pragma clang diagnostic push
85 #pragma clang diagnostic ignored "-Wnonnull"
86 errno = 0;
87 EXPECT_EQ(nullptr, newlocale(LC_ALL, nullptr, nullptr));
88 EXPECT_ERRNO(EINVAL);
89 #pragma clang diagnostic pop
90 }
91
TEST(locale,newlocale_bad_locale_name)92 TEST(locale, newlocale_bad_locale_name) {
93 errno = 0;
94 EXPECT_EQ(nullptr, newlocale(LC_ALL, "this-is-not-a-locale", nullptr));
95 EXPECT_ERRNO(ENOENT); // POSIX specified, not an implementation detail!
96 }
97
TEST(locale,newlocale)98 TEST(locale, newlocale) {
99 locale_t l = newlocale(LC_ALL, "C", nullptr);
100 ASSERT_TRUE(l != nullptr);
101 freelocale(l);
102 }
103
TEST(locale,duplocale)104 TEST(locale, duplocale) {
105 locale_t cloned_global = duplocale(LC_GLOBAL_LOCALE);
106 ASSERT_TRUE(cloned_global != nullptr);
107 freelocale(cloned_global);
108 }
109
TEST(locale,uselocale)110 TEST(locale, uselocale) {
111 locale_t original = uselocale(nullptr);
112 EXPECT_FALSE(original == nullptr);
113 EXPECT_EQ(LC_GLOBAL_LOCALE, original);
114
115 locale_t n = newlocale(LC_ALL, "C", nullptr);
116 EXPECT_FALSE(n == nullptr);
117 EXPECT_FALSE(n == original);
118
119 locale_t old = uselocale(n);
120 EXPECT_TRUE(old == original);
121
122 EXPECT_EQ(n, uselocale(nullptr));
123 }
124
TEST(locale,mb_cur_max)125 TEST(locale, mb_cur_max) {
126 // We can't reliably test the behavior with setlocale(3) or the behavior for
127 // initial program conditions because (unless we're the only test that was
128 // run), another test has almost certainly called uselocale(3) in this thread.
129 // See b/16685652.
130 locale_t cloc = newlocale(LC_ALL, "C", nullptr);
131 locale_t cloc_utf8 = newlocale(LC_ALL, "C.UTF-8", nullptr);
132
133 locale_t old_locale = uselocale(cloc);
134 ASSERT_EQ(1U, MB_CUR_MAX);
135 uselocale(cloc_utf8);
136 ASSERT_EQ(4U, MB_CUR_MAX);
137
138 uselocale(old_locale);
139 freelocale(cloc);
140 freelocale(cloc_utf8);
141 }
142