/[ascend]/trunk/ascend/utilities/test/test_readln.c
ViewVC logotype

Contents of /trunk/ascend/utilities/test/test_readln.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2017 - (show annotations) (download) (as text)
Wed Apr 29 02:39:32 2009 UTC (15 years, 9 months ago) by jpye
File MIME type: text/x-csrc
File size: 12971 byte(s)
Fixed building of test suite after code reorg.
Test suite may still be broken though: seems to segfault and give lots of errors.
1 /*
2 * Unit test functions for ASCEND: utilities/readln.c
3 *
4 * Copyright (C) 2005 Jerry St.Clair
5 *
6 * This file is part of the Ascend Environment.
7 *
8 * The Ascend Environment is free software; you can redistribute it
9 * and/or modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * The Ascend Environment is distributed in hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with the program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named
21 * COPYING.
22 */
23
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <utilities/ascConfig.h>
27 #ifdef __WIN32__
28 #include <io.h>
29 #endif
30 #include <utilities/ascMalloc.h>
31 #include <utilities/readln.h>
32 #include "CUnit/CUnit.h"
33 #include "test_readln.h"
34 #include "test/printutil.h"
35 #include "test/redirectStdStreams.h"
36
37 #define STR_LEN 100
38
39 static void test_readln(void)
40 {
41 FILE *infile;
42 char infilename[] = "test_readln.txt";
43 char str1[STR_LEN];
44 char str2[STR_LEN];
45 char *pstr;
46 long long_value;
47 double double_value;
48 int i_enabled_printing = FALSE;
49 unsigned long prior_meminuse;
50
51 prior_meminuse = ascmeminuse(); /* save meminuse() at start of test function */
52
53 /* this test requires message printing to be enabled */
54 if (FALSE == test_printing_enabled()) {
55 test_enable_printing();
56 i_enabled_printing = TRUE;
57 }
58
59 /* test readln() */
60
61 #if 0 /* removing support for stream redirection (too confusing) */
62 if (NULL != (infile = fopen(infilename, "w"))) {
63 fclose(infile);
64 infile = redirect_redirectstdin(infilename);
65 CU_TEST(-1 == readln(NULL, STR_LEN)); /* NULL str */
66 rewind(infile);
67 CU_TEST(0 == readln(str1, 0)); /* max = 0 */
68 rewind(infile);
69 CU_TEST(-1 == readln(str1, STR_LEN)); /* read, but stream empty */
70 reset_stdin();
71 }
72 else {
73 CU_FAIL("Output file could not be opened in test_readln().");
74 }
75 #endif
76
77 #if 0 /* removing support for stream redirection (too confusing) */
78 if (NULL != (infile = fopen(infilename, "w"))) {
79 snprintf(str2, STR_LEN, "This is the string we expect back from readln()?\n");
80 fputs(str2, infile);
81 fclose(infile);
82 infile = redirect_stdin(infilename);
83 CU_TEST(-1 == readln(NULL, STR_LEN)); /* NULL str */
84 rewind(infile);
85 CU_TEST(0 == readln(str1, 0)); /* max = 0 */
86 rewind(infile);
87 CU_TEST((int)(strlen(str2)-1) == readln(str1, STR_LEN)); /* read from stream */
88 CU_TEST(0 == strncmp(str1, str2, strlen(str2)-1));
89 reset_stdin();
90 }
91 else {
92 CU_FAIL("Output file could not be opened in test_readln().");
93 }
94 #endif
95
96 #if 0 /* removing support for stream redirection (too confusing) */
97 if (NULL != (infile = fopen(infilename, "w"))) {
98 snprintf(str2, STR_LEN, "\nThis is the string we expect back from readln()?\n");
99 fputs(str2, infile);
100 fclose(infile);
101 infile = redirect_stdin(infilename);
102 CU_TEST(0 == readln(str1, 0)); /* max = 0 */
103 rewind(infile);
104 CU_TEST(0 == readln(str1, STR_LEN)); /* read from stream with '\n' at start */
105 CU_TEST(0 == strlen(str1));
106 CU_TEST((int)(strlen(str2)-2) == readln(str1, STR_LEN)); /* read more from stream */
107 CU_TEST(0 == strncmp(str1, str2+1, strlen(str2)-2));
108 reset_stdin();
109 }
110 else {
111 CU_FAIL("Output file could not be opened in test_readln().");
112 }
113 #endif
114
115 /* test freadln() */
116
117 if (NULL != (infile = fopen(infilename, "w+"))) {
118 CU_TEST(-1 == freadln(NULL, STR_LEN, infile)); /* NULL str */
119 rewind(infile);
120 CU_TEST(0 == freadln(str1, 0, infile)); /* max = 0 */
121 rewind(infile);
122 CU_TEST(-1 == freadln(str1, STR_LEN, infile)); /* read, but stream empty */
123 fclose(infile);
124 }
125 else {
126 CU_FAIL("Output file could not be opened in test_readln().");
127 }
128
129 if (NULL != (infile = fopen(infilename, "w+"))) {
130 snprintf(str2, STR_LEN, "This is the string we expect back from readln()?\n");
131 fputs(str2, infile);
132 rewind(infile);
133 CU_TEST(-1 == freadln(NULL, STR_LEN, infile)); /* NULL str */
134 rewind(infile);
135 CU_TEST(0 == freadln(str1, 0, infile)); /* max = 0 */
136 rewind(infile);
137 CU_TEST((int)(strlen(str2)-1) == freadln(str1, STR_LEN, infile)); /* read from stream */
138 CU_TEST(0 == strncmp(str1, str2, strlen(str2)-1));
139 fclose(infile);
140 }
141 else {
142 CU_FAIL("Output file could not be opened in test_readln().");
143 }
144
145 if (NULL != (infile = fopen(infilename, "w+"))) {
146 snprintf(str2, STR_LEN, "\nThis is the string we expect back from readln()?\n");
147 fputs(str2, infile);
148 rewind(infile);
149 CU_TEST(0 == freadln(str1, 0, infile)); /* max = 0 */
150 rewind(infile);
151 CU_TEST(0 == freadln(str1, STR_LEN, infile)); /* read from stream with '\n' at start */
152 CU_TEST(0 == strlen(str1));
153 CU_TEST((int)(strlen(str2)-2) == freadln(str1, STR_LEN, infile)); /* read more from stream */
154 CU_TEST(0 == strncmp(str1, str2+1, strlen(str2)-2));
155 fclose(infile);
156 }
157 else {
158 CU_FAIL("Output file could not be opened in test_readln().");
159 }
160
161 /* test areadln() */
162
163 #if 0 /* removing support for stream redirection (too confusing) */
164 if (NULL != (infile = fopen(infilename, "w"))) {
165 fclose(infile);
166 infile = redirect_stdin(infilename);
167 pstr = areadln(); /* read from empty stream */
168 CU_TEST(NULL == pstr);
169 if (NULL != pstr)
170 ascfree(pstr);
171 reset_stdin();
172 }
173 else {
174 CU_FAIL("Output file could not be opened in test_readln().");
175 }
176 #endif
177
178 #if 0 /* removing support for stream redirection (too confusing) */
179 if (NULL != (infile = fopen(infilename, "w"))) {
180 snprintf(str2, STR_LEN, "This is the string we expect back from areadln()?\n");
181 fputs(str2, infile);
182 fclose(infile);
183 infile = redirect_stdin(infilename);
184 pstr = areadln(); /* read from typical stream */
185 CU_TEST(NULL != pstr);
186 CU_TEST((strlen(str2)-1) == strlen(pstr));
187 CU_TEST(0 == strncmp(pstr, str2, strlen(str2)-1));
188 if (NULL != pstr)
189 ascfree(pstr);
190 reset_stdin();
191 }
192 else {
193 CU_FAIL("Output file could not be opened in test_readln().");
194 }
195 #endif
196
197 #if 0 /* removing support for stream redirection (too confusing) */
198 if (NULL != (infile = fopen(infilename, "w"))) {
199 snprintf(str2, STR_LEN, "\nThis is the string we expect back from areadln()?\n");
200 fputs(str2, infile);
201 fclose(infile);
202 infile = redirect_stdin(infilename);
203 pstr = areadln(); /* read from stream with '\n' at start */
204 CU_TEST(NULL != pstr)
205 CU_TEST(0 == strlen(pstr));
206 if (NULL != pstr)
207 ascfree(pstr);
208 pstr = areadln(); /* read more from stream */
209 CU_TEST(NULL != pstr)
210 CU_TEST((strlen(str2)-2) == strlen(pstr));
211 CU_TEST(0 == strncmp(pstr, str2+1, strlen(str2)-2));
212 if (NULL != pstr)
213 ascfree(pstr);
214 reset_stdin();
215 }
216 else {
217 CU_FAIL("Output file could not be opened in test_readln().");
218 }
219 #endif
220
221 /* test afreadln() */
222
223 if (NULL != (infile = fopen(infilename, "w+"))) {
224 rewind(infile);
225 pstr = afreadln(infile); /* read from empty stream */
226 CU_TEST(NULL == pstr);
227 fclose(infile);
228 }
229 else {
230 CU_FAIL("Output file could not be opened in test_readln().");
231 }
232
233 if (NULL != (infile = fopen(infilename, "w+"))) {
234 snprintf(str2, STR_LEN, "This is the string we expect back from readln()?\n");
235 fputs(str2, infile);
236 rewind(infile);
237 pstr = afreadln(infile); /* read from typical stream */
238 CU_TEST(NULL != pstr);
239 CU_TEST((strlen(str2)-1) == strlen(pstr));
240 CU_TEST(0 == strncmp(pstr, str2, strlen(str2)-1));
241 if (NULL != pstr)
242 ascfree(pstr);
243 fclose(infile);
244 }
245 else {
246 CU_FAIL("Output file could not be opened in test_readln().");
247 }
248
249 if (NULL != (infile = fopen(infilename, "w+"))) {
250 snprintf(str2, STR_LEN, "\nThis is the string we expect back from readln()?\n");
251 fputs(str2, infile);
252 rewind(infile);
253 pstr = afreadln(infile); /* read from stream with '\n' at start */
254 CU_TEST(NULL != pstr);
255 CU_TEST(0 == strlen(pstr));
256 if (NULL != pstr)
257 ascfree(pstr);
258 pstr = afreadln(infile); /* read more from stream */
259 CU_TEST(NULL != pstr)
260 CU_TEST((strlen(str2)-2) == strlen(pstr));
261 CU_TEST(0 == strncmp(pstr, str2+1, strlen(str2)-2));
262 if (NULL != pstr)
263 ascfree(pstr);
264 fclose(infile);
265 }
266 else {
267 CU_FAIL("Output file could not be opened in test_readln().");
268 }
269
270 /* test readlong() */
271
272 #if 0 /* removing support for stream redirection (too confusing) */
273 if (NULL != (infile = fopen(infilename, "w"))) {
274 fclose(infile);
275 infile = redirect_stdin(infilename);
276 long_value = readlong(123456); /* read from empty stream */
277 CU_TEST(123456 == long_value);
278 reset_stdin();
279 }
280 else {
281 CU_FAIL("Output file could not be opened in test_readln().");
282 }
283 #endif
284
285 #if 0 /* removing support for stream redirection (too confusing) */
286 if (NULL != (infile = fopen(infilename, "w"))) {
287 snprintf(str2, STR_LEN, "98765This is the string we expect back from areadln()?\n");
288 fputs(str2, infile);
289 fclose(infile);
290 infile = redirect_stdin(infilename);
291 long_value = readlong(0); /* read from typical stream */
292 CU_TEST(98765 == long_value);
293 reset_stdin();
294 }
295 else {
296 CU_FAIL("Output file could not be opened in test_readln().");
297 }
298 #endif
299
300 #if 0 /* removing support for stream redirection (too confusing) */
301 if (NULL != (infile = fopen(infilename, "w"))) {
302 snprintf(str2, STR_LEN, "\n-837\n");
303 fputs(str2, infile);
304 fclose(infile);
305 infile = redirect_stdin(infilename);
306 long_value = readlong(123456); /* read from stream with '\n' at start */
307 CU_TEST(123456 == long_value);
308 long_value = readlong(123456); /* read more from stream */
309 CU_TEST(-837 == long_value);
310 reset_stdin();
311 }
312 else {
313 CU_FAIL("Output file could not be opened in test_readln().");
314 }
315 #endif
316
317 /* test readdouble() */
318
319 #if 0 /* removing support for stream redirection (too confusing) */
320 if (NULL != (infile = fopen(infilename, "w"))) {
321 fclose(infile);
322 infile = redirect_stdin(infilename);
323 double_value = readdouble(1.45734); /* read from empty stream */
324 CU_ASSERT_DOUBLE_EQUAL(1.45734, double_value, 0.00001);
325 reset_stdin();
326 }
327 else {
328 CU_FAIL("Output file could not be opened in test_readln().");
329 }
330 #endif
331
332 #if 0 /* removing support for stream redirection (too confusing) */
333 if (NULL != (infile = fopen(infilename, "w"))) {
334 snprintf(str2, STR_LEN, "-3.5670384e199This is the string we expect back from areadln()?\n");
335 fputs(str2, infile);
336 fclose(infile);
337 infile = redirect_stdin(infilename);
338 double_value = readdouble(0.0); /* read from typical stream */
339 CU_ASSERT_DOUBLE_EQUAL(-3.5670384e199, double_value, 0.00001);
340 reset_stdin();
341 }
342 else {
343 CU_FAIL("Output file could not be opened in test_readln().");
344 }
345 #endif
346
347 #if 0 /* removing support for stream redirection (too confusing) */
348 if (NULL != (infile = fopen(infilename, "w"))) {
349 snprintf(str2, STR_LEN, "\n-642542146Good bye!\n");
350 fputs(str2, infile);
351 fclose(infile);
352 infile = redirect_stdin(infilename);
353 double_value = readdouble(0.0); /* read from stream with '\n' at start */
354 CU_ASSERT_DOUBLE_EQUAL(0.0, double_value, 0.00001);
355 double_value = readdouble(0.0); /* read more from stream */
356 CU_ASSERT_DOUBLE_EQUAL(-642542146, double_value, 0.00001);
357 reset_stdin();
358 }
359 else {
360 CU_FAIL("Output file could not be opened in test_readln().");
361 }
362 #endif
363
364 if (TRUE == i_enabled_printing) {
365 test_disable_printing();
366 }
367
368 CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */
369 }
370
371 /*===========================================================================*/
372 /* Registration information */
373
374 static CU_TestInfo readln_test_list[] = {
375 {"readln", test_readln},
376 CU_TEST_INFO_NULL
377 };
378
379 static CU_SuiteInfo suites[] = {
380 {"utilities_readln", NULL, NULL, readln_test_list},
381 CU_SUITE_INFO_NULL
382 };
383
384 /*-------------------------------------------------------------------*/
385 CU_ErrorCode test_register_utilities_readln(void)
386 {
387 return CU_register_suites(suites);
388 }

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22