Pavel Chupin | 50282f7 | 2014-03-25 13:43:04 +0400 | [diff] [blame] | 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 | |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 19 | #include <errno.h> |
Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 20 | #include <limits.h> |
Pavel Chupin | 50282f7 | 2014-03-25 13:43:04 +0400 | [diff] [blame] | 21 | #include <locale.h> |
| 22 | |
| 23 | TEST(locale, localeconv) { |
Elliott Hughes | 5363a45 | 2014-04-08 14:34:12 -0700 | [diff] [blame] | 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); |
Pavel Chupin | 50282f7 | 2014-03-25 13:43:04 +0400 | [diff] [blame] | 48 | } |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 49 | |
| 50 | TEST(locale, setlocale) { |
| 51 | EXPECT_STREQ("C", setlocale(LC_ALL, NULL)); |
| 52 | EXPECT_STREQ("C", setlocale(LC_CTYPE, NULL)); |
| 53 | |
| 54 | errno = 0; |
| 55 | EXPECT_EQ(NULL, setlocale(-1, NULL)); |
| 56 | EXPECT_EQ(EINVAL, errno); |
| 57 | errno = 0; |
| 58 | EXPECT_EQ(NULL, setlocale(13, NULL)); |
| 59 | EXPECT_EQ(EINVAL, errno); |
| 60 | |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 61 | #if defined(__BIONIC__) |
Elliott Hughes | c4936e2 | 2014-04-08 17:05:05 -0700 | [diff] [blame] | 62 | // The "" locale is implementation-defined. For bionic, it's the C locale. |
| 63 | // glibc will give us something like "en_US.UTF-8", depending on the user's configuration. |
| 64 | EXPECT_STREQ("C", setlocale(LC_ALL, "")); |
| 65 | #endif |
| 66 | EXPECT_STREQ("C", setlocale(LC_ALL, "C")); |
| 67 | EXPECT_STREQ("C", setlocale(LC_ALL, "POSIX")); |
| 68 | |
| 69 | errno = 0; |
| 70 | EXPECT_EQ(NULL, setlocale(LC_ALL, "this-is-not-a-locale")); |
| 71 | EXPECT_EQ(ENOENT, errno); // POSIX specified, not an implementation detail! |
| 72 | } |
| 73 | |
| 74 | TEST(locale, newlocale) { |
| 75 | errno = 0; |
| 76 | EXPECT_EQ(0, newlocale(1 << 20, "C", 0)); |
| 77 | EXPECT_EQ(EINVAL, errno); |
| 78 | |
| 79 | locale_t l = newlocale(LC_ALL, "C", 0); |
| 80 | ASSERT_TRUE(l != NULL); |
| 81 | freelocale(l); |
| 82 | |
| 83 | errno = 0; |
| 84 | EXPECT_EQ(0, newlocale(LC_ALL, "this-is-not-a-locale", 0)); |
| 85 | EXPECT_EQ(ENOENT, errno); // POSIX specified, not an implementation detail! |
| 86 | } |
| 87 | |
| 88 | TEST(locale, duplocale) { |
| 89 | locale_t cloned_global = duplocale(LC_GLOBAL_LOCALE); |
| 90 | ASSERT_TRUE(cloned_global != NULL); |
| 91 | freelocale(cloned_global); |
| 92 | } |
| 93 | |
| 94 | TEST(locale, uselocale) { |
| 95 | locale_t original = uselocale(NULL); |
| 96 | EXPECT_FALSE(original == 0); |
| 97 | EXPECT_EQ(LC_GLOBAL_LOCALE, original); |
| 98 | |
| 99 | locale_t n = newlocale(LC_ALL, "C", 0); |
| 100 | EXPECT_FALSE(n == 0); |
| 101 | EXPECT_FALSE(n == original); |
| 102 | |
| 103 | locale_t old = uselocale(n); |
| 104 | EXPECT_TRUE(old == original); |
| 105 | |
| 106 | EXPECT_EQ(n, uselocale(NULL)); |
| 107 | } |