1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/strings/utf_string_conversion_utils.h"
6
7 #include <string>
8
9 #include "base/strings/string_piece.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace base {
13
TEST(UtfStringConversionUtilsTest,CountUnicodeCharacters)14 TEST(UtfStringConversionUtilsTest, CountUnicodeCharacters) {
15 const struct TestCase {
16 std::string value;
17 size_t limit;
18 std::optional<size_t> count;
19 } test_cases[] = {
20 {"", 0, 0},
21 {"abc", 1, 1},
22 {"abc", 3, 3},
23 {"abc", 0, 0},
24 {"abc", 4, 3},
25 // The casts and u8 string literals are needed here so that we don't
26 // trigger linter errors about invalid ascii values.
27 {reinterpret_cast<const char*>(u8"abc\U0001F4A9"), 4, 4},
28 {reinterpret_cast<const char*>(u8"\U0001F4A9"), 1, 1},
29 {{1, static_cast<char>(-1)}, 5, std::nullopt},
30 };
31 for (const auto& test_case : test_cases) {
32 EXPECT_EQ(CountUnicodeCharacters(test_case.value, test_case.limit),
33 test_case.count);
34 }
35 }
36
37 } // namespace base
38