1 /*
2  * Copyright (C) 2017 Open Source Robotics Foundation
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 #include <gtest/gtest.h>
18 #include <string>
19 #include <vector>
20 
21 
22 #include "ignition/common/StringUtils.hh"
23 
24 using namespace ignition;
25 
26 /////////////////////////////////////////////////
TEST(StringUtils,SplitNoDelimiterPresent)27 TEST(StringUtils, SplitNoDelimiterPresent)
28 {
29   char delim = ':';
30   std::string orig = "Hello World!";
31   std::vector<std::string> pieces = common::Split(orig, delim);
32   ASSERT_LT(0u, pieces.size());
33   EXPECT_EQ(1u, pieces.size());
34   EXPECT_EQ(orig, pieces[0]);
35 }
36 
37 /////////////////////////////////////////////////
TEST(StringUtils,SplitOneDelimiterInMiddle)38 TEST(StringUtils, SplitOneDelimiterInMiddle)
39 {
40   char delim = ' ';
41   std::string orig = "Hello World!";
42   std::vector<std::string> pieces = common::Split(orig, delim);
43   ASSERT_LT(1u, pieces.size());
44   EXPECT_EQ(2u, pieces.size());
45   EXPECT_EQ("Hello", pieces[0]);
46   EXPECT_EQ("World!", pieces[1]);
47 }
48 
49 /////////////////////////////////////////////////
TEST(StringUtils,SplitOneDelimiterAtBeginning)50 TEST(StringUtils, SplitOneDelimiterAtBeginning)
51 {
52   char delim = ':';
53   std::string orig = ":Hello World!";
54   std::vector<std::string> pieces = common::Split(orig, delim);
55   ASSERT_LT(1u, pieces.size());
56   EXPECT_EQ(2u, pieces.size());
57   EXPECT_EQ("", pieces[0]);
58   EXPECT_EQ("Hello World!", pieces[1]);
59 }
60 
61 /////////////////////////////////////////////////
TEST(StringUtils,SplitOneDelimiterAtEnd)62 TEST(StringUtils, SplitOneDelimiterAtEnd)
63 {
64   char delim = '!';
65   std::string orig = "Hello World!";
66   std::vector<std::string> pieces = common::Split(orig, delim);
67   ASSERT_LT(1u, pieces.size());
68   EXPECT_EQ(2u, pieces.size());
69   EXPECT_EQ("Hello World", pieces[0]);
70   EXPECT_EQ("", pieces[1]);
71 }
72 
73 /////////////////////////////////////////////////
TEST(StartsWith,NotInString)74 TEST(StartsWith, NotInString)
75 {
76   std::string big = "Hello World!";
77   std::string little = "asdf";
78   EXPECT_FALSE(common::StartsWith(big, little));
79 }
80 
81 /////////////////////////////////////////////////
TEST(StartsWith,InMiddle)82 TEST(StartsWith, InMiddle)
83 {
84   std::string big = "Hello World!";
85   std::string little = "ello";
86   EXPECT_FALSE(common::StartsWith(big, little));
87 }
88 
89 /////////////////////////////////////////////////
TEST(StartsWith,AtEnd)90 TEST(StartsWith, AtEnd)
91 {
92   std::string big = "Hello World!";
93   std::string little = "!";
94   EXPECT_FALSE(common::StartsWith(big, little));
95 }
96 
97 /////////////////////////////////////////////////
TEST(StartsWith,AtBeginning)98 TEST(StartsWith, AtBeginning)
99 {
100   std::string big = "Hello World!";
101   std::string little = "He";
102   EXPECT_TRUE(common::StartsWith(big, little));
103 }
104 
105 /////////////////////////////////////////////////
TEST(EndsWith,NotInString)106 TEST(EndsWith, NotInString)
107 {
108   std::string big = "Hello World!";
109   std::string little = "asdf";
110   EXPECT_FALSE(common::EndsWith(big, little));
111 }
112 
113 /////////////////////////////////////////////////
TEST(EndsWith,InMiddle)114 TEST(EndsWith, InMiddle)
115 {
116   std::string big = "Hello World!";
117   std::string little = "ello";
118   EXPECT_FALSE(common::EndsWith(big, little));
119 }
120 
121 /////////////////////////////////////////////////
TEST(EndsWith,AtEnd)122 TEST(EndsWith, AtEnd)
123 {
124   std::string big = "Hello World!";
125   std::string little = "!";
126   EXPECT_TRUE(common::EndsWith(big, little));
127 }
128 
129 /////////////////////////////////////////////////
TEST(EndsWith,AtBeginning)130 TEST(EndsWith, AtBeginning)
131 {
132   std::string big = "Hello World!";
133   std::string little = "He";
134   EXPECT_FALSE(common::EndsWith(big, little));
135 }
136 
137 /////////////////////////////////////////////////
TEST(EndsWith,PluralCast)138 TEST(EndsWith, PluralCast)
139 {
140   EXPECT_EQ("cows", common::PluralCast("cow", 0));
141   EXPECT_EQ("cow",  common::PluralCast("cow", 1));
142   EXPECT_EQ("cows", common::PluralCast("cow", 2));
143   EXPECT_EQ("cows", common::PluralCast("cow", 4));
144 
145   EXPECT_EQ("cacti",  common::PluralCast("cactus", "cacti", 0));
146   EXPECT_EQ("cactus", common::PluralCast("cactus", "cacti", 1));
147   EXPECT_EQ("cacti",  common::PluralCast("cactus", "cacti", 2));
148   EXPECT_EQ("cacti",  common::PluralCast("cactus", "cacti", 4));
149 
150   EXPECT_EQ("bear", common::PluralCast("bear", -1));
151   EXPECT_EQ("bears", common::PluralCast("bear", -2));
152   EXPECT_EQ("bears", common::PluralCast("bear", -3));
153   EXPECT_EQ("bears", common::PluralCast("bear", -4));
154 
155   EXPECT_EQ("ox", common::PluralCast("ox", "oxen", -1));
156   EXPECT_EQ("oxen", common::PluralCast("ox", "oxen", -2));
157   EXPECT_EQ("oxen", common::PluralCast("ox", "oxen", -3));
158   EXPECT_EQ("oxen", common::PluralCast("ox", "oxen", -4));
159 }
160 
161 /////////////////////////////////////////////////
main(int argc,char ** argv)162 int main(int argc, char **argv)
163 {
164   ::testing::InitGoogleTest(&argc, argv);
165   return RUN_ALL_TESTS();
166 }
167 
168