libStatGen Software 1
Loading...
Searching...
No Matches
TestWrite Class Reference

Public Member Functions

void testWrite ()
 

Detailed Description

Definition at line 28 of file WriteFiles.h.

Member Function Documentation

◆ testWrite()

void TestWrite::testWrite ( )

Definition at line 37 of file WriteFiles.cpp.

38{
39 GlfFile glfOut;
40
41 std::string testFile = "results/MyTestOut1.glf";
42
43 assert(glfOut.openForWrite(testFile.c_str(), false));
44
45 // Create a glf header.
46 GlfHeader glfHeader;
47 GlfRefSection glfSection;
48 GlfRecord record;
49
50 // Test writing refsection with no header - exception
51 bool caughtException = false;
52 try
53 {
54 assert(glfOut.writeRefSection(glfSection) == false);
55 }
56 catch (std::exception& e)
57 {
58 caughtException = true;
59 }
60 assert(caughtException);
61
62 // Test writing record with no header - exception.
63 caughtException = false;
64 try
65 {
66 assert(glfOut.writeRecord(record) == false);
67 }
68 catch (std::exception& e)
69 {
70 caughtException = true;
71 }
72 assert(caughtException);
73
74 // Write the header.
75 writeHeader(glfOut, 1);
76
77 // Test writing record with no refsection - exception.
78 caughtException = false;
79 try
80 {
81 assert(glfOut.writeRecord(record) == false);
82 }
83 catch (std::exception& e)
84 {
85 caughtException = true;
86 }
87 assert(caughtException);
88
89
90 //////////////////////////////////////////////
91 writeRefSection1(glfOut);
92
93 // Test writing header after refSection - exception
94 caughtException = false;
95 try
96 {
97 assert(glfOut.writeHeader(glfHeader) == false);
98 }
99 catch (std::exception& e)
100 {
101 caughtException = true;
102 }
103 assert(caughtException);
104
105 writeSec1Record1(glfOut);
106 // Test writing header after record - exception
107 caughtException = false;
108 try
109 {
110 assert(glfOut.writeHeader(glfHeader) == false);
111 }
112 catch (std::exception& e)
113 {
114 caughtException = true;
115 }
116 assert(caughtException);
117
118 writeSec1Record2(glfOut);
119 writeEndMarker(glfOut);
120
121 writeRefSection2(glfOut);
122 writeSec2Record1(glfOut);
123 writeEndMarker(glfOut);
124
125 ////////////////////
126 // Close the file.
127 glfOut.close();
128
129 //////////////////////////////////////////////
130 // Validate the just written file.
131 GlfFile glfIn;
132 assert(glfIn.openForRead(testFile.c_str()));
133
134 readHeader(glfIn, 1);
135 readRefSection1(glfIn);
136 readSec1Record1(glfIn);
137 readSec1Record2(glfIn);
138 readEndMarker(glfIn);
139 readRefSection2(glfIn);
140 readSec2Record1(glfIn);
141 readEndMarker(glfIn);
142 checkEOF(glfIn);
143
144 ////////////////////////////////
145 // NEW FILE
146 testFile = "results/MyTestOut2.glf";
147 assert(glfOut.openForWrite(testFile.c_str()));
148
149 writeHeader(glfOut, 2);
150 writeRefSection1(glfOut);
151 writeSec1Record1(glfOut);
152 writeSec1Record2(glfOut);
153 // Test writing new section without end of section marker - auto-added.
154 writeRefSection2(glfOut);
155 writeSec2Record1(glfOut);
156 // Test closing file with no end of section marker - auto-added.
157 glfOut.close();
158
159 //////////////////////////////////////////////
160 // Validate the just written file.
161 assert(glfIn.openForRead(testFile.c_str()));
162
163 readHeader(glfIn, 2);
164 readRefSection1(glfIn);
165 readSec1Record1(glfIn);
166 readSec1Record2(glfIn);
167 readEndMarker(glfIn);
168 readRefSection2(glfIn);
169 readSec2Record1(glfIn);
170 readEndMarker(glfIn);
171 checkEOF(glfIn);
172
173
174 ////////////////////////////////
175 // NEW FILE
176 testFile = "results/MyTestOut3.glf";
177 {
178 GlfFile glfOutScoped;
179 assert(glfOutScoped.openForWrite(testFile.c_str()));
180
181 writeHeader(glfOutScoped, 3);
182 writeRefSection1(glfOutScoped);
183 writeSec1Record1(glfOutScoped);
184 writeSec1Record2(glfOutScoped);
185 // Test writing new section without end of section marker - auto-added.
186 writeRefSection2(glfOutScoped);
187 writeSec2Record1(glfOutScoped);
188 // Test just letting the file go out of scope with no end
189 // of section marker - auto added.
190 }
191 //////////////////////////////////////////////
192 // Validate the just written file.
193 assert(glfIn.openForRead(testFile.c_str()));
194
195 // Test reading refsection with no header - exception.
196 caughtException = false;
197 try
198 {
199 assert(glfIn.getNextRefSection(glfSection) == false);
200 }
201 catch (std::exception& e)
202 {
203 caughtException = true;
204 }
205 assert(caughtException);
206
207 // Test reading record with no header - exception.
208 caughtException = false;
209 try
210 {
211 assert(glfIn.getNextRecord(record) == false);
212 }
213 catch (std::exception& e)
214 {
215 caughtException = true;
216 }
217 assert(caughtException);
218
219 readHeader(glfIn, 3);
220
221 // Test reading record with no reference section - exception.
222 caughtException = false;
223 try
224 {
225 assert(glfIn.getNextRecord(record) == false);
226 }
227 catch (std::exception& e)
228 {
229 caughtException = true;
230 }
231 assert(caughtException);
232
233 // Test reading header after already read - exception
234 caughtException = false;
235 try
236 {
237 assert(glfIn.readHeader(glfHeader) == false);
238 }
239 catch (std::exception& e)
240 {
241 caughtException = true;
242 }
243 assert(caughtException);
244
245 readRefSection1(glfIn);
246 readSec1Record1(glfIn);
247 readSec1Record2(glfIn);
248 readEndMarker(glfIn);
249 readRefSection2(glfIn);
250 readSec2Record1(glfIn);
251 readEndMarker(glfIn);
252 checkEOF(glfIn);
253
254
255 // Read again, but text reading next refsection before
256 //end of current section - consumes the rest of the records.
257 assert(glfIn.openForRead(testFile.c_str()));
258
259 readHeader(glfIn, 3);
260 readRefSection1(glfIn);
261 readRefSection2(glfIn);
262 readSec2Record1(glfIn);
263 readEndMarker(glfIn);
264 checkEOF(glfIn);
265
266
267}
This class allows a user to easily read/write a GLF file.
Definition GlfFile.h:29
bool getNextRefSection(GlfRefSection &refSection)
Gets the next reference section from the file & stores it in the passed in section,...
Definition GlfFile.cpp:240
bool writeRefSection(const GlfRefSection &refSection)
Write the reference section to the file, adding an end marker record if there is a previous section a...
Definition GlfFile.cpp:308
bool openForWrite(const char *filename, bool compressed=true)
Open a glf file for writing with the specified filename.
Definition GlfFile.cpp:109
bool getNextRecord(GlfRecord &record)
Gets the nextrecord from the file & stores it in the passed in record.
Definition GlfFile.cpp:368
bool openForRead(const char *filename)
Open a glf file for reading with the specified filename.
Definition GlfFile.cpp:66
void close()
Close the file if there is one open, adding an end marker record if there is a previous section and o...
Definition GlfFile.cpp:142
bool writeHeader(GlfHeader &header)
Writes the specified header into the file.
Definition GlfFile.cpp:200
bool readHeader(GlfHeader &header)
Reads the header section from the file and stores it in the passed in header.
Definition GlfFile.cpp:165
bool writeRecord(const GlfRecord &record)
Writes the specified record into the file.
Definition GlfFile.cpp:429
This class allows a user to easily get/set the fields in a GLF header.
Definition GlfHeader.h:30
This class allows a user to easily get/set the fields in a GLF record.
Definition GlfRecord.h:29
This class allows a user to easily get/set the fields in a GLF section/chromosome header.

The documentation for this class was generated from the following files: