1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 #include <gtest/gtest.h>
18 #include <StringUtil.h>
19 
20 
21 class StringUtilTest : public testing::Test {
22 
23 };
24 
TEST_F(StringUtilTest,compareTest)25 TEST_F(StringUtilTest, compareTest) {
26     android::String8 str("hello");
27     ASSERT_TRUE(StringUtil::compare(str, "hello") == 0);
28     ASSERT_TRUE(StringUtil::compare(str, "hi") != 0);
29 }
30 
TEST_F(StringUtilTest,substrTest)31 TEST_F(StringUtilTest, substrTest) {
32     android::String8 str("hello there");
33 
34     android::String8 sub1 = StringUtil::substr(str, 0, 5);
35     ASSERT_TRUE(StringUtil::compare(sub1, "hello") == 0);
36 
37     android::String8 sub2 = StringUtil::substr(str, 10, 5);
38     ASSERT_TRUE(StringUtil::compare(sub2, "e") == 0);
39 
40     android::String8 sub3 = StringUtil::substr(str, 6, 5);
41     ASSERT_TRUE(StringUtil::compare(sub3, "there") == 0);
42 
43     android::String8 sub4 = StringUtil::substr(str, 100, 5);
44     ASSERT_TRUE(sub4.length() == 0);
45 }
46 
TEST_F(StringUtilTest,endsWithTest)47 TEST_F(StringUtilTest, endsWithTest) {
48     android::String8 str("hello there");
49     ASSERT_TRUE(StringUtil::endsWith(str, "there"));
50     ASSERT_TRUE(StringUtil::endsWith(str, "hello there"));
51     ASSERT_TRUE(!StringUtil::endsWith(str, "not there"));
52 }
53 
TEST_F(StringUtilTest,splitTest)54 TEST_F(StringUtilTest, splitTest) {
55     android::String8 str("hello:there:break:this:");
56     std::vector<android::String8>* tokens = StringUtil::split(str, ':');
57     ASSERT_TRUE(tokens != NULL);
58     ASSERT_TRUE(tokens->size() == 4);
59     ASSERT_TRUE(StringUtil::compare(tokens->at(0), "hello") == 0);
60     ASSERT_TRUE(StringUtil::compare(tokens->at(1), "there") == 0);
61     ASSERT_TRUE(StringUtil::compare(tokens->at(2), "break") == 0);
62     ASSERT_TRUE(StringUtil::compare(tokens->at(3), "this") == 0);
63     delete tokens;
64 
65     android::String8 str2("::::");
66     std::vector<android::String8>* tokens2 = StringUtil::split(str2, ':');
67     ASSERT_TRUE(tokens2 != NULL);
68     ASSERT_TRUE(tokens2->size() == 0);
69     delete tokens2;
70 }
71 
72 
73 
74