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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
#include "UnityPrefix.h"
#if ENABLE_UNIT_TESTS
#include "Word.h"
#include "Runtime/Testing/Testing.h"
using namespace std;
SUITE (WordTests)
{
TEST (IntToString_Works)
{
CHECK (IntToString (123456) == "123456");
CHECK (IntToString (-123456) == "-123456");
}
TEST (Int64ToString_Works)
{
CHECK (Int64ToString (1099511627776) == "1099511627776");
CHECK (Int64ToString (-1099511627776) == "-1099511627776");
}
TEST (UnsignedIntToString_Works)
{
CHECK (IntToString (123456) == "123456");
}
TEST (UnsignedInt64ToString_Works)
{
CHECK (UnsignedInt64ToString (1099511627776) == "1099511627776");
}
TEST (Word_EndsWith)
{
CHECK(EndsWith("abc","c"));
CHECK(EndsWith("abc","bc"));
CHECK(EndsWith("abc","abc"));
CHECK(EndsWith("abc",""));
CHECK(!EndsWith("abc","d"));
CHECK(!EndsWith("abc","abcd"));
}
TEST (Word_IsStringNumber)
{
CHECK_EQUAL(true, IsStringNumber ("-1"));
CHECK_EQUAL(true, IsStringNumber ("+2"));
CHECK_EQUAL(false, IsStringNumber ("2+"));
CHECK_EQUAL(false, IsStringNumber ("a"));
CHECK_EQUAL(false, IsStringNumber ("1b"));
}
TEST (Word_ReplaceString)
{
string s;
s = "foo bar foo"; replace_string (s, "foo", "x"); CHECK_EQUAL("x bar x", s);
s = "foo bar foo"; replace_string (s, "", ""); CHECK_EQUAL("foo bar foo", s);
}
TEST (Word_SimpleStringToFloatWorks)
{
int len;
CHECK_EQUAL (0.0f, SimpleStringToFloat("0",&len)); CHECK_EQUAL(1,len);
CHECK_EQUAL (0.0f, SimpleStringToFloat("0.0",&len)); CHECK_EQUAL(3,len);
CHECK_EQUAL (0.0f, SimpleStringToFloat(".0",&len)); CHECK_EQUAL(2,len);
CHECK_EQUAL (12.05f, SimpleStringToFloat("12.05",&len)); CHECK_EQUAL(5,len);
CHECK_EQUAL (-3.5f, SimpleStringToFloat("-3.5",&len)); CHECK_EQUAL(4,len);
CHECK_EQUAL (3.14f, SimpleStringToFloat("3.14",&len)); CHECK_EQUAL(4,len);
CHECK_EQUAL (-1024.5f, SimpleStringToFloat("-1024.500",&len)); CHECK_EQUAL(9,len);
}
TEST (Word_Trim)
{
string s;
s=Trim(" \tspaces in front\n"); CHECK_EQUAL("spaces in front\n",s);
s=Trim("spaces behind \t \t\t"); CHECK_EQUAL("spaces behind",s);
s=Trim("\t\t\t\tspaces at both ends \t \t\t"); CHECK_EQUAL("spaces at both ends",s);
s=Trim(""); CHECK_EQUAL("",s);
s=Trim("\t\t\t \t \t"); CHECK_EQUAL("",s);
s=Trim("\n\n Custom Whitespace\r\n","\r\n"); CHECK_EQUAL(" Custom Whitespace",s);
}
TEST (Word_Split)
{
const int kNumtests = 6;
const int kMaxtokens = 3;
const char splitChar = ';';
const char* splitChars = ";/";
string inputs[kNumtests] =
{
"Normal;string;split",
"Adjacent;;separators",
"NoSeparators",
"EndWithSeparator;",
";StartWithSeparator",
";" // No non-separators
};
string inputsMulti[kNumtests] =
{
"Normal;string/split",
"Adjacent/;separators",
"NoSeparators",
"EndWithSeparator;/",
";StartWithSeparator",
";" // No non-separators
};
int outputSizes[kNumtests] =
{
3, 2, 1, 1, 1, 0
};
string outputTokens[][kMaxtokens] =
{
{ "Normal", "string", "split" },
{ "Adjacent", "separators", "" },
{ "NoSeparators", "", "" },
{ "EndWithSeparator", "", "" },
{ "StartWithSeparator", "", "" },
{ "", "", "" }
};
for (int test = 0; test < kNumtests; ++test)
{
string s = inputs[test];
vector<string> tokens;
Split (inputs[test], splitChar, tokens);
CHECK_EQUAL (outputSizes[test], tokens.size ()); // Verify number of tokens
for (int token = 0; token < outputSizes[test]; ++token)
{
CHECK_EQUAL (outputTokens[test][token], tokens[token]); // Verify each token
}
}
for (int test = 0; test < kNumtests; ++test)
{
string s = inputs[test];
vector<string> tokens;
Split (inputsMulti[test], splitChars, tokens);
CHECK_EQUAL (outputSizes[test], tokens.size ()); // Verify number of tokens
for (int token = 0; token < outputSizes[test]; ++token)
{
CHECK_EQUAL (outputTokens[test][token], tokens[token]); // Verify each token
}
}
}
}
#endif // ENABLE_UNIT_TESTS
|