1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/logging.h"
6 #include "base/string_piece.h"
7 #include "base/utf_offset_string_conversions.h"
8 #include "testing/gtest/include/gtest/gtest.h"
14 // Given a null-terminated string of wchar_t with each wchar_t representing
15 // a UTF-16 code unit, returns a string16 made up of wchar_t's in the input.
16 // Each wchar_t should be <= 0xFFFF and a non-BMP character (> U+FFFF)
17 // should be represented as a surrogate pair (two UTF-16 units)
18 // *even* where wchar_t is 32-bit (Linux and Mac).
20 // This is to help write tests for functions with string16 params until
21 // the C++ 0x UTF-16 literal is well-supported by compilers.
22 string16
BuildString16(const wchar_t* s
) {
23 #if defined(WCHAR_T_IS_UTF16)
25 #elif defined(WCHAR_T_IS_UTF32)
28 DCHECK(static_cast<unsigned int>(*s
) <= 0xFFFFu
);
37 TEST(UTFOffsetStringConversionsTest
, AdjustOffset
) {
38 struct UTF8ToWideCase
{
42 } utf8_to_wide_cases
[] = {
43 {"", 0, std::wstring::npos
},
44 {"\xe4\xbd\xa0\xe5\xa5\xbd", 1, std::wstring::npos
},
45 {"\xe4\xbd\xa0\xe5\xa5\xbd", 3, 1},
46 {"\xed\xb0\x80z", 3, 1},
47 {"A\xF0\x90\x8C\x80z", 1, 1},
48 {"A\xF0\x90\x8C\x80z", 2, std::wstring::npos
},
49 #if defined(WCHAR_T_IS_UTF16)
50 {"A\xF0\x90\x8C\x80z", 5, 3},
51 #elif defined(WCHAR_T_IS_UTF32)
52 {"A\xF0\x90\x8C\x80z", 5, 2},
55 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(utf8_to_wide_cases
); ++i
) {
56 size_t offset
= utf8_to_wide_cases
[i
].input_offset
;
57 UTF8ToWideAndAdjustOffset(utf8_to_wide_cases
[i
].utf8
, &offset
);
58 EXPECT_EQ(utf8_to_wide_cases
[i
].output_offset
, offset
);
61 #if defined(WCHAR_T_IS_UTF32)
62 struct UTF16ToWideCase
{
66 } utf16_to_wide_cases
[] = {
67 {L
"\xD840\xDC00\x4E00", 0, 0},
68 {L
"\xD840\xDC00\x4E00", 1, std::wstring::npos
},
69 {L
"\xD840\xDC00\x4E00", 2, 1},
71 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(utf16_to_wide_cases
); ++i
) {
72 size_t offset
= utf16_to_wide_cases
[i
].input_offset
;
73 UTF16ToWideAndAdjustOffset(BuildString16(utf16_to_wide_cases
[i
].wide
),
75 EXPECT_EQ(utf16_to_wide_cases
[i
].output_offset
, offset
);