Path: blob/main/unittest/src/utils/common/StringTokenizerTest.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 StringTokenizerTest.cpp14/// @author Matthias Heppner15/// @author Michael Behrisch16/// @date 2009-03-2417///18//19/****************************************************************************/20#include <config.h>2122#include <gtest/gtest.h>23#include <utils/common/StringTokenizer.h>24#include <utils/common/UtilExceptions.h>2526/*27Tests StringTokenizer class from <SUMO>/src/utils/common28*/2930/* Tests the behaviour with a StringTokenizer::WHITECHAR for splitting a string.*/31TEST(StringTokenizer, test_split_with_whitechar) {32StringTokenizer strTok("Hello World", StringTokenizer::WHITECHARS);33EXPECT_TRUE(strTok.hasNext()) << "There must be more tokens available.";34EXPECT_EQ("Hello", strTok.next());35EXPECT_EQ("World", strTok.next());36EXPECT_FALSE(strTok.hasNext()) << "No tokens should be available.";37}3839/* Tests the behaviour with a StringTokenizer::NEWLINE for splitting a string.*/40TEST(StringTokenizer, test_split_with_newline) {41StringTokenizer strTok("Hello\nWorld", StringTokenizer::NEWLINE);42EXPECT_TRUE(strTok.hasNext()) << "There must be more tokens available.";43EXPECT_EQ("Hello", strTok.next());44EXPECT_EQ("World", strTok.next());45EXPECT_FALSE(strTok.hasNext()) << "No tokens should be available.";46}4748/* Tests the behaviour with any tokens for splitting a string.*/49TEST(StringTokenizer, test_split_with_x) {50StringTokenizer strTok("HelloxxWorld", "x");51EXPECT_TRUE(strTok.hasNext()) << "There must be more tokens available.";52EXPECT_EQ("Hello", strTok.next());53EXPECT_EQ("", strTok.next());54EXPECT_EQ("World", strTok.next());55EXPECT_FALSE(strTok.hasNext()) << "No tokens should be available.";56}5758/* Tests the behaviour with any tokens for splitting a string with the option splitAtAllChars=true*/59TEST(StringTokenizer, test_split_any_char) {60StringTokenizer strTok("HelloxWyorld", "xy", true);61EXPECT_TRUE(strTok.hasNext()) << "There must be more tokens available.";62EXPECT_EQ("Hello", strTok.next());63EXPECT_EQ("W", strTok.next());64EXPECT_EQ("orld", strTok.next());65EXPECT_FALSE(strTok.hasNext()) << "No tokens should be available.";66}6768/* Tests the method reinit*/69TEST(StringTokenizer, test_method_reinit) {70StringTokenizer strTok("Hello");71strTok.next();72EXPECT_FALSE(strTok.hasNext()) << "No tokens should be available.";73strTok.reinit();74EXPECT_TRUE(strTok.hasNext()) << "There must be more tokens available.";75}7677/* Tests the method size*/78TEST(StringTokenizer, test_method_size) {79StringTokenizer strTok("Hello little World");80EXPECT_EQ(3, strTok.size()) << "The number of the token is not right.";81StringTokenizer strTok2("");82EXPECT_EQ(0, strTok2.size()) << "The number of the token is not right.";83}8485/* Tests the method front*/86TEST(StringTokenizer, test_method_front) {87StringTokenizer strTok("Hello World");88EXPECT_EQ("Hello", strTok.front()) << "The first token is not right.";89strTok.next();90EXPECT_EQ("Hello", strTok.front()) << "The first token is not right.";91}9293/* Tests the method get*/94TEST(StringTokenizer, test_method_get) {95StringTokenizer strTok("Hello World");96EXPECT_EQ("Hello", strTok.get(0)) << "The first token is not right.";97EXPECT_EQ("World", strTok.get(1)) << "The second token is not right.";98ASSERT_THROW(strTok.get(2), OutOfBoundsException) << "Expect an OutOfBoundsException exception.";99}100101/* Tests the method get with empty data*/102TEST(StringTokenizer, test_method_get_with_empty_data) {103StringTokenizer strTok;104ASSERT_THROW(strTok.get(0), OutOfBoundsException) << "Expect an OutOfBoundsException exception.";105}106107/* Tests the method getVector*/108TEST(StringTokenizer, test_method_getVector) {109StringTokenizer strTok("Hello World");110std::vector<std::string> strVek = strTok.getVector();111EXPECT_EQ("World", strVek.back());112EXPECT_EQ("Hello", strVek.front());113}114115116