Path: blob/main/unittest/src/utils/common/StringUtilsTest.cpp
169684 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file StringUtilsTest.cpp14/// @author Matthias Heppner15/// @author Michael Behrisch16/// @date 200917///18// Tests StringUtils class from <SUMO>/src/utils/common19/****************************************************************************/2021// ===========================================================================22// included modules23// ===========================================================================24#include <config.h>2526#include <gtest/gtest.h>27#include <utils/common/StringUtils.h>28#include <utils/common/UtilExceptions.h>293031// ===========================================================================32// test definitions33// ===========================================================================34/* Tests the method prune. Cut the blanks at the beginning and at the end of a string*/35TEST(StringUtils, test_method_prune) {36EXPECT_EQ("result", StringUtils::prune(" result ")) << "Blanks at the beginning and at the end of a string must be removed.";37EXPECT_EQ("", StringUtils::prune(" ")) << "Blanks at the beginning and at the end of a string must be removed.";38}3940/* Tests the method to_lower_case.*/41TEST(StringUtils, test_method_to_lower_case) {42EXPECT_EQ("hello", StringUtils::to_lower_case("HELLO")) << "String should be converted into small letter.";43EXPECT_EQ("world", StringUtils::to_lower_case("World")) << "String should be converted into small letter.";44std::string str;45EXPECT_EQ("", StringUtils::to_lower_case(str));46}4748/* Tests the method to_lower_case.*/49TEST(StringUtils, test_method_latin1_to_utf8) {50EXPECT_EQ("\xC3\xA4", StringUtils::latin1_to_utf8("\xE4"));51EXPECT_EQ("\xC3\xB6", StringUtils::latin1_to_utf8("\xF6"));52std::string str;53EXPECT_EQ("", StringUtils::latin1_to_utf8(str));54}5556/* Tests the method convertUmlaute.*/57TEST(StringUtils, test_method_convertUmlaute) {58EXPECT_EQ("ae", StringUtils::convertUmlaute("\xE4"));59EXPECT_EQ("Ae", StringUtils::convertUmlaute("\xC4"));60EXPECT_EQ("oe", StringUtils::convertUmlaute("\xF6"));61EXPECT_EQ("Oe", StringUtils::convertUmlaute("\xD6"));62EXPECT_EQ("ue", StringUtils::convertUmlaute("\xFC"));63EXPECT_EQ("Ue", StringUtils::convertUmlaute("\xDC"));64EXPECT_EQ("ss", StringUtils::convertUmlaute("\xDF"));65EXPECT_EQ("E", StringUtils::convertUmlaute("\xC9"));66EXPECT_EQ("e", StringUtils::convertUmlaute("\xE9"));67EXPECT_EQ("E", StringUtils::convertUmlaute("\xC8"));68EXPECT_EQ("e", StringUtils::convertUmlaute("\xE8"));69EXPECT_EQ("normal_string_no_umlaute", StringUtils::convertUmlaute("normal_string_no_umlaute"));70}7172/* Tests the method replace. */73TEST(StringUtils, test_method_replace) {74EXPECT_EQ("helt", StringUtils::replace("hello", "lo", "t"));75EXPECT_EQ("heststo", StringUtils::replace("hello", "l", "st"));76EXPECT_EQ("", StringUtils::replace("", "l", "st"));77}7879/* Tests the method replace with empty string. */80TEST(StringUtils, test_method_replace_empty_string) {81EXPECT_EQ("", StringUtils::replace("", "l", "st"));82}8384/* Tests the method replace with empty second_argument */85TEST(StringUtils, test_method_replace_empty_second_argument) {86EXPECT_EQ("hello", StringUtils::replace("hello", "", "a"));87}8889/* Tests the method replace with empty third_argument */90TEST(StringUtils, test_method_replace_empty_third_argument) {91EXPECT_EQ("hello", StringUtils::replace("hello", "a", ""));92EXPECT_EQ("heo", StringUtils::replace("hello", "l", ""));93EXPECT_EQ("he", StringUtils::replace("hell", "l", ""));94EXPECT_EQ("test", StringUtils::replace("ltestl", "l", ""));95}9697/* Tests the method escapeXML. */98TEST(StringUtils, test_method_escapeXML) {99std::string str;100EXPECT_EQ("", StringUtils::escapeXML(str));101EXPECT_EQ("test", StringUtils::escapeXML("test")) << "nothing to be replaced.";102EXPECT_EQ("test's", StringUtils::escapeXML("test's")) << "' must be replaced.";103EXPECT_EQ("1<2", StringUtils::escapeXML("1<2")) << "< must be replaced.";104EXPECT_EQ("2>1", StringUtils::escapeXML("2>1")) << "> must be replaced.";105EXPECT_EQ("M&M", StringUtils::escapeXML("M&M")) << "& must be replaced.";106EXPECT_EQ(""test"", StringUtils::escapeXML("\"test\"")) << "\" must be replaced.";107EXPECT_EQ("test", StringUtils::escapeXML("\01test\01"));108}109110111TEST(StringUtils, test_toInt) {112EXPECT_EQ(0, StringUtils::toInt("0"));113EXPECT_EQ(1, StringUtils::toInt("+1"));114EXPECT_EQ(-1, StringUtils::toInt("-1"));115EXPECT_THROW(StringUtils::toInt("1e0"), NumberFormatException);116EXPECT_THROW(StringUtils::toInt("100000000000"), NumberFormatException);117EXPECT_THROW(StringUtils::toInt(""), EmptyData);118}119120TEST(StringUtils, test_toLong) {121EXPECT_EQ(0, StringUtils::toLong("0"));122EXPECT_EQ(1, StringUtils::toLong("+1"));123EXPECT_EQ(-1, StringUtils::toLong("-1"));124EXPECT_THROW(StringUtils::toLong("1e0"), NumberFormatException);125EXPECT_EQ(100000000000, StringUtils::toLong("100000000000"));126EXPECT_THROW(StringUtils::toLong(""), EmptyData);127}128129TEST(StringUtils, test_toDouble) {130EXPECT_EQ(0., StringUtils::toDouble("0"));131EXPECT_EQ(1., StringUtils::toDouble("+1"));132EXPECT_EQ(-1., StringUtils::toDouble("-1"));133EXPECT_EQ(1., StringUtils::toDouble("1e0"));134EXPECT_EQ(10., StringUtils::toDouble("1e1"));135EXPECT_EQ(1., StringUtils::toDouble("1."));136EXPECT_EQ(1.1, StringUtils::toDouble("1.1"));137EXPECT_EQ(.1, StringUtils::toDouble(".1"));138EXPECT_THROW(StringUtils::toDouble("1,1"), NumberFormatException);139EXPECT_THROW(StringUtils::toDouble(",1"), NumberFormatException);140EXPECT_EQ(100000000000., StringUtils::toDouble("100000000000"));141EXPECT_THROW(StringUtils::toDouble(""), EmptyData);142EXPECT_THROW(StringUtils::toDouble("1e0x"), NumberFormatException);143EXPECT_THROW(StringUtils::toDouble("1x"), NumberFormatException);144}145146TEST(StringUtils, test_toBool) {147// according to gtest issue 322 EXPECT_EQ(false, ...) triggers a gcc bug148EXPECT_EQ(true, StringUtils::toBool("true"));149EXPECT_FALSE(StringUtils::toBool("false"));150EXPECT_EQ(true, StringUtils::toBool("True"));151EXPECT_FALSE(StringUtils::toBool("False"));152EXPECT_EQ(true, StringUtils::toBool("yes"));153EXPECT_FALSE(StringUtils::toBool("no"));154EXPECT_EQ(true, StringUtils::toBool("on"));155EXPECT_FALSE(StringUtils::toBool("off"));156EXPECT_EQ(true, StringUtils::toBool("1"));157EXPECT_FALSE(StringUtils::toBool("0"));158EXPECT_EQ(true, StringUtils::toBool("x"));159EXPECT_FALSE(StringUtils::toBool("-"));160EXPECT_EQ(true, StringUtils::toBool("ON"));161EXPECT_THROW(StringUtils::toBool(""), EmptyData);162EXPECT_THROW(StringUtils::toBool("1e0"), BoolFormatException);163EXPECT_THROW(StringUtils::toBool("Trari"), BoolFormatException);164EXPECT_THROW(StringUtils::toBool("yessir"), BoolFormatException);165}166167TEST(StringUtils, test_parseDist) {168EXPECT_EQ(0., StringUtils::parseDist("0"));169EXPECT_EQ(1., StringUtils::parseDist("1"));170EXPECT_EQ(1., StringUtils::parseDist("1m"));171EXPECT_EQ(1., StringUtils::parseDist("1 m"));172EXPECT_EQ(1000., StringUtils::parseDist("1 km"));173EXPECT_THROW(StringUtils::parseDist(""), EmptyData);174EXPECT_THROW(StringUtils::parseDist("1xm"), NumberFormatException);175}176177TEST(StringUtils, test_parseSpeed) {178EXPECT_EQ(0., StringUtils::parseSpeed("0"));179EXPECT_EQ(1., StringUtils::parseSpeed("3.6"));180EXPECT_EQ(1., StringUtils::parseSpeed("1m/s"));181EXPECT_EQ(1., StringUtils::parseSpeed("1 m/s"));182EXPECT_EQ(1., StringUtils::parseSpeed("3.6 km/h"));183EXPECT_EQ(1., StringUtils::parseSpeed("3.6 kmh"));184EXPECT_EQ(1., StringUtils::parseSpeed("3.6 kmph"));185EXPECT_DOUBLE_EQ(1.609344 / 3.6, StringUtils::parseSpeed("1 mph"));186EXPECT_THROW(StringUtils::parseSpeed(""), EmptyData);187EXPECT_THROW(StringUtils::parseSpeed("1xm"), NumberFormatException);188}189190191