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

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