2 * CUnit - A Unit testing framework library for C.
3 * Copyright (C) 2001 Anil Kumar
5 * This library is open source software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Library General Public License for more details.
20 #include "stringbuf_test.h"
21 #include "../lib/libcitadel.h"
24 static HashList *GetFilledHash(int n, int stepwidth)
30 TestHash = NewHash(1, Flathash);
32 for (i = 0; i < n; i+=stepwidth)
34 val = (int*) malloc(sizeof(int));
36 Put(TestHash, IKEY(i), val, NULL);
43 static void test_iterate_hash(HashList *testh, int forward, int stepwidth)
54 it = GetNewHashPos(testh, dir * stepwidth);
55 while (GetNextHashPos(testh, it, &len, &Key, &vTest) &&
58 printf("i: %d c: %d\n", i, *(int*) vTest);
64 static void ValidateBackAndForth(HashList *H)
66 test_iterate_hash(H, 1, 1);
69 test_iterate_hash(H, 0, 1);
72 test_iterate_hash(H, 1, 2);
75 test_iterate_hash(H, 0, 2);
78 test_iterate_hash(H, 1, 3);
81 test_iterate_hash(H, 0, 3);
85 static void TestHashlistIteratorForward (void)
89 H = GetFilledHash (10, 1);
91 ValidateBackAndForth(H);
97 static void TestHashlistAddDelete (void)
103 H = GetFilledHash (10, 1);
105 at = GetNewHashPos(H, 0);
107 printf("Remove first\n");
108 DeleteEntryFromHash(H, at);
111 ValidateBackAndForth(H);
113 printf("Insert 15\n");
115 val = (int*) malloc(sizeof(int));
117 Put(H, IKEY(i), val, NULL);
119 ValidateBackAndForth(H);
121 printf("Remove third\n");
122 at = GetNewHashPos(H, 0);
126 DeleteEntryFromHash(H, at);
128 ValidateBackAndForth(H);
129 printf("Insert -15\n");
132 val = (int*) malloc(sizeof(int));
134 Put(H, IKEY(i), val, NULL);
136 ValidateBackAndForth(H);
143 Some samples from the original...
144 CU_ASSERT_EQUAL(10, 10);
145 CU_ASSERT_EQUAL(0, -0);
146 CU_ASSERT_EQUAL(-12, -12);
147 CU_ASSERT_NOT_EQUAL(10, 11);
148 CU_ASSERT_NOT_EQUAL(0, -1);
149 CU_ASSERT_NOT_EQUAL(-12, -11);
150 CU_ASSERT_PTR_EQUAL((void*)0x100, (void*)0x100);
151 CU_ASSERT_PTR_NOT_EQUAL((void*)0x100, (void*)0x101);
152 CU_ASSERT_PTR_NULL(NULL);
153 CU_ASSERT_PTR_NULL(0x0);
154 CU_ASSERT_PTR_NOT_NULL((void*)0x23);
155 CU_ASSERT_STRING_EQUAL(str1, str2);
156 CU_ASSERT_STRING_NOT_EQUAL(str1, str2);
157 CU_ASSERT_NSTRING_EQUAL(str1, str2, strlen(str1));
158 CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1));
159 CU_ASSERT_NSTRING_EQUAL(str1, str1, strlen(str1) + 1);
160 CU_ASSERT_NSTRING_NOT_EQUAL(str1, str2, 3);
161 CU_ASSERT_NSTRING_NOT_EQUAL(str1, str3, strlen(str1) + 1);
162 CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, 0.0001);
163 CU_ASSERT_DOUBLE_EQUAL(10, 10.0001, -0.0001);
164 CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, 0.0001);
165 CU_ASSERT_DOUBLE_EQUAL(-10, -10.0001, -0.0001);
166 CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, 0.0001);
167 CU_ASSERT_DOUBLE_NOT_EQUAL(10, 10.001, -0.0001);
168 CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, 0.0001);
169 CU_ASSERT_DOUBLE_NOT_EQUAL(-10, -10.001, -0.0001);
174 const char *MSetStrings[] = {
184 const long MessageNumbers[5][2][10] = {
187 {11, 59, 63, 0, 0, 0, 0, 0, 0, 0}, /* In */
188 {2, 10, 64, 65, 0, 0, 0, 0, 0, 0} /* NotIn */
191 {65, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* In */
192 {1, 64, 66, 0, 0, 0, 0, 0, 0, 0} /* NotIn */
196 {1, 65, 77, 0, 0, 0, 0, 0, 0, 0}, /* In */
197 {2, 64, 66, 76, 78, 0, 0, 0, 0, 0} /* NotIn */
200 {1, 2, 30, 64, 65, 77, 80, 81, 222222, 0}, /* In */
201 {66, 76, 78, 79, 0, 0, 0, 0, 0, 0} /* NotIn */
204 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* In */
205 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} /* NotIn */
211 static void TestMSetHashlist (void)
220 MSetStr = NewStrBuf();
221 Assert = NewStrBuf();
222 while (MSetStrings[nTest] != NULL)
224 StrBufPlain(MSetStr, MSetStrings[nTest], -1);
225 ParseMSet(&OneMSet, MSetStr);
227 //#ifdef VERBOSE_TEST
228 printf("---%s---\n", ChrPtr(MSetStr));
232 HashList *ScanMe = (HashList*) OneMSet;
237 at = GetNewHashPos(ScanMe, 0);
238 while (GetNextHashPos(ScanMe, at, &HKLen, &HashKey, &vMsg)) {
239 /* Are you a new message, or an old message? */
241 printf("[%ld][%ld]\n", *(long*)HashKey, *end);
248 while (MessageNumbers[nTest][InMSet][j] != 0)
250 if (!IsInMSetList(OneMSet, MessageNumbers[nTest][InMSet][j]))
252 StrBufPrintf(Assert, "InFail: %s <-> %ld\n",
254 MessageNumbers[nTest][InMSet][j]);
255 CU_FAIL(ChrPtr(Assert));
256 printf("%s", ChrPtr(Assert));
260 StrBufPrintf(Assert, "InPass: %s <-> %ld\n",
262 MessageNumbers[nTest][InMSet][j]);
263 CU_PASS(ChrPtr(Assert));
268 while (MessageNumbers[nTest][NotInMSet][j] != 0)
270 if (IsInMSetList(OneMSet, MessageNumbers[nTest][NotInMSet][j]))
272 StrBufPrintf(Assert, "NOT-InFail: %s <-> %ld\n",
274 MessageNumbers[nTest][NotInMSet][j]);
275 CU_FAIL(ChrPtr(Assert));
276 printf("%s", ChrPtr(Assert));
280 StrBufPrintf(Assert, "NOT-InPass: %s <-> %ld\n",
282 MessageNumbers[nTest][InMSet][j]);
283 CU_PASS(ChrPtr(Assert));
289 DeleteMSet(&OneMSet);
293 FreeStrBuf(&MSetStr);
299 static void AddHashlistTests(void)
301 CU_pSuite pGroup = NULL;
302 CU_pTest pTest = NULL;
304 pGroup = CU_add_suite("TestStringBufSimpleAppenders", NULL, NULL);
305 pTest = CU_add_test(pGroup, "TestHashListIteratorForward", TestHashlistIteratorForward);
306 pTest = CU_add_test(pGroup, "TestHashlistAddDelete", TestHashlistAddDelete);
307 pTest = CU_add_test(pGroup, "TestMSetHashlist", TestMSetHashlist);
311 int main(int argc, char* argv[])
313 setvbuf(stdout, NULL, _IONBF, 0);
316 CU_BOOL Run = CU_FALSE ;
318 CU_set_output_filename("TestAutomated");
319 if (CU_initialize_registry()) {
320 printf("\nInitialize of test Registry failed.");
326 if (CU_TRUE == Run) {
327 //CU_console_run_tests();
328 printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
330 ///CU_automated_run_tests();
333 CU_cleanup_registry();