OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include <stddef.h> |
| 6 |
5 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
6 #include "ui/gfx/utf16_indexing.h" | 8 #include "ui/gfx/utf16_indexing.h" |
7 | 9 |
8 namespace gfx { | 10 namespace gfx { |
9 | 11 |
10 TEST(UTF16IndexingTest, IndexOffsetConversions) { | 12 TEST(UTF16IndexingTest, IndexOffsetConversions) { |
11 // Valid surrogate pair surrounded by unpaired surrogates | 13 // Valid surrogate pair surrounded by unpaired surrogates |
12 const base::char16 foo[] = | 14 const base::char16 foo[] = |
13 {0xDC00, 0xD800, 0xD800, 0xDFFF, 0xDFFF, 0xDBFF, 0}; | 15 {0xDC00, 0xD800, 0xD800, 0xDFFF, 0xDFFF, 0xDBFF, 0}; |
14 const base::string16 s(foo); | 16 const base::string16 s(foo); |
15 const size_t the_invalid_index = 3; | 17 const size_t the_invalid_index = 3; |
16 for (size_t i = 0; i <= s.length(); ++i) | 18 for (size_t i = 0; i <= s.length(); ++i) |
17 EXPECT_EQ(i != the_invalid_index, IsValidCodePointIndex(s, i)); | 19 EXPECT_EQ(i != the_invalid_index, IsValidCodePointIndex(s, i)); |
18 for (size_t i = 0; i <= s.length(); ++i) { | 20 for (size_t i = 0; i <= s.length(); ++i) { |
19 for (size_t j = i; j <= s.length(); ++j) { | 21 for (size_t j = i; j <= s.length(); ++j) { |
20 ptrdiff_t offset = static_cast<ptrdiff_t>(j - i); | 22 ptrdiff_t offset = static_cast<ptrdiff_t>(j - i); |
21 if (i <= the_invalid_index && j > the_invalid_index) | 23 if (i <= the_invalid_index && j > the_invalid_index) |
22 --offset; | 24 --offset; |
23 EXPECT_EQ(offset, UTF16IndexToOffset(s, i, j)); | 25 EXPECT_EQ(offset, UTF16IndexToOffset(s, i, j)); |
24 EXPECT_EQ(-offset, UTF16IndexToOffset(s, j, i)); | 26 EXPECT_EQ(-offset, UTF16IndexToOffset(s, j, i)); |
25 size_t adjusted_j = (j == the_invalid_index) ? j + 1 : j; | 27 size_t adjusted_j = (j == the_invalid_index) ? j + 1 : j; |
26 EXPECT_EQ(adjusted_j, UTF16OffsetToIndex(s, i, offset)); | 28 EXPECT_EQ(adjusted_j, UTF16OffsetToIndex(s, i, offset)); |
27 size_t adjusted_i = (i == the_invalid_index) ? i + 1 : i; | 29 size_t adjusted_i = (i == the_invalid_index) ? i + 1 : i; |
28 EXPECT_EQ(adjusted_i, UTF16OffsetToIndex(s, j, -offset)); | 30 EXPECT_EQ(adjusted_i, UTF16OffsetToIndex(s, j, -offset)); |
29 } | 31 } |
30 } | 32 } |
31 } | 33 } |
32 | 34 |
33 } // namespace gfx | 35 } // namespace gfx |
OLD | NEW |