1 // Copyright 2013 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
5 #include <sstream>
6 #include <unordered_set>
7
8 #include "base/strings/string16.h"
9
10 #include "base/strings/utf_string_conversions.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace base {
14
15 // We define a custom operator<< for string16 so we can use it with logging.
16 // This tests that conversion.
TEST(String16Test,OutputStream)17 TEST(String16Test, OutputStream) {
18 // Basic stream test.
19 {
20 std::ostringstream stream;
21 stream << "Empty '" << string16() << "' standard '"
22 << string16(ASCIIToUTF16("Hello, world")) << "'";
23 EXPECT_STREQ("Empty '' standard 'Hello, world'",
24 stream.str().c_str());
25 }
26
27 // Interesting edge cases.
28 {
29 // These should each get converted to the invalid character: EF BF BD.
30 string16 initial_surrogate;
31 initial_surrogate.push_back(0xd800);
32 string16 final_surrogate;
33 final_surrogate.push_back(0xdc00);
34
35 // Old italic A = U+10300, will get converted to: F0 90 8C 80 'z'.
36 string16 surrogate_pair;
37 surrogate_pair.push_back(0xd800);
38 surrogate_pair.push_back(0xdf00);
39 surrogate_pair.push_back('z');
40
41 // Will get converted to the invalid char + 's': EF BF BD 's'.
42 string16 unterminated_surrogate;
43 unterminated_surrogate.push_back(0xd800);
44 unterminated_surrogate.push_back('s');
45
46 std::ostringstream stream;
47 stream << initial_surrogate << "," << final_surrogate << ","
48 << surrogate_pair << "," << unterminated_surrogate;
49
50 EXPECT_STREQ("\xef\xbf\xbd,\xef\xbf\xbd,\xf0\x90\x8c\x80z,\xef\xbf\xbds",
51 stream.str().c_str());
52 }
53 }
54
TEST(String16Test,Hash)55 TEST(String16Test, Hash) {
56 string16 str1 = ASCIIToUTF16("hello");
57 string16 str2 = ASCIIToUTF16("world");
58
59 std::unordered_set<string16> set;
60
61 set.insert(str1);
62 EXPECT_EQ(1u, set.count(str1));
63 EXPECT_EQ(0u, set.count(str2));
64 }
65
66 } // namespace base
67