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