2014-04-05 02:34:51 +02: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-18 02:30:03 +02:00
|
|
|
#include <errno.h>
|
2017-08-09 23:55:27 +02:00
|
|
|
#include <inttypes.h>
|
2014-04-05 02:34:51 +02:00
|
|
|
#include <limits.h>
|
2014-05-01 07:03:12 +02:00
|
|
|
#include <locale.h>
|
2017-08-29 20:29:28 +02:00
|
|
|
#include <math.h>
|
2014-04-29 23:46:56 +02:00
|
|
|
#include <stdint.h>
|
2021-08-17 00:51:59 +02:00
|
|
|
#include <sys/cdefs.h>
|
2014-04-05 02:34:51 +02:00
|
|
|
#include <wchar.h>
|
|
|
|
|
2017-08-29 20:29:28 +02:00
|
|
|
#include "utils.h"
|
2016-08-27 01:17:17 +02:00
|
|
|
|
2016-06-03 19:18:07 +02:00
|
|
|
#define NUM_WCHARS(num_bytes) ((num_bytes)/sizeof(wchar_t))
|
2014-11-14 00:48:39 +01:00
|
|
|
|
2023-08-03 21:00:09 +02:00
|
|
|
#ifdef __GLIBC__
|
|
|
|
// glibc immediately dereferences the locale passed to all wcsto*_l functions,
|
|
|
|
// even if it won't be used, and even if it's LC_GLOBAL_LOCALE, which isn't a
|
|
|
|
// pointer to valid memory.
|
|
|
|
static locale_t SAFE_LC_GLOBAL_LOCALE = duplocale(LC_GLOBAL_LOCALE);
|
|
|
|
#else
|
|
|
|
static locale_t SAFE_LC_GLOBAL_LOCALE = LC_GLOBAL_LOCALE;
|
|
|
|
#endif
|
|
|
|
|
2023-08-03 21:39:39 +02:00
|
|
|
// Modern versions of UTF-8 (https://datatracker.ietf.org/doc/html/rfc3629 and
|
|
|
|
// newer) explicitly disallow code points beyond U+10FFFF, which exclude all 5-
|
|
|
|
// and 6-byte sequences. Earlier versions of UTF-8 allowed the wider range:
|
|
|
|
// https://datatracker.ietf.org/doc/html/rfc2279.
|
|
|
|
//
|
|
|
|
// Bionic's unicode implementation was written after the high values were
|
|
|
|
// excluded, so it has never supported them. Other implementations (at least
|
|
|
|
// as of glibc 2.36), do support those sequences.
|
|
|
|
#if defined(__ANDROID__) || defined(ANDROID_HOST_MUSL)
|
|
|
|
constexpr bool kLibcRejectsOverLongUtf8Sequences = true;
|
|
|
|
#elif defined(__GLIBC__)
|
|
|
|
constexpr bool kLibcRejectsOverLongUtf8Sequences = false;
|
|
|
|
#else
|
|
|
|
#error kLibcRejectsOverLongUtf8Sequences must be configured for this platform
|
|
|
|
#endif
|
|
|
|
|
2023-08-03 21:34:42 +02:00
|
|
|
// C23 7.31.6.3.2 (mbrtowc) says:
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
//
|
|
|
|
// 0 if the next n or fewer bytes complete the multibyte character that
|
|
|
|
// corresponds to the null wide character (which is the value stored).
|
|
|
|
//
|
|
|
|
// (size_t)(-2) if the next n bytes contribute to an incomplete (but
|
|
|
|
// potentially valid) multibyte character, and all n bytes have been
|
|
|
|
// processed (no value is stored).
|
|
|
|
//
|
|
|
|
// Bionic historically interpreted the behavior when n is 0 to be the next 0
|
|
|
|
// bytes decoding to the null. That's a pretty bad interpretation, and both
|
|
|
|
// glibc and musl return -2 for that case.
|
|
|
|
//
|
|
|
|
// The tests currently checks the incorrect behavior for bionic because gtest
|
|
|
|
// doesn't support explicit xfail annotations. The behavior difference here
|
|
|
|
// should be fixed, but danalbert wants to add more tests before tackling the
|
|
|
|
// bugs.
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
constexpr size_t kExpectedResultForZeroLength = 0U;
|
|
|
|
#else
|
|
|
|
constexpr size_t kExpectedResultForZeroLength = static_cast<size_t>(-2);
|
|
|
|
#endif
|
|
|
|
|
2014-04-05 02:34:51 +02:00
|
|
|
TEST(wchar, sizeof_wchar_t) {
|
|
|
|
EXPECT_EQ(4U, sizeof(wchar_t));
|
|
|
|
EXPECT_EQ(4U, sizeof(wint_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, mbrlen) {
|
|
|
|
char bytes[] = { 'h', 'e', 'l', 'l', 'o', '\0' };
|
2023-08-03 21:34:42 +02:00
|
|
|
EXPECT_EQ(kExpectedResultForZeroLength, mbrlen(&bytes[0], 0, nullptr));
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(1U, mbrlen(&bytes[0], 1, nullptr));
|
2014-04-05 02:34:51 +02:00
|
|
|
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(1U, mbrlen(&bytes[4], 1, nullptr));
|
|
|
|
EXPECT_EQ(0U, mbrlen(&bytes[5], 1, nullptr));
|
2014-04-05 02:34:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wctomb_wcrtomb) {
|
|
|
|
// wctomb and wcrtomb behave differently when s == NULL.
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(0, wctomb(nullptr, L'h'));
|
|
|
|
EXPECT_EQ(0, wctomb(nullptr, L'\0'));
|
|
|
|
EXPECT_EQ(1U, wcrtomb(nullptr, L'\0', nullptr));
|
|
|
|
EXPECT_EQ(1U, wcrtomb(nullptr, L'h', nullptr));
|
2014-04-05 02:34:51 +02:00
|
|
|
|
|
|
|
char bytes[MB_LEN_MAX];
|
|
|
|
|
|
|
|
// wctomb and wcrtomb behave similarly for the null wide character.
|
|
|
|
EXPECT_EQ(1, wctomb(bytes, L'\0'));
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(1U, wcrtomb(bytes, L'\0', nullptr));
|
2014-04-05 02:34:51 +02:00
|
|
|
|
|
|
|
// ...and for regular characters.
|
2014-05-01 07:03:12 +02:00
|
|
|
memset(bytes, 0, sizeof(bytes));
|
2014-04-05 02:34:51 +02:00
|
|
|
EXPECT_EQ(1, wctomb(bytes, L'h'));
|
|
|
|
EXPECT_EQ('h', bytes[0]);
|
2014-05-01 07:03:12 +02:00
|
|
|
memset(bytes, 0, sizeof(bytes));
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(1U, wcrtomb(bytes, L'h', nullptr));
|
2014-05-01 07:03:12 +02:00
|
|
|
EXPECT_EQ('h', bytes[0]);
|
|
|
|
|
|
|
|
ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
|
|
|
|
uselocale(LC_GLOBAL_LOCALE);
|
2014-04-05 02:34:51 +02:00
|
|
|
|
2014-05-01 07:03:12 +02:00
|
|
|
// 1-byte UTF-8.
|
|
|
|
memset(bytes, 0, sizeof(bytes));
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(1U, wcrtomb(bytes, L'h', nullptr));
|
2014-04-05 02:34:51 +02:00
|
|
|
EXPECT_EQ('h', bytes[0]);
|
2014-05-01 07:03:12 +02:00
|
|
|
// 2-byte UTF-8.
|
|
|
|
memset(bytes, 0, sizeof(bytes));
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(2U, wcrtomb(bytes, 0x00a2, nullptr));
|
2014-05-01 07:03:12 +02:00
|
|
|
EXPECT_EQ('\xc2', bytes[0]);
|
|
|
|
EXPECT_EQ('\xa2', bytes[1]);
|
|
|
|
// 3-byte UTF-8.
|
|
|
|
memset(bytes, 0, sizeof(bytes));
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(3U, wcrtomb(bytes, 0x20ac, nullptr));
|
2014-05-01 07:03:12 +02:00
|
|
|
EXPECT_EQ('\xe2', bytes[0]);
|
|
|
|
EXPECT_EQ('\x82', bytes[1]);
|
|
|
|
EXPECT_EQ('\xac', bytes[2]);
|
|
|
|
// 4-byte UTF-8.
|
|
|
|
memset(bytes, 0, sizeof(bytes));
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(4U, wcrtomb(bytes, 0x24b62, nullptr));
|
2014-05-01 07:03:12 +02:00
|
|
|
EXPECT_EQ('\xf0', bytes[0]);
|
|
|
|
EXPECT_EQ('\xa4', bytes[1]);
|
|
|
|
EXPECT_EQ('\xad', bytes[2]);
|
|
|
|
EXPECT_EQ('\xa2', bytes[3]);
|
|
|
|
// Invalid code point.
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(static_cast<size_t>(-1), wcrtomb(bytes, 0xffffffff, nullptr));
|
2014-05-01 07:03:12 +02:00
|
|
|
EXPECT_EQ(EILSEQ, errno);
|
2014-04-05 02:34:51 +02:00
|
|
|
}
|
2014-04-18 02:30:03 +02:00
|
|
|
|
2014-05-08 15:38:35 +02:00
|
|
|
TEST(wchar, wcrtomb_start_state) {
|
2023-08-03 21:49:09 +02:00
|
|
|
ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
|
|
|
|
uselocale(LC_GLOBAL_LOCALE);
|
|
|
|
|
2014-05-08 15:38:35 +02:00
|
|
|
char out[MB_LEN_MAX];
|
|
|
|
mbstate_t ps;
|
|
|
|
|
|
|
|
// Any non-initial state is invalid when calling wcrtomb.
|
|
|
|
memset(&ps, 0, sizeof(ps));
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
|
2014-05-08 15:38:35 +02:00
|
|
|
EXPECT_EQ(static_cast<size_t>(-1), wcrtomb(out, 0x00a2, &ps));
|
|
|
|
EXPECT_EQ(EILSEQ, errno);
|
|
|
|
|
|
|
|
// If the first argument to wcrtomb is NULL or the second is L'\0' the shift
|
|
|
|
// state should be reset.
|
|
|
|
memset(&ps, 0, sizeof(ps));
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
|
|
|
|
EXPECT_EQ(1U, wcrtomb(nullptr, 0x00a2, &ps));
|
2014-05-08 15:38:35 +02:00
|
|
|
EXPECT_TRUE(mbsinit(&ps));
|
|
|
|
|
|
|
|
memset(&ps, 0, sizeof(ps));
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xf0\xa4", 1, &ps));
|
2014-05-08 15:38:35 +02:00
|
|
|
EXPECT_EQ(1U, wcrtomb(out, L'\0', &ps));
|
|
|
|
EXPECT_TRUE(mbsinit(&ps));
|
|
|
|
}
|
|
|
|
|
2014-04-18 02:30:03 +02:00
|
|
|
TEST(wchar, wcstombs_wcrtombs) {
|
2023-08-03 21:49:09 +02:00
|
|
|
ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
|
|
|
|
uselocale(LC_GLOBAL_LOCALE);
|
|
|
|
|
2014-04-18 22:32:33 +02:00
|
|
|
const wchar_t chars[] = { L'h', L'e', L'l', L'l', L'o', 0 };
|
2014-05-02 02:14:59 +02:00
|
|
|
const wchar_t bad_chars[] = { L'h', L'i', static_cast<wchar_t>(0xffffffff), 0 };
|
2014-04-18 02:30:03 +02:00
|
|
|
const wchar_t* src;
|
|
|
|
char bytes[BUFSIZ];
|
|
|
|
|
|
|
|
// Given a NULL destination, these functions count valid characters.
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(5U, wcstombs(nullptr, chars, 0));
|
|
|
|
EXPECT_EQ(5U, wcstombs(nullptr, chars, 4));
|
|
|
|
EXPECT_EQ(5U, wcstombs(nullptr, chars, 256));
|
2014-04-18 02:30:03 +02:00
|
|
|
src = chars;
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 0, nullptr));
|
2014-04-18 02:30:03 +02:00
|
|
|
EXPECT_EQ(&chars[0], src);
|
|
|
|
src = chars;
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 4, nullptr));
|
2014-04-18 02:30:03 +02:00
|
|
|
EXPECT_EQ(&chars[0], src);
|
|
|
|
src = chars;
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(5U, wcsrtombs(nullptr, &src, 256, nullptr));
|
2014-04-18 02:30:03 +02:00
|
|
|
EXPECT_EQ(&chars[0], src);
|
|
|
|
|
|
|
|
// An unrepresentable char just returns an error from wcstombs...
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(static_cast<size_t>(-1), wcstombs(nullptr, bad_chars, 0));
|
2014-04-18 02:30:03 +02:00
|
|
|
EXPECT_EQ(EILSEQ, errno);
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(static_cast<size_t>(-1), wcstombs(nullptr, bad_chars, 256));
|
2014-04-18 02:30:03 +02:00
|
|
|
EXPECT_EQ(EILSEQ, errno);
|
|
|
|
|
|
|
|
// And wcsrtombs doesn't tell us where it got stuck because we didn't ask it
|
|
|
|
// to actually convert anything...
|
|
|
|
errno = 0;
|
|
|
|
src = bad_chars;
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 0, nullptr));
|
2014-04-18 02:30:03 +02:00
|
|
|
EXPECT_EQ(&bad_chars[0], src);
|
|
|
|
EXPECT_EQ(EILSEQ, errno);
|
|
|
|
errno = 0;
|
|
|
|
src = bad_chars;
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 256, nullptr));
|
2014-04-18 02:30:03 +02:00
|
|
|
EXPECT_EQ(&bad_chars[0], src);
|
|
|
|
EXPECT_EQ(EILSEQ, errno);
|
|
|
|
|
|
|
|
// Okay, now let's test actually converting something...
|
|
|
|
memset(bytes, 'x', sizeof(bytes));
|
|
|
|
EXPECT_EQ(0U, wcstombs(bytes, chars, 0));
|
|
|
|
memset(bytes, 'x', sizeof(bytes));
|
|
|
|
EXPECT_EQ(4U, wcstombs(bytes, chars, 4));
|
|
|
|
bytes[5] = 0;
|
|
|
|
EXPECT_STREQ("hellx", bytes);
|
|
|
|
memset(bytes, 'x', sizeof(bytes));
|
|
|
|
EXPECT_EQ(5U, wcstombs(bytes, chars, 256));
|
|
|
|
EXPECT_STREQ("hello", bytes);
|
|
|
|
memset(bytes, 'x', sizeof(bytes));
|
|
|
|
EXPECT_EQ(5U, wcstombs(bytes, chars, 6));
|
|
|
|
EXPECT_STREQ("hello", bytes);
|
|
|
|
errno = 0;
|
|
|
|
memset(bytes, 'x', sizeof(bytes));
|
|
|
|
EXPECT_EQ(static_cast<size_t>(-1), wcstombs(bytes, bad_chars, 256));
|
|
|
|
EXPECT_EQ(EILSEQ, errno);
|
|
|
|
bytes[3] = 0;
|
|
|
|
EXPECT_STREQ("hix", bytes);
|
|
|
|
|
|
|
|
// wcsrtombs is a bit more informative...
|
|
|
|
memset(bytes, 'x', sizeof(bytes));
|
|
|
|
src = chars;
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(0U, wcsrtombs(bytes, &src, 0, nullptr));
|
2014-04-18 02:30:03 +02:00
|
|
|
EXPECT_EQ(&chars[0], src); // No input consumed.
|
|
|
|
EXPECT_EQ(EILSEQ, errno);
|
|
|
|
|
|
|
|
memset(bytes, 'x', sizeof(bytes));
|
|
|
|
src = chars;
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(4U, wcsrtombs(bytes, &src, 4, nullptr));
|
2014-04-18 02:30:03 +02:00
|
|
|
EXPECT_EQ(&chars[4], src); // Some input consumed.
|
|
|
|
EXPECT_EQ(EILSEQ, errno);
|
|
|
|
bytes[5] = 0;
|
|
|
|
EXPECT_STREQ("hellx", bytes);
|
|
|
|
|
|
|
|
memset(bytes, 'x', sizeof(bytes));
|
|
|
|
src = chars;
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(5U, wcsrtombs(bytes, &src, 256, nullptr));
|
|
|
|
EXPECT_EQ(nullptr, src); // All input consumed!
|
2014-04-18 02:30:03 +02:00
|
|
|
EXPECT_EQ(EILSEQ, errno);
|
|
|
|
EXPECT_STREQ("hello", bytes);
|
|
|
|
|
|
|
|
memset(bytes, 'x', sizeof(bytes));
|
|
|
|
src = chars;
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(5U, wcsrtombs(bytes, &src, 6, nullptr));
|
|
|
|
EXPECT_EQ(nullptr, src); // All input consumed.
|
2014-04-18 02:30:03 +02:00
|
|
|
EXPECT_EQ(EILSEQ, errno);
|
|
|
|
EXPECT_STREQ("hello", bytes);
|
|
|
|
|
|
|
|
memset(bytes, 'x', sizeof(bytes));
|
|
|
|
src = bad_chars;
|
2018-08-03 02:31:13 +02:00
|
|
|
EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(bytes, &src, 256, nullptr));
|
2014-04-18 02:30:03 +02:00
|
|
|
EXPECT_EQ(&bad_chars[2], src);
|
|
|
|
EXPECT_EQ(EILSEQ, errno);
|
|
|
|
bytes[3] = 0;
|
|
|
|
EXPECT_STREQ("hix", bytes);
|
2014-05-08 15:38:35 +02:00
|
|
|
|
|
|
|
// Any non-initial state is invalid when calling wcsrtombs.
|
|
|
|
mbstate_t ps;
|
|
|
|
src = chars;
|
|
|
|
memset(&ps, 0, sizeof(ps));
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(nullptr, "\xc2", 1, &ps));
|
|
|
|
EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(nullptr, &src, 0, &ps));
|
2014-05-08 15:38:35 +02:00
|
|
|
EXPECT_EQ(EILSEQ, errno);
|
2014-04-18 02:30:03 +02:00
|
|
|
}
|
2014-04-22 03:09:46 +02:00
|
|
|
|
|
|
|
TEST(wchar, limits) {
|
|
|
|
ASSERT_LT(WCHAR_MIN, WCHAR_MAX);
|
|
|
|
}
|
2014-04-29 01:28:51 +02:00
|
|
|
|
2014-06-04 18:53:06 +02:00
|
|
|
TEST(wchar, wcsstr) {
|
2021-04-09 17:51:09 +02:00
|
|
|
const wchar_t* haystack = L"big daddy/giant haystacks!";
|
|
|
|
const wchar_t* empty_haystack = L"";
|
2014-04-29 01:28:51 +02:00
|
|
|
|
2021-04-09 17:51:09 +02:00
|
|
|
// The empty needle is a special case.
|
|
|
|
ASSERT_EQ(haystack, wcsstr(haystack, L""));
|
|
|
|
ASSERT_EQ(empty_haystack, wcsstr(empty_haystack, L""));
|
|
|
|
|
|
|
|
ASSERT_EQ(haystack, wcsstr(haystack, L"b"));
|
|
|
|
ASSERT_EQ(haystack, wcsstr(haystack, L"big"));
|
|
|
|
ASSERT_EQ(haystack + 9, wcsstr(haystack, L"/"));
|
|
|
|
ASSERT_EQ(haystack + 9, wcsstr(haystack, L"/giant"));
|
|
|
|
ASSERT_EQ(haystack + 25, wcsstr(haystack, L"!"));
|
|
|
|
ASSERT_EQ(haystack + 19, wcsstr(haystack, L"stacks!"));
|
|
|
|
|
|
|
|
ASSERT_EQ(nullptr, wcsstr(haystack, L"monkey"));
|
|
|
|
ASSERT_EQ(nullptr, wcsstr(empty_haystack, L"monkey"));
|
2014-04-29 01:28:51 +02:00
|
|
|
}
|
2014-04-29 02:51:13 +02:00
|
|
|
|
2015-02-14 23:11:50 +01:00
|
|
|
TEST(wchar, wcsstr_80199) {
|
|
|
|
// https://code.google.com/p/android/issues/detail?id=80199
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_TRUE(wcsstr(L"romrom", L"rom") != nullptr);
|
2015-02-14 23:11:50 +01:00
|
|
|
}
|
|
|
|
|
2014-04-29 02:51:13 +02:00
|
|
|
TEST(wchar, mbtowc) {
|
|
|
|
wchar_t out[8];
|
|
|
|
|
2023-08-03 21:34:42 +02:00
|
|
|
// bionic has the same misunderstanding of the result for a zero-length
|
|
|
|
// conversion for mbtowc as it does for all the other multibyte conversion
|
|
|
|
// functions but mbtowc returns different values than all the others:
|
|
|
|
//
|
|
|
|
// C23 7.24.7.2.4:
|
|
|
|
//
|
|
|
|
// If s is a null pointer, the mbtowc function returns a nonzero or zero
|
|
|
|
// value, if multibyte character encodings, respectively, do or do not have
|
|
|
|
// state-dependent encodings. If s is not a null pointer, the mbtowc function
|
|
|
|
// either returns 0 (if s points to the null character), or returns the number
|
|
|
|
// of bytes that are contained in the converted multibyte character (if the
|
|
|
|
// next n or fewer bytes form a valid multibyte character), or returns -1 (if
|
|
|
|
// they do not form a valid multibyte character).
|
|
|
|
|
|
|
|
#ifdef __BIONIC__
|
|
|
|
int expected_result_for_zero_length = 0;
|
|
|
|
#else
|
|
|
|
int expected_result_for_zero_length = -1;
|
|
|
|
#endif
|
|
|
|
|
2014-04-29 02:51:13 +02:00
|
|
|
out[0] = 'x';
|
2023-08-03 21:34:42 +02:00
|
|
|
EXPECT_EQ(expected_result_for_zero_length, mbtowc(out, "hello", 0));
|
|
|
|
EXPECT_EQ('x', out[0]);
|
2014-04-29 02:51:13 +02:00
|
|
|
|
2023-08-03 21:34:42 +02:00
|
|
|
EXPECT_EQ(expected_result_for_zero_length, mbtowc(out, "hello", 0));
|
|
|
|
EXPECT_EQ(0, mbtowc(out, "", 0));
|
|
|
|
EXPECT_EQ(1, mbtowc(out, "hello", 1));
|
|
|
|
EXPECT_EQ(L'h', out[0]);
|
2014-04-29 02:51:13 +02:00
|
|
|
|
2023-08-03 21:34:42 +02:00
|
|
|
EXPECT_EQ(expected_result_for_zero_length, mbtowc(nullptr, "hello", 0));
|
|
|
|
EXPECT_EQ(0, mbtowc(nullptr, "", 0));
|
|
|
|
EXPECT_EQ(1, mbtowc(nullptr, "hello", 1));
|
2014-04-29 02:51:13 +02:00
|
|
|
|
2023-08-03 21:34:42 +02:00
|
|
|
EXPECT_EQ(0, mbtowc(nullptr, nullptr, 0));
|
2014-04-29 02:51:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, mbrtowc) {
|
|
|
|
wchar_t out[8];
|
|
|
|
|
|
|
|
out[0] = 'x';
|
2023-08-03 21:34:42 +02:00
|
|
|
EXPECT_EQ(kExpectedResultForZeroLength, mbrtowc(out, "hello", 0, nullptr));
|
|
|
|
EXPECT_EQ('x', out[0]);
|
2014-04-29 02:51:13 +02:00
|
|
|
|
2023-08-03 21:34:42 +02:00
|
|
|
EXPECT_EQ(kExpectedResultForZeroLength, mbrtowc(out, "hello", 0, nullptr));
|
|
|
|
EXPECT_EQ(kExpectedResultForZeroLength, mbrtowc(out, "", 0, nullptr));
|
|
|
|
EXPECT_EQ(1U, mbrtowc(out, "hello", 1, nullptr));
|
|
|
|
EXPECT_EQ(L'h', out[0]);
|
2014-04-29 02:51:13 +02:00
|
|
|
|
2023-08-03 21:34:42 +02:00
|
|
|
EXPECT_EQ(kExpectedResultForZeroLength, mbrtowc(nullptr, "hello", 0, nullptr));
|
|
|
|
EXPECT_EQ(kExpectedResultForZeroLength, mbrtowc(nullptr, "", 0, nullptr));
|
|
|
|
EXPECT_EQ(1U, mbrtowc(nullptr, "hello", 1, nullptr));
|
2014-04-29 02:51:13 +02:00
|
|
|
|
2023-08-03 21:34:42 +02:00
|
|
|
EXPECT_EQ(0U, mbrtowc(nullptr, nullptr, 0, nullptr));
|
2014-05-01 07:03:12 +02:00
|
|
|
|
2023-08-03 21:34:42 +02:00
|
|
|
EXPECT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
|
2014-05-01 07:03:12 +02:00
|
|
|
uselocale(LC_GLOBAL_LOCALE);
|
|
|
|
|
|
|
|
// 1-byte UTF-8.
|
2023-08-03 21:34:42 +02:00
|
|
|
EXPECT_EQ(1U, mbrtowc(out, "abcdef", 6, nullptr));
|
|
|
|
EXPECT_EQ(L'a', out[0]);
|
2014-05-01 07:03:12 +02:00
|
|
|
// 2-byte UTF-8.
|
2023-08-03 21:34:42 +02:00
|
|
|
EXPECT_EQ(2U, mbrtowc(out,
|
|
|
|
"\xc2\xa2"
|
|
|
|
"cdef",
|
|
|
|
6, nullptr));
|
|
|
|
EXPECT_EQ(static_cast<wchar_t>(0x00a2), out[0]);
|
2014-05-01 07:03:12 +02:00
|
|
|
// 3-byte UTF-8.
|
2023-08-03 21:34:42 +02:00
|
|
|
EXPECT_EQ(3U, mbrtowc(out,
|
|
|
|
"\xe2\x82\xac"
|
|
|
|
"def",
|
|
|
|
6, nullptr));
|
|
|
|
EXPECT_EQ(static_cast<wchar_t>(0x20ac), out[0]);
|
2014-05-01 07:03:12 +02:00
|
|
|
// 4-byte UTF-8.
|
2023-08-03 21:34:42 +02:00
|
|
|
EXPECT_EQ(4U, mbrtowc(out,
|
|
|
|
"\xf0\xa4\xad\xa2"
|
|
|
|
"ef",
|
|
|
|
6, nullptr));
|
|
|
|
EXPECT_EQ(static_cast<wchar_t>(0x24b62), out[0]);
|
2014-05-13 20:19:57 +02:00
|
|
|
#if defined(__BIONIC__) // glibc allows this.
|
2014-05-01 07:03:12 +02:00
|
|
|
// Illegal 5-byte UTF-8.
|
2023-08-03 21:34:42 +02:00
|
|
|
EXPECT_EQ(static_cast<size_t>(-1), mbrtowc(out,
|
|
|
|
"\xf8\xa1\xa2\xa3\xa4"
|
|
|
|
"f",
|
|
|
|
6, nullptr));
|
|
|
|
EXPECT_EQ(EILSEQ, errno);
|
2014-05-01 07:03:12 +02:00
|
|
|
#endif
|
|
|
|
// Illegal over-long sequence.
|
2023-08-03 21:34:42 +02:00
|
|
|
EXPECT_EQ(static_cast<size_t>(-1), mbrtowc(out,
|
|
|
|
"\xf0\x82\x82\xac"
|
|
|
|
"ef",
|
|
|
|
6, nullptr));
|
|
|
|
EXPECT_EQ(EILSEQ, errno);
|
2014-04-29 02:51:13 +02:00
|
|
|
}
|
2014-04-29 23:46:56 +02:00
|
|
|
|
2018-07-07 02:18:05 +02:00
|
|
|
TEST(wchar, mbrtowc_valid_non_characters) {
|
|
|
|
ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
|
|
|
|
uselocale(LC_GLOBAL_LOCALE);
|
|
|
|
|
|
|
|
wchar_t out[8] = {};
|
|
|
|
|
|
|
|
ASSERT_EQ(3U, mbrtowc(out, "\xef\xbf\xbe", 3, nullptr));
|
|
|
|
ASSERT_EQ(static_cast<wchar_t>(0xfffe), out[0]);
|
|
|
|
ASSERT_EQ(3U, mbrtowc(out, "\xef\xbf\xbf", 3, nullptr));
|
|
|
|
ASSERT_EQ(static_cast<wchar_t>(0xffff), out[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, mbrtowc_out_of_range) {
|
|
|
|
ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
|
|
|
|
uselocale(LC_GLOBAL_LOCALE);
|
|
|
|
|
|
|
|
wchar_t out[8] = {};
|
|
|
|
errno = 0;
|
2023-08-03 21:39:39 +02:00
|
|
|
auto result = mbrtowc(out, "\xf5\x80\x80\x80", 4, nullptr);
|
|
|
|
if (kLibcRejectsOverLongUtf8Sequences) {
|
|
|
|
ASSERT_EQ(static_cast<size_t>(-1), result);
|
|
|
|
ASSERT_EQ(EILSEQ, errno);
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ(4U, result);
|
|
|
|
ASSERT_EQ(0, errno);
|
|
|
|
}
|
2018-07-07 02:18:05 +02:00
|
|
|
}
|
|
|
|
|
2016-09-30 02:21:43 +02:00
|
|
|
static void test_mbrtowc_incomplete(mbstate_t* ps) {
|
2014-05-08 15:38:35 +02:00
|
|
|
ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
|
|
|
|
uselocale(LC_GLOBAL_LOCALE);
|
|
|
|
|
|
|
|
wchar_t out;
|
|
|
|
// 2-byte UTF-8.
|
|
|
|
ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
|
|
|
|
ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "cdef", 5, ps));
|
2014-05-19 23:27:31 +02:00
|
|
|
ASSERT_EQ(static_cast<wchar_t>(0x00a2), out);
|
2014-05-08 15:38:35 +02:00
|
|
|
ASSERT_TRUE(mbsinit(ps));
|
|
|
|
// 3-byte UTF-8.
|
|
|
|
ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xe2", 1, ps));
|
|
|
|
ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\x82", 1, ps));
|
|
|
|
ASSERT_EQ(1U, mbrtowc(&out, "\xac" "def", 4, ps));
|
2014-05-19 23:27:31 +02:00
|
|
|
ASSERT_EQ(static_cast<wchar_t>(0x20ac), out);
|
2014-05-08 15:38:35 +02:00
|
|
|
ASSERT_TRUE(mbsinit(ps));
|
|
|
|
// 4-byte UTF-8.
|
|
|
|
ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xf0", 1, ps));
|
|
|
|
ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xa4\xad", 2, ps));
|
|
|
|
ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "ef", 3, ps));
|
2014-05-19 23:27:31 +02:00
|
|
|
ASSERT_EQ(static_cast<wchar_t>(0x24b62), out);
|
2014-05-08 15:38:35 +02:00
|
|
|
ASSERT_TRUE(mbsinit(ps));
|
|
|
|
|
|
|
|
// Invalid 2-byte
|
|
|
|
ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
|
|
|
|
ASSERT_EQ(static_cast<size_t>(-1), mbrtowc(&out, "\x20" "cdef", 5, ps));
|
|
|
|
ASSERT_EQ(EILSEQ, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, mbrtowc_incomplete) {
|
|
|
|
mbstate_t ps;
|
|
|
|
memset(&ps, 0, sizeof(ps));
|
|
|
|
|
|
|
|
test_mbrtowc_incomplete(&ps);
|
2018-08-03 02:31:13 +02:00
|
|
|
test_mbrtowc_incomplete(nullptr);
|
2014-05-08 15:38:35 +02:00
|
|
|
}
|
|
|
|
|
2016-09-30 02:21:43 +02:00
|
|
|
static void test_mbsrtowcs(mbstate_t* ps) {
|
|
|
|
constexpr const char* VALID = "A" "\xc2\xa2" "\xe2\x82\xac" "\xf0\xa4\xad\xa2" "ef";
|
|
|
|
constexpr const char* INVALID = "A" "\xc2\x20" "ef";
|
|
|
|
constexpr const char* INCOMPLETE = "A" "\xc2";
|
2014-05-08 15:38:35 +02:00
|
|
|
wchar_t out[4];
|
|
|
|
|
2016-09-30 02:21:43 +02:00
|
|
|
const char* valid = VALID;
|
2014-05-08 15:38:35 +02:00
|
|
|
ASSERT_EQ(4U, mbsrtowcs(out, &valid, 4, ps));
|
|
|
|
ASSERT_EQ(L'A', out[0]);
|
2014-05-19 23:27:31 +02:00
|
|
|
ASSERT_EQ(static_cast<wchar_t>(0x00a2), out[1]);
|
|
|
|
ASSERT_EQ(static_cast<wchar_t>(0x20ac), out[2]);
|
|
|
|
ASSERT_EQ(static_cast<wchar_t>(0x24b62), out[3]);
|
2014-07-20 20:51:26 +02:00
|
|
|
// Check that valid has advanced to the next unread character.
|
2014-05-08 15:38:35 +02:00
|
|
|
ASSERT_EQ('e', *valid);
|
|
|
|
|
2014-11-14 00:48:39 +01:00
|
|
|
wmemset(out, L'x', NUM_WCHARS(sizeof(out)));
|
2014-07-20 20:51:26 +02:00
|
|
|
ASSERT_EQ(2U, mbsrtowcs(out, &valid, 4, ps));
|
|
|
|
ASSERT_EQ(L'e', out[0]);
|
|
|
|
ASSERT_EQ(L'f', out[1]);
|
|
|
|
ASSERT_EQ(L'\0', out[2]);
|
|
|
|
// Check that we didn't clobber the rest of out.
|
|
|
|
ASSERT_EQ(L'x', out[3]);
|
|
|
|
// Check that valid has advanced to the end of the string.
|
2014-07-31 20:31:03 +02:00
|
|
|
ASSERT_EQ(nullptr, valid);
|
2014-07-20 20:51:26 +02:00
|
|
|
|
2016-09-30 02:21:43 +02:00
|
|
|
const char* invalid = INVALID;
|
2014-05-08 15:38:35 +02:00
|
|
|
ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &invalid, 4, ps));
|
|
|
|
EXPECT_EQ(EILSEQ, errno);
|
|
|
|
ASSERT_EQ('\xc2', *invalid);
|
|
|
|
|
2016-09-30 02:21:43 +02:00
|
|
|
const char* incomplete = INCOMPLETE;
|
2014-05-08 15:38:35 +02:00
|
|
|
ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &incomplete, 2, ps));
|
|
|
|
EXPECT_EQ(EILSEQ, errno);
|
|
|
|
ASSERT_EQ('\xc2', *incomplete);
|
2016-09-30 02:21:43 +02:00
|
|
|
|
|
|
|
// If dst is null, *src shouldn't be updated.
|
|
|
|
// https://code.google.com/p/android/issues/detail?id=166381
|
|
|
|
const char* mbs = VALID;
|
|
|
|
EXPECT_EQ(6U, mbsrtowcs(nullptr, &mbs, 0, ps));
|
|
|
|
EXPECT_EQ(VALID, mbs);
|
|
|
|
mbs = INVALID;
|
|
|
|
EXPECT_EQ(static_cast<size_t>(-1), mbsrtowcs(nullptr, &mbs, 0, ps));
|
|
|
|
EXPECT_EQ(INVALID, mbs);
|
|
|
|
mbs = INCOMPLETE;
|
|
|
|
EXPECT_EQ(static_cast<size_t>(-1), mbsrtowcs(nullptr, &mbs, 0, ps));
|
|
|
|
EXPECT_EQ(INCOMPLETE, mbs);
|
2014-05-08 15:38:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, mbsrtowcs) {
|
|
|
|
ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
|
|
|
|
uselocale(LC_GLOBAL_LOCALE);
|
|
|
|
|
|
|
|
mbstate_t ps;
|
|
|
|
memset(&ps, 0, sizeof(ps));
|
|
|
|
test_mbsrtowcs(&ps);
|
2018-08-03 02:31:13 +02:00
|
|
|
test_mbsrtowcs(nullptr);
|
2014-05-08 15:38:35 +02:00
|
|
|
|
|
|
|
// Invalid multi byte continuation.
|
|
|
|
const char* invalid = "\x20";
|
|
|
|
wchar_t out;
|
|
|
|
ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, &ps));
|
|
|
|
ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(&out, &invalid, 1, &ps));
|
|
|
|
EXPECT_EQ(EILSEQ, errno);
|
|
|
|
ASSERT_EQ('\x20', *invalid);
|
|
|
|
}
|
|
|
|
|
2017-08-09 23:55:27 +02:00
|
|
|
template <typename T>
|
|
|
|
using WcsToIntFn = T (*)(const wchar_t*, wchar_t**, int);
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void TestSingleWcsToInt(WcsToIntFn<T> fn, const wchar_t* str, int base,
|
|
|
|
T expected_value, ptrdiff_t expected_len) {
|
|
|
|
wchar_t* p;
|
2023-08-03 21:49:37 +02:00
|
|
|
EXPECT_EQ(expected_value, fn(str, &p, base)) << str << " " << base;
|
|
|
|
EXPECT_EQ(expected_len, p - str) << str << " " << base;
|
2017-08-09 23:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void TestWcsToInt(WcsToIntFn<T> fn) {
|
|
|
|
TestSingleWcsToInt(fn, L"123", 10, static_cast<T>(123), 3);
|
|
|
|
TestSingleWcsToInt(fn, L"123", 0, static_cast<T>(123), 3);
|
|
|
|
TestSingleWcsToInt(fn, L"123#", 10, static_cast<T>(123), 3);
|
|
|
|
TestSingleWcsToInt(fn, L"01000", 8, static_cast<T>(512), 5);
|
|
|
|
TestSingleWcsToInt(fn, L"01000", 0, static_cast<T>(512), 5);
|
|
|
|
TestSingleWcsToInt(fn, L" 123 45", 0, static_cast<T>(123), 6);
|
|
|
|
TestSingleWcsToInt(fn, L" -123", 0, static_cast<T>(-123), 6);
|
|
|
|
TestSingleWcsToInt(fn, L"0x10000", 0, static_cast<T>(65536), 7);
|
2022-08-06 00:51:05 +02:00
|
|
|
TestSingleWcsToInt(fn, L"0b1011", 0, static_cast<T>(0b1011), 6);
|
2017-08-09 23:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void TestWcsToIntLimits(WcsToIntFn<T> fn, const wchar_t* min_str,
|
|
|
|
const wchar_t* max_str) {
|
|
|
|
if (std::is_signed<T>::value) {
|
|
|
|
ASSERT_EQ(std::numeric_limits<T>::min(), fn(min_str, nullptr, 0)) << min_str;
|
|
|
|
} else {
|
|
|
|
// If the subject sequence begins with a <hyphen-minus>, the value resulting
|
|
|
|
// from the conversion shall be negated.
|
|
|
|
// http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtoul.html
|
|
|
|
ASSERT_EQ(std::numeric_limits<T>::max(), fn(min_str, nullptr, 0)) << min_str;
|
|
|
|
}
|
|
|
|
ASSERT_EQ(std::numeric_limits<T>::max(), fn(max_str, nullptr, 0)) << max_str;
|
|
|
|
}
|
|
|
|
|
2014-04-29 23:46:56 +02:00
|
|
|
TEST(wchar, wcstol) {
|
2017-08-09 23:55:27 +02:00
|
|
|
TestWcsToInt(wcstol);
|
2014-04-29 23:46:56 +02:00
|
|
|
}
|
|
|
|
|
2017-08-09 23:55:27 +02:00
|
|
|
TEST(wchar, wcstol_limits) {
|
|
|
|
if (sizeof(long) == 8) {
|
|
|
|
TestWcsToIntLimits(wcstol, L"-9223372036854775809", L"9223372036854775808");
|
|
|
|
} else {
|
|
|
|
TestWcsToIntLimits(wcstol, L"-2147483649", L"2147483648");
|
|
|
|
}
|
2014-04-29 23:46:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstoul) {
|
2017-08-09 23:55:27 +02:00
|
|
|
TestWcsToInt(wcstoul);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstoul_limits) {
|
|
|
|
if (sizeof(long) == 8) {
|
|
|
|
TestWcsToIntLimits(wcstoul, L"-1", L"18446744073709551616");
|
|
|
|
} else {
|
|
|
|
TestWcsToIntLimits(wcstoul, L"-1", L"4294967296");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstoll) {
|
|
|
|
TestWcsToInt(wcstoll);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstoll_limits) {
|
|
|
|
TestWcsToIntLimits(wcstoll, L"-9223372036854775809", L"9223372036854775808");
|
2014-04-29 23:46:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstoull) {
|
2017-08-09 23:55:27 +02:00
|
|
|
TestWcsToInt(wcstoull);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstoull_limits) {
|
|
|
|
TestWcsToIntLimits(wcstoull, L"-1", L"18446744073709551616");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstoimax) {
|
|
|
|
TestWcsToInt(wcstoimax);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstoimax_limits) {
|
|
|
|
TestWcsToIntLimits(wcstoimax, L"-9223372036854775809",
|
|
|
|
L"9223372036854775808");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstoumax) {
|
|
|
|
TestWcsToInt(wcstoumax);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstoumax_limits) {
|
|
|
|
TestWcsToIntLimits(wcstoumax, L"-1", L"18446744073709551616");
|
2014-04-29 23:46:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, mbsnrtowcs) {
|
|
|
|
wchar_t dst[128];
|
|
|
|
const char* s = "hello, world!";
|
|
|
|
const char* src;
|
|
|
|
|
|
|
|
memset(dst, 0, sizeof(dst));
|
|
|
|
src = s;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(0U, mbsnrtowcs(dst, &src, 0, 0, nullptr));
|
2014-04-29 23:46:56 +02:00
|
|
|
|
|
|
|
memset(dst, 0, sizeof(dst));
|
|
|
|
src = s;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(2U, mbsnrtowcs(dst, &src, 2, 123, nullptr)); // glibc chokes on SIZE_MAX here.
|
2014-04-29 23:46:56 +02:00
|
|
|
ASSERT_EQ(L'h', dst[0]);
|
|
|
|
ASSERT_EQ(L'e', dst[1]);
|
|
|
|
ASSERT_EQ(&s[2], src);
|
|
|
|
|
|
|
|
memset(dst, 0, sizeof(dst));
|
|
|
|
src = s;
|
2018-08-03 02:31:13 +02:00
|
|
|
ASSERT_EQ(3U, mbsnrtowcs(dst, &src, SIZE_MAX, 3, nullptr));
|
2014-04-29 23:46:56 +02:00
|
|
|
ASSERT_EQ(L'h', dst[0]);
|
|
|
|
ASSERT_EQ(L'e', dst[1]);
|
|
|
|
ASSERT_EQ(L'l', dst[2]);
|
|
|
|
ASSERT_EQ(&s[3], src);
|
2017-07-15 02:00:05 +02:00
|
|
|
|
|
|
|
memset(dst, 0, sizeof(dst));
|
|
|
|
const char* incomplete = "\xc2"; // Incomplete UTF-8 sequence.
|
|
|
|
src = incomplete;
|
|
|
|
errno = 0;
|
|
|
|
ASSERT_EQ(static_cast<size_t>(-1), mbsnrtowcs(dst, &src, SIZE_MAX, 3, nullptr));
|
|
|
|
ASSERT_EQ(EILSEQ, errno);
|
|
|
|
|
|
|
|
src = incomplete;
|
|
|
|
errno = 0;
|
|
|
|
ASSERT_EQ(static_cast<size_t>(-1), mbsnrtowcs(nullptr, &src, SIZE_MAX, 3, nullptr));
|
|
|
|
ASSERT_EQ(EILSEQ, errno);
|
2014-04-29 23:46:56 +02:00
|
|
|
}
|
2014-05-03 00:53:03 +02:00
|
|
|
|
2018-02-14 08:14:12 +01:00
|
|
|
TEST(wchar, wcsftime__wcsftime_l) {
|
2014-05-03 00:53:03 +02:00
|
|
|
setenv("TZ", "UTC", 1);
|
|
|
|
|
|
|
|
struct tm t;
|
|
|
|
memset(&t, 0, sizeof(tm));
|
|
|
|
t.tm_year = 200;
|
|
|
|
t.tm_mon = 2;
|
|
|
|
t.tm_mday = 10;
|
|
|
|
|
|
|
|
wchar_t buf[64];
|
|
|
|
|
|
|
|
EXPECT_EQ(24U, wcsftime(buf, sizeof(buf), L"%c", &t));
|
|
|
|
EXPECT_STREQ(L"Sun Mar 10 00:00:00 2100", buf);
|
2023-08-03 21:00:09 +02:00
|
|
|
EXPECT_EQ(24U, wcsftime_l(buf, sizeof(buf), L"%c", &t, SAFE_LC_GLOBAL_LOCALE));
|
2018-02-14 08:14:12 +01:00
|
|
|
EXPECT_STREQ(L"Sun Mar 10 00:00:00 2100", buf);
|
2014-05-03 00:53:03 +02:00
|
|
|
}
|
2014-05-23 17:44:18 +02:00
|
|
|
|
2014-11-14 00:48:39 +01:00
|
|
|
TEST(wchar, wmemmove_smoke) {
|
2014-05-23 17:44:18 +02:00
|
|
|
const wchar_t const_wstr[] = L"This is a test of something or other.....";
|
2014-11-14 00:48:39 +01:00
|
|
|
wchar_t wstr[NUM_WCHARS(sizeof(const_wstr))];
|
2014-05-23 17:44:18 +02:00
|
|
|
|
2014-11-14 00:48:39 +01:00
|
|
|
EXPECT_EQ(wstr, wmemmove(wstr, const_wstr, NUM_WCHARS(sizeof(const_wstr))));
|
2014-05-23 17:44:18 +02:00
|
|
|
EXPECT_STREQ(const_wstr, wstr);
|
|
|
|
|
2014-11-14 00:48:39 +01:00
|
|
|
EXPECT_EQ(wstr+5, wmemmove(wstr+5, wstr, NUM_WCHARS(sizeof(const_wstr)) - 6));
|
2014-05-30 00:42:42 +02:00
|
|
|
EXPECT_STREQ(L"This This is a test of something or other", wstr);
|
2014-05-23 17:44:18 +02:00
|
|
|
}
|
2014-06-06 05:10:09 +02:00
|
|
|
|
2014-11-14 00:48:39 +01:00
|
|
|
TEST(wchar, wmemcpy_smoke) {
|
|
|
|
const wchar_t src[] = L"Source string";
|
|
|
|
wchar_t dst[NUM_WCHARS(sizeof(src))];
|
|
|
|
|
|
|
|
EXPECT_EQ(dst, wmemcpy(dst, src, NUM_WCHARS(sizeof(src))));
|
|
|
|
EXPECT_STREQ(dst, src);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcpcpy_smoke) {
|
|
|
|
const wchar_t src[] = L"Source string";
|
|
|
|
wchar_t dst[NUM_WCHARS(sizeof(src))];
|
|
|
|
|
|
|
|
EXPECT_EQ(dst + NUM_WCHARS(sizeof(src)) - 1, wcpcpy(dst, src));
|
|
|
|
EXPECT_STREQ(dst, src);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcpncpy_smoke) {
|
|
|
|
const wchar_t src[] = L"Source string";
|
|
|
|
wchar_t dst[NUM_WCHARS(sizeof(src)) + 5];
|
|
|
|
|
|
|
|
size_t src_len = NUM_WCHARS(sizeof(src)) - 1;
|
|
|
|
EXPECT_EQ(dst + src_len, wcpncpy(dst, src, src_len + 1));
|
|
|
|
EXPECT_STREQ(dst, src);
|
|
|
|
|
|
|
|
EXPECT_EQ(dst + 6, wcpncpy(dst, src, 6));
|
|
|
|
dst[6] = L'\0';
|
|
|
|
EXPECT_STREQ(dst, L"Source");
|
|
|
|
|
|
|
|
wmemset(dst, L'x', NUM_WCHARS(sizeof(dst)));
|
|
|
|
EXPECT_EQ(dst + src_len, wcpncpy(dst, src, src_len + 4));
|
|
|
|
EXPECT_STREQ(dst, src);
|
|
|
|
EXPECT_EQ(dst[src_len], L'\0');
|
|
|
|
EXPECT_EQ(dst[src_len+1], L'\0');
|
|
|
|
EXPECT_EQ(dst[src_len+2], L'\0');
|
|
|
|
EXPECT_EQ(dst[src_len+3], L'\0');
|
|
|
|
EXPECT_EQ(dst[src_len+4], L'x');
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcscpy_smoke) {
|
|
|
|
const wchar_t src[] = L"Source string";
|
|
|
|
wchar_t dst[NUM_WCHARS(sizeof(src))];
|
|
|
|
|
|
|
|
EXPECT_EQ(dst, wcscpy(dst, src));
|
|
|
|
EXPECT_STREQ(src, dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcsncpy_smoke) {
|
|
|
|
const wchar_t src[] = L"Source string";
|
|
|
|
wchar_t dst[NUM_WCHARS(sizeof(src)) + 5];
|
|
|
|
|
|
|
|
size_t src_len = NUM_WCHARS(sizeof(src)) - 1;
|
|
|
|
EXPECT_EQ(dst, wcsncpy(dst, src, src_len + 1));
|
|
|
|
EXPECT_STREQ(dst, src);
|
|
|
|
|
|
|
|
EXPECT_EQ(dst, wcsncpy(dst, src, 6));
|
|
|
|
dst[6] = L'\0';
|
|
|
|
EXPECT_STREQ(dst, L"Source");
|
2021-04-09 17:51:09 +02:00
|
|
|
EXPECT_EQ(dst, wcsncpy(dst, L"clobber", 0));
|
|
|
|
EXPECT_STREQ(dst, L"Source");
|
2014-11-14 00:48:39 +01:00
|
|
|
|
|
|
|
wmemset(dst, L'x', NUM_WCHARS(sizeof(dst)));
|
|
|
|
EXPECT_EQ(dst, wcsncpy(dst, src, src_len + 4));
|
|
|
|
EXPECT_STREQ(dst, src);
|
|
|
|
EXPECT_EQ(dst[src_len], L'\0');
|
|
|
|
EXPECT_EQ(dst[src_len+1], L'\0');
|
|
|
|
EXPECT_EQ(dst[src_len+2], L'\0');
|
|
|
|
EXPECT_EQ(dst[src_len+3], L'\0');
|
|
|
|
EXPECT_EQ(dst[src_len+4], L'x');
|
|
|
|
}
|
|
|
|
|
2014-06-06 05:10:09 +02:00
|
|
|
TEST(wchar, mbrtowc_15439554) {
|
|
|
|
// http://b/15439554
|
|
|
|
ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
|
|
|
|
uselocale(LC_GLOBAL_LOCALE);
|
|
|
|
|
|
|
|
ASSERT_GE(static_cast<size_t>(MB_LEN_MAX), MB_CUR_MAX);
|
|
|
|
ASSERT_GE(MB_CUR_MAX, 4U);
|
|
|
|
|
|
|
|
wchar_t wc;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
// 1-byte character.
|
2018-08-03 02:31:13 +02:00
|
|
|
n = mbrtowc(&wc, "x", MB_CUR_MAX, nullptr);
|
2014-06-06 05:10:09 +02:00
|
|
|
EXPECT_EQ(1U, n);
|
|
|
|
EXPECT_EQ(L'x', wc);
|
|
|
|
// 2-byte character.
|
2018-08-03 02:31:13 +02:00
|
|
|
n = mbrtowc(&wc, "\xc2\xa2", MB_CUR_MAX, nullptr);
|
2014-06-06 05:10:09 +02:00
|
|
|
EXPECT_EQ(2U, n);
|
|
|
|
EXPECT_EQ(L'¢', wc);
|
|
|
|
// 3-byte character.
|
2018-08-03 02:31:13 +02:00
|
|
|
n = mbrtowc(&wc, "\xe2\x82\xac", MB_CUR_MAX, nullptr);
|
2014-06-06 05:10:09 +02:00
|
|
|
EXPECT_EQ(3U, n);
|
|
|
|
EXPECT_EQ(L'€', wc);
|
|
|
|
// 4-byte character.
|
2018-08-03 02:31:13 +02:00
|
|
|
n = mbrtowc(&wc, "\xf0\xa4\xad\xa2", MB_CUR_MAX, nullptr);
|
2014-06-06 05:10:09 +02:00
|
|
|
EXPECT_EQ(4U, n);
|
|
|
|
EXPECT_EQ(L'𤭢', wc);
|
|
|
|
}
|
2014-08-21 01:10:49 +02:00
|
|
|
|
|
|
|
TEST(wchar, open_wmemstream) {
|
|
|
|
wchar_t* p = nullptr;
|
|
|
|
size_t size = 0;
|
|
|
|
FILE* fp = open_wmemstream(&p, &size);
|
|
|
|
ASSERT_NE(EOF, fputws(L"hello, world!", fp));
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
ASSERT_STREQ(L"hello, world!", p);
|
|
|
|
ASSERT_EQ(wcslen(L"hello, world!"), size);
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(stdio, open_wmemstream_EINVAL) {
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
wchar_t* p;
|
|
|
|
size_t size;
|
2023-04-04 01:13:57 +02:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wnonnull"
|
2014-08-21 01:10:49 +02:00
|
|
|
// Invalid buffer.
|
|
|
|
errno = 0;
|
|
|
|
ASSERT_EQ(nullptr, open_wmemstream(nullptr, &size));
|
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
|
|
|
|
// Invalid size.
|
|
|
|
errno = 0;
|
|
|
|
ASSERT_EQ(nullptr, open_wmemstream(&p, nullptr));
|
|
|
|
ASSERT_EQ(EINVAL, errno);
|
2023-04-04 01:13:57 +02:00
|
|
|
#pragma clang diagnostic pop
|
2014-08-21 01:10:49 +02:00
|
|
|
#else
|
2019-03-09 00:20:23 +01:00
|
|
|
GTEST_SKIP() << "This test is bionic-specific";
|
2014-08-21 01:10:49 +02:00
|
|
|
#endif
|
|
|
|
}
|
2014-09-23 23:53:10 +02:00
|
|
|
|
|
|
|
TEST(wchar, wcstol_EINVAL) {
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
wcstol(L"123", nullptr, -1);
|
2014-09-23 23:53:10 +02:00
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
wcstol(L"123", nullptr, 1);
|
2014-09-23 23:53:10 +02:00
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
wcstol(L"123", nullptr, 37);
|
2014-09-23 23:53:10 +02:00
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstoll_EINVAL) {
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
wcstoll(L"123", nullptr, -1);
|
2014-09-23 23:53:10 +02:00
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
wcstoll(L"123", nullptr, 1);
|
2014-09-23 23:53:10 +02:00
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
wcstoll(L"123", nullptr, 37);
|
2014-09-23 23:53:10 +02:00
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstoul_EINVAL) {
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
wcstoul(L"123", nullptr, -1);
|
2014-09-23 23:53:10 +02:00
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
wcstoul(L"123", nullptr, 1);
|
2014-09-23 23:53:10 +02:00
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
wcstoul(L"123", nullptr, 37);
|
2014-09-23 23:53:10 +02:00
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstoull_EINVAL) {
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
wcstoull(L"123", nullptr, -1);
|
2014-09-23 23:53:10 +02:00
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
wcstoull(L"123", nullptr, 1);
|
2014-09-23 23:53:10 +02:00
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
errno = 0;
|
2018-08-03 02:31:13 +02:00
|
|
|
wcstoull(L"123", nullptr, 37);
|
2014-09-23 23:53:10 +02:00
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstoll_l_EINVAL) {
|
|
|
|
errno = 0;
|
2023-08-03 21:00:09 +02:00
|
|
|
wcstoll_l(L"123", nullptr, -1, SAFE_LC_GLOBAL_LOCALE);
|
2014-09-23 23:53:10 +02:00
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
errno = 0;
|
2023-08-03 21:00:09 +02:00
|
|
|
wcstoll_l(L"123", nullptr, 1, SAFE_LC_GLOBAL_LOCALE);
|
2014-09-23 23:53:10 +02:00
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
errno = 0;
|
2023-08-03 21:00:09 +02:00
|
|
|
wcstoll_l(L"123", nullptr, 37, SAFE_LC_GLOBAL_LOCALE);
|
2014-09-23 23:53:10 +02:00
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstoull_l_EINVAL) {
|
|
|
|
errno = 0;
|
2023-08-03 21:00:09 +02:00
|
|
|
wcstoull_l(L"123", nullptr, -1, SAFE_LC_GLOBAL_LOCALE);
|
2014-09-23 23:53:10 +02:00
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
errno = 0;
|
2023-08-03 21:00:09 +02:00
|
|
|
wcstoull_l(L"123", nullptr, 1, SAFE_LC_GLOBAL_LOCALE);
|
2014-09-23 23:53:10 +02:00
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
errno = 0;
|
2023-08-03 21:00:09 +02:00
|
|
|
wcstoull_l(L"123", nullptr, 37, SAFE_LC_GLOBAL_LOCALE);
|
2014-09-23 23:53:10 +02:00
|
|
|
ASSERT_EQ(EINVAL, errno);
|
|
|
|
}
|
2015-02-19 06:29:13 +01:00
|
|
|
|
|
|
|
TEST(wchar, wmempcpy) {
|
2021-08-17 00:51:59 +02:00
|
|
|
#if !defined(ANDROID_HOST_MUSL)
|
2015-02-19 06:29:13 +01:00
|
|
|
wchar_t dst[6];
|
|
|
|
ASSERT_EQ(&dst[4], wmempcpy(dst, L"hello", 4));
|
2021-07-28 20:18:11 +02:00
|
|
|
#else
|
|
|
|
GTEST_SKIP() << "musl doesn't have wmempcpy";
|
|
|
|
#endif
|
2015-02-19 06:29:13 +01:00
|
|
|
}
|
2016-08-27 01:17:17 +02:00
|
|
|
|
|
|
|
template <typename T>
|
2017-08-09 23:55:27 +02:00
|
|
|
using WcsToFloatFn = T (*)(const wchar_t*, wchar_t**);
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void TestSingleWcsToFloat(WcsToFloatFn<T> fn, const wchar_t* str,
|
|
|
|
T expected_value, ptrdiff_t expected_len) {
|
|
|
|
wchar_t* p;
|
|
|
|
ASSERT_EQ(expected_value, fn(str, &p));
|
|
|
|
ASSERT_EQ(expected_len, p - str);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void TestWcsToFloat(WcsToFloatFn<T> fn) {
|
2017-08-22 11:52:02 +02:00
|
|
|
TestSingleWcsToFloat(fn, L"123", static_cast<T>(123.0L), 3);
|
|
|
|
TestSingleWcsToFloat(fn, L"123#", static_cast<T>(123.0L), 3);
|
|
|
|
TestSingleWcsToFloat(fn, L" 123 45", static_cast<T>(123.0L), 6);
|
|
|
|
TestSingleWcsToFloat(fn, L"9.0", static_cast<T>(9.0L), 3);
|
|
|
|
TestSingleWcsToFloat(fn, L"-9.0", static_cast<T>(-9.0L), 4);
|
|
|
|
TestSingleWcsToFloat(fn, L" \t\v\f\r\n9.0", static_cast<T>(9.0L), 9);
|
2017-08-09 23:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void TestWcsToFloatHexFloats(WcsToFloatFn<T> fn) {
|
2017-08-22 11:52:02 +02:00
|
|
|
TestSingleWcsToFloat(fn, L"0.9e1", static_cast<T>(9.0L), 5);
|
|
|
|
TestSingleWcsToFloat(fn, L"0x1.2p3", static_cast<T>(9.0L), 7);
|
|
|
|
TestSingleWcsToFloat(fn, L"+1e+100", static_cast<T>(1e100L), 7);
|
|
|
|
TestSingleWcsToFloat(fn, L"0x10000.80", static_cast<T>(65536.50L), 10);
|
2017-08-09 23:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void TestWcsToFloatInfNan(WcsToFloatFn<T> fn) {
|
|
|
|
ASSERT_TRUE(isnan(fn(L"+nan", nullptr)));
|
|
|
|
ASSERT_TRUE(isnan(fn(L"nan", nullptr)));
|
|
|
|
ASSERT_TRUE(isnan(fn(L"-nan", nullptr)));
|
2016-08-27 01:17:17 +02:00
|
|
|
|
2017-08-09 23:55:27 +02:00
|
|
|
ASSERT_TRUE(isnan(fn(L"+nan(0xff)", nullptr)));
|
|
|
|
ASSERT_TRUE(isnan(fn(L"nan(0xff)", nullptr)));
|
|
|
|
ASSERT_TRUE(isnan(fn(L"-nan(0xff)", nullptr)));
|
2016-08-27 01:17:17 +02:00
|
|
|
|
2016-12-02 21:02:03 +01:00
|
|
|
wchar_t* p;
|
2017-08-09 23:55:27 +02:00
|
|
|
ASSERT_TRUE(isnan(fn(L"+nanny", &p)));
|
|
|
|
ASSERT_STREQ(L"ny", p);
|
|
|
|
ASSERT_TRUE(isnan(fn(L"nanny", &p)));
|
|
|
|
ASSERT_STREQ(L"ny", p);
|
|
|
|
ASSERT_TRUE(isnan(fn(L"-nanny", &p)));
|
|
|
|
ASSERT_STREQ(L"ny", p);
|
|
|
|
|
|
|
|
ASSERT_EQ(0, fn(L"muppet", &p));
|
|
|
|
ASSERT_STREQ(L"muppet", p);
|
|
|
|
ASSERT_EQ(0, fn(L" muppet", &p));
|
|
|
|
ASSERT_STREQ(L" muppet", p);
|
|
|
|
|
|
|
|
ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+inf", nullptr));
|
|
|
|
ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"inf", nullptr));
|
|
|
|
ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-inf", nullptr));
|
|
|
|
|
|
|
|
ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+infinity", nullptr));
|
|
|
|
ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"infinity", nullptr));
|
|
|
|
ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-infinity", nullptr));
|
|
|
|
|
|
|
|
ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"+infinitude", &p));
|
|
|
|
ASSERT_STREQ(L"initude", p);
|
|
|
|
ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"infinitude", &p));
|
|
|
|
ASSERT_STREQ(L"initude", p);
|
|
|
|
ASSERT_EQ(-std::numeric_limits<T>::infinity(), fn(L"-infinitude", &p));
|
|
|
|
ASSERT_STREQ(L"initude", p);
|
2016-08-27 01:17:17 +02:00
|
|
|
|
|
|
|
// Check case-insensitivity.
|
2017-08-09 23:55:27 +02:00
|
|
|
ASSERT_EQ(std::numeric_limits<T>::infinity(), fn(L"InFiNiTy", nullptr));
|
|
|
|
ASSERT_TRUE(isnan(fn(L"NaN", nullptr)));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstof) {
|
|
|
|
TestWcsToFloat(wcstof);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstof_hex_floats) {
|
|
|
|
TestWcsToFloatHexFloats(wcstof);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstof_hex_inf_nan) {
|
|
|
|
TestWcsToFloatInfNan(wcstof);
|
2016-08-27 01:17:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstod) {
|
2017-08-09 23:55:27 +02:00
|
|
|
TestWcsToFloat(wcstod);
|
2016-08-27 01:17:17 +02:00
|
|
|
}
|
|
|
|
|
2017-08-09 23:55:27 +02:00
|
|
|
TEST(wchar, wcstod_hex_floats) {
|
|
|
|
TestWcsToFloatHexFloats(wcstod);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstod_hex_inf_nan) {
|
|
|
|
TestWcsToFloatInfNan(wcstod);
|
2016-08-27 01:17:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstold) {
|
2017-08-09 23:55:27 +02:00
|
|
|
TestWcsToFloat(wcstold);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstold_hex_floats) {
|
|
|
|
TestWcsToFloatHexFloats(wcstold);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstold_hex_inf_nan) {
|
|
|
|
TestWcsToFloatInfNan(wcstold);
|
2016-08-27 01:17:17 +02:00
|
|
|
}
|
2017-07-28 02:08:08 +02:00
|
|
|
|
2018-02-14 08:14:12 +01:00
|
|
|
TEST(wchar, wcstod_l) {
|
2021-08-17 00:51:59 +02:00
|
|
|
#if !defined(ANDROID_HOST_MUSL)
|
2023-08-03 21:00:09 +02:00
|
|
|
EXPECT_EQ(1.23, wcstod_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
|
2021-07-28 20:18:11 +02:00
|
|
|
#else
|
|
|
|
GTEST_SKIP() << "musl doesn't have wcstod_l";
|
|
|
|
#endif
|
2018-02-14 08:14:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstof_l) {
|
2021-08-17 00:51:59 +02:00
|
|
|
#if !defined(ANDROID_HOST_MUSL)
|
2023-08-03 21:00:09 +02:00
|
|
|
EXPECT_EQ(1.23f, wcstof_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
|
2021-07-28 20:18:11 +02:00
|
|
|
#else
|
|
|
|
GTEST_SKIP() << "musl doesn't have wcstof_l";
|
|
|
|
#endif
|
2018-02-14 08:14:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstol_l) {
|
2021-08-17 00:51:59 +02:00
|
|
|
#if !defined(ANDROID_HOST_MUSL)
|
2023-08-03 21:00:09 +02:00
|
|
|
EXPECT_EQ(123L, wcstol_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
|
2021-07-28 20:18:11 +02:00
|
|
|
#else
|
|
|
|
GTEST_SKIP() << "musl doesn't have wcstol_l";
|
|
|
|
#endif
|
2018-02-14 08:14:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstold_l) {
|
2023-08-03 21:00:09 +02:00
|
|
|
EXPECT_EQ(1.23L, wcstold_l(L"1.23", nullptr, SAFE_LC_GLOBAL_LOCALE));
|
2018-02-14 08:14:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstoll_l) {
|
2023-08-03 21:00:09 +02:00
|
|
|
EXPECT_EQ(123LL, wcstoll_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
|
2018-02-14 08:14:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstoul_l) {
|
2021-08-17 00:51:59 +02:00
|
|
|
#if !defined(ANDROID_HOST_MUSL)
|
2023-08-03 21:00:09 +02:00
|
|
|
EXPECT_EQ(123UL, wcstoul_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
|
2021-07-28 20:18:11 +02:00
|
|
|
#else
|
|
|
|
GTEST_SKIP() << "musl doesn't have wcstoul_l";
|
|
|
|
#endif
|
2018-02-14 08:14:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstoull_l) {
|
2023-08-03 21:00:09 +02:00
|
|
|
EXPECT_EQ(123ULL, wcstoull_l(L"123", nullptr, 10, SAFE_LC_GLOBAL_LOCALE));
|
2018-02-14 08:14:12 +01:00
|
|
|
}
|
|
|
|
|
2017-07-28 02:08:08 +02:00
|
|
|
static void AssertWcwidthRange(wchar_t begin, wchar_t end, int expected) {
|
|
|
|
for (wchar_t i = begin; i < end; ++i) {
|
|
|
|
EXPECT_EQ(expected, wcwidth(i)) << static_cast<int>(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcwidth_NUL) {
|
|
|
|
// NUL is defined to return 0 rather than -1, despite being a C0 control.
|
|
|
|
EXPECT_EQ(0, wcwidth(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcwidth_ascii) {
|
|
|
|
AssertWcwidthRange(0x20, 0x7f, 1); // Non-C0 non-DEL ASCII.
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcwidth_controls) {
|
|
|
|
AssertWcwidthRange(0x01, 0x20, -1); // C0 controls.
|
|
|
|
EXPECT_EQ(-1, wcwidth(0x7f)); // DEL.
|
|
|
|
AssertWcwidthRange(0x80, 0xa0, -1); // C1 controls.
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcwidth_non_spacing_and_enclosing_marks_and_format) {
|
2017-08-29 20:29:28 +02:00
|
|
|
if (!have_dl()) return;
|
|
|
|
|
2017-07-28 02:08:08 +02:00
|
|
|
EXPECT_EQ(0, wcwidth(0x0300)); // Combining grave.
|
|
|
|
EXPECT_EQ(0, wcwidth(0x20dd)); // Combining enclosing circle.
|
|
|
|
EXPECT_EQ(0, wcwidth(0x00ad)); // Soft hyphen (SHY).
|
|
|
|
EXPECT_EQ(0, wcwidth(0x200b)); // Zero width space.
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcwidth_cjk) {
|
2017-08-29 20:29:28 +02:00
|
|
|
if (!have_dl()) return;
|
|
|
|
|
2017-07-28 02:08:08 +02:00
|
|
|
EXPECT_EQ(2, wcwidth(0x4e00)); // Start of CJK unified block.
|
|
|
|
EXPECT_EQ(2, wcwidth(0x9fff)); // End of CJK unified block.
|
|
|
|
EXPECT_EQ(2, wcwidth(0x3400)); // Start of CJK extension A block.
|
|
|
|
EXPECT_EQ(2, wcwidth(0x4dbf)); // End of CJK extension A block.
|
|
|
|
EXPECT_EQ(2, wcwidth(0x20000)); // Start of CJK extension B block.
|
|
|
|
EXPECT_EQ(2, wcwidth(0x2a6df)); // End of CJK extension B block.
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcwidth_korean_combining_jamo) {
|
2017-08-29 20:29:28 +02:00
|
|
|
if (!have_dl()) return;
|
|
|
|
|
2017-07-28 02:08:08 +02:00
|
|
|
AssertWcwidthRange(0x1160, 0x1200, 0); // Original range.
|
|
|
|
EXPECT_EQ(0, wcwidth(0xd7b0)); // Newer.
|
|
|
|
EXPECT_EQ(0, wcwidth(0xd7cb));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcwidth_korean_jeongeul_syllables) {
|
2017-08-29 20:29:28 +02:00
|
|
|
if (!have_dl()) return;
|
|
|
|
|
2017-07-28 02:08:08 +02:00
|
|
|
EXPECT_EQ(2, wcwidth(0xac00)); // Start of block.
|
|
|
|
EXPECT_EQ(2, wcwidth(0xd7a3)); // End of defined code points in Unicode 7.
|
|
|
|
// Undefined characters at the end of the block have width 1.
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcwidth_kana) {
|
2017-08-29 20:29:28 +02:00
|
|
|
if (!have_dl()) return;
|
|
|
|
|
2017-07-28 02:08:08 +02:00
|
|
|
// Hiragana (most, not undefined).
|
|
|
|
AssertWcwidthRange(0x3041, 0x3097, 2);
|
|
|
|
// Katakana.
|
|
|
|
AssertWcwidthRange(0x30a0, 0x3100, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcwidth_circled_two_digit_cjk) {
|
2017-08-29 20:29:28 +02:00
|
|
|
if (!have_dl()) return;
|
|
|
|
|
2017-07-28 02:08:08 +02:00
|
|
|
// Circled two-digit CJK "speed sign" numbers are wide,
|
|
|
|
// though EastAsianWidth is ambiguous.
|
|
|
|
AssertWcwidthRange(0x3248, 0x3250, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcwidth_hexagrams) {
|
2017-08-29 20:29:28 +02:00
|
|
|
if (!have_dl()) return;
|
|
|
|
|
2017-07-28 02:08:08 +02:00
|
|
|
// Hexagrams are wide, though EastAsianWidth is neutral.
|
|
|
|
AssertWcwidthRange(0x4dc0, 0x4e00, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcwidth_default_ignorables) {
|
2017-08-29 20:29:28 +02:00
|
|
|
if (!have_dl()) return;
|
|
|
|
|
2017-07-28 02:08:08 +02:00
|
|
|
AssertWcwidthRange(0xfff0, 0xfff8, 0); // Unassigned by default ignorable.
|
|
|
|
EXPECT_EQ(0, wcwidth(0xe0000)); // ...through 0xe0fff.
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcwidth_korean_common_non_syllables) {
|
2017-08-29 20:29:28 +02:00
|
|
|
if (!have_dl()) return;
|
|
|
|
|
2017-07-28 02:08:08 +02:00
|
|
|
EXPECT_EQ(2, wcwidth(L'ㅜ')); // Korean "crying" emoticon.
|
|
|
|
EXPECT_EQ(2, wcwidth(L'ㅋ')); // Korean "laughing" emoticon.
|
|
|
|
}
|
2021-04-09 17:51:09 +02:00
|
|
|
|
|
|
|
TEST(wchar, wcswidth) {
|
|
|
|
EXPECT_EQ(2, wcswidth(L"abc", 2));
|
|
|
|
EXPECT_EQ(2, wcswidth(L"ab\t", 2));
|
|
|
|
EXPECT_EQ(-1, wcswidth(L"a\tb", 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcslcpy) {
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
wchar_t dst[32];
|
|
|
|
ASSERT_EQ(11U, wcslcpy(dst, L"hello world", 3));
|
|
|
|
ASSERT_STREQ(L"he", dst);
|
|
|
|
ASSERT_EQ(11U, wcslcpy(dst, L"hello world", 32));
|
|
|
|
ASSERT_STREQ(L"hello world", dst);
|
|
|
|
#else
|
|
|
|
GTEST_SKIP() << "no wcslcpy in glibc";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcscat) {
|
|
|
|
wchar_t dst[32];
|
|
|
|
ASSERT_EQ(dst, wcscat(dst, L"hello"));
|
|
|
|
ASSERT_STREQ(dst, L"hello");
|
|
|
|
ASSERT_EQ(dst, wcscat(dst, L" world"));
|
|
|
|
ASSERT_STREQ(dst, L"hello world");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcscpy) {
|
|
|
|
wchar_t dst[32];
|
|
|
|
ASSERT_EQ(dst, wcscpy(dst, L"hello"));
|
|
|
|
ASSERT_STREQ(dst, L"hello");
|
|
|
|
ASSERT_EQ(dst, wcscpy(dst, L"world"));
|
|
|
|
ASSERT_STREQ(dst, L"world");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcscasecmp) {
|
|
|
|
ASSERT_EQ(0, wcscasecmp(L"hello", L"HELLO"));
|
|
|
|
ASSERT_TRUE(wcscasecmp(L"hello1", L"HELLO2") < 0);
|
|
|
|
ASSERT_TRUE(wcscasecmp(L"hello2", L"HELLO1") > 0);
|
|
|
|
ASSERT_TRUE(wcscasecmp(L"hello", L"HELL") > 0);
|
|
|
|
ASSERT_TRUE(wcscasecmp(L"hell", L"HELLO") < 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcscspn) {
|
|
|
|
ASSERT_EQ(0U, wcscspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz"));
|
|
|
|
ASSERT_EQ(5U, wcscspn(L"hello world", L" "));
|
|
|
|
ASSERT_EQ(11U, wcscspn(L"hello world", L"!"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcsspn) {
|
|
|
|
ASSERT_EQ(0U, wcsspn(L"hello world", L"!"));
|
|
|
|
ASSERT_EQ(5U, wcsspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz"));
|
|
|
|
ASSERT_EQ(11U, wcsspn(L"hello world", L"abcdefghijklmnopqrstuvwxyz "));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcsdup) {
|
|
|
|
wchar_t* s = wcsdup(L"hello");
|
|
|
|
ASSERT_STREQ(s, L"hello");
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcslcat) {
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
wchar_t dst[4] = {};
|
|
|
|
ASSERT_EQ(1U, wcslcat(dst, L"a", 4));
|
|
|
|
ASSERT_EQ(7U, wcslcat(dst, L"bcdefg", 4));
|
|
|
|
ASSERT_STREQ(dst, L"abc");
|
|
|
|
#else
|
|
|
|
GTEST_SKIP() << "no wcslcpy in glibc";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcsncasecmp) {
|
|
|
|
ASSERT_EQ(0, wcsncasecmp(L"foo", L"bar", 0));
|
|
|
|
|
|
|
|
ASSERT_EQ(0, wcsncasecmp(L"hello1", L"HELLO2", 5));
|
|
|
|
ASSERT_TRUE(wcsncasecmp(L"hello1", L"HELLO2", 6) < 0);
|
|
|
|
ASSERT_TRUE(wcsncasecmp(L"hello2", L"HELLO1", 6) > 0);
|
|
|
|
ASSERT_TRUE(wcsncasecmp(L"hello", L"HELL", 5) > 0);
|
|
|
|
ASSERT_TRUE(wcsncasecmp(L"hell", L"HELLO", 5) < 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcsncat) {
|
|
|
|
wchar_t dst[32];
|
|
|
|
ASSERT_EQ(dst, wcsncat(dst, L"hello, world!", 5));
|
|
|
|
ASSERT_STREQ(dst, L"hello");
|
|
|
|
ASSERT_EQ(dst, wcsncat(dst, L"hello, world!", 0));
|
|
|
|
ASSERT_STREQ(dst, L"hello");
|
|
|
|
ASSERT_EQ(dst, wcsncat(dst, L", world!", 8));
|
|
|
|
ASSERT_STREQ(dst, L"hello, world!");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcsncmp) {
|
|
|
|
ASSERT_EQ(0, wcsncmp(L"foo", L"bar", 0));
|
|
|
|
ASSERT_EQ(0, wcsncmp(L"aaaa", L"aaab", 3));
|
|
|
|
ASSERT_TRUE(wcsncmp(L"aaaa", L"aaab", 4) < 0);
|
|
|
|
ASSERT_TRUE(wcsncmp(L"aaab", L"aaaa", 4) > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcsnlen) {
|
|
|
|
ASSERT_EQ(2U, wcsnlen(L"hello", 2));
|
|
|
|
ASSERT_EQ(5U, wcsnlen(L"hello", 5));
|
|
|
|
ASSERT_EQ(5U, wcsnlen(L"hello", 666));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcspbrk) {
|
|
|
|
const wchar_t* s = L"hello, world!";
|
|
|
|
ASSERT_EQ(nullptr, wcspbrk(s, L"-"));
|
|
|
|
ASSERT_EQ(s, wcspbrk(s, L"abch"));
|
|
|
|
ASSERT_EQ(s + 2, wcspbrk(s, L"l"));
|
|
|
|
ASSERT_EQ(s + 5, wcspbrk(s, L",. !"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wcstok) {
|
|
|
|
wchar_t s[] = L"this is\ta\nstring";
|
|
|
|
wchar_t* p;
|
|
|
|
ASSERT_EQ(s, wcstok(s, L"\t\n ", &p));
|
|
|
|
ASSERT_STREQ(s, L"this");
|
|
|
|
ASSERT_STREQ(p, L"is\ta\nstring");
|
|
|
|
ASSERT_EQ(s + 5, wcstok(nullptr, L"\t\n ", &p));
|
|
|
|
ASSERT_STREQ(s + 5, L"is");
|
|
|
|
ASSERT_STREQ(p, L"a\nstring");
|
|
|
|
ASSERT_EQ(s + 8, wcstok(nullptr, L"\t\n ", &p));
|
|
|
|
ASSERT_STREQ(s + 8, L"a");
|
|
|
|
ASSERT_STREQ(p, L"string");
|
|
|
|
ASSERT_EQ(s + 10, wcstok(nullptr, L"\t\n ", &p));
|
|
|
|
ASSERT_STREQ(s + 10, L"string");
|
|
|
|
ASSERT_EQ(nullptr, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wmemchr) {
|
|
|
|
const wchar_t* s = L"hello, world!";
|
|
|
|
ASSERT_EQ(s, wmemchr(s, L'h', 13));
|
|
|
|
ASSERT_EQ(s + 5, wmemchr(s, L',', 13));
|
|
|
|
ASSERT_EQ(s + 12, wmemchr(s, L'!', 13));
|
|
|
|
ASSERT_EQ(nullptr, wmemchr(s, L'a', 13));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wmemcmp) {
|
|
|
|
ASSERT_EQ(0, wmemcmp(L"aaaa", L"aaab", 3));
|
|
|
|
ASSERT_TRUE(wmemcmp(L"aaaa", L"aaab", 4) < 0);
|
|
|
|
ASSERT_TRUE(wmemcmp(L"aaab", L"aaaa", 4) > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wmemcpy) {
|
|
|
|
wchar_t dst[32] = {};
|
|
|
|
ASSERT_EQ(dst, wmemcpy(dst, L"hello", 5));
|
|
|
|
ASSERT_STREQ(dst, L"hello");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wmemmove) {
|
|
|
|
wchar_t dst[32] = {};
|
|
|
|
ASSERT_EQ(dst, wmemmove(dst, L"hello", 5));
|
|
|
|
ASSERT_STREQ(dst, L"hello");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wchar, wmemset) {
|
|
|
|
wchar_t dst[4] = {};
|
|
|
|
ASSERT_EQ(dst, wmemset(dst, 0x12345678, 3));
|
|
|
|
ASSERT_EQ(dst[0], wchar_t(0x12345678));
|
|
|
|
ASSERT_EQ(dst[1], wchar_t(0x12345678));
|
|
|
|
ASSERT_EQ(dst[2], wchar_t(0x12345678));
|
|
|
|
ASSERT_EQ(dst[3], wchar_t(0));
|
|
|
|
ASSERT_EQ(dst, wmemset(dst, L'y', 0));
|
|
|
|
ASSERT_EQ(dst[0], wchar_t(0x12345678));
|
|
|
|
}
|