3 * CUnit - A Unit testing framework library for C.
4 * Copyright (C) 2001 Anil Kumar
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "stringbuf_test.h"
26 #include "../lib/libcitadel.h"
29 static HashList *GetFilledHash(int n, int stepwidth)
35 TestHash = NewHash(1, Flathash);
37 for (i = 0; i < n; i+=stepwidth)
39 val = (int*) malloc(sizeof(int));
41 Put(TestHash, IKEY(i), val, NULL);
48 static void test_iterate_hash(HashList *testh, int forward, int stepwidth)
59 it = GetNewHashPos(testh, dir * stepwidth);
60 while (GetNextHashPos(testh, it, &len, &Key, &vTest) &&
63 printf("i: %d c: %d\n", i, *(int*) vTest);
69 static void ValidateBackAndForth(HashList *H)
71 test_iterate_hash(H, 1, 1);
74 test_iterate_hash(H, 0, 1);
77 test_iterate_hash(H, 1, 2);
80 test_iterate_hash(H, 0, 2);
83 test_iterate_hash(H, 1, 3);
86 test_iterate_hash(H, 0, 3);
90 static void TestHashlistIteratorForward (void)
94 H = GetFilledHash (10, 1);
96 ValidateBackAndForth(H);
102 static void TestHashlistAddDelete (void)
108 H = GetFilledHash (10, 1);
110 at = GetNewHashPos(H, 0);
112 printf("Remove first\n");
113 DeleteEntryFromHash(H, at);
115 ValidateBackAndForth(H);
117 printf("Insert 15\n");
119 val = (int*) malloc(sizeof(int));
121 Put(H, IKEY(i), val, NULL);
123 ValidateBackAndForth(H);
125 printf("Remove third\n");
126 at = GetNewHashPos(H, 0);
130 DeleteEntryFromHash(H, at);
132 ValidateBackAndForth(H);
133 printf("Insert -15\n");
136 val = (int*) malloc(sizeof(int));
138 Put(H, IKEY(i), val, NULL);
140 ValidateBackAndForth(H);
147 Some samples from the original...
148 CU_ASSERT_EQUAL(10, 10);
149 CU_ASSERT_EQUAL(0, -0);
150 CU_ASSERT_EQUAL(-12, -12);
151 CU_ASSERT_NOT_EQUAL(10, 11);
152 CU_ASSERT_NOT_EQUAL(0, -1);
153 CU_ASSERT_NOT_EQUAL(-12, -11);
154 CU_ASSERT_PTR_EQUAL((void*)0x100, (void*)0x100);
155 CU_ASSERT_PTR_NOT_EQUAL((void*)0x100, (void*)0x101);
156 CU_ASSERT_PTR_NULL(NULL);
157 CU_ASSERT_PTR_NULL(0x0);
158 CU_ASSERT_PTR_NOT_NULL((void*)0x23);
159 CU_ASSERT_STRING_EQUAL(str1, str2);
160 CU_ASSERT_STRING_NOT_EQUAL(str1, str2);
161 CU_ASSERT_NSTRING_EQUAL(str1, str2, strlen(str1));
162 CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1));
163 CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1) + 1);
164 CU_ASSERT_NSTRING_NOT_EQUAL(str1, str2, 3);
165 CU_ASSERT_NSTRING_NOT_EQUAL(str1, str3, strlen(str1) + 1);
166 CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, 0.0001);
167 CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, -0.0001);
168 CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, 0.0001);
169 CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, -0.0001);
170 CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, 0.0001);
171 CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, -0.0001);
172 CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, 0.0001);
173 CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, -0.0001);
178 const char *MSetStrings[] = {
188 const long MessageNumbers[5][2][10] = {
191 {11, 59, 63, 0, 0, 0, 0, 0, 0, 0}, /* In */
192 {2, 10, 64, 65, 0, 0, 0, 0, 0, 0} /* NotIn */
195 {65, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* In */
196 {1, 64, 66, 0, 0, 0, 0, 0, 0, 0} /* NotIn */
200 {1, 65, 77, 0, 0, 0, 0, 0, 0, 0}, /* In */
201 {2, 64, 66, 76, 78, 0, 0, 0, 0, 0} /* NotIn */
204 {1, 2, 30, 64, 65, 77, 80, 81, 222222, 0}, /* In */
205 {66, 76, 78, 79, 0, 0, 0, 0, 0, 0} /* NotIn */
208 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* In */
209 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} /* NotIn */
215 static void TestMSetHashlist (void)
224 MSetStr = NewStrBuf();
225 Assert = NewStrBuf();
226 while (MSetStrings[nTest] != NULL)
228 StrBufPlain(MSetStr, MSetStrings[nTest], -1);
229 ParseMSet(&OneMSet, MSetStr);
231 //#ifdef VERBOSE_TEST
232 printf("---%s---\n", ChrPtr(MSetStr));
236 HashList *ScanMe = (HashList*) OneMSet;
241 at = GetNewHashPos(ScanMe, 0);
242 while (GetNextHashPos(ScanMe, at, &HKLen, &HashKey, &vMsg)) {
243 /* Are you a new message, or an old message? */
245 printf("[%ld][%ld]\n", *(long*)HashKey, *end);
252 while (MessageNumbers[nTest][InMSet][j] != 0)
254 if (!IsInMSetList(OneMSet, MessageNumbers[nTest][InMSet][j]))
256 StrBufPrintf(Assert, "InFail: %s <-> %ld\n",
258 MessageNumbers[nTest][InMSet][j]);
259 CU_FAIL(ChrPtr(Assert));
260 printf(ChrPtr(Assert));
264 StrBufPrintf(Assert, "InPass: %s <-> %ld\n",
266 MessageNumbers[nTest][InMSet][j]);
267 CU_PASS(ChrPtr(Assert));
272 while (MessageNumbers[nTest][NotInMSet][j] != 0)
274 if (IsInMSetList(OneMSet, MessageNumbers[nTest][NotInMSet][j]))
276 StrBufPrintf(Assert, "NOT-InFail: %s <-> %ld\n",
278 MessageNumbers[nTest][NotInMSet][j]);
279 CU_FAIL(ChrPtr(Assert));
280 printf(ChrPtr(Assert));
284 StrBufPrintf(Assert, "NOT-InPass: %s <-> %ld\n",
286 MessageNumbers[nTest][InMSet][j]);
287 CU_PASS(ChrPtr(Assert));
293 DeleteMSet(&OneMSet);
297 FreeStrBuf(&MSetStr);
303 static void AddHashlistTests(void)
305 CU_pSuite pGroup = NULL;
306 CU_pTest pTest = NULL;
308 pGroup = CU_add_suite("TestStringBufSimpleAppenders", NULL, NULL);
309 pTest = CU_add_test(pGroup, "TestHashListIteratorForward", TestHashlistIteratorForward);
310 pTest = CU_add_test(pGroup, "TestHashlistAddDelete", TestHashlistAddDelete);
311 pTest = CU_add_test(pGroup, "TestMSetHashlist", TestMSetHashlist);
315 int main(int argc, char* argv[])
317 setvbuf(stdout, NULL, _IONBF, 0);
320 CU_BOOL Run = CU_FALSE ;
322 CU_set_output_filename("TestAutomated");
323 if (CU_initialize_registry()) {
324 printf("\nInitialize of test Registry failed.");
330 if (CU_TRUE == Run) {
331 //CU_console_run_tests();
332 printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
334 ///CU_automated_run_tests();
337 CU_cleanup_registry();