• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Android Open Source Project
2 //
3 // This software is licensed under the terms of the GNU General Public
4 // License version 2, as published by the Free Software Foundation, and
5 // may be copied, distributed, and modified under those terms.
6 //
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 // GNU General Public License for more details.
11 
12 #include "android/base/StringView.h"
13 
14 #include <gtest/gtest.h>
15 
16 namespace android {
17 namespace base {
18 
TEST(StringView,InitEmpty)19 TEST(StringView, InitEmpty) {
20     StringView view;
21     EXPECT_TRUE(view.empty());
22 }
23 
TEST(StringView,InitWithCString)24 TEST(StringView, InitWithCString) {
25     static const char kString[] = "Hello";
26     StringView view(kString);
27     EXPECT_STREQ(kString, view.str());
28     EXPECT_FALSE(view.empty());
29     EXPECT_EQ(strlen(kString), view.size());
30 }
31 
TEST(StringView,InitWithStringView)32 TEST(StringView, InitWithStringView) {
33     static const char kString[] = "Hello2";
34     StringView view1(kString);
35     StringView view2(view1);
36     EXPECT_FALSE(view2.empty());
37     EXPECT_STREQ(kString, view2.str());
38     EXPECT_EQ(strlen(kString), view2.size());
39 }
40 
TEST(StringView,Clear)41 TEST(StringView, Clear) {
42     StringView view("Hello3");
43     EXPECT_FALSE(view.empty());
44     view.clear();
45     EXPECT_TRUE(view.empty());
46     EXPECT_EQ(0U, view.size());
47     EXPECT_FALSE(view.str());
48 }
49 
TEST(StringView,SetEmpty)50 TEST(StringView, SetEmpty) {
51     StringView view("Hello4");
52     view.set("");
53     EXPECT_TRUE(view.empty());
54 }
55 
TEST(StringView,SetEmptyWithLength)56 TEST(StringView, SetEmptyWithLength) {
57     StringView view("Hello5");
58     view.set("Oops", 0U);
59     EXPECT_TRUE(view.empty());
60 }
61 
TEST(StringView,SetWithCString)62 TEST(StringView, SetWithCString) {
63     static const char kString[] = "Wow";
64     StringView view("Hello6");
65     view.set(kString);
66     EXPECT_EQ(kString, view.str());
67     EXPECT_EQ(strlen(kString), view.size());
68 }
69 
TEST(StringView,SetWithStringView)70 TEST(StringView, SetWithStringView) {
71     static const char kString[] = "Wazza";
72     StringView view1(kString);
73     StringView view("Nope");
74     view.set(view1);
75     EXPECT_EQ(kString, view.str());
76     EXPECT_EQ(strlen(kString), view.size());
77 }
78 
TEST(StringView,OperatorAt)79 TEST(StringView, OperatorAt) {
80     static const char kString[] = "Whatever";
81     static const size_t kStringLen = sizeof(kString) - 1;
82     StringView view(kString);
83     for (size_t n = 0; n < kStringLen; ++n) {
84         EXPECT_EQ(kString[n], view[n]) << "at index " << n;
85     }
86 }
87 
TEST(StringView,Iterators)88 TEST(StringView, Iterators) {
89     static const char kString[] = "What else?";
90     static const size_t kStringLen = sizeof(kString) - 1;
91     StringView view(kString);
92     EXPECT_EQ(kString, view.begin());
93     EXPECT_EQ(kString + kStringLen, view.end());
94 
95     size_t n = 0;
96     for (StringView::const_iterator it = view.begin();
97          it != view.end(); ++it, ++n) {
98         EXPECT_EQ(kString[n], *it);
99     }
100 }
101 
TEST(StringView,ComparisonOperators)102 TEST(StringView, ComparisonOperators) {
103     char kHello1[] = "Hello";
104     char kHello2[] = "Hello";
105     StringView view1(kHello1);
106     StringView view2(kHello2);
107     EXPECT_TRUE(view1 == view2);
108     EXPECT_FALSE(view1 != view2);
109     EXPECT_TRUE(view1 <= view2);
110     EXPECT_TRUE(view1 >= view2);
111     EXPECT_FALSE(view1 < view2);
112     EXPECT_FALSE(view1 > view2);
113 
114     StringView view3("hell");  // Shorter, but first char is larger.
115     EXPECT_FALSE(view1 == view3);
116     EXPECT_TRUE(view1 != view3);
117     EXPECT_TRUE(view1 < view3);
118     EXPECT_TRUE(view1 <= view3);
119     EXPECT_FALSE(view1 > view3);
120     EXPECT_FALSE(view1 >= view3);
121 
122     StringView view4("Hell");  // Shorter, but first char is smaller.
123     EXPECT_FALSE(view1 == view4);
124     EXPECT_TRUE(view1 != view4);
125     EXPECT_FALSE(view1 < view4);
126     EXPECT_FALSE(view1 <= view4);
127     EXPECT_TRUE(view1 > view4);
128     EXPECT_TRUE(view1 >= view4);
129 }
130 
131 // TODO(digit): String
132 
133 }  // namespace base
134 }  // namespace android
135