2014-03-25 10:43:04 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2014-04-09 02:05:05 +02:00
|
|
|
#include <errno.h>
|
2014-04-08 23:34:12 +02:00
|
|
|
#include <limits.h>
|
2014-03-25 10:43:04 +01:00
|
|
|
#include <locale.h>
|
|
|
|
|
2023-09-21 23:11:19 +02:00
|
|
|
#include "utils.h"
|
|
|
|
|
2014-03-25 10:43:04 +01:00
|
|
|
TEST(locale, localeconv) {
|
2014-04-08 23:34:12 +02:00
|
|
|
EXPECT_STREQ(".", localeconv()->decimal_point);
|
|
|
|
EXPECT_STREQ("", localeconv()->thousands_sep);
|
|
|
|
EXPECT_STREQ("", localeconv()->grouping);
|
|
|
|
EXPECT_STREQ("", localeconv()->int_curr_symbol);
|
|
|
|
EXPECT_STREQ("", localeconv()->currency_symbol);
|
|
|
|
EXPECT_STREQ("", localeconv()->mon_decimal_point);
|
|
|
|
EXPECT_STREQ("", localeconv()->mon_thousands_sep);
|
|
|
|
EXPECT_STREQ("", localeconv()->mon_grouping);
|
|
|
|
EXPECT_STREQ("", localeconv()->positive_sign);
|
|
|
|
EXPECT_STREQ("", localeconv()->negative_sign);
|
|
|
|
EXPECT_EQ(CHAR_MAX, localeconv()->int_frac_digits);
|
|
|
|
EXPECT_EQ(CHAR_MAX, localeconv()->frac_digits);
|
|
|
|
EXPECT_EQ(CHAR_MAX, localeconv()->p_cs_precedes);
|
|
|
|
EXPECT_EQ(CHAR_MAX, localeconv()->p_sep_by_space);
|
|
|
|
EXPECT_EQ(CHAR_MAX, localeconv()->n_cs_precedes);
|
|
|
|
EXPECT_EQ(CHAR_MAX, localeconv()->n_sep_by_space);
|
|
|
|
EXPECT_EQ(CHAR_MAX, localeconv()->p_sign_posn);
|
|
|
|
EXPECT_EQ(CHAR_MAX, localeconv()->n_sign_posn);
|
|
|
|
EXPECT_EQ(CHAR_MAX, localeconv()->int_p_cs_precedes);
|
|
|
|
EXPECT_EQ(CHAR_MAX, localeconv()->int_p_sep_by_space);
|
|
|
|
EXPECT_EQ(CHAR_MAX, localeconv()->int_n_cs_precedes);
|
|
|
|
EXPECT_EQ(CHAR_MAX, localeconv()->int_n_sep_by_space);
|
|
|
|
EXPECT_EQ(CHAR_MAX, localeconv()->int_p_sign_posn);
|
|
|
|
EXPECT_EQ(CHAR_MAX, localeconv()->int_n_sign_posn);
|
2014-03-25 10:43:04 +01:00
|
|
|
}
|
2014-04-09 02:05:05 +02:00
|
|
|
|
|
|
|
TEST(locale, setlocale) {
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_STREQ("C.UTF-8", setlocale(LC_ALL, nullptr));
|
|
|
|
EXPECT_STREQ("C.UTF-8", setlocale(LC_CTYPE, nullptr));
|
2014-04-09 02:05:05 +02:00
|
|
|
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(nullptr, setlocale(-1, nullptr));
|
2023-09-21 23:11:19 +02:00
|
|
|
EXPECT_ERRNO(EINVAL);
|
2014-04-09 02:05:05 +02:00
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(nullptr, setlocale(13, nullptr));
|
2023-09-21 23:11:19 +02:00
|
|
|
EXPECT_ERRNO(EINVAL);
|
2014-04-09 02:05:05 +02:00
|
|
|
|
2014-05-13 20:19:57 +02:00
|
|
|
#if defined(__BIONIC__)
|
2016-10-24 23:50:31 +02:00
|
|
|
// The "" locale is implementation-defined. For bionic, it's the C.UTF-8 locale, which is
|
|
|
|
// pretty much all we support anyway.
|
2014-04-09 02:05:05 +02:00
|
|
|
// glibc will give us something like "en_US.UTF-8", depending on the user's configuration.
|
2016-10-24 23:50:31 +02:00
|
|
|
EXPECT_STREQ("C.UTF-8", setlocale(LC_ALL, ""));
|
2014-04-09 02:05:05 +02:00
|
|
|
#endif
|
|
|
|
EXPECT_STREQ("C", setlocale(LC_ALL, "C"));
|
|
|
|
EXPECT_STREQ("C", setlocale(LC_ALL, "POSIX"));
|
|
|
|
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(nullptr, setlocale(LC_ALL, "this-is-not-a-locale"));
|
2023-09-21 23:11:19 +02:00
|
|
|
EXPECT_ERRNO(ENOENT); // POSIX specified, not an implementation detail!
|
2014-04-09 02:05:05 +02:00
|
|
|
}
|
|
|
|
|
2014-11-05 03:03:44 +01:00
|
|
|
TEST(locale, newlocale_invalid_category_mask) {
|
2014-04-09 02:05:05 +02:00
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(nullptr, newlocale(1 << 20, "C", nullptr));
|
2023-09-21 23:11:19 +02:00
|
|
|
EXPECT_ERRNO(EINVAL);
|
2014-11-05 03:03:44 +01:00
|
|
|
}
|
2014-04-09 02:05:05 +02:00
|
|
|
|
2014-11-05 03:03:44 +01:00
|
|
|
TEST(locale, newlocale_NULL_locale_name) {
|
2023-03-11 02:48:33 +01:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wnonnull"
|
2014-11-05 03:03:44 +01:00
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(nullptr, newlocale(LC_ALL, nullptr, nullptr));
|
2023-09-21 23:11:19 +02:00
|
|
|
EXPECT_ERRNO(EINVAL);
|
2023-03-11 02:48:33 +01:00
|
|
|
#pragma clang diagnostic pop
|
2014-11-05 03:03:44 +01:00
|
|
|
}
|
2014-04-09 02:05:05 +02:00
|
|
|
|
2014-11-05 03:03:44 +01:00
|
|
|
TEST(locale, newlocale_bad_locale_name) {
|
2014-04-09 02:05:05 +02:00
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(nullptr, newlocale(LC_ALL, "this-is-not-a-locale", nullptr));
|
2023-09-21 23:11:19 +02:00
|
|
|
EXPECT_ERRNO(ENOENT); // POSIX specified, not an implementation detail!
|
2014-04-09 02:05:05 +02:00
|
|
|
}
|
|
|
|
|
2014-11-05 03:03:44 +01:00
|
|
|
TEST(locale, newlocale) {
|
2018-08-03 02:31:13 +02:00
|
|
|
locale_t l = newlocale(LC_ALL, "C", nullptr);
|
|
|
|
ASSERT_TRUE(l != nullptr);
|
2014-11-05 03:03:44 +01:00
|
|
|
freelocale(l);
|
|
|
|
}
|
|
|
|
|
2014-04-09 02:05:05 +02:00
|
|
|
TEST(locale, duplocale) {
|
|
|
|
locale_t cloned_global = duplocale(LC_GLOBAL_LOCALE);
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_TRUE(cloned_global != nullptr);
|
2014-04-09 02:05:05 +02:00
|
|
|
freelocale(cloned_global);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(locale, uselocale) {
|
2018-08-03 02:31:13 +02:00
|
|
|
locale_t original = uselocale(nullptr);
|
|
|
|
EXPECT_FALSE(original == nullptr);
|
2014-04-09 02:05:05 +02:00
|
|
|
EXPECT_EQ(LC_GLOBAL_LOCALE, original);
|
|
|
|
|
2018-08-03 02:31:13 +02:00
|
|
|
locale_t n = newlocale(LC_ALL, "C", nullptr);
|
|
|
|
EXPECT_FALSE(n == nullptr);
|
2014-04-09 02:05:05 +02:00
|
|
|
EXPECT_FALSE(n == original);
|
|
|
|
|
|
|
|
locale_t old = uselocale(n);
|
|
|
|
EXPECT_TRUE(old == original);
|
|
|
|
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(n, uselocale(nullptr));
|
2014-04-09 02:05:05 +02:00
|
|
|
}
|
2014-07-30 19:53:48 +02:00
|
|
|
|
|
|
|
TEST(locale, mb_cur_max) {
|
|
|
|
// We can't reliably test the behavior with setlocale(3) or the behavior for
|
|
|
|
// initial program conditions because (unless we're the only test that was
|
|
|
|
// run), another test has almost certainly called uselocale(3) in this thread.
|
|
|
|
// See b/16685652.
|
2018-08-03 02:31:13 +02:00
|
|
|
locale_t cloc = newlocale(LC_ALL, "C", nullptr);
|
|
|
|
locale_t cloc_utf8 = newlocale(LC_ALL, "C.UTF-8", nullptr);
|
2014-07-30 19:53:48 +02:00
|
|
|
|
2014-08-26 18:47:23 +02:00
|
|
|
locale_t old_locale = uselocale(cloc);
|
2014-07-30 19:53:48 +02:00
|
|
|
ASSERT_EQ(1U, MB_CUR_MAX);
|
|
|
|
uselocale(cloc_utf8);
|
|
|
|
ASSERT_EQ(4U, MB_CUR_MAX);
|
|
|
|
|
2014-08-26 18:47:23 +02:00
|
|
|
uselocale(old_locale);
|
2014-07-30 19:53:48 +02:00
|
|
|
freelocale(cloc);
|
|
|
|
freelocale(cloc_utf8);
|
|
|
|
}
|