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