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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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