utf16_indexing_unittest.cc 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  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
  3. // found in the LICENSE file.
  4. #include <stddef.h>
  5. #include "testing/gtest/include/gtest/gtest.h"
  6. #include "ui/gfx/utf16_indexing.h"
  7. namespace gfx {
  8. TEST(UTF16IndexingTest, IndexOffsetConversions) {
  9. // Valid surrogate pair surrounded by unpaired surrogates
  10. const char16_t foo[] = {0xDC00, 0xD800, 0xD800, 0xDFFF, 0xDFFF, 0xDBFF, 0};
  11. const std::u16string s(foo);
  12. const size_t the_invalid_index = 3;
  13. for (size_t i = 0; i <= s.length(); ++i)
  14. EXPECT_EQ(i != the_invalid_index, IsValidCodePointIndex(s, i));
  15. for (size_t i = 0; i <= s.length(); ++i) {
  16. for (size_t j = i; j <= s.length(); ++j) {
  17. ptrdiff_t offset = static_cast<ptrdiff_t>(j - i);
  18. if (i <= the_invalid_index && j > the_invalid_index)
  19. --offset;
  20. EXPECT_EQ(offset, UTF16IndexToOffset(s, i, j));
  21. EXPECT_EQ(-offset, UTF16IndexToOffset(s, j, i));
  22. size_t adjusted_j = (j == the_invalid_index) ? j + 1 : j;
  23. EXPECT_EQ(adjusted_j, UTF16OffsetToIndex(s, i, offset));
  24. size_t adjusted_i = (i == the_invalid_index) ? i + 1 : i;
  25. EXPECT_EQ(adjusted_i, UTF16OffsetToIndex(s, j, -offset));
  26. }
  27. }
  28. }
  29. } // namespace gfx