1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207

base / i18n / char_iterator_unittest.cc [blame]

// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/i18n/char_iterator.h"

#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {
namespace i18n {

// This test string contains 4 characters:
//   x
//   u with circumflex - 2 bytes in UTF8, 1 codeword in UTF16
//   math double-struck A - 4 bytes in UTF8, 2 codewords in UTF16
//   z
static const char* const kTestString = "x\u00FB\U0001D538z";

TEST(CharIteratorsTest, TestUTF8) {
  std::string empty;
  UTF8CharIterator empty_iter(empty);
  EXPECT_TRUE(empty_iter.end());
  EXPECT_EQ(0u, empty_iter.array_pos());
  EXPECT_EQ(0u, empty_iter.char_pos());
  EXPECT_FALSE(empty_iter.Advance());

  std::string str("s\303\273r");  // [u with circumflex]
  UTF8CharIterator iter(str);
  EXPECT_FALSE(iter.end());
  EXPECT_EQ(0u, iter.array_pos());
  EXPECT_EQ(0u, iter.char_pos());
  EXPECT_EQ('s', iter.get());
  EXPECT_TRUE(iter.Advance());

  EXPECT_FALSE(iter.end());
  EXPECT_EQ(1u, iter.array_pos());
  EXPECT_EQ(1u, iter.char_pos());
  EXPECT_EQ(251, iter.get());
  EXPECT_TRUE(iter.Advance());

  EXPECT_FALSE(iter.end());
  EXPECT_EQ(3u, iter.array_pos());
  EXPECT_EQ(2u, iter.char_pos());
  EXPECT_EQ('r', iter.get());
  EXPECT_TRUE(iter.Advance());

  EXPECT_TRUE(iter.end());
  EXPECT_EQ(4u, iter.array_pos());
  EXPECT_EQ(3u, iter.char_pos());

  // Don't care what it returns, but this shouldn't crash
  iter.get();

  EXPECT_FALSE(iter.Advance());
}

TEST(CharIteratorsTest, TestUTF16_Empty) {
  std::u16string empty;
  UTF16CharIterator empty_iter(empty);
  EXPECT_TRUE(empty_iter.end());
  EXPECT_TRUE(empty_iter.start());
  EXPECT_EQ(0u, empty_iter.array_pos());
  EXPECT_EQ(0, empty_iter.char_offset());
  EXPECT_FALSE(empty_iter.Advance());

  // These shouldn't crash.
  empty_iter.get();
  empty_iter.NextCodePoint();
  empty_iter.PreviousCodePoint();
}

TEST(CharIteratorsTest, TestUTF16) {
  std::u16string str = UTF8ToUTF16(kTestString);
  UTF16CharIterator iter(str);
  EXPECT_FALSE(iter.end());
  EXPECT_TRUE(iter.start());
  EXPECT_EQ(0u, iter.array_pos());
  EXPECT_EQ(0, iter.char_offset());
  EXPECT_EQ('x', iter.get());
  // This shouldn't crash.
  iter.PreviousCodePoint();
  EXPECT_EQ(0xFB, iter.NextCodePoint());
  EXPECT_TRUE(iter.Advance());

  EXPECT_FALSE(iter.end());
  EXPECT_FALSE(iter.start());
  EXPECT_EQ(1u, iter.array_pos());
  EXPECT_EQ(1, iter.char_offset());
  EXPECT_EQ(0xFB, iter.get());
  EXPECT_EQ('x', iter.PreviousCodePoint());
  EXPECT_EQ(0x1D538, iter.NextCodePoint());
  EXPECT_TRUE(iter.Advance());

  EXPECT_FALSE(iter.end());
  EXPECT_FALSE(iter.start());
  EXPECT_EQ(2u, iter.array_pos());
  EXPECT_EQ(2, iter.char_offset());
  EXPECT_EQ(0x1D538, iter.get());
  EXPECT_EQ(0xFB, iter.PreviousCodePoint());
  EXPECT_EQ('z', iter.NextCodePoint());
  EXPECT_TRUE(iter.Advance());

  EXPECT_FALSE(iter.end());
  EXPECT_FALSE(iter.start());
  EXPECT_EQ(4u, iter.array_pos());
  EXPECT_EQ(3, iter.char_offset());
  EXPECT_EQ('z', iter.get());
  EXPECT_EQ(0x1D538, iter.PreviousCodePoint());
  // This shouldn't crash.
  iter.NextCodePoint();
  EXPECT_TRUE(iter.Advance());

  EXPECT_TRUE(iter.end());
  EXPECT_FALSE(iter.start());
  EXPECT_EQ(5u, iter.array_pos());
  EXPECT_EQ(4, iter.char_offset());
  EXPECT_EQ('z', iter.PreviousCodePoint());

  // Don't care what it returns, but these shouldn't crash
  iter.get();
  iter.NextCodePoint();

  EXPECT_FALSE(iter.Advance());
}

TEST(CharIteratorsTest, TestUTF16_Rewind) {
  std::u16string str = UTF8ToUTF16(kTestString);

  // It is valid for the starting array index to be on the terminating null
  // character; in fact, this is where end() reports true. So we'll start on the
  // terminator for this test so we can check the behavior of end().
  UTF16CharIterator iter = UTF16CharIterator::UpperBound(str, str.length());
  EXPECT_TRUE(iter.end());
  EXPECT_FALSE(iter.start());
  // This is the index of the terminating null character, and the length of the
  // string in char16s.
  EXPECT_EQ(5u, iter.array_pos());
  EXPECT_EQ(0, iter.char_offset());
  EXPECT_EQ('z', iter.PreviousCodePoint());
  // Don't care what it returns, but these shouldn't crash
  iter.get();
  iter.NextCodePoint();
  EXPECT_TRUE(iter.Rewind());

  EXPECT_FALSE(iter.end());
  EXPECT_FALSE(iter.start());
  EXPECT_EQ(4u, iter.array_pos());
  EXPECT_EQ(-1, iter.char_offset());
  EXPECT_EQ('z', iter.get());
  EXPECT_EQ(0x1D538, iter.PreviousCodePoint());
  // This shouldn't crash.
  iter.NextCodePoint();
  EXPECT_TRUE(iter.Rewind());

  EXPECT_FALSE(iter.end());
  EXPECT_FALSE(iter.start());
  EXPECT_EQ(2u, iter.array_pos());
  EXPECT_EQ(-2, iter.char_offset());
  EXPECT_EQ(0x1D538, iter.get());
  EXPECT_EQ(0xFB, iter.PreviousCodePoint());
  EXPECT_EQ('z', iter.NextCodePoint());
  EXPECT_TRUE(iter.Rewind());

  EXPECT_FALSE(iter.end());
  EXPECT_FALSE(iter.start());
  EXPECT_EQ(1u, iter.array_pos());
  EXPECT_EQ(-3, iter.char_offset());
  EXPECT_EQ(0xFB, iter.get());
  EXPECT_EQ('x', iter.PreviousCodePoint());
  EXPECT_EQ(0x1D538, iter.NextCodePoint());
  EXPECT_TRUE(iter.Rewind());

  EXPECT_FALSE(iter.end());
  EXPECT_TRUE(iter.start());
  EXPECT_EQ(0u, iter.array_pos());
  EXPECT_EQ(-4, iter.char_offset());
  EXPECT_EQ('x', iter.get());
  EXPECT_EQ(0xFB, iter.NextCodePoint());
  // This shouldn't crash.
  iter.PreviousCodePoint();

  EXPECT_FALSE(iter.Rewind());
}

TEST(CharIteratorsTest, TestUTF16_UpperBound) {
  std::u16string str = UTF8ToUTF16(kTestString);
  ASSERT_EQ(0u, UTF16CharIterator::UpperBound(str, 0).array_pos());
  ASSERT_EQ(1u, UTF16CharIterator::UpperBound(str, 1).array_pos());
  ASSERT_EQ(2u, UTF16CharIterator::UpperBound(str, 2).array_pos());
  ASSERT_EQ(4u, UTF16CharIterator::UpperBound(str, 3).array_pos());
  ASSERT_EQ(4u, UTF16CharIterator::UpperBound(str, 4).array_pos());
  ASSERT_EQ(5u, UTF16CharIterator::UpperBound(str, 5).array_pos());
}

TEST(CharIteratorsTest, TestUTF16_LowerBound) {
  std::u16string str = UTF8ToUTF16(kTestString);
  ASSERT_EQ(0u, UTF16CharIterator::LowerBound(str, 0).array_pos());
  ASSERT_EQ(1u, UTF16CharIterator::LowerBound(str, 1).array_pos());
  ASSERT_EQ(2u, UTF16CharIterator::LowerBound(str, 2).array_pos());
  ASSERT_EQ(2u, UTF16CharIterator::LowerBound(str, 3).array_pos());
  ASSERT_EQ(4u, UTF16CharIterator::LowerBound(str, 4).array_pos());
  ASSERT_EQ(5u, UTF16CharIterator::LowerBound(str, 5).array_pos());
}

}  // namespace i18n
}  // namespace base