String8_test.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (C) 2010 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #define LOG_TAG "String8_test"
  17. #include <utils/Log.h>
  18. #include <utils/String8.h>
  19. #include <utils/String16.h>
  20. #include <gtest/gtest.h>
  21. namespace android {
  22. class String8Test : public testing::Test {
  23. protected:
  24. virtual void SetUp() {
  25. }
  26. virtual void TearDown() {
  27. }
  28. };
  29. TEST_F(String8Test, Cstr) {
  30. String8 tmp("Hello, world!");
  31. EXPECT_STREQ(tmp.string(), "Hello, world!");
  32. }
  33. TEST_F(String8Test, OperatorPlus) {
  34. String8 src1("Hello, ");
  35. // Test adding String8 + const char*
  36. const char* ccsrc2 = "world!";
  37. String8 dst1 = src1 + ccsrc2;
  38. EXPECT_STREQ(dst1.string(), "Hello, world!");
  39. EXPECT_STREQ(src1.string(), "Hello, ");
  40. EXPECT_STREQ(ccsrc2, "world!");
  41. // Test adding String8 + String8
  42. String8 ssrc2("world!");
  43. String8 dst2 = src1 + ssrc2;
  44. EXPECT_STREQ(dst2.string(), "Hello, world!");
  45. EXPECT_STREQ(src1.string(), "Hello, ");
  46. EXPECT_STREQ(ssrc2.string(), "world!");
  47. }
  48. TEST_F(String8Test, OperatorPlusEquals) {
  49. String8 src1("My voice");
  50. // Testing String8 += String8
  51. String8 src2(" is my passport.");
  52. src1 += src2;
  53. EXPECT_STREQ(src1.string(), "My voice is my passport.");
  54. EXPECT_STREQ(src2.string(), " is my passport.");
  55. // Adding const char* to the previous string.
  56. const char* src3 = " Verify me.";
  57. src1 += src3;
  58. EXPECT_STREQ(src1.string(), "My voice is my passport. Verify me.");
  59. EXPECT_STREQ(src2.string(), " is my passport.");
  60. EXPECT_STREQ(src3, " Verify me.");
  61. }
  62. TEST_F(String8Test, SetToSizeMaxReturnsNoMemory) {
  63. const char *in = "some string";
  64. EXPECT_EQ(NO_MEMORY, String8("").setTo(in, SIZE_MAX));
  65. }
  66. // http://b/29250543
  67. TEST_F(String8Test, CorrectInvalidSurrogate) {
  68. // d841d8 is an invalid start for a surrogate pair. Make sure this is handled by ignoring the
  69. // first character in the pair and handling the rest correctly.
  70. String16 string16(u"\xd841\xd841\xdc41\x0000");
  71. String8 string8(string16);
  72. EXPECT_EQ(4U, string8.length());
  73. }
  74. TEST_F(String8Test, CheckUtf32Conversion) {
  75. // Since bound checks were added, check the conversion can be done without fatal errors.
  76. // The utf8 lengths of these are chars are 1 + 2 + 3 + 4 = 10.
  77. const char32_t string32[] = U"\x0000007f\x000007ff\x0000911\x0010fffe";
  78. String8 string8(string32);
  79. EXPECT_EQ(10U, string8.length());
  80. }
  81. }