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 17 #define LOG_TAG "String8_test" 18 #include <utils/Log.h> 19 #include <utils/String8.h> 20 21 #include <gtest/gtest.h> 22 23 namespace android { 24 25 class String8Test : public testing::Test { 26 protected: 27 virtual void SetUp() { 28 } 29 30 virtual void TearDown() { 31 } 32 }; 33 34 TEST_F(String8Test, Cstr) { 35 String8 tmp("Hello, world!"); 36 37 EXPECT_STREQ(tmp.string(), "Hello, world!"); 38 } 39 40 TEST_F(String8Test, OperatorPlus) { 41 String8 src1("Hello, "); 42 43 // Test adding String8 + const char* 44 const char* ccsrc2 = "world!"; 45 String8 dst1 = src1 + ccsrc2; 46 EXPECT_STREQ(dst1.string(), "Hello, world!"); 47 EXPECT_STREQ(src1.string(), "Hello, "); 48 EXPECT_STREQ(ccsrc2, "world!"); 49 50 // Test adding String8 + String8 51 String8 ssrc2("world!"); 52 String8 dst2 = src1 + ssrc2; 53 EXPECT_STREQ(dst2.string(), "Hello, world!"); 54 EXPECT_STREQ(src1.string(), "Hello, "); 55 EXPECT_STREQ(ssrc2.string(), "world!"); 56 } 57 58 TEST_F(String8Test, OperatorPlusEquals) { 59 String8 src1("My voice"); 60 61 // Testing String8 += String8 62 String8 src2(" is my passport."); 63 src1 += src2; 64 EXPECT_STREQ(src1.string(), "My voice is my passport."); 65 EXPECT_STREQ(src2.string(), " is my passport."); 66 67 // Adding const char* to the previous string. 68 const char* src3 = " Verify me."; 69 src1 += src3; 70 EXPECT_STREQ(src1.string(), "My voice is my passport. Verify me."); 71 EXPECT_STREQ(src2.string(), " is my passport."); 72 EXPECT_STREQ(src3, " Verify me."); 73 } 74 75 } 76