string16_unittest.cc revision 4a5e2dc747d50c653511c68ccb2cfbfb740bd5a7
1// Copyright (c) 2009 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
7#include "base/string16.h"
8#include "base/utf_string_conversions.h"
9#include "testing/gtest/include/gtest/gtest.h"
10
11#if defined(WCHAR_T_IS_UTF32)
12
13// We define a custom operator<< for string16 so we can use it with logging.
14// This tests that conversion.
15TEST(String16Test, OutputStream) {
16  // Basic stream test.
17  {
18    std::ostringstream stream;
19    stream << "Empty '" << string16() << "' standard '"
20           << string16(ASCIIToUTF16("Hello, world")) << "'";
21    EXPECT_STREQ("Empty '' standard 'Hello, world'",
22                 stream.str().c_str());
23  }
24
25  // Interesting edge cases.
26  {
27    // These should each get converted to the invalid character: EF BF BD.
28    string16 initial_surrogate;
29    initial_surrogate.push_back(0xd800);
30    string16 final_surrogate;
31    final_surrogate.push_back(0xdc00);
32
33    // Old italic A = U+10300, will get converted to: F0 90 8C 80 'z'.
34    string16 surrogate_pair;
35    surrogate_pair.push_back(0xd800);
36    surrogate_pair.push_back(0xdf00);
37    surrogate_pair.push_back('z');
38
39    // Will get converted to the invalid char + 's': EF BF BD 's'.
40    string16 unterminated_surrogate;
41    unterminated_surrogate.push_back(0xd800);
42    unterminated_surrogate.push_back('s');
43
44    std::ostringstream stream;
45    stream << initial_surrogate << "," << final_surrogate << ","
46           << surrogate_pair << "," << unterminated_surrogate;
47
48    EXPECT_STREQ("\xef\xbf\xbd,\xef\xbf\xbd,\xf0\x90\x8c\x80z,\xef\xbf\xbds",
49                 stream.str().c_str());
50  }
51}
52
53#endif
54