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

Annotation of /trunk/base/generic/utilities/test/test_readln.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 100 - (hide annotations) (download) (as text)
Fri Dec 9 23:25:15 2005 UTC (18 years, 10 months ago) by jds
File MIME type: text/x-csrc
File size: 12063 byte(s)
ascMalloc now working on Linux.
Fixed minor bugs in ascPanic.c, test suite functions.
1 jds 59 /*
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 jds 100 #include <stdarg.h>
26     #include "utilities/ascConfig.h"
27 jds 60 #ifdef __WIN32__
28 jds 59 #include <io.h>
29 jds 60 #endif
30 jds 59 #include "utilities/ascMalloc.h"
31     #include "utilities/readln.h"
32     #include "CUnit/CUnit.h"
33     #include "test_readln.h"
34     #include "printutil.h"
35     #include "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 (NULL != (infile = fopen(infilename, "w"))) {
62     fclose(infile);
63     infile = redirect_stdin(infilename);
64     CU_TEST(-1 == readln(NULL, STR_LEN)); /* NULL str */
65     rewind(infile);
66 jds 61 CU_TEST(0 == readln(str1, 0)); /* max = 0 */
67 jds 59 rewind(infile);
68     CU_TEST(-1 == readln(str1, STR_LEN)); /* read, but stream empty */
69     reset_stdin();
70     }
71     else {
72     CU_FAIL("Output file could not be opened in test_readln().");
73     }
74    
75     if (NULL != (infile = fopen(infilename, "w"))) {
76     snprintf(str2, STR_LEN, "This is the string we expect back from readln()?\n");
77     fputs(str2, infile);
78     fclose(infile);
79     infile = redirect_stdin(infilename);
80     CU_TEST(-1 == readln(NULL, STR_LEN)); /* NULL str */
81     rewind(infile);
82     CU_TEST(0 == readln(str1, 0)); /* max = 0 */
83     rewind(infile);
84     CU_TEST((int)(strlen(str2)-1) == readln(str1, STR_LEN)); /* read from stream */
85     CU_TEST(0 == strncmp(str1, str2, strlen(str2)-1));
86     reset_stdin();
87     }
88     else {
89     CU_FAIL("Output file could not be opened in test_readln().");
90     }
91    
92     if (NULL != (infile = fopen(infilename, "w"))) {
93     snprintf(str2, STR_LEN, "\nThis is the string we expect back from readln()?\n");
94     fputs(str2, infile);
95     fclose(infile);
96     infile = redirect_stdin(infilename);
97     CU_TEST(0 == readln(str1, 0)); /* max = 0 */
98     rewind(infile);
99     CU_TEST(0 == readln(str1, STR_LEN)); /* read from stream with '\n' at start */
100     CU_TEST(0 == strlen(str1));
101     CU_TEST((int)(strlen(str2)-2) == readln(str1, STR_LEN)); /* read more from stream */
102     CU_TEST(0 == strncmp(str1, str2+1, strlen(str2)-2));
103     reset_stdin();
104     }
105     else {
106     CU_FAIL("Output file could not be opened in test_readln().");
107     }
108    
109     /* test freadln() */
110    
111     if (NULL != (infile = fopen(infilename, "w+"))) {
112     CU_TEST(-1 == freadln(NULL, STR_LEN, infile)); /* NULL str */
113     rewind(infile);
114     CU_TEST(0 == freadln(str1, 0, infile)); /* max = 0 */
115     rewind(infile);
116     CU_TEST(-1 == freadln(str1, STR_LEN, infile)); /* read, but stream empty */
117     fclose(infile);
118     }
119     else {
120     CU_FAIL("Output file could not be opened in test_readln().");
121     }
122    
123     if (NULL != (infile = fopen(infilename, "w+"))) {
124     snprintf(str2, STR_LEN, "This is the string we expect back from readln()?\n");
125     fputs(str2, infile);
126     rewind(infile);
127     CU_TEST(-1 == freadln(NULL, STR_LEN, infile)); /* NULL str */
128     rewind(infile);
129     CU_TEST(0 == freadln(str1, 0, infile)); /* max = 0 */
130     rewind(infile);
131     CU_TEST((int)(strlen(str2)-1) == freadln(str1, STR_LEN, infile)); /* read from stream */
132     CU_TEST(0 == strncmp(str1, str2, strlen(str2)-1));
133     fclose(infile);
134     }
135     else {
136     CU_FAIL("Output file could not be opened in test_readln().");
137     }
138    
139     if (NULL != (infile = fopen(infilename, "w+"))) {
140     snprintf(str2, STR_LEN, "\nThis is the string we expect back from readln()?\n");
141     fputs(str2, infile);
142     rewind(infile);
143     CU_TEST(0 == freadln(str1, 0, infile)); /* max = 0 */
144     rewind(infile);
145     CU_TEST(0 == freadln(str1, STR_LEN, infile)); /* read from stream with '\n' at start */
146     CU_TEST(0 == strlen(str1));
147     CU_TEST((int)(strlen(str2)-2) == freadln(str1, STR_LEN, infile)); /* read more from stream */
148     CU_TEST(0 == strncmp(str1, str2+1, strlen(str2)-2));
149     fclose(infile);
150     }
151     else {
152     CU_FAIL("Output file could not be opened in test_readln().");
153     }
154    
155     /* test areadln() */
156    
157     if (NULL != (infile = fopen(infilename, "w"))) {
158     fclose(infile);
159     infile = redirect_stdin(infilename);
160     pstr = areadln(); /* read from empty stream */
161     CU_TEST(NULL == pstr);
162     if (NULL != pstr)
163     ascfree(pstr);
164     reset_stdin();
165     }
166     else {
167     CU_FAIL("Output file could not be opened in test_readln().");
168     }
169    
170     if (NULL != (infile = fopen(infilename, "w"))) {
171     snprintf(str2, STR_LEN, "This is the string we expect back from areadln()?\n");
172     fputs(str2, infile);
173     fclose(infile);
174     infile = redirect_stdin(infilename);
175     pstr = areadln(); /* read from typical stream */
176     CU_TEST(NULL != pstr);
177     CU_TEST((strlen(str2)-1) == strlen(pstr));
178     CU_TEST(0 == strncmp(pstr, str2, strlen(str2)-1));
179     if (NULL != pstr)
180     ascfree(pstr);
181     reset_stdin();
182     }
183     else {
184     CU_FAIL("Output file could not be opened in test_readln().");
185     }
186    
187     if (NULL != (infile = fopen(infilename, "w"))) {
188     snprintf(str2, STR_LEN, "\nThis is the string we expect back from areadln()?\n");
189     fputs(str2, infile);
190     fclose(infile);
191     infile = redirect_stdin(infilename);
192     pstr = areadln(); /* read from stream with '\n' at start */
193     CU_TEST(NULL != pstr)
194     CU_TEST(0 == strlen(pstr));
195     if (NULL != pstr)
196     ascfree(pstr);
197     pstr = areadln(); /* read more from stream */
198     CU_TEST(NULL != pstr)
199     CU_TEST((strlen(str2)-2) == strlen(pstr));
200     CU_TEST(0 == strncmp(pstr, str2+1, strlen(str2)-2));
201     if (NULL != pstr)
202     ascfree(pstr);
203     reset_stdin();
204     }
205     else {
206     CU_FAIL("Output file could not be opened in test_readln().");
207     }
208    
209     /* test afreadln() */
210    
211     if (NULL != (infile = fopen(infilename, "w+"))) {
212     rewind(infile);
213     pstr = afreadln(infile); /* read from empty stream */
214     CU_TEST(NULL == pstr);
215     fclose(infile);
216     }
217     else {
218     CU_FAIL("Output file could not be opened in test_readln().");
219     }
220    
221     if (NULL != (infile = fopen(infilename, "w+"))) {
222     snprintf(str2, STR_LEN, "This is the string we expect back from readln()?\n");
223     fputs(str2, infile);
224     rewind(infile);
225     pstr = afreadln(infile); /* read from typical stream */
226     CU_TEST(NULL != pstr);
227     CU_TEST((strlen(str2)-1) == strlen(pstr));
228     CU_TEST(0 == strncmp(pstr, str2, strlen(str2)-1));
229     if (NULL != pstr)
230     ascfree(pstr);
231     fclose(infile);
232     }
233     else {
234     CU_FAIL("Output file could not be opened in test_readln().");
235     }
236    
237     if (NULL != (infile = fopen(infilename, "w+"))) {
238     snprintf(str2, STR_LEN, "\nThis is the string we expect back from readln()?\n");
239     fputs(str2, infile);
240     rewind(infile);
241     pstr = afreadln(infile); /* read from stream with '\n' at start */
242     CU_TEST(NULL != pstr);
243     CU_TEST(0 == strlen(pstr));
244     if (NULL != pstr)
245     ascfree(pstr);
246     pstr = afreadln(infile); /* read more from stream */
247     CU_TEST(NULL != pstr)
248     CU_TEST((strlen(str2)-2) == strlen(pstr));
249     CU_TEST(0 == strncmp(pstr, str2+1, strlen(str2)-2));
250     if (NULL != pstr)
251     ascfree(pstr);
252     fclose(infile);
253     }
254     else {
255     CU_FAIL("Output file could not be opened in test_readln().");
256     }
257    
258     /* test readlong() */
259    
260     if (NULL != (infile = fopen(infilename, "w"))) {
261     fclose(infile);
262     infile = redirect_stdin(infilename);
263     long_value = readlong(123456); /* read from empty stream */
264     CU_TEST(123456 == long_value);
265     reset_stdin();
266     }
267     else {
268     CU_FAIL("Output file could not be opened in test_readln().");
269     }
270    
271     if (NULL != (infile = fopen(infilename, "w"))) {
272     snprintf(str2, STR_LEN, "98765This is the string we expect back from areadln()?\n");
273     fputs(str2, infile);
274     fclose(infile);
275     infile = redirect_stdin(infilename);
276     long_value = readlong(0); /* read from typical stream */
277     CU_TEST(98765 == long_value);
278     reset_stdin();
279     }
280     else {
281     CU_FAIL("Output file could not be opened in test_readln().");
282     }
283    
284     if (NULL != (infile = fopen(infilename, "w"))) {
285     snprintf(str2, STR_LEN, "\n-837\n");
286     fputs(str2, infile);
287     fclose(infile);
288     infile = redirect_stdin(infilename);
289     long_value = readlong(123456); /* read from stream with '\n' at start */
290     CU_TEST(123456 == long_value);
291     long_value = readlong(123456); /* read more from stream */
292     CU_TEST(-837 == long_value);
293     reset_stdin();
294     }
295     else {
296     CU_FAIL("Output file could not be opened in test_readln().");
297     }
298    
299     /* test readdouble() */
300    
301     if (NULL != (infile = fopen(infilename, "w"))) {
302     fclose(infile);
303     infile = redirect_stdin(infilename);
304     double_value = readdouble(1.45734); /* read from empty stream */
305     CU_ASSERT_DOUBLE_EQUAL(1.45734, double_value, 0.00001);
306     reset_stdin();
307     }
308     else {
309     CU_FAIL("Output file could not be opened in test_readln().");
310     }
311    
312     if (NULL != (infile = fopen(infilename, "w"))) {
313     snprintf(str2, STR_LEN, "-3.5670384e199This is the string we expect back from areadln()?\n");
314     fputs(str2, infile);
315     fclose(infile);
316     infile = redirect_stdin(infilename);
317     double_value = readdouble(0.0); /* read from typical stream */
318     CU_ASSERT_DOUBLE_EQUAL(-3.5670384e199, double_value, 0.00001);
319     reset_stdin();
320     }
321     else {
322     CU_FAIL("Output file could not be opened in test_readln().");
323     }
324    
325     if (NULL != (infile = fopen(infilename, "w"))) {
326     snprintf(str2, STR_LEN, "\n-642542146Good bye!\n");
327     fputs(str2, infile);
328     fclose(infile);
329     infile = redirect_stdin(infilename);
330     double_value = readdouble(0.0); /* read from stream with '\n' at start */
331     CU_ASSERT_DOUBLE_EQUAL(0.0, double_value, 0.00001);
332     double_value = readdouble(0.0); /* read more from stream */
333     CU_ASSERT_DOUBLE_EQUAL(-642542146, double_value, 0.00001);
334     reset_stdin();
335     }
336     else {
337     CU_FAIL("Output file could not be opened in test_readln().");
338     }
339    
340     if (TRUE == i_enabled_printing) {
341     test_disable_printing();
342     }
343    
344     CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */
345     }
346    
347     /*===========================================================================*/
348     /* Registration information */
349    
350     static CU_TestInfo readln_test_list[] = {
351     {"test_readln", test_readln},
352     CU_TEST_INFO_NULL
353     };
354    
355     static CU_SuiteInfo suites[] = {
356     {"test_utilities_readln", NULL, NULL, readln_test_list},
357     CU_SUITE_INFO_NULL
358     };
359    
360     /*-------------------------------------------------------------------*/
361     CU_ErrorCode test_register_utilities_readln(void)
362     {
363     return CU_register_suites(suites);
364     }

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