1 |
/* |
2 |
* Unit test functions for ASCEND: utilities/set.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 <stdlib.h> |
25 |
#include <stdio.h> |
26 |
#include <utilities/ascConfig.h> |
27 |
#include <utilities/ascMalloc.h> |
28 |
#include <utilities/set.h> |
29 |
#include "CUnit/CUnit.h" |
30 |
#include "test/assertimpl.h" |
31 |
#include "test_set.h" |
32 |
|
33 |
static void test_set(void) |
34 |
{ |
35 |
unsigned *set1; |
36 |
unsigned int i; |
37 |
unsigned long prior_meminuse; |
38 |
|
39 |
prior_meminuse = ascmeminuse(); /* save meminuse() at start of test function */ |
40 |
|
41 |
#ifndef MALLOC_DEBUG |
42 |
CU_FAIL("test_set() compiled without MALLOC_DEBUG - memory management not tested."); |
43 |
#endif |
44 |
|
45 |
/* test set_size() */ |
46 |
|
47 |
CU_TEST(0 == set_size(0)); |
48 |
for (i=1 ; i<=WORDSIZE ; ++i) { |
49 |
CU_TEST(1 == set_size(i)); |
50 |
} |
51 |
for (i=(WORDSIZE+1) ; i<=(WORDSIZE*2) ; ++i) { |
52 |
CU_TEST(2 == set_size(i)); |
53 |
} |
54 |
for (i=((500*WORDSIZE)+1) ; i<=(WORDSIZE*501) ; ++i) { |
55 |
CU_TEST(501 == set_size(i)); |
56 |
} |
57 |
|
58 |
/* test set_create(), set_destroy() */ |
59 |
|
60 |
set1 = set_create(-10); /* error - negative size */ |
61 |
CU_TEST(NULL == set1); |
62 |
#ifdef MALLOC_DEBUG |
63 |
CU_TEST(0 == AllocatedMemory(set1, 0)); |
64 |
#endif |
65 |
|
66 |
if (NULL != set1) |
67 |
set_destroy(set1); |
68 |
|
69 |
set1 = set_create(0); /* error - zero size */ |
70 |
CU_TEST(NULL == set1); |
71 |
#ifdef MALLOC_DEBUG |
72 |
CU_TEST(0 == AllocatedMemory(set1, 0)); |
73 |
#endif |
74 |
if (NULL != set1) |
75 |
set_destroy(set1); |
76 |
|
77 |
set1 = set_create(10); /* ok - valid size */ |
78 |
CU_TEST(NULL != set1); |
79 |
#ifdef MALLOC_DEBUG |
80 |
CU_TEST(2 == AllocatedMemory(set1, sizeof(unsigned)*set_size(10))); |
81 |
#endif |
82 |
if (NULL != set1) |
83 |
set_destroy(set1); |
84 |
#ifdef MALLOC_DEBUG |
85 |
CU_TEST(0 == AllocatedMemory(set1, sizeof(unsigned)*set_size(10))); |
86 |
#endif |
87 |
|
88 |
/* test set_ndx() */ |
89 |
|
90 |
for (i=0 ; i<WORDSIZE ; ++i) { |
91 |
CU_TEST(0 == set_ndx(i)); |
92 |
} |
93 |
for (i=WORDSIZE ; i<(WORDSIZE*2) ; ++i) { |
94 |
CU_TEST(1 == set_ndx(i)); |
95 |
} |
96 |
for (i=(500*WORDSIZE) ; i<(WORDSIZE*501) ; ++i) { |
97 |
CU_TEST(500 == set_ndx(i)); |
98 |
} |
99 |
|
100 |
/* test set_mask() */ |
101 |
|
102 |
for (i=0 ; i<WORDSIZE ; ++i) { |
103 |
CU_TEST(((unsigned)1 << i) == set_mask(i)); |
104 |
} |
105 |
for (i=((500*WORDSIZE)+1) ; i<=(WORDSIZE*501) ; ++i) { |
106 |
CU_TEST(((unsigned)1 << (i-(500*WORDSIZE))) == set_mask(i)); |
107 |
} |
108 |
|
109 |
/* test set_is_member() */ |
110 |
|
111 |
set1 = set_create(10); /* create a set & zero it */ |
112 |
CU_TEST(NULL != set1); |
113 |
|
114 |
if (NULL != set1) { |
115 |
set_null(set1, 10); |
116 |
|
117 |
for (i=0 ; i<10 ; ++i) { |
118 |
CU_TEST(FALSE == set_is_member(set1, i)); /* set should not contain member */ |
119 |
set_change_member(set1, i, TRUE); /* add member to set */ |
120 |
CU_TEST(TRUE == set_is_member(set1, i)); /* set should contain member */ |
121 |
} |
122 |
|
123 |
set_destroy(set1); |
124 |
} |
125 |
|
126 |
/* test set_chk_is_member() */ |
127 |
|
128 |
set1 = set_create(10); /* create a set & zero it */ |
129 |
CU_TEST(NULL != set1); |
130 |
|
131 |
if (NULL != set1) { |
132 |
set_null(set1, 10); |
133 |
CU_TEST(FALSE == set_chk_is_member(set1, -1, 10)); /* error - negative element */ |
134 |
CU_TEST(FALSE == set_chk_is_member(set1, 11, 10)); /* error - element larger than size */ |
135 |
|
136 |
for (i=0 ; i<10 ; ++i) { |
137 |
CU_TEST(FALSE == set_chk_is_member(set1, (int)i, 10)); /* set should not contain member */ |
138 |
set_change_member(set1, i, TRUE); /* add member to set */ |
139 |
CU_TEST(TRUE == set_chk_is_member(set1, (int)i, 10)); /* set should contain member */ |
140 |
} |
141 |
|
142 |
CU_TEST(FALSE == set_chk_is_member(set1, -1, 10)); /* error - negative element */ |
143 |
CU_TEST(FALSE == set_chk_is_member(set1, 11, 10)); /* error - element lsrger than size */ |
144 |
|
145 |
set_destroy(set1); |
146 |
} |
147 |
|
148 |
/* test set_null() */ |
149 |
|
150 |
#ifndef ASC_NO_ASSERTIONS |
151 |
asc_assert_catch(TRUE); /* prepare to test assertions */ |
152 |
|
153 |
asc_assert_reset(); |
154 |
if (0 == setjmp(g_asc_test_env)) |
155 |
set_null(NULL, 10); /* error - set NULL */ |
156 |
CU_TEST(TRUE == asc_assert_failed()); |
157 |
|
158 |
asc_assert_catch(FALSE); /* done testing assertions */ |
159 |
#endif /* !ASC_NO_ASSERTIONS */ |
160 |
|
161 |
set1 = set_create(10); /* create a set & zero it */ |
162 |
CU_TEST(NULL != set1); |
163 |
|
164 |
if (NULL != set1) { |
165 |
set_null(set1, 10); |
166 |
|
167 |
for (i=0 ; i<10 ; ++i) { |
168 |
CU_TEST(FALSE == set_is_member(set1, i)); /* set should not contain any members */ |
169 |
} |
170 |
|
171 |
for (i=0 ; i<10 ; ++i) { |
172 |
set_change_member(set1, i, TRUE); /* add member to set */ |
173 |
} |
174 |
|
175 |
for (i=0 ; i<10 ; ++i) { |
176 |
CU_TEST(TRUE == set_is_member(set1, i)); /* set should contain all members */ |
177 |
} |
178 |
|
179 |
set_null(set1, 10); |
180 |
|
181 |
for (i=0 ; i<10 ; ++i) { |
182 |
CU_TEST(FALSE == set_is_member(set1, i)); /* set should not contain any members */ |
183 |
} |
184 |
|
185 |
set_destroy(set1); |
186 |
} |
187 |
|
188 |
/* test set_change_member() */ |
189 |
|
190 |
#ifndef ASC_NO_ASSERTIONS |
191 |
asc_assert_catch(TRUE); /* prepare to test assertions */ |
192 |
|
193 |
asc_assert_reset(); |
194 |
if (0 == setjmp(g_asc_test_env)) |
195 |
set_change_member(NULL, 1, TRUE); |
196 |
CU_TEST(TRUE == asc_assert_failed()); |
197 |
|
198 |
asc_assert_reset(); |
199 |
if (0 == setjmp(g_asc_test_env)) |
200 |
set_change_member(NULL, 1, FALSE); |
201 |
CU_TEST(TRUE == asc_assert_failed()); |
202 |
|
203 |
asc_assert_catch(FALSE); /* done testing assertions */ |
204 |
#endif /* !ASC_NO_ASSERTIONS */ |
205 |
|
206 |
set1 = set_create(10); /* create a set & zero it */ |
207 |
CU_TEST(NULL != set1); |
208 |
|
209 |
if (NULL != set1) { |
210 |
set_null(set1, 10); |
211 |
|
212 |
for (i=0 ; i<10 ; ++i) { |
213 |
CU_TEST(FALSE == set_is_member(set1, i)); /* set should not contain member */ |
214 |
set_change_member(set1, i, TRUE); /* add member to set */ |
215 |
CU_TEST(TRUE == set_is_member(set1, i)); /* set should contain member */ |
216 |
set_change_member(set1, i, FALSE); /* remove member from set */ |
217 |
CU_TEST(FALSE == set_is_member(set1, i)); /* set should not contain member */ |
218 |
} |
219 |
|
220 |
set_destroy(set1); |
221 |
} |
222 |
|
223 |
CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */ |
224 |
} |
225 |
|
226 |
/*===========================================================================*/ |
227 |
/* Registration information */ |
228 |
|
229 |
static CU_TestInfo set_test_list[] = { |
230 |
{"set", test_set}, |
231 |
CU_TEST_INFO_NULL |
232 |
}; |
233 |
|
234 |
static CU_SuiteInfo suites[] = { |
235 |
{"utilities_set", NULL, NULL, set_test_list}, |
236 |
CU_SUITE_INFO_NULL |
237 |
}; |
238 |
|
239 |
/*-------------------------------------------------------------------*/ |
240 |
CU_ErrorCode test_register_utilities_set(void) |
241 |
{ |
242 |
return CU_register_suites(suites); |
243 |
} |