Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/unittest/src/utils/common/StringUtilsTest.cpp
169684 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 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
TEST(StringUtils, test_method_prune) {
37
EXPECT_EQ("result", StringUtils::prune(" result ")) << "Blanks at the beginning and at the end of a string must be removed.";
38
EXPECT_EQ("", StringUtils::prune(" ")) << "Blanks at the beginning and at the end of a string must be removed.";
39
}
40
41
/* Tests the method to_lower_case.*/
42
TEST(StringUtils, test_method_to_lower_case) {
43
EXPECT_EQ("hello", StringUtils::to_lower_case("HELLO")) << "String should be converted into small letter.";
44
EXPECT_EQ("world", StringUtils::to_lower_case("World")) << "String should be converted into small letter.";
45
std::string str;
46
EXPECT_EQ("", StringUtils::to_lower_case(str));
47
}
48
49
/* Tests the method to_lower_case.*/
50
TEST(StringUtils, test_method_latin1_to_utf8) {
51
EXPECT_EQ("\xC3\xA4", StringUtils::latin1_to_utf8("\xE4"));
52
EXPECT_EQ("\xC3\xB6", StringUtils::latin1_to_utf8("\xF6"));
53
std::string str;
54
EXPECT_EQ("", StringUtils::latin1_to_utf8(str));
55
}
56
57
/* Tests the method convertUmlaute.*/
58
TEST(StringUtils, test_method_convertUmlaute) {
59
EXPECT_EQ("ae", StringUtils::convertUmlaute("\xE4"));
60
EXPECT_EQ("Ae", StringUtils::convertUmlaute("\xC4"));
61
EXPECT_EQ("oe", StringUtils::convertUmlaute("\xF6"));
62
EXPECT_EQ("Oe", StringUtils::convertUmlaute("\xD6"));
63
EXPECT_EQ("ue", StringUtils::convertUmlaute("\xFC"));
64
EXPECT_EQ("Ue", StringUtils::convertUmlaute("\xDC"));
65
EXPECT_EQ("ss", StringUtils::convertUmlaute("\xDF"));
66
EXPECT_EQ("E", StringUtils::convertUmlaute("\xC9"));
67
EXPECT_EQ("e", StringUtils::convertUmlaute("\xE9"));
68
EXPECT_EQ("E", StringUtils::convertUmlaute("\xC8"));
69
EXPECT_EQ("e", StringUtils::convertUmlaute("\xE8"));
70
EXPECT_EQ("normal_string_no_umlaute", StringUtils::convertUmlaute("normal_string_no_umlaute"));
71
}
72
73
/* Tests the method replace. */
74
TEST(StringUtils, test_method_replace) {
75
EXPECT_EQ("helt", StringUtils::replace("hello", "lo", "t"));
76
EXPECT_EQ("heststo", StringUtils::replace("hello", "l", "st"));
77
EXPECT_EQ("", StringUtils::replace("", "l", "st"));
78
}
79
80
/* Tests the method replace with empty string. */
81
TEST(StringUtils, test_method_replace_empty_string) {
82
EXPECT_EQ("", StringUtils::replace("", "l", "st"));
83
}
84
85
/* Tests the method replace with empty second_argument */
86
TEST(StringUtils, test_method_replace_empty_second_argument) {
87
EXPECT_EQ("hello", StringUtils::replace("hello", "", "a"));
88
}
89
90
/* Tests the method replace with empty third_argument */
91
TEST(StringUtils, test_method_replace_empty_third_argument) {
92
EXPECT_EQ("hello", StringUtils::replace("hello", "a", ""));
93
EXPECT_EQ("heo", StringUtils::replace("hello", "l", ""));
94
EXPECT_EQ("he", StringUtils::replace("hell", "l", ""));
95
EXPECT_EQ("test", StringUtils::replace("ltestl", "l", ""));
96
}
97
98
/* Tests the method escapeXML. */
99
TEST(StringUtils, test_method_escapeXML) {
100
std::string str;
101
EXPECT_EQ("", StringUtils::escapeXML(str));
102
EXPECT_EQ("test", StringUtils::escapeXML("test")) << "nothing to be replaced.";
103
EXPECT_EQ("test&apos;s", StringUtils::escapeXML("test's")) << "' must be replaced.";
104
EXPECT_EQ("1&lt;2", StringUtils::escapeXML("1<2")) << "< must be replaced.";
105
EXPECT_EQ("2&gt;1", StringUtils::escapeXML("2>1")) << "> must be replaced.";
106
EXPECT_EQ("M&amp;M", StringUtils::escapeXML("M&M")) << "& must be replaced.";
107
EXPECT_EQ("&quot;test&quot;", StringUtils::escapeXML("\"test\"")) << "\" must be replaced.";
108
EXPECT_EQ("test", StringUtils::escapeXML("\01test\01"));
109
}
110
111
112
TEST(StringUtils, test_toInt) {
113
EXPECT_EQ(0, StringUtils::toInt("0"));
114
EXPECT_EQ(1, StringUtils::toInt("+1"));
115
EXPECT_EQ(-1, StringUtils::toInt("-1"));
116
EXPECT_THROW(StringUtils::toInt("1e0"), NumberFormatException);
117
EXPECT_THROW(StringUtils::toInt("100000000000"), NumberFormatException);
118
EXPECT_THROW(StringUtils::toInt(""), EmptyData);
119
}
120
121
TEST(StringUtils, test_toLong) {
122
EXPECT_EQ(0, StringUtils::toLong("0"));
123
EXPECT_EQ(1, StringUtils::toLong("+1"));
124
EXPECT_EQ(-1, StringUtils::toLong("-1"));
125
EXPECT_THROW(StringUtils::toLong("1e0"), NumberFormatException);
126
EXPECT_EQ(100000000000, StringUtils::toLong("100000000000"));
127
EXPECT_THROW(StringUtils::toLong(""), EmptyData);
128
}
129
130
TEST(StringUtils, test_toDouble) {
131
EXPECT_EQ(0., StringUtils::toDouble("0"));
132
EXPECT_EQ(1., StringUtils::toDouble("+1"));
133
EXPECT_EQ(-1., StringUtils::toDouble("-1"));
134
EXPECT_EQ(1., StringUtils::toDouble("1e0"));
135
EXPECT_EQ(10., StringUtils::toDouble("1e1"));
136
EXPECT_EQ(1., StringUtils::toDouble("1."));
137
EXPECT_EQ(1.1, StringUtils::toDouble("1.1"));
138
EXPECT_EQ(.1, StringUtils::toDouble(".1"));
139
EXPECT_THROW(StringUtils::toDouble("1,1"), NumberFormatException);
140
EXPECT_THROW(StringUtils::toDouble(",1"), NumberFormatException);
141
EXPECT_EQ(100000000000., StringUtils::toDouble("100000000000"));
142
EXPECT_THROW(StringUtils::toDouble(""), EmptyData);
143
EXPECT_THROW(StringUtils::toDouble("1e0x"), NumberFormatException);
144
EXPECT_THROW(StringUtils::toDouble("1x"), NumberFormatException);
145
}
146
147
TEST(StringUtils, test_toBool) {
148
// according to gtest issue 322 EXPECT_EQ(false, ...) triggers a gcc bug
149
EXPECT_EQ(true, StringUtils::toBool("true"));
150
EXPECT_FALSE(StringUtils::toBool("false"));
151
EXPECT_EQ(true, StringUtils::toBool("True"));
152
EXPECT_FALSE(StringUtils::toBool("False"));
153
EXPECT_EQ(true, StringUtils::toBool("yes"));
154
EXPECT_FALSE(StringUtils::toBool("no"));
155
EXPECT_EQ(true, StringUtils::toBool("on"));
156
EXPECT_FALSE(StringUtils::toBool("off"));
157
EXPECT_EQ(true, StringUtils::toBool("1"));
158
EXPECT_FALSE(StringUtils::toBool("0"));
159
EXPECT_EQ(true, StringUtils::toBool("x"));
160
EXPECT_FALSE(StringUtils::toBool("-"));
161
EXPECT_EQ(true, StringUtils::toBool("ON"));
162
EXPECT_THROW(StringUtils::toBool(""), EmptyData);
163
EXPECT_THROW(StringUtils::toBool("1e0"), BoolFormatException);
164
EXPECT_THROW(StringUtils::toBool("Trari"), BoolFormatException);
165
EXPECT_THROW(StringUtils::toBool("yessir"), BoolFormatException);
166
}
167
168
TEST(StringUtils, test_parseDist) {
169
EXPECT_EQ(0., StringUtils::parseDist("0"));
170
EXPECT_EQ(1., StringUtils::parseDist("1"));
171
EXPECT_EQ(1., StringUtils::parseDist("1m"));
172
EXPECT_EQ(1., StringUtils::parseDist("1 m"));
173
EXPECT_EQ(1000., StringUtils::parseDist("1 km"));
174
EXPECT_THROW(StringUtils::parseDist(""), EmptyData);
175
EXPECT_THROW(StringUtils::parseDist("1xm"), NumberFormatException);
176
}
177
178
TEST(StringUtils, test_parseSpeed) {
179
EXPECT_EQ(0., StringUtils::parseSpeed("0"));
180
EXPECT_EQ(1., StringUtils::parseSpeed("3.6"));
181
EXPECT_EQ(1., StringUtils::parseSpeed("1m/s"));
182
EXPECT_EQ(1., StringUtils::parseSpeed("1 m/s"));
183
EXPECT_EQ(1., StringUtils::parseSpeed("3.6 km/h"));
184
EXPECT_EQ(1., StringUtils::parseSpeed("3.6 kmh"));
185
EXPECT_EQ(1., StringUtils::parseSpeed("3.6 kmph"));
186
EXPECT_DOUBLE_EQ(1.609344 / 3.6, StringUtils::parseSpeed("1 mph"));
187
EXPECT_THROW(StringUtils::parseSpeed(""), EmptyData);
188
EXPECT_THROW(StringUtils::parseSpeed("1xm"), NumberFormatException);
189
}
190
191