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 "testing/gtest/include/gtest/gtest.h"
10
11 namespace base {
12
TEST(UtfStringConversionUtilsTest,CountUnicodeCharacters)13 TEST(UtfStringConversionUtilsTest, CountUnicodeCharacters) {
14 const struct TestCase {
15 std::string value;
16 size_t limit;
17 std::optional<size_t> count;
18 } test_cases[] = {
19 {"", 0, 0},
20 {"abc", 1, 1},
21 {"abc", 3, 3},
22 {"abc", 0, 0},
23 {"abc", 4, 3},
24 // The casts and u8 string literals are needed here so that we don't
25 // trigger linter errors about invalid ascii values.
26 {reinterpret_cast<const char*>(u8"abc\U0001F4A9"), 4, 4},
27 {reinterpret_cast<const char*>(u8"\U0001F4A9"), 1, 1},
28 {{1, static_cast<char>(-1)}, 5, std::nullopt},
29 };
30 for (const auto& test_case : test_cases) {
31 EXPECT_EQ(CountUnicodeCharacters(test_case.value, test_case.limit),
32 test_case.count);
33 }
34 }
35
36 } // namespace base
37