string16_unittest.cc 1.7 KB
Newer Older
gejun's avatar
gejun committed
1 2 3 4 5 6
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <sstream>

7
#include "butil/strings/string16.h"
gejun's avatar
gejun committed
8

9
#include "butil/strings/utf_string_conversions.h"
gejun's avatar
gejun committed
10 11
#include <gtest/gtest.h>

12
namespace butil {
gejun's avatar
gejun committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

#if defined(WCHAR_T_IS_UTF32)

// We define a custom operator<< for string16 so we can use it with logging.
// This tests that conversion.
TEST(String16Test, OutputStream) {
  // Basic stream test.
  {
    std::ostringstream stream;
    stream << "Empty '" << string16() << "' standard '"
           << string16(ASCIIToUTF16("Hello, world")) << "'";
    EXPECT_STREQ("Empty '' standard 'Hello, world'",
                 stream.str().c_str());
  }

  // Interesting edge cases.
  {
    // These should each get converted to the invalid character: EF BF BD.
    string16 initial_surrogate;
    initial_surrogate.push_back(0xd800);
    string16 final_surrogate;
    final_surrogate.push_back(0xdc00);

    // Old italic A = U+10300, will get converted to: F0 90 8C 80 'z'.
    string16 surrogate_pair;
    surrogate_pair.push_back(0xd800);
    surrogate_pair.push_back(0xdf00);
    surrogate_pair.push_back('z');

    // Will get converted to the invalid char + 's': EF BF BD 's'.
    string16 unterminated_surrogate;
    unterminated_surrogate.push_back(0xd800);
    unterminated_surrogate.push_back('s');

    std::ostringstream stream;
    stream << initial_surrogate << "," << final_surrogate << ","
           << surrogate_pair << "," << unterminated_surrogate;

    EXPECT_STREQ("\xef\xbf\xbd,\xef\xbf\xbd,\xf0\x90\x8c\x80z,\xef\xbf\xbds",
                 stream.str().c_str());
  }
}

#endif

58
}  // namespace butil