Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/unittest/src/utils/common/StringTokenizerTest.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 StringTokenizerTest.cpp
15
/// @author Matthias Heppner
16
/// @author Michael Behrisch
17
/// @date 2009-03-24
18
///
19
//
20
/****************************************************************************/
21
#include <config.h>
22
23
#include <gtest/gtest.h>
24
#include <utils/common/StringTokenizer.h>
25
#include <utils/common/UtilExceptions.h>
26
27
/*
28
Tests StringTokenizer class from <SUMO>/src/utils/common
29
*/
30
31
/* Tests the behaviour with a StringTokenizer::WHITECHAR for splitting a string.*/
32
TEST(StringTokenizer, test_split_with_whitechar) {
33
StringTokenizer strTok("Hello World", StringTokenizer::WHITECHARS);
34
EXPECT_TRUE(strTok.hasNext()) << "There must be more tokens available.";
35
EXPECT_EQ("Hello", strTok.next());
36
EXPECT_EQ("World", strTok.next());
37
EXPECT_FALSE(strTok.hasNext()) << "No tokens should be available.";
38
}
39
40
/* Tests the behaviour with a StringTokenizer::NEWLINE for splitting a string.*/
41
TEST(StringTokenizer, test_split_with_newline) {
42
StringTokenizer strTok("Hello\nWorld", StringTokenizer::NEWLINE);
43
EXPECT_TRUE(strTok.hasNext()) << "There must be more tokens available.";
44
EXPECT_EQ("Hello", strTok.next());
45
EXPECT_EQ("World", strTok.next());
46
EXPECT_FALSE(strTok.hasNext()) << "No tokens should be available.";
47
}
48
49
/* Tests the behaviour with any tokens for splitting a string.*/
50
TEST(StringTokenizer, test_split_with_x) {
51
StringTokenizer strTok("HelloxxWorld", "x");
52
EXPECT_TRUE(strTok.hasNext()) << "There must be more tokens available.";
53
EXPECT_EQ("Hello", strTok.next());
54
EXPECT_EQ("", strTok.next());
55
EXPECT_EQ("World", strTok.next());
56
EXPECT_FALSE(strTok.hasNext()) << "No tokens should be available.";
57
}
58
59
/* Tests the behaviour with any tokens for splitting a string with the option splitAtAllChars=true*/
60
TEST(StringTokenizer, test_split_any_char) {
61
StringTokenizer strTok("HelloxWyorld", "xy", true);
62
EXPECT_TRUE(strTok.hasNext()) << "There must be more tokens available.";
63
EXPECT_EQ("Hello", strTok.next());
64
EXPECT_EQ("W", strTok.next());
65
EXPECT_EQ("orld", strTok.next());
66
EXPECT_FALSE(strTok.hasNext()) << "No tokens should be available.";
67
}
68
69
/* Tests the method reinit*/
70
TEST(StringTokenizer, test_method_reinit) {
71
StringTokenizer strTok("Hello");
72
strTok.next();
73
EXPECT_FALSE(strTok.hasNext()) << "No tokens should be available.";
74
strTok.reinit();
75
EXPECT_TRUE(strTok.hasNext()) << "There must be more tokens available.";
76
}
77
78
/* Tests the method size*/
79
TEST(StringTokenizer, test_method_size) {
80
StringTokenizer strTok("Hello little World");
81
EXPECT_EQ(3, strTok.size()) << "The number of the token is not right.";
82
StringTokenizer strTok2("");
83
EXPECT_EQ(0, strTok2.size()) << "The number of the token is not right.";
84
}
85
86
/* Tests the method front*/
87
TEST(StringTokenizer, test_method_front) {
88
StringTokenizer strTok("Hello World");
89
EXPECT_EQ("Hello", strTok.front()) << "The first token is not right.";
90
strTok.next();
91
EXPECT_EQ("Hello", strTok.front()) << "The first token is not right.";
92
}
93
94
/* Tests the method get*/
95
TEST(StringTokenizer, test_method_get) {
96
StringTokenizer strTok("Hello World");
97
EXPECT_EQ("Hello", strTok.get(0)) << "The first token is not right.";
98
EXPECT_EQ("World", strTok.get(1)) << "The second token is not right.";
99
ASSERT_THROW(strTok.get(2), OutOfBoundsException) << "Expect an OutOfBoundsException exception.";
100
}
101
102
/* Tests the method get with empty data*/
103
TEST(StringTokenizer, test_method_get_with_empty_data) {
104
StringTokenizer strTok;
105
ASSERT_THROW(strTok.get(0), OutOfBoundsException) << "Expect an OutOfBoundsException exception.";
106
}
107
108
/* Tests the method getVector*/
109
TEST(StringTokenizer, test_method_getVector) {
110
StringTokenizer strTok("Hello World");
111
std::vector<std::string> strVek = strTok.getVector();
112
EXPECT_EQ("World", strVek.back());
113
EXPECT_EQ("Hello", strVek.front());
114
}
115
116