StringTest.cpp

00001 /*
00002  *  Copyright (C) 2011  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #include "StringTest.h"
00018 #include <assert.h>
00019 
00020 
00021 int main(int argc, char ** argv)
00022 {
00023 
00024     testAsInteger();
00025 
00026     testReadLine();
00027 }
00028 
00029 void testAsInteger()
00030 {
00031     // Test AsInteger with ints & negative ints.
00032     String intString = "123";
00033     String negIntString = "-123";
00034     assert(intString.AsInteger() == 123);
00035     assert(negIntString.AsInteger() == -123);
00036 
00037     // Run the same tests with AsInteger that returns a bool and takes
00038     // in a long to set.
00039     long retValue;
00040     assert(intString.AsInteger(retValue));
00041     assert(retValue == 123);
00042     assert(negIntString.AsInteger(retValue));
00043     assert(retValue == -123);
00044 
00045 
00046     // Strings that are not integers
00047     // For AsInteger, it returns just the starting integer portion.
00048     // For AsInteger that returns a bool and a long set, it returns false
00049     // and sets the long to the starting int.
00050     String nonIntString = "abd";
00051     assert(nonIntString.AsInteger() == 0);
00052     assert(!nonIntString.AsInteger(retValue));
00053 
00054     nonIntString = "12ab33";
00055     assert(nonIntString.AsInteger() == 12);
00056     assert(!nonIntString.AsInteger(retValue));
00057     assert(retValue == 12);
00058     nonIntString = "as12ab3a4sd";
00059     assert(nonIntString.AsInteger() == 0);
00060     assert(!nonIntString.AsInteger(retValue));
00061     assert(retValue == 0);
00062     // Negatives are only recognized as the first characer.
00063     nonIntString = "-12ab3a4sd";
00064     assert(nonIntString.AsInteger() == -12);
00065     assert(!nonIntString.AsInteger(retValue));
00066     assert(retValue == -12);
00067     nonIntString = "-as12ab3a4sd";
00068     assert(nonIntString.AsInteger() == 0);
00069     assert(!nonIntString.AsInteger(retValue));
00070     assert(retValue == 0);
00071     nonIntString = "as-12ab3a4sd";
00072     assert(nonIntString.AsInteger() == 0);
00073     assert(!nonIntString.AsInteger(retValue));
00074     assert(retValue == 0);
00075     nonIntString = "as12-ab3a4sd";
00076     assert(nonIntString.AsInteger() == 0);
00077     assert(!nonIntString.AsInteger(retValue));
00078     assert(retValue == 0);
00079 }
00080 
00081 int temp1 = 0;
00082 
00083 void testReadLine()
00084 {
00085     IFILE filePtr = ifopen("testFiles/testFile.txt", "rb");
00086     assert(filePtr != NULL);
00087     
00088     String line = "";
00089     line.ReadLine(filePtr);
00090 
00091     assert(line == "  Hello, I am a testFile.  ");
00092 
00093     line.Trim();
00094     assert(line == "Hello, I am a testFile.");
00095 
00096 
00097     // Does not compile in current version, but compiles in old verison.
00098     // This can be added back in to ensure that it will catch the difference
00099     // in return value for ReadLine (now: int; used to be: string&)
00100     //    testMethod(line.ReadLine(filePtr));
00101     line.ReadLine(filePtr);
00102     assert(temp1 == 0);
00103     testMethod(line);
00104     assert(temp1 == 1);
00105 
00106     //    line.ReadLine(filePtr).Trim();
00107     line.ReadLine(filePtr);
00108     line.Trim();
00109 
00110     assert(line == "ThirdLine.");
00111 
00112     ifclose(filePtr);
00113 }
00114 
00115 
00116 void testMethod(String temp)
00117 {
00118     temp1 = 1;
00119 }
Generated on Tue Sep 6 17:52:01 2011 for libStatGen Software by  doxygen 1.6.3