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
TEST(locale,localeconv)23 TEST(locale, localeconv) {
24 EXPECT_STREQ(".", localeconv()->decimal_point);
25 EXPECT_STREQ("", localeconv()->thousands_sep);
26 EXPECT_STREQ("", localeconv()->grouping);
27 EXPECT_STREQ("", localeconv()->int_curr_symbol);
28 EXPECT_STREQ("", localeconv()->currency_symbol);
29 EXPECT_STREQ("", localeconv()->mon_decimal_point);
30 EXPECT_STREQ("", localeconv()->mon_thousands_sep);
31 EXPECT_STREQ("", localeconv()->mon_grouping);
32 EXPECT_STREQ("", localeconv()->positive_sign);
33 EXPECT_STREQ("", localeconv()->negative_sign);
34 EXPECT_EQ(CHAR_MAX, localeconv()->int_frac_digits);
35 EXPECT_EQ(CHAR_MAX, localeconv()->frac_digits);
36 EXPECT_EQ(CHAR_MAX, localeconv()->p_cs_precedes);
37 EXPECT_EQ(CHAR_MAX, localeconv()->p_sep_by_space);
38 EXPECT_EQ(CHAR_MAX, localeconv()->n_cs_precedes);
39 EXPECT_EQ(CHAR_MAX, localeconv()->n_sep_by_space);
40 EXPECT_EQ(CHAR_MAX, localeconv()->p_sign_posn);
41 EXPECT_EQ(CHAR_MAX, localeconv()->n_sign_posn);
42 EXPECT_EQ(CHAR_MAX, localeconv()->int_p_cs_precedes);
43 EXPECT_EQ(CHAR_MAX, localeconv()->int_p_sep_by_space);
44 EXPECT_EQ(CHAR_MAX, localeconv()->int_n_cs_precedes);
45 EXPECT_EQ(CHAR_MAX, localeconv()->int_n_sep_by_space);
46 EXPECT_EQ(CHAR_MAX, localeconv()->int_p_sign_posn);
47 EXPECT_EQ(CHAR_MAX, localeconv()->int_n_sign_posn);
48 }
49
TEST(locale,setlocale)50 TEST(locale, setlocale) {
51 EXPECT_STREQ("C.UTF-8", setlocale(LC_ALL, nullptr));
52 EXPECT_STREQ("C.UTF-8", setlocale(LC_CTYPE, nullptr));
53
54 errno = 0;
55 EXPECT_EQ(nullptr, setlocale(-1, nullptr));
56 EXPECT_EQ(EINVAL, errno);
57 errno = 0;
58 EXPECT_EQ(nullptr, setlocale(13, nullptr));
59 EXPECT_EQ(EINVAL, errno);
60
61 #if defined(__BIONIC__)
62 // The "" locale is implementation-defined. For bionic, it's the C.UTF-8 locale, which is
63 // pretty much all we support anyway.
64 // glibc will give us something like "en_US.UTF-8", depending on the user's configuration.
65 EXPECT_STREQ("C.UTF-8", setlocale(LC_ALL, ""));
66 #endif
67 EXPECT_STREQ("C", setlocale(LC_ALL, "C"));
68 EXPECT_STREQ("C", setlocale(LC_ALL, "POSIX"));
69
70 errno = 0;
71 EXPECT_EQ(nullptr, setlocale(LC_ALL, "this-is-not-a-locale"));
72 EXPECT_EQ(ENOENT, errno); // POSIX specified, not an implementation detail!
73 }
74
TEST(locale,newlocale_invalid_category_mask)75 TEST(locale, newlocale_invalid_category_mask) {
76 errno = 0;
77 EXPECT_EQ(nullptr, newlocale(1 << 20, "C", nullptr));
78 EXPECT_EQ(EINVAL, errno);
79 }
80
TEST(locale,newlocale_NULL_locale_name)81 TEST(locale, newlocale_NULL_locale_name) {
82 errno = 0;
83 EXPECT_EQ(nullptr, newlocale(LC_ALL, nullptr, nullptr));
84 EXPECT_EQ(EINVAL, errno);
85 }
86
TEST(locale,newlocale_bad_locale_name)87 TEST(locale, newlocale_bad_locale_name) {
88 errno = 0;
89 EXPECT_EQ(nullptr, newlocale(LC_ALL, "this-is-not-a-locale", nullptr));
90 EXPECT_EQ(ENOENT, errno); // POSIX specified, not an implementation detail!
91 }
92
TEST(locale,newlocale)93 TEST(locale, newlocale) {
94 locale_t l = newlocale(LC_ALL, "C", nullptr);
95 ASSERT_TRUE(l != nullptr);
96 freelocale(l);
97 }
98
TEST(locale,duplocale)99 TEST(locale, duplocale) {
100 locale_t cloned_global = duplocale(LC_GLOBAL_LOCALE);
101 ASSERT_TRUE(cloned_global != nullptr);
102 freelocale(cloned_global);
103 }
104
TEST(locale,uselocale)105 TEST(locale, uselocale) {
106 locale_t original = uselocale(nullptr);
107 EXPECT_FALSE(original == nullptr);
108 EXPECT_EQ(LC_GLOBAL_LOCALE, original);
109
110 locale_t n = newlocale(LC_ALL, "C", nullptr);
111 EXPECT_FALSE(n == nullptr);
112 EXPECT_FALSE(n == original);
113
114 locale_t old = uselocale(n);
115 EXPECT_TRUE(old == original);
116
117 EXPECT_EQ(n, uselocale(nullptr));
118 }
119
TEST(locale,mb_cur_max)120 TEST(locale, mb_cur_max) {
121 // We can't reliably test the behavior with setlocale(3) or the behavior for
122 // initial program conditions because (unless we're the only test that was
123 // run), another test has almost certainly called uselocale(3) in this thread.
124 // See b/16685652.
125 locale_t cloc = newlocale(LC_ALL, "C", nullptr);
126 locale_t cloc_utf8 = newlocale(LC_ALL, "C.UTF-8", nullptr);
127
128 locale_t old_locale = uselocale(cloc);
129 ASSERT_EQ(1U, MB_CUR_MAX);
130 uselocale(cloc_utf8);
131 ASSERT_EQ(4U, MB_CUR_MAX);
132
133 uselocale(old_locale);
134 freelocale(cloc);
135 freelocale(cloc_utf8);
136 }
137