1 #include "TestString.h"
2 
3 using namespace std;
4 
TestString()5 TestString::TestString()
6 {
7 }
8 
~TestString()9 TestString::~TestString()
10 {
11 }
12 
run()13 void TestString::run()
14 {
15   testStringConstructors();
16   testNewStringConstructors();
17   testAccessors();
18   testOperators();
19   testSearch();
20   testMiscFuncs();
21 }
22 
testStringConstructors()23 void TestString::testStringConstructors()
24 {
25   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26   // Tests using the String constructor
27   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28   // Default Constructor
29   String defaultStr;
30   _test( !strcmp(defaultStr.c_str(), "" ));
31 
32   // Single char constructor
33   const char* cstr = "c";
34   String charStr( 'c' );
35   _test( !strcmp(charStr.c_str(), cstr));
36 
37   // C string constructor
38   String cstrStr( "Hello World" );
39   const char* c2str = "Hello World";
40   _test( !strcmp(cstrStr.c_str(), c2str) );
41 
42   // Selected Element Constructor
43   String someElems( "Hello World", 5);
44   _test( !strcmp(someElems.c_str(), "Hello"));
45   String noElems( "Hello World" , 0);
46   _test( !strcmp(noElems.c_str(), "") );
47 
48   // Copy Constructor
49   String copyStr( "Hello World");
50   String other(copyStr);
51   _test( !strcmp(other.c_str(), copyStr.c_str()));
52   // 5 tests in this function
53 }
54 
testNewStringConstructors()55 void TestString::testNewStringConstructors()
56 {
57   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
58   // Tests using the new STL String Constructors
59   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
60   // Default constructor
61   String defaultStr;
62 //  string tmp = defaultStr.stlString();
63   _test( !defaultStr.compare("") );
64 
65   // Single char constructor
66   String charStr('c');
67 //  tmp = charStr.stlString();
68   _test( !charStr.compare("c"));
69 
70   String nullcharStr('\0');
71 //  tmp = nullcharStr.stlString();
72   _test( !nullcharStr.compare(""));
73 
74   // C string constructor
75   String cstrStr( "Hello World" );
76   _test( !cstrStr.compare("Hello World"));
77 
78   String nullcStr( "" );
79   _test( !nullcStr.compare(""));
80 
81   // Selected Element constructor
82   String someElems( "Hello World", 5);
83   _test( !someElems.compare("Hello"));
84   String noElems( "Hello World" , 0);
85   _test( !noElems.compare("") );
86 
87   // Copy constructor
88   String copyStr( "Hello World");
89   String other(copyStr);
90   _test( !copyStr.compare(other.c_str()));
91   // note that the null string copy constructor is not possible, at least
92   // using gcc
93 }
94 
testAccessors()95 void TestString::testAccessors()
96 {
97   //~~~~~~~~~~~~~~~~~~~~~~~~~~
98   // Tests using old Accessors
99   //~~~~~~~~~~~~~~~~~~~~~~~~~~
100   String accessorStr("Hello World");
101   String nullStr;
102   const char* tmp2 = accessorStr.c_str();
103 
104   //Test length
105   _test( accessorStr.length() == 11);
106   _test( nullStr.length() == 0);
107 
108 
109   // Read-only Element Access
110   _test( accessorStr[1] == 'e');
111 }
112 
testOperators()113 void TestString::testOperators()
114 {
115   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
116   // Tests for Operators. Note that there can only be one set of
117   // operators at a time in a class, therefore there will not be
118   // a testNewOperators() method.
119   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
120   String operatorStr("Hello World");
121   String nullStr;
122   String otherStr("Hello World");
123   String other2Str("Hi Planet");
124 
125   _test( operatorStr == "Hello World" );
126   _test( nullStr == "" );
127   _test( operatorStr == otherStr );
128   _test( operatorStr != other2Str );
129   _test( operatorStr != nullStr );
130 
131 //  _test( operatorStr < other2Str );
132 
133   operatorStr += other2Str;
134   _test( operatorStr == "Hello WorldHi Planet");
135   operatorStr += nullStr;
136   _test( operatorStr == "Hello WorldHi Planet");
137 
138   String yetAnotherStr = otherStr + other2Str;
139   _test( yetAnotherStr == "Hello WorldHi Planet");
140   yetAnotherStr = otherStr + nullStr;
141   _test( yetAnotherStr == "Hello World" );
142 
143   yetAnotherStr = otherStr + 2;
144   _test( yetAnotherStr == "Hello World2" );
145   yetAnotherStr = nullStr + 2;
146   _test( yetAnotherStr == "2" );
147 
148   yetAnotherStr = otherStr + 3.432;
149   _test( yetAnotherStr == "Hello World  3.4319999999999999e+00" );
150   yetAnotherStr = nullStr + 3.432;
151   _test( yetAnotherStr == "  3.4319999999999999e+00" );
152 
153 
154  // 14 tests in this function, 29 tests up to this point
155 }
156 
testSearch()157 void TestString::testSearch()
158 {
159   String search1 = "Hello World";
160   String search2 = "lo";
161   String search3 = "qzt";
162   String search4 = "Hel";
163   String search5 = " World";
164   String search6, search7;
165   String nullStr;
166 
167 // find method. taking both a c string and a String as input
168   _test( search1.find("lo") == 3 );
169   _test( search1.find(search2) == 3 );
170   _test( search1.find("qzt") == -1 );
171   _test( search1.find(search3) == -1);
172 
173   _test( nullStr.find("lo") == -1 );
174   _test( nullStr.find(search2) == -1 );
175 /**
176 // before method. again, taking both a c string and a String as input
177   _test( search1.before("lo") == search4 );
178   _test( search1.before(search2) == search4 );
179   _test( search1.before("qzt") == search1 );
180   _test( search1.before(search3) == search1 );
181   _test( search1.before(nullStr) == nullStr );
182 
183   _test( nullStr.before("lo") == nullStr );
184   _test( nullStr.before(search2) == nullStr );
185   _test( nullStr.before(nullStr) == nullStr );
186 
187 // after method. taking both a c strign and a String as input
188   _test( search1.after("lo") == search5 );
189   _test( search1.after(search2) == search5 );
190   _test( search1.after("qzt") == nullStr );
191   _test( search1.after(search3) == nullStr );
192   _test( search1.after(nullStr) == nullStr );
193 
194   _test( search1.after("rld") == nullStr);
195   _test( search1.after("d") == nullStr);
196 
197   _test( nullStr.after("qzt") == nullStr );
198   _test( nullStr.after(search3) == nullStr );
199   _test( nullStr.after(nullStr) == nullStr );
200 */
201 /**
202 // between method. only one version
203   _test( search1.between("H","r",search6, search7) == "ello Wo");
204   _test( search6 == "" &&  search7 == "ld" );
205   cout << "s1: " << search1.between("H","r",search6, search7) << "***" << endl;
206   cout << "s6: " << search6 << "***" << "s7: " << search7 << "***" << endl;
207   _test( search1.between("llo","or", search6, search7) == " W" );
208   _test( search6 == "He" && search7 == "ld");
209   _test( search1.between("qzt","rld",search6, search7) == "");
210   _test( search6 == "Hello Wo" &&  search7 == "");
211   _test( search1.between("H","qzt", search6, search7) == "");
212   _test( search6 == "ello World" && search7 == "");
213   _test( search1.between("ell","qzt", search6, search7) == "");
214   _test( search6 == "H" && search7 == "o World");
215   _test( search1.between(nullStr,"Wor", search6, search7) == "");
216   _test( search6 == "Hello " && search7 == "ld" );
217   _test( search1.between("el", nullStr, search6, search7) == "");
218   _test( search6 == "H" && search7 == "lo World");
219   _test( search1.between(nullStr, nullStr, search6, search7) == "");
220   _test( search6 == "Hello World" && search7 == "");
221 
222   _test( nullStr.between("H","qzt",search6, search7) == "");
223   _test( search6 == "" && search7 == "");
224   _test( nullStr.between(nullStr,"qzt", search6, search7) == "");
225   _test( search6 == "" && search7 == "");
226   _test( nullStr.between("H",nullStr, search6, search7) == "" );
227   _test( search6 == "" && search7 == "");
228   _test( nullStr.between(nullStr,nullStr, search6, search7) == "");
229   _test( search6 == "" && search7 == "");
230 
231 
232 // subString method
233   _test( search1.subString(0,0) == nullStr );
234   _test( search1.subString(0,5) == "Hello" );
235   _test( search1.subString(11,11) == nullStr );
236   _test( search1.subString(6,11) == "World" );
237 
238   String search8("12345");
239   _test( search8.subString(1,2) == "2");
240 
241   _test( nullStr.subString(0,0) == "");
242 */
243 }
244 
testMiscFuncs()245 void TestString::testMiscFuncs()
246 {
247   String misc1( "Hello World" );
248   String misc2( " \n\t\r" );
249   String misc3( "    Hi Planet" );
250   String misc4( "GOOD DAY");
251   String nullStr;
252 /**
253   _test( misc1.allCaps() == "HELLO WORLD" );
254   _test( misc2.allCaps() == misc2 );
255   _test( misc4.allCaps() == misc4 );
256   _test( nullStr.allCaps() == nullStr );
257 
258 // trimInitialWhitespace.
259   _test( misc1.trimInitialWhitespace() == misc1 );
260   _test( misc2.trimInitialWhitespace() == nullStr );
261   _test( misc3.trimInitialWhitespace() == "Hi Planet" );
262   _test( nullStr.trimInitialWhitespace() == nullStr );
263 */
264 // conversion tools
265 /**
266   String num1("1");
267   String num2("3.029384203");
268   String bool1("True");
269   String bool2("TrUE");
270 
271   _test( num1.atoi() == 1 );
272   _test( num2.atof() - 3.029384203 <= .000000001 || num2.atof() - 3.029384203 >= .000000001 );
273   _test( bool1.atob() );
274   _test( bool2.atob() );
275   _test( !misc1.atob() );
276 */
277 }
278