Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file StringUtilsTest.cpp
15 : /// @author Matthias Heppner
16 : /// @author Michael Behrisch
17 : /// @date 2009
18 : ///
19 : // Tests StringUtils class from <SUMO>/src/utils/common
20 : /****************************************************************************/
21 :
22 : // ===========================================================================
23 : // included modules
24 : // ===========================================================================
25 : #include <config.h>
26 :
27 : #include <gtest/gtest.h>
28 : #include <utils/common/StringUtils.h>
29 : #include <utils/common/UtilExceptions.h>
30 :
31 :
32 : // ===========================================================================
33 : // test definitions
34 : // ===========================================================================
35 : /* Tests the method prune. Cut the blanks at the beginning and at the end of a string*/
36 1 : TEST(StringUtils, test_method_prune) {
37 2 : EXPECT_EQ("result", StringUtils::prune(" result ")) << "Blanks at the beginning and at the end of a string must be removed.";
38 2 : EXPECT_EQ("", StringUtils::prune(" ")) << "Blanks at the beginning and at the end of a string must be removed.";
39 1 : }
40 :
41 : /* Tests the method to_lower_case.*/
42 1 : TEST(StringUtils, test_method_to_lower_case) {
43 2 : EXPECT_EQ("hello", StringUtils::to_lower_case("HELLO")) << "String should be converted into small letter.";
44 2 : EXPECT_EQ("world", StringUtils::to_lower_case("World")) << "String should be converted into small letter.";
45 : std::string str;
46 2 : EXPECT_EQ("", StringUtils::to_lower_case(str));
47 1 : }
48 :
49 : /* Tests the method to_lower_case.*/
50 1 : TEST(StringUtils, test_method_latin1_to_utf8) {
51 2 : EXPECT_EQ("\xC3\xA4", StringUtils::latin1_to_utf8("\xE4"));
52 2 : EXPECT_EQ("\xC3\xB6", StringUtils::latin1_to_utf8("\xF6"));
53 : std::string str;
54 2 : EXPECT_EQ("", StringUtils::latin1_to_utf8(str));
55 1 : }
56 :
57 : /* Tests the method convertUmlaute.*/
58 1 : TEST(StringUtils, test_method_convertUmlaute) {
59 2 : EXPECT_EQ("ae", StringUtils::convertUmlaute("\xE4"));
60 2 : EXPECT_EQ("Ae", StringUtils::convertUmlaute("\xC4"));
61 2 : EXPECT_EQ("oe", StringUtils::convertUmlaute("\xF6"));
62 2 : EXPECT_EQ("Oe", StringUtils::convertUmlaute("\xD6"));
63 2 : EXPECT_EQ("ue", StringUtils::convertUmlaute("\xFC"));
64 2 : EXPECT_EQ("Ue", StringUtils::convertUmlaute("\xDC"));
65 2 : EXPECT_EQ("ss", StringUtils::convertUmlaute("\xDF"));
66 2 : EXPECT_EQ("E", StringUtils::convertUmlaute("\xC9"));
67 2 : EXPECT_EQ("e", StringUtils::convertUmlaute("\xE9"));
68 2 : EXPECT_EQ("E", StringUtils::convertUmlaute("\xC8"));
69 2 : EXPECT_EQ("e", StringUtils::convertUmlaute("\xE8"));
70 2 : EXPECT_EQ("normal_string_no_umlaute", StringUtils::convertUmlaute("normal_string_no_umlaute"));
71 1 : }
72 :
73 : /* Tests the method replace. */
74 1 : TEST(StringUtils, test_method_replace) {
75 2 : EXPECT_EQ("helt", StringUtils::replace("hello", "lo", "t"));
76 2 : EXPECT_EQ("heststo", StringUtils::replace("hello", "l", "st"));
77 2 : EXPECT_EQ("", StringUtils::replace("", "l", "st"));
78 1 : }
79 :
80 : /* Tests the method replace with empty string. */
81 1 : TEST(StringUtils, test_method_replace_empty_string) {
82 2 : EXPECT_EQ("", StringUtils::replace("", "l", "st"));
83 1 : }
84 :
85 : /* Tests the method replace with empty second_argument */
86 1 : TEST(StringUtils, test_method_replace_empty_second_argument) {
87 2 : EXPECT_EQ("hello", StringUtils::replace("hello", "", "a"));
88 1 : }
89 :
90 : /* Tests the method replace with empty third_argument */
91 1 : TEST(StringUtils, test_method_replace_empty_third_argument) {
92 2 : EXPECT_EQ("hello", StringUtils::replace("hello", "a", ""));
93 2 : EXPECT_EQ("heo", StringUtils::replace("hello", "l", ""));
94 2 : EXPECT_EQ("he", StringUtils::replace("hell", "l", ""));
95 2 : EXPECT_EQ("test", StringUtils::replace("ltestl", "l", ""));
96 1 : }
97 :
98 : /* Tests the method escapeXML. */
99 2 : TEST(StringUtils, test_method_escapeXML) {
100 : std::string str;
101 2 : EXPECT_EQ("", StringUtils::escapeXML(str));
102 2 : EXPECT_EQ("test", StringUtils::escapeXML("test")) << "nothing to be replaced.";
103 2 : EXPECT_EQ("test's", StringUtils::escapeXML("test's")) << "' must be replaced.";
104 2 : EXPECT_EQ("1<2", StringUtils::escapeXML("1<2")) << "< must be replaced.";
105 2 : EXPECT_EQ("2>1", StringUtils::escapeXML("2>1")) << "> must be replaced.";
106 2 : EXPECT_EQ("M&M", StringUtils::escapeXML("M&M")) << "& must be replaced.";
107 2 : EXPECT_EQ(""test"", StringUtils::escapeXML("\"test\"")) << "\" must be replaced.";
108 2 : EXPECT_EQ("test", StringUtils::escapeXML("\01test\01"));
109 1 : }
110 :
111 :
112 1 : TEST(StringUtils, test_toInt) {
113 1 : EXPECT_EQ(0, StringUtils::toInt("0"));
114 1 : EXPECT_EQ(1, StringUtils::toInt("+1"));
115 1 : EXPECT_EQ(-1, StringUtils::toInt("-1"));
116 2 : EXPECT_THROW(StringUtils::toInt("1e0"), NumberFormatException);
117 2 : EXPECT_THROW(StringUtils::toInt("100000000000"), NumberFormatException);
118 2 : EXPECT_THROW(StringUtils::toInt(""), EmptyData);
119 1 : }
120 :
121 1 : TEST(StringUtils, test_toLong) {
122 1 : EXPECT_EQ(0, StringUtils::toLong("0"));
123 1 : EXPECT_EQ(1, StringUtils::toLong("+1"));
124 1 : EXPECT_EQ(-1, StringUtils::toLong("-1"));
125 2 : EXPECT_THROW(StringUtils::toLong("1e0"), NumberFormatException);
126 1 : EXPECT_EQ(100000000000, StringUtils::toLong("100000000000"));
127 2 : EXPECT_THROW(StringUtils::toLong(""), EmptyData);
128 1 : }
129 :
130 1 : TEST(StringUtils, test_toDouble) {
131 1 : EXPECT_EQ(0., StringUtils::toDouble("0"));
132 1 : EXPECT_EQ(1., StringUtils::toDouble("+1"));
133 1 : EXPECT_EQ(-1., StringUtils::toDouble("-1"));
134 1 : EXPECT_EQ(1., StringUtils::toDouble("1e0"));
135 1 : EXPECT_EQ(10., StringUtils::toDouble("1e1"));
136 1 : EXPECT_EQ(1., StringUtils::toDouble("1."));
137 1 : EXPECT_EQ(1.1, StringUtils::toDouble("1.1"));
138 1 : EXPECT_EQ(.1, StringUtils::toDouble(".1"));
139 2 : EXPECT_THROW(StringUtils::toDouble("1,1"), NumberFormatException);
140 2 : EXPECT_THROW(StringUtils::toDouble(",1"), NumberFormatException);
141 1 : EXPECT_EQ(100000000000., StringUtils::toDouble("100000000000"));
142 2 : EXPECT_THROW(StringUtils::toDouble(""), EmptyData);
143 2 : EXPECT_THROW(StringUtils::toDouble("1e0x"), NumberFormatException);
144 2 : EXPECT_THROW(StringUtils::toDouble("1x"), NumberFormatException);
145 1 : }
146 :
147 1 : TEST(StringUtils, test_toBool) {
148 : // according to gtest issue 322 EXPECT_EQ(false, ...) triggers a gcc bug
149 1 : EXPECT_EQ(true, StringUtils::toBool("true"));
150 1 : EXPECT_FALSE(StringUtils::toBool("false"));
151 1 : EXPECT_EQ(true, StringUtils::toBool("True"));
152 1 : EXPECT_FALSE(StringUtils::toBool("False"));
153 1 : EXPECT_EQ(true, StringUtils::toBool("yes"));
154 1 : EXPECT_FALSE(StringUtils::toBool("no"));
155 1 : EXPECT_EQ(true, StringUtils::toBool("on"));
156 1 : EXPECT_FALSE(StringUtils::toBool("off"));
157 1 : EXPECT_EQ(true, StringUtils::toBool("1"));
158 1 : EXPECT_FALSE(StringUtils::toBool("0"));
159 1 : EXPECT_EQ(true, StringUtils::toBool("x"));
160 1 : EXPECT_FALSE(StringUtils::toBool("-"));
161 1 : EXPECT_EQ(true, StringUtils::toBool("ON"));
162 2 : EXPECT_THROW(StringUtils::toBool(""), EmptyData);
163 2 : EXPECT_THROW(StringUtils::toBool("1e0"), BoolFormatException);
164 2 : EXPECT_THROW(StringUtils::toBool("Trari"), BoolFormatException);
165 2 : EXPECT_THROW(StringUtils::toBool("yessir"), BoolFormatException);
166 1 : }
167 :
168 1 : TEST(StringUtils, test_parseDist) {
169 1 : EXPECT_EQ(0., StringUtils::parseDist("0"));
170 1 : EXPECT_EQ(1., StringUtils::parseDist("1"));
171 1 : EXPECT_EQ(1., StringUtils::parseDist("1m"));
172 1 : EXPECT_EQ(1., StringUtils::parseDist("1 m"));
173 1 : EXPECT_EQ(1000., StringUtils::parseDist("1 km"));
174 2 : EXPECT_THROW(StringUtils::parseDist(""), EmptyData);
175 2 : EXPECT_THROW(StringUtils::parseDist("1xm"), NumberFormatException);
176 1 : }
177 :
178 1 : TEST(StringUtils, test_parseSpeed) {
179 1 : EXPECT_EQ(0., StringUtils::parseSpeed("0"));
180 1 : EXPECT_EQ(1., StringUtils::parseSpeed("3.6"));
181 1 : EXPECT_EQ(1., StringUtils::parseSpeed("1m/s"));
182 1 : EXPECT_EQ(1., StringUtils::parseSpeed("1 m/s"));
183 1 : EXPECT_EQ(1., StringUtils::parseSpeed("3.6 km/h"));
184 1 : EXPECT_EQ(1., StringUtils::parseSpeed("3.6 kmh"));
185 1 : EXPECT_EQ(1., StringUtils::parseSpeed("3.6 kmph"));
186 2 : EXPECT_DOUBLE_EQ(1.609344 / 3.6, StringUtils::parseSpeed("1 mph"));
187 2 : EXPECT_THROW(StringUtils::parseSpeed(""), EmptyData);
188 2 : EXPECT_THROW(StringUtils::parseSpeed("1xm"), NumberFormatException);
189 1 : }
|