StringTokenizerTest.cpp

Go to the documentation of this file.
00001 #include <gtest/gtest.h>
00002 #include <utils/common/StringTokenizer.h>
00003 #include <utils/common/UtilExceptions.h>
00004 
00005 using namespace std;
00006 
00007 /*
00008 Tests StringTokenizer class from <SUMO>/src/utils/common
00009 */
00010 
00011 /* Tests the behaviour with a StringTokenizer::WHITECHAR for splitting a string.*/
00012 TEST(StringTokenizer, test_split_with_whitechar) {
00013     StringTokenizer *strTok = new StringTokenizer("Hello  World", StringTokenizer::WHITECHARS);
00014     EXPECT_TRUE(strTok->hasNext()) << "There must be more tokens available.";
00015     EXPECT_EQ("Hello",strTok->next());
00016     EXPECT_EQ("World",strTok->next());
00017     EXPECT_FALSE(strTok->hasNext()) << "No tokens should be available.";
00018 }
00019 
00020 /* Tests the behaviour with a StringTokenizer::NEWLINE for splitting a string.*/
00021 TEST(StringTokenizer, test_split_with_newline) {
00022     StringTokenizer *strTok = new StringTokenizer("Hello\nWorld", StringTokenizer::NEWLINE);
00023     EXPECT_TRUE(strTok->hasNext()) << "There must be more tokens available.";
00024     EXPECT_EQ("Hello",strTok->next());
00025     EXPECT_EQ("World",strTok->next());
00026     EXPECT_FALSE(strTok->hasNext()) << "No tokens should be available.";
00027 }
00028 
00029 /* Tests the behaviour with any tokens for splitting a string.*/
00030 TEST(StringTokenizer, test_split_with_x) {
00031     StringTokenizer *strTok = new StringTokenizer("HelloxxWorld", "x");
00032     EXPECT_TRUE(strTok->hasNext()) << "There must be more tokens available.";
00033     EXPECT_EQ("Hello",strTok->next());
00034     EXPECT_EQ("",strTok->next());
00035     EXPECT_EQ("World",strTok->next());
00036     EXPECT_FALSE(strTok->hasNext()) << "No tokens should be available.";
00037 }
00038 
00039 /* Tests the behaviour with any tokens for splitting a string with the option splitAtAllChars=true*/
00040 TEST(StringTokenizer, test_split_any_char) {
00041     StringTokenizer *strTok = new StringTokenizer("HelloxWyorld", "xy", true);
00042     EXPECT_TRUE(strTok->hasNext()) << "There must be more tokens available.";
00043     EXPECT_EQ("Hello",strTok->next());
00044     EXPECT_EQ("W",strTok->next());
00045     EXPECT_EQ("orld",strTok->next());
00046     EXPECT_FALSE(strTok->hasNext()) << "No tokens should be available.";
00047 }
00048 
00049 /* Tests the method reinit*/
00050 TEST(StringTokenizer, test_method_reinit) {
00051     StringTokenizer *strTok = new StringTokenizer("Hello");
00052     strTok->next();
00053     EXPECT_FALSE(strTok->hasNext()) << "No tokens should be available.";
00054     strTok->reinit();
00055     EXPECT_TRUE(strTok->hasNext()) << "There must be more tokens available.";
00056 }
00057 
00058 /* Tests the method size*/
00059 TEST(StringTokenizer, test_method_size) {
00060     StringTokenizer *strTok = new StringTokenizer("Hello little World");
00061     EXPECT_EQ(3,strTok->size()) << "The number of the token is not right.";
00062     StringTokenizer *strTok2 = new StringTokenizer("");
00063     EXPECT_EQ(0,strTok2->size()) << "The number of the token is not right.";
00064 }
00065 
00066 /* Tests the method front*/
00067 TEST(StringTokenizer, test_method_front) {
00068     StringTokenizer *strTok = new StringTokenizer("Hello World");
00069     EXPECT_EQ("Hello",strTok->front()) << "The first token is not right.";
00070     strTok->next();
00071     EXPECT_EQ("Hello",strTok->front()) << "The first token is not right.";
00072 }
00073 
00074 /* Tests the method get*/
00075 TEST(StringTokenizer, test_method_get) {
00076     StringTokenizer *strTok = new StringTokenizer("Hello World");
00077     EXPECT_EQ("Hello",strTok->get(0)) << "The first token is not right.";
00078     EXPECT_EQ("World",strTok->get(1)) << "The second token is not right.";
00079     ASSERT_THROW(strTok->get(2),OutOfBoundsException) << "Expect an OutOfBoundsException exception.";
00080 }
00081 
00082 /* Tests the method get with empty data*/
00083 TEST(StringTokenizer, test_method_get_with_empty_data) {
00084     StringTokenizer *strTok = new StringTokenizer();
00085     ASSERT_THROW(strTok->get(0),OutOfBoundsException) << "Expect an OutOfBoundsException exception.";
00086 }
00087 
00088 /* Tests the method getVector*/
00089 TEST(StringTokenizer, test_method_getVector) {
00090     StringTokenizer *strTok = new StringTokenizer("Hello World");
00091     vector<string> strVek = strTok->getVector();
00092     EXPECT_EQ("World",strVek.back());
00093     EXPECT_EQ("Hello",strVek.front());
00094 }
00095 
00096 
00097 
00098 
00099 

Generated on Wed May 5 00:06:36 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6