string16_unittest.cc 1.7 KB

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