// ------------------------------- // // -------- Start of File -------- // // ------------------------------- // // ----------------------------------------------------------- // // C++ Source Code File Name: testprog.cpp // Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24 // Author: Doug Gaer // File Creation Date: 11/29/1996 // Date Last Modified: 06/21/2000 // Copyright (c) 2000 Douglas M. Gaer // ----------------------------------------------------------- // // ------------- Program Description and Details ------------- // // ----------------------------------------------------------- // /* The VBS C++ classes are copyright (c) 2000, by Douglas M. Gaer. All those who put this code or its derivatives in a commercial product MUST mention this copyright in their documentation for users of the products in which this code or its derivative classes are used. Otherwise, you have the freedom to redistribute verbatim copies of this source code, adapt it to your specific needs, or improve the code and release your improvements to the public provided that the modified files carry prominent notices stating that you changed the files and the date of any change. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE, YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION. This is a test program for the vbString class. */ // ----------------------------------------------------------- // #include <iostream.h> #include "vbstring.h" void PausePrg() { cout << endl; cout << "Press enter to continue..." << endl; cin.get(); } main() { cout << "Testing constructors..." << endl; vbString a("The quick brown fox jumps over the lazy dog"); cout << a << endl; vbString b; vbString c("This is a test", 14); cout << c << endl; cout << "Constructing a substring..." << endl; vbString d(c, 0, 14); cout << d << endl; cout << endl; cout << "Testing overloaded assignment operators..." << endl; b = a; cout << b << endl; b = c; cout << b << endl; b = "0123456789"; cout << b << endl; cout << endl; cout << "Testing copy constructor..." << endl; vbString aa(a); cout << aa << endl; vbString bb(b); cout << bb << endl; PausePrg(); cout << "Testing overloaded += operator..." << endl; vbString buf(" 0123456789"); a+=buf; cout << a << endl; c+=" message"; cout << c << endl; c+='X'; cout << c << endl; PausePrg(); cout << "Testing concatenation..." << endl; vbString s1("String 1"), s2("String 2"); char *str3 = " and String 3"; s1.Cat(" and ", 5); s1 += s2; cout << s1 << endl; s1.Cat(str3); cout << s1 << endl; PausePrg(); cout << "Testing find functions..." << endl; if(s1.Find("xyz") == vbString::NoMatch) cout << "Pattern not found!" << endl; else cout << "Pattern found." << endl; if(s1.Find(str3, strlen(str3)) == vbString::NoMatch) cout << "Pattern not found!" << endl; else cout << "Pattern found." << endl; if(s1.Find(s2) == vbString::NoMatch) cout << "Pattern not found!" << endl; else cout << "Pattern found." << endl; cout << "Testing repeated pattern finding..." << endl; s1 = "This is a test message"; unsigned Offset = 0; while(1) { Offset = s1.Find("is", Offset); if (Offset == vbString::NoMatch) break; cout << "Pattern match found at index: " << Offset << endl; Offset++; } PausePrg(); cout << "Testing copy functions..." << endl; s1.Copy("The quick brown fox jumps over the lazy dog"); vbString s3("0123456789"); cout << s1 << endl; s1.Copy(s3); cout << s1 << endl; PausePrg(); cout << "Testing delete function..." << endl; vbString x1("The quick brown fox jumps over the lazy dog"); cout << x1 << endl; char *xx = "fox"; int Index = x1.Find(xx); cout << "Deleting fox from string..." << endl; x1.DeleteAt(Index, strlen(xx)); cout << x1 << endl; PausePrg(); cout << "Testing replace functions..." << endl; x1 = "The quick brown fox jumps over the lazy dog"; cout << x1 << endl; char *xy = "cat"; Index = x1.Find(xx); cout << "Replacing fox:" << endl; x1.ReplaceAt(Index, xy, strlen(xy)); cout << x1 << endl; cout << "Replacing jumps:" << endl; Index = x1.Find("jumps"); x1.ReplaceAt(Index, "runs "); cout << x1 << endl; vbString x2("cow"); cout << "Replacing dog:" << endl; Index = x1.Find("dog"); x1.ReplaceAt(Index, x2); cout << x1 << endl; PausePrg(); cout << "Testing the insert functions..." << endl; x1 = "The quick brown fox jumps over the lazy dog"; cout << x1 << endl; char *xz = "and yellow "; cout << "Inserting text into string:" << endl; Index = x1.Find(xx); x1.InsertAt(Index, xz, strlen(xz)); cout << x1 << endl; Index = x1.Find("over"); x1.InsertAt(Index, "around and "); cout << x1 << endl; vbString x3("cow and "); Index = x1.Find("dog"); x1.InsertAt(Index, x3); cout << x1 << endl; PausePrg(); cout << "Testing fill functions..." << endl; vbString y1(15,0); // 0 for GrowBy will not allow string to grow y1.Fill('Z'); cout << y1 << endl; y1.Fill("0123456789"); cout << y1 << endl; vbString y3("TEST"); y1.Fill(y3); cout << y1 << endl; PausePrg(); cout << "Testing substring functions..." << endl; vbString z1("The quick brown fox jumps over the lazy dog"); cout << z1 << endl; Index = z1.Find("fox"); cout << "Testing mid function:" << endl; vbString Fox = z1.Mid(Index, 3); // 3 bytes for fox cout << Fox << endl; cout << "Testing left function:" << endl; vbString The = z1.Left(3); // 3 bytes for the cout << The << endl; cout << "Testing right function:" << endl; vbString Dog = z1.Right(3); // 3 bytes for dog cout << Dog << endl; PausePrg(); cout << "Testing Growth and Null Terminate function..." << endl; vbString z("This is a test"); // Default GrowBy of 16 cout << z << endl; cout << "String's logical length = " << z.GetLength() << endl; cout << "String's allocated length = " << z.GetDimLen() << endl; cout << "Changing allocated length..." << endl; z.Grow(); cout << "String's allocated length = " << z.GetDimLen() << endl; cout << "Freeing unused allocated space..." << endl; z.FreeExtra(); cout << "String's allocated length = " << z.GetDimLen() << endl; cout << "Null terminating string: " << endl; cout << z.NullTermStr() << endl; cout << "String's allocated length = " << z.GetDimLen() << endl; cout << "Freeing unused allocated space..." << endl; z.FreeExtra(); cout << "String's allocated length = " << z.GetDimLen() << endl; cout << "Disallowing growth..." << endl; cout << "String's logical length = " << z.GetLength() << endl; cout << "String's allocated length = " << z.GetDimLen() << endl; cout << "Null terminating string: " << endl; z.ChgGrowByInc(0); cout << z.NullTermStr() << endl; PausePrg(); cout << "Testing resizing function" << endl; vbString rs("DOG", 3); cout << "String = " << rs.c_str() << endl; cout << "String's logical length = " << rs.GetLength() << endl; cout << "String's allocated length = " << rs.GetDimLen() << endl; cout << "Resizing the string" << endl; rs.Resize(24); cout << "String's logical length = " << rs.GetLength() << endl; cout << "String's allocated length = " << rs.GetDimLen() << endl; PausePrg(); cout << "Testing c_str functions" << endl; vbString gs("COW"); const vbString &cs = gs; cout << "String = " << gs.c_str() << endl; cout << "Const String = " << cs.c_str() << endl; PausePrg(); cout << "Testing overload subscript operator..." << endl; char *Message = "This is a test message"; vbString ss(Message); for(unsigned i = 0; i < strlen(Message); i++) cout << ss[i]; cout << endl; PausePrg(); cout << "Testing general-purpose filter funftions" << endl; cout << endl; vbString s(" ?The quick brown fox jumps over the lazy dog\n\r\ 0123456789\r\n?~!@#$%^&*()_+<> "); unsigned num; cout << "Original string: " << endl; cout << s.c_str() << endl; cout << endl; cout << "Trimming all leading and trailing spaces" << endl; cout << "begin" << s.c_str() << "end" << endl; num = s.TrimLeadingSpaces(); cout << "Leading spaces = " << num << endl; cout << s.c_str() << endl; num = s.TrimTrailingSpaces(); cout << "Trialing spaces = " << num << endl; cout << s.c_str(); cout << "end"; PausePrg(); cout << "Changing to upper case" << endl; if(s.ToUpper()) cout << s.c_str() << endl; cout << "Changing to lower case" << endl; if(s.ToLower()) cout << s.c_str() << endl; PausePrg(); cout << "Replacing carriage returns/line feeds with spaces" << endl; num = s.ReplaceChar('\r', ' '); num = s.ReplaceChar('\n', ' '); cout << s.c_str() << endl; PausePrg(); cout << "Filtering all \'?\' characters" << endl; num = s.FilterChar('?'); cout << s.c_str() << endl; PausePrg(); cout << "Filtering the \"he\" string" << endl; s.FilterString("he"); cout << s.c_str() << endl; PausePrg(); cout << "Testing the comparison functions..." << endl; vbString x(80), y(80); cout << "Enter first string to compare: "; cin >> x; cout << "Enter second string to compare: "; cin >> y; vbString No = "No"; vbString Yes = "Yes"; vbString NoYes[2] = { No, Yes }; cout << x << " == " << y << " ? " << NoYes[x == y] << '\n'; cout << x << " != " << y << " ? " << NoYes[x != y] << '\n'; cout << x << " > " << y << " ? " << NoYes[x > y] << '\n'; cout << x << " >= " << y << " ? " << NoYes[x >= y] << '\n'; cout << x << " < " << y << " ? " << NoYes[x < y] << '\n'; cout << x << " <= " << y << " ? " << NoYes[x <= y] << '\n'; return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //