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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 607 - (show annotations) (download) (as text)
Tue May 16 04:19:20 2006 UTC (14 years, 11 months ago) by johnpye
File MIME type: text/x-csrc
File size: 14471 byte(s)
Updated license/doc blocks at start of files.
Added test_ospath.c and test_ospath.h.
1 /* ASCEND modelling environment
2 Copyright (C) 2005 Jerry St.Clair
3 Copyright (C) 2006 Carnegie Mellon University
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 *//**
20 @file
21 Unit test functions for ASCEND: general/dstring.c
22 */
23 #include <string.h>
24 #include <CUnit/CUnit.h>
25
26 #include <utilities/ascConfig.h>
27 #include <general/dstring.h>
28 #include <utilities/ascMalloc.h>
29
30 #include "test_dstring.h"
31 #include <assertimpl.h>
32
33 static void test_dstring(void)
34 {
35 Asc_DString ds1;
36 const char NULL_STRING[] = "";
37 const char str1[] = "This string doesn't have much to say.";
38 char str_statsize_m1[ASC_DSTRING_STATIC_SIZE-1];
39 char str_statsize[ASC_DSTRING_STATIC_SIZE];
40 char str_statsize_p1[ASC_DSTRING_STATIC_SIZE+1];
41 char str_statcat1[2*ASC_DSTRING_STATIC_SIZE];
42 char str_statcat2[2*ASC_DSTRING_STATIC_SIZE+2];
43 char *my_str = NULL;
44 unsigned long prior_meminuse;
45
46 prior_meminuse = ascmeminuse();
47
48 /* set up test strings */
49 memset(&str_statsize_m1, '?', ASC_DSTRING_STATIC_SIZE-3);
50 str_statsize_m1[ASC_DSTRING_STATIC_SIZE-2] = '\0';
51
52 memset(&str_statsize, '=', ASC_DSTRING_STATIC_SIZE-2);
53 str_statsize[ASC_DSTRING_STATIC_SIZE-1] = '\0';
54
55 memset(&str_statsize_p1, '|', ASC_DSTRING_STATIC_SIZE-1);
56 str_statsize_p1[ASC_DSTRING_STATIC_SIZE] = '\0';
57
58 strcpy(str_statcat1, str_statsize_m1);
59 strcat(str_statcat1, str_statsize);
60
61 strcpy(str_statcat2, str_statsize);
62 strcat(str_statcat2, str_statsize_p1);
63
64 #ifdef NDEBUG
65 CU_FAIL("test_dstring() compiled with NDEBUG - some features not tested.");
66 #endif
67
68 /* test Asc_DStrintInit(), Asc_DStringFree() */
69
70 #ifndef ASC_NO_ASSERTIONS
71 asc_assert_catch(TRUE); /* prepare to test assertions */
72
73 asc_assert_reset();
74 if (0 == setjmp(g_asc_test_env))
75 Asc_DStringInit(NULL); /* error if dstring* is NULL */
76 CU_TEST(TRUE == asc_assert_failed());
77
78 asc_assert_reset();
79 if (0 == setjmp(g_asc_test_env))
80 Asc_DStringFree(NULL); /* error if dstring* is NULL */
81 CU_TEST(TRUE == asc_assert_failed());
82
83 asc_assert_catch(FALSE); /* done testing assertions */
84 #endif /* !ASC_NO_ASSERTIONS */
85
86 /* Initialize the dstrings and test initial conditions */
87 Asc_DStringInit(&ds1);
88
89 CU_TEST(Asc_DStringLength(&ds1) == 0);
90 CU_TEST(0 == strlen(Asc_DStringValue(&ds1)));
91 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), NULL_STRING);
92
93 /* test Asc_DStringSet */
94
95 #ifndef ASC_NO_ASSERTIONS
96 asc_assert_catch(TRUE); /* prepare to test assertions */
97
98 asc_assert_reset();
99 if (0 == setjmp(g_asc_test_env))
100 Asc_DStringSet(NULL, str1); /* error if dstring* is NULL */
101 CU_TEST(TRUE == asc_assert_failed());
102
103 asc_assert_reset();
104 if (0 == setjmp(g_asc_test_env))
105 Asc_DStringSet(&ds1, NULL); /* error if string* is NULL */
106 CU_TEST(TRUE == asc_assert_failed());
107
108 asc_assert_catch(FALSE); /* done testing assertions */
109 #endif /* !ASC_NO_ASSERTIONS */
110
111 Asc_DStringSet(&ds1, NULL_STRING); /* empty string */
112
113 CU_TEST(Asc_DStringLength(&ds1) == 0);
114 CU_TEST(0 == strlen(Asc_DStringValue(&ds1)));
115 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), NULL_STRING);
116
117 Asc_DStringSet(&ds1, str1); /* regular empty string */
118
119 CU_TEST(Asc_DStringLength(&ds1) == (int) strlen(str1));
120 CU_TEST(strlen(str1) == strlen(Asc_DStringValue(&ds1)));
121 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), str1);
122
123 Asc_DStringSet(&ds1, str_statsize_m1); /* regular string, length near static size */
124
125 CU_TEST(Asc_DStringLength(&ds1) == (int) strlen(str_statsize_m1));
126 CU_TEST(strlen(str_statsize_m1) == strlen(Asc_DStringValue(&ds1)));
127 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), str_statsize_m1);
128
129 Asc_DStringSet(&ds1, str_statsize); /* regular string, length at static size */
130
131 CU_TEST(Asc_DStringLength(&ds1) == (int) strlen(str_statsize));
132 CU_TEST(strlen(str_statsize) == strlen(Asc_DStringValue(&ds1)));
133 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), str_statsize);
134
135 Asc_DStringSet(&ds1, str_statsize_p1); /* regular string, length near static size */
136
137 CU_TEST(Asc_DStringLength(&ds1) == (int) strlen(str_statsize_p1));
138 CU_TEST(strlen(str_statsize_p1) == strlen(Asc_DStringValue(&ds1)));
139 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), str_statsize_p1);
140
141 Asc_DStringSet(&ds1, NULL_STRING); /* empty string again */
142
143 CU_TEST(Asc_DStringLength(&ds1) == 0);
144 CU_TEST(0 == strlen(Asc_DStringValue(&ds1)));
145 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), NULL_STRING);
146
147 /* test Asc_DStringAppend */
148
149 #ifndef ASC_NO_ASSERTIONS
150 asc_assert_catch(TRUE); /* prepare to test assertions */
151
152 asc_assert_reset();
153 if (0 == setjmp(g_asc_test_env))
154 Asc_DStringAppend(NULL, str1, 1); /* error if dstring* is NULL */
155 CU_TEST(TRUE == asc_assert_failed());
156
157 asc_assert_reset();
158 if (0 == setjmp(g_asc_test_env))
159 Asc_DStringAppend(&ds1, NULL, 1); /* error if string* is NULL */
160 CU_TEST(TRUE == asc_assert_failed());
161
162 asc_assert_catch(FALSE); /* done testing assertions */
163 #endif /* !ASC_NO_ASSERTIONS */
164
165 Asc_DStringFree(&ds1); /* free the string */
166
167 CU_TEST(Asc_DStringLength(&ds1) == 0);
168 CU_TEST(0 == strlen(Asc_DStringValue(&ds1)));
169 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), NULL_STRING);
170
171 Asc_DStringAppend(&ds1, NULL_STRING, 0); /* empty string, length = 0 */
172
173 CU_TEST(Asc_DStringLength(&ds1) == 0);
174 CU_TEST(0 == strlen(Asc_DStringValue(&ds1)));
175 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), NULL_STRING);
176
177 Asc_DStringAppend(&ds1, NULL_STRING, -10); /* empty string, length < 0 */
178
179 CU_TEST(Asc_DStringLength(&ds1) == 0);
180 CU_TEST(0 == strlen(Asc_DStringValue(&ds1)));
181 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), NULL_STRING);
182
183 Asc_DStringAppend(&ds1, NULL_STRING, 10); /* empty string, length > strlen() */
184
185 CU_TEST(Asc_DStringLength(&ds1) == 0);
186 CU_TEST(0 == strlen(Asc_DStringValue(&ds1)));
187 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), NULL_STRING);
188
189 Asc_DStringAppend(&ds1, str1, 0); /* regular str, length = 0 */
190
191 CU_TEST(Asc_DStringLength(&ds1) == 0);
192 CU_TEST(0 == strlen(Asc_DStringValue(&ds1)));
193 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), NULL_STRING);
194
195 Asc_DStringAppend(&ds1, str1, 1); /* regular str, length = 1 */
196
197 CU_TEST(Asc_DStringLength(&ds1) == 1);
198 CU_TEST(1 == strlen(Asc_DStringValue(&ds1)));
199 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), "T");
200
201 Asc_DStringAppend(&ds1, str1, -1); /* regular str, length < 0 */
202
203 CU_TEST(Asc_DStringLength(&ds1) == ((int)strlen(str1) + 1));
204 CU_TEST((strlen(str1) + 1) == strlen(Asc_DStringValue(&ds1)));
205 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1),
206 "TThis string doesn't have much to say.");
207
208 Asc_DStringAppend(&ds1, str1, strlen(str1)); /* regular str, length = strlen() */
209
210 CU_TEST(Asc_DStringLength(&ds1) == (2 * (int)strlen(str1) + 1));
211 CU_TEST((2 * strlen(str1) + 1) == strlen(Asc_DStringValue(&ds1)));
212 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1),
213 "TThis string doesn't have much to say.This string doesn't have much to say.");
214
215 Asc_DStringFree(&ds1); /* free the string */
216
217 CU_TEST(Asc_DStringLength(&ds1) == 0);
218 CU_TEST(0 == strlen(Asc_DStringValue(&ds1)));
219 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), NULL_STRING);
220
221 Asc_DStringAppend(&ds1, str_statsize_m1, -1); /* string near static size */
222 Asc_DStringAppend(&ds1, str_statsize, -1);
223
224 CU_TEST(Asc_DStringLength(&ds1) == (int) strlen(str_statcat1));
225 CU_TEST(strlen(str_statcat1) == strlen(Asc_DStringValue(&ds1)));
226 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), str_statcat1);
227
228 Asc_DStringFree(&ds1); /* free the string */
229
230 CU_TEST(Asc_DStringLength(&ds1) == 0);
231 CU_TEST(0 == strlen(Asc_DStringValue(&ds1)));
232 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), NULL_STRING);
233
234 Asc_DStringAppend(&ds1, str_statsize, -1); /* string near static size */
235 Asc_DStringAppend(&ds1, str_statsize_p1, -1);
236
237 CU_TEST(Asc_DStringLength(&ds1) == (int)strlen(str_statcat2));
238 CU_TEST(strlen(str_statcat2) == strlen(Asc_DStringValue(&ds1)));
239 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), str_statcat2);
240
241 Asc_DStringFree(&ds1); /* free the string */
242
243 CU_TEST(Asc_DStringLength(&ds1) == 0);
244 CU_TEST(0 == strlen(Asc_DStringValue(&ds1)));
245 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), NULL_STRING);
246
247 Asc_DStringAppend(&ds1, "string 1", -1); /* append string to itself */
248 Asc_DStringAppend(&ds1, Asc_DStringValue(&ds1), -1);
249
250 CU_TEST(Asc_DStringLength(&ds1) == (int)strlen("string 1string 1"));
251 CU_TEST(strlen("string 1string 1") == strlen(Asc_DStringValue(&ds1)));
252 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), "string 1string 1");
253
254 /* test Asc_DStringTrunc */
255
256 #ifndef ASC_NO_ASSERTIONS
257 asc_assert_catch(TRUE); /* prepare to test assertions */
258
259 asc_assert_reset();
260 if (0 == setjmp(g_asc_test_env))
261 Asc_DStringTrunc(NULL, 1); /* error if dstring* is NULL */
262 CU_TEST(TRUE == asc_assert_failed());
263
264 asc_assert_catch(FALSE); /* done testing assertions */
265 #endif /* !ASC_NO_ASSERTIONS */
266
267 Asc_DStringFree(&ds1);
268 Asc_DStringTrunc(&ds1, 0); /* empty dstring, length = 0 */
269
270 CU_TEST(Asc_DStringLength(&ds1) == 0);
271 CU_TEST(0 == strlen(Asc_DStringValue(&ds1)));
272 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), NULL_STRING);
273
274 Asc_DStringFree(&ds1);
275 Asc_DStringTrunc(&ds1, 10); /* empty dstring, length > 0 */
276
277 CU_TEST(Asc_DStringLength(&ds1) == 0);
278 CU_TEST(0 == strlen(Asc_DStringValue(&ds1)));
279 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), NULL_STRING);
280
281 Asc_DStringFree(&ds1);
282 Asc_DStringTrunc(&ds1, -1); /* empty dstring, length < 0 */
283
284 CU_TEST(Asc_DStringLength(&ds1) == 0);
285 CU_TEST(0 == strlen(Asc_DStringValue(&ds1)));
286 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), NULL_STRING);
287
288 Asc_DStringSet(&ds1, str1);
289 Asc_DStringTrunc(&ds1, 0); /* length = 0 */
290
291 CU_TEST(Asc_DStringLength(&ds1) == 0);
292 CU_TEST(0 == strlen(Asc_DStringValue(&ds1)));
293 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), NULL_STRING);
294
295 Asc_DStringSet(&ds1, str1);
296 Asc_DStringTrunc(&ds1, 10); /* length > 0 */
297
298 CU_TEST(Asc_DStringLength(&ds1) == 10);
299 CU_TEST(10 == strlen(Asc_DStringValue(&ds1)));
300 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), "This strin");
301
302 Asc_DStringSet(&ds1, str1);
303 Asc_DStringTrunc(&ds1, strlen(str1) + 10); /* length > strlen() */
304
305 CU_TEST(Asc_DStringLength(&ds1) == (int)strlen(str1));
306 CU_TEST(strlen(str1) == strlen(Asc_DStringValue(&ds1)));
307 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), str1);
308
309 Asc_DStringSet(&ds1, str1);
310 Asc_DStringTrunc(&ds1, -1); /* length < 0 */
311
312 CU_TEST(Asc_DStringLength(&ds1) == 0);
313 CU_TEST(0 == strlen(Asc_DStringValue(&ds1)));
314 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), NULL_STRING);
315
316 /* test Asc_DStringResult */
317
318 #ifndef ASC_NO_ASSERTIONS
319 asc_assert_catch(TRUE); /* prepare to test assertions */
320
321 asc_assert_reset();
322 if (0 == setjmp(g_asc_test_env))
323 Asc_DStringResult(NULL); /* error if dstring* is NULL */
324 CU_TEST(TRUE == asc_assert_failed());
325
326 asc_assert_catch(FALSE); /* done testing assertions */
327 #endif /* !ASC_NO_ASSERTIONS */
328
329 Asc_DStringFree(&ds1);
330 my_str = Asc_DStringResult(&ds1); /* empty string */
331
332 CU_TEST(NULL != my_str);
333 CU_TEST(0 == strlen(my_str));
334 CU_ASSERT_STRING_EQUAL(my_str, NULL_STRING);
335
336 CU_TEST(Asc_DStringLength(&ds1) == 0);
337 CU_TEST(0 == strlen(Asc_DStringValue(&ds1)));
338 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), NULL_STRING);
339
340 ascfree(my_str);
341
342 Asc_DStringSet(&ds1, str_statsize_m1);
343 my_str = Asc_DStringResult(&ds1); /* string near static size */
344
345 CU_TEST(NULL != my_str);
346 CU_TEST(strlen(my_str) == strlen(str_statsize_m1));
347 CU_ASSERT_STRING_EQUAL(my_str, str_statsize_m1);
348
349 CU_TEST(Asc_DStringLength(&ds1) == 0);
350 CU_TEST(0 == strlen(Asc_DStringValue(&ds1)));
351 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), NULL_STRING);
352
353 ascfree(my_str);
354
355 Asc_DStringSet(&ds1, str_statsize);
356 my_str = Asc_DStringResult(&ds1); /* string at static size */
357
358 CU_TEST(NULL != my_str);
359 CU_TEST(strlen(my_str) == strlen(str_statsize));
360 CU_ASSERT_STRING_EQUAL(my_str, str_statsize);
361
362 CU_TEST(Asc_DStringLength(&ds1) == 0);
363 CU_TEST(0 == strlen(Asc_DStringValue(&ds1)));
364 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), NULL_STRING);
365
366 ascfree(my_str);
367
368 Asc_DStringSet(&ds1, str_statsize_p1);
369 my_str = Asc_DStringResult(&ds1); /* string above static size */
370
371 CU_TEST(NULL != my_str);
372 CU_TEST(strlen(my_str) == strlen(str_statsize_p1));
373 CU_ASSERT_STRING_EQUAL(my_str, str_statsize_p1);
374
375 CU_TEST(Asc_DStringLength(&ds1) == 0);
376 CU_TEST(0 == strlen(Asc_DStringValue(&ds1)));
377 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), NULL_STRING);
378
379 ascfree(my_str);
380
381 /* free the dynamic strings */
382 Asc_DStringFree(&ds1);
383
384 CU_TEST(Asc_DStringLength(&ds1) == 0);
385 CU_TEST(0 == strlen(Asc_DStringValue(&ds1)));
386 CU_ASSERT_STRING_EQUAL(Asc_DStringValue(&ds1), NULL_STRING);
387
388 CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */
389 }
390
391 /*===========================================================================*/
392 /* Registration information */
393
394 static CU_TestInfo dstring_test_list[] = {
395 {"test_dstring", test_dstring},
396 CU_TEST_INFO_NULL
397 };
398
399 static CU_SuiteInfo suites[] = {
400 {"test_general_dstring", NULL, NULL, dstring_test_list},
401 CU_SUITE_INFO_NULL
402 };
403
404 /*-------------------------------------------------------------------*/
405 CU_ErrorCode test_register_general_dstring(void)
406 {
407 return CU_register_suites(suites);
408 }

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