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 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 1 : TEST(StringTokenizer, test_split_with_whitechar) {
33 1 : StringTokenizer strTok("Hello World", StringTokenizer::WHITECHARS);
34 1 : EXPECT_TRUE(strTok.hasNext()) << "There must be more tokens available.";
35 2 : EXPECT_EQ("Hello", strTok.next());
36 2 : EXPECT_EQ("World", strTok.next());
37 1 : EXPECT_FALSE(strTok.hasNext()) << "No tokens should be available.";
38 1 : }
39 :
40 : /* Tests the behaviour with a StringTokenizer::NEWLINE for splitting a string.*/
41 1 : TEST(StringTokenizer, test_split_with_newline) {
42 1 : StringTokenizer strTok("Hello\nWorld", StringTokenizer::NEWLINE);
43 1 : EXPECT_TRUE(strTok.hasNext()) << "There must be more tokens available.";
44 2 : EXPECT_EQ("Hello", strTok.next());
45 2 : EXPECT_EQ("World", strTok.next());
46 1 : EXPECT_FALSE(strTok.hasNext()) << "No tokens should be available.";
47 1 : }
48 :
49 : /* Tests the behaviour with any tokens for splitting a string.*/
50 1 : TEST(StringTokenizer, test_split_with_x) {
51 2 : StringTokenizer strTok("HelloxxWorld", "x");
52 1 : EXPECT_TRUE(strTok.hasNext()) << "There must be more tokens available.";
53 2 : EXPECT_EQ("Hello", strTok.next());
54 2 : EXPECT_EQ("", strTok.next());
55 2 : EXPECT_EQ("World", strTok.next());
56 1 : EXPECT_FALSE(strTok.hasNext()) << "No tokens should be available.";
57 1 : }
58 :
59 : /* Tests the behaviour with any tokens for splitting a string with the option splitAtAllChars=true*/
60 1 : TEST(StringTokenizer, test_split_any_char) {
61 2 : StringTokenizer strTok("HelloxWyorld", "xy", true);
62 1 : EXPECT_TRUE(strTok.hasNext()) << "There must be more tokens available.";
63 2 : EXPECT_EQ("Hello", strTok.next());
64 2 : EXPECT_EQ("W", strTok.next());
65 2 : EXPECT_EQ("orld", strTok.next());
66 1 : EXPECT_FALSE(strTok.hasNext()) << "No tokens should be available.";
67 1 : }
68 :
69 : /* Tests the method reinit*/
70 1 : TEST(StringTokenizer, test_method_reinit) {
71 1 : StringTokenizer strTok("Hello");
72 1 : strTok.next();
73 1 : EXPECT_FALSE(strTok.hasNext()) << "No tokens should be available.";
74 1 : strTok.reinit();
75 1 : EXPECT_TRUE(strTok.hasNext()) << "There must be more tokens available.";
76 1 : }
77 :
78 : /* Tests the method size*/
79 1 : TEST(StringTokenizer, test_method_size) {
80 1 : StringTokenizer strTok("Hello little World");
81 1 : EXPECT_EQ(3, strTok.size()) << "The number of the token is not right.";
82 2 : StringTokenizer strTok2("");
83 1 : EXPECT_EQ(0, strTok2.size()) << "The number of the token is not right.";
84 1 : }
85 :
86 : /* Tests the method front*/
87 1 : TEST(StringTokenizer, test_method_front) {
88 1 : StringTokenizer strTok("Hello World");
89 2 : EXPECT_EQ("Hello", strTok.front()) << "The first token is not right.";
90 1 : strTok.next();
91 2 : EXPECT_EQ("Hello", strTok.front()) << "The first token is not right.";
92 1 : }
93 :
94 : /* Tests the method get*/
95 1 : TEST(StringTokenizer, test_method_get) {
96 1 : StringTokenizer strTok("Hello World");
97 2 : EXPECT_EQ("Hello", strTok.get(0)) << "The first token is not right.";
98 2 : EXPECT_EQ("World", strTok.get(1)) << "The second token is not right.";
99 1 : ASSERT_THROW(strTok.get(2), OutOfBoundsException) << "Expect an OutOfBoundsException exception.";
100 1 : }
101 :
102 : /* Tests the method get with empty data*/
103 1 : TEST(StringTokenizer, test_method_get_with_empty_data) {
104 1 : StringTokenizer strTok;
105 1 : ASSERT_THROW(strTok.get(0), OutOfBoundsException) << "Expect an OutOfBoundsException exception.";
106 1 : }
107 :
108 : /* Tests the method getVector*/
109 1 : TEST(StringTokenizer, test_method_getVector) {
110 1 : StringTokenizer strTok("Hello World");
111 1 : std::vector<std::string> strVek = strTok.getVector();
112 1 : EXPECT_EQ("World", strVek.back());
113 1 : EXPECT_EQ("Hello", strVek.front());
114 1 : }
|