/[ascend]/trunk/base/generic/general/test/test_listio.c
ViewVC logotype

Annotation of /trunk/base/generic/general/test/test_listio.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1243 - (hide annotations) (download) (as text)
Fri Jan 26 12:35:17 2007 UTC (15 years, 6 months ago) by johnpye
File MIME type: text/x-csrc
File size: 4779 byte(s)
Removed header files from test_general. We can do without them.
1 jds 59 /*
2     * Unit test functions for ASCEND: general/listio.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 johnpye 399 #include <utilities/ascConfig.h>
26 jds 60 #ifdef __WIN32__
27 jds 59 #include <io.h>
28 jds 60 #endif
29 johnpye 399 #include <utilities/ascMalloc.h>
30     #include <general/list.h>
31     #include <general/listio.h>
32 jds 59 #include "CUnit/CUnit.h"
33     #include "assertimpl.h"
34     #include "redirectStdStreams.h"
35    
36     /*
37     * listio.[ch] implements writing of a list's details to file.
38     * Because there is no specification on the file contents, this
39     * test routine does not do more than check boundary conditions
40     * and make sure something is written to a specified file stream.
41     */
42     static void test_listio(void)
43     {
44     struct gl_list_t *p_list1;
45     unsigned long *pint_array[20];
46     unsigned long i;
47     FILE *file_normal;
48     FILE *file_stderr;
49     int i_initialized_lists = FALSE;
50     unsigned long prior_meminuse;
51    
52     prior_meminuse = ascmeminuse(); /* save meminuse() at start of test function */
53    
54     #ifdef NDEBUG
55     CU_FAIL("test_listio() compiled with NDEBUG - some features not tested.");
56     #endif
57    
58     /* set up pooling & recycling */
59     if (FALSE == gl_pool_initialized()) {
60     gl_init();
61     gl_init_pool();
62     i_initialized_lists = TRUE;
63     }
64    
65     for (i=0 ; i<20 ; ++i) { /* create some test data */
66 johnpye 709 pint_array[i] = ASC_NEW(unsigned long);
67 jds 59 *pint_array[i] = i;
68     }
69    
70     p_list1 = gl_create(20); /* create and fill a list with data */
71     for (i=0 ; i<20 ; ++i) {
72     gl_append_ptr(p_list1, pint_array[i]);
73     }
74    
75     /* test gl_write_list() */
76    
77     #ifndef ASC_NO_ASSERTIONS
78     asc_assert_catch(TRUE); /* prepare to test assertions */
79    
80     asc_assert_reset();
81     if (0 == setjmp(g_asc_test_env))
82     gl_write_list(NULL, NULL); /* error if NULL list* */
83     CU_TEST(TRUE == asc_assert_failed());
84    
85     asc_assert_catch(FALSE); /* done testing assertions */
86     #endif /* !ASC_NO_ASSERTIONS */
87    
88     if (NULL != (file_normal = fopen("listiotempfile.tmp", "w+"))) {
89    
90     gl_write_list(file_normal, p_list1);/* write to normal open file */
91     rewind(file_normal);
92     CU_TEST(EOF != fgetc(file_normal)); /* test that file is not empty */
93     fclose(file_normal);
94     }
95     else {
96     CU_FAIL("Error opening output file 1 in test_listio.c");
97     }
98    
99 johnpye 956 #if 0 /* removing stream redirection (too damn confusing) */
100    
101 jds 59 if (NULL != (file_stderr = redirect_stderr("listiotempstderr.tmp"))) {
102    
103     gl_write_list(NULL, p_list1); /* write to stderr */
104     rewind(file_stderr);
105    
106     if (NULL != (file_normal = fopen("listiotempfile.tmp", "r"))) {
107     int ch1 = fgetc(file_stderr);
108     int ch2 = fgetc(file_normal);
109     while ((EOF != ch1) && (EOF != ch2)) { /* test that files are the same */
110     if (ch1 != ch2) {
111     CU_FAIL("Files differ in test_listio.");
112     break;
113     }
114     ch1 = fgetc(file_stderr);
115     ch2 = fgetc(file_normal);
116     }
117     if (ch1 == ch2) {
118     CU_PASS("Files are the same in test_listio.");
119     }
120     fclose(file_normal);
121     }
122     reset_stderr();
123     remove("listiotempstderr.tmp");
124     }
125     else {
126     CU_FAIL("Error opening output file 2 in test_listio.c");
127     }
128     remove("listiotempfile.tmp");
129 johnpye 956 #endif
130 jds 59
131     gl_destroy(p_list1); /* clean up the list, preserving data */
132    
133     /* clean up and exit */
134     for (i=0 ; i<20 ; ++i)
135     ascfree(pint_array[i]);
136    
137     if (TRUE == i_initialized_lists) {
138     gl_destroy_pool();
139     }
140    
141     CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */
142     }
143    
144     /*===========================================================================*/
145     /* Registration information */
146    
147     static CU_TestInfo list_test_listio[] = {
148     {"test_listio", test_listio},
149     CU_TEST_INFO_NULL
150     };
151    
152     static CU_SuiteInfo suites[] = {
153     {"test_general_listio", NULL, NULL, list_test_listio},
154     CU_SUITE_INFO_NULL
155     };
156    
157     /*-------------------------------------------------------------------*/
158     CU_ErrorCode test_register_general_listio(void)
159     {
160     return CU_register_suites(suites);
161     }

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