libStatGen Software 1
Loading...
Searching...
No Matches
StringTest.cpp
1/*
2 * Copyright (C) 2011 Regents of the University of Michigan
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17#include "StringTest.h"
18#include <assert.h>
19
20
21int main(int argc, char ** argv)
22{
23
24 testAsInteger();
25
26 testReadLine();
27}
28
29void testAsInteger()
30{
31 // Test AsInteger with ints & negative ints.
32 String intString = "123";
33 String negIntString = "-123";
34 assert(intString.AsInteger() == 123);
35 assert(negIntString.AsInteger() == -123);
36
37 // Run the same tests with AsInteger that returns a bool and takes
38 // in a long to set.
39 long retValue;
40 assert(intString.AsInteger(retValue));
41 assert(retValue == 123);
42 assert(negIntString.AsInteger(retValue));
43 assert(retValue == -123);
44
45
46 // Strings that are not integers
47 // For AsInteger, it returns just the starting integer portion.
48 // For AsInteger that returns a bool and a long set, it returns false
49 // and sets the long to the starting int.
50 String nonIntString = "abd";
51 assert(nonIntString.AsInteger() == 0);
52 assert(!nonIntString.AsInteger(retValue));
53
54 nonIntString = "12ab33";
55 assert(nonIntString.AsInteger() == 12);
56 assert(!nonIntString.AsInteger(retValue));
57 assert(retValue == 12);
58 nonIntString = "as12ab3a4sd";
59 assert(nonIntString.AsInteger() == 0);
60 assert(!nonIntString.AsInteger(retValue));
61 assert(retValue == 0);
62 // Negatives are only recognized as the first characer.
63 nonIntString = "-12ab3a4sd";
64 assert(nonIntString.AsInteger() == -12);
65 assert(!nonIntString.AsInteger(retValue));
66 assert(retValue == -12);
67 nonIntString = "-as12ab3a4sd";
68 assert(nonIntString.AsInteger() == 0);
69 assert(!nonIntString.AsInteger(retValue));
70 assert(retValue == 0);
71 nonIntString = "as-12ab3a4sd";
72 assert(nonIntString.AsInteger() == 0);
73 assert(!nonIntString.AsInteger(retValue));
74 assert(retValue == 0);
75 nonIntString = "as12-ab3a4sd";
76 assert(nonIntString.AsInteger() == 0);
77 assert(!nonIntString.AsInteger(retValue));
78 assert(retValue == 0);
79}
80
81int temp1 = 0;
82
83void testReadLine()
84{
85 IFILE filePtr = ifopen("testFiles/testFile.txt", "rb");
86 assert(filePtr != NULL);
87
88 String line = "";
89 line.ReadLine(filePtr);
90
91 assert(line == " Hello, I am a testFile. ");
92
93 line.Trim();
94 assert(line == "Hello, I am a testFile.");
95
96
97 // Does not compile in current version, but compiles in old verison.
98 // This can be added back in to ensure that it will catch the difference
99 // in return value for ReadLine (now: int; used to be: string&)
100 // testMethod(line.ReadLine(filePtr));
101 line.ReadLine(filePtr);
102 assert(temp1 == 0);
103 testMethod(line);
104 assert(temp1 == 1);
105
106 // line.ReadLine(filePtr).Trim();
107 line.ReadLine(filePtr);
108 line.Trim();
109
110 assert(line == "ThirdLine.");
111
112 ifclose(filePtr);
113}
114
115
116void testMethod(String temp)
117{
118 temp1 = 1;
119}
IFILE ifopen(const char *filename, const char *mode, InputFile::ifileCompression compressionMode=InputFile::DEFAULT)
Open a file with the specified name and mode, using a filename of "-" to indicate stdin/stdout.
Definition InputFile.h:562
int ifclose(IFILE &file)
Close the file.
Definition InputFile.h:580
Class for easily reading/writing files without having to worry about file type (uncompressed,...
Definition InputFile.h:37