aboutsummaryrefslogtreecommitdiff
path: root/test/common/tokenizer.h
blob: c6a73d77b710c5d49de92784ddf1011effd5d36f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <cxxtest/TestSuite.h>
#include "common/util.h"
#include "common/tokenizer.h"

class TokenizerTestSuite : public CxxTest::TestSuite {
public:
	void test_nextToken() {

		// test normal behavior
		Common::StringTokenizer strTokenizer("Now, this is a test!", " ,!");
		Common::String tokenArray[] = {"Now", "this", "is", "a", "test"};

		for (int i = 0; i < ARRAYSIZE(tokenArray); ++i) {
			// make sure nextToken works correctly
			TS_ASSERT_EQUALS(tokenArray[i], strTokenizer.nextToken());
		}
		TS_ASSERT(strTokenizer.empty());

		// Test edge cases:

		// empty string
		Common::StringTokenizer s1("");
		TS_ASSERT_EQUALS("", s1.nextToken());
		TS_ASSERT(s1.empty());

		// empty delimiter
		Common::StringTokenizer s2("test String", "");
		TS_ASSERT_EQUALS("test String", s2.nextToken());

		// string is the delimiter
		Common::StringTokenizer s3("abc", "abc");
		TS_ASSERT_EQUALS("", s3.nextToken());
		TS_ASSERT(s3.empty());

		// consecutive delimiters in the string
		Common::StringTokenizer s4("strstr,after all!!", "str, !");
		TS_ASSERT_EQUALS("af", s4.nextToken());
	}

	void test_resetAndEmpty() {
		Common::StringTokenizer strTokenizer("Just, another test!", " ,!");

		// test reset()
		Common::String token1 = strTokenizer.nextToken(); //Just
		TS_ASSERT_EQUALS(token1, "Just");
		strTokenizer.reset();
		Common::String token2 = strTokenizer.nextToken(); //Just
		TS_ASSERT_EQUALS(token2, "Just");

		// test empty()
		TS_ASSERT(!strTokenizer.empty());
		strTokenizer.nextToken(); //another
		strTokenizer.nextToken(); //test
		TS_ASSERT(strTokenizer.empty());
	}

};