You're here: Snippet Directory » C/C++ (495)
Language:

Linked List - Test

Language: English
Programming Language: C
Published by: gaiper [not registered]
Last Update: 5/15/2006
Views: 127


Description

A set of tests to verify that the Linked List operations work correctly. This requires you to use the "check" libary which is downloadable from check.sourceforge.net

Code

1 Please see the Linked List and Linked List - Implementation snippets to 2 see what is being tested. This requires you to install the "check" library. 3 See check.sourceforge.net for more info about the check testing library. 4 Note that this file depends on the List.c and List.h defined in the other 5 snipppets. 6 7 There are two files defined below: ListTest.c and RunSuite.c, note that you 8 will need to have the check.h header file in your include path (-I with gcc), 9 and you will need to link with the check library. You can also simply look 10 at the ListTest.c to simply see how to use the Linked List. 11 12 13 ----------------------------------------- ListTest.c 14 /* Author: Brian Maher <maherb@cc.wwu.edu> 15 * 16 * License: LGPL - Lesser General Public License 17 * See: http://www.gnu.org/copyleft/lesser.html 18 * Quick License: 19 * -Don't claim that you created this code, please give me credit. 20 * -Any good ideas for improvement/fixes/etc, send to the above 21 * author with modifications to source code. 22 * 23 * $Revision$ 24 * $Source$ 25 * 26 */ 27 #include <check.h> 28 #include <stdio.h> 29 #include <assert.h> 30 #include "List.h" 31 32 static void Test_splice_outMiddle(char* str, int size, int offset, int length); 33 static void Test_splice_insertMiddle(char* orig_str, int orig_size, 34 char* mid_str, int mid_size, 35 int offset, int length); 36 37 static int destroyCallbackCount = 0; 38 void static destroyCallback(char* item) { 39 destroyCallbackCount++; 40 } 41 42 START_TEST(test_create) 43 { 44 List list = NULL; 45 List_new(&list); 46 assert(list != NULL); 47 destroyCallbackCount = 0; 48 List_destroy(list,(ListFreeFunc*)destroyCallback); 49 list = NULL; 50 assert( destroyCallbackCount == 0 ); 51 } 52 END_TEST 53 54 START_TEST(test_length) 55 { 56 char a = 'A'; 57 List list = NULL; 58 List_new(&list); 59 List_push(list,&a); 60 List_push(list,&a); 61 if(List_length(list) != 2) { 62 fail("In correct length"); 63 } 64 destroyCallbackCount = 0; 65 List_destroy(list,(ListFreeFunc*)destroyCallback); 66 list = NULL; 67 assert( destroyCallbackCount == 2 ); 68 } 69 END_TEST 70 71 START_TEST(test_push) 72 { 73 char str[] = "ABCDEFGHIJKLMNO"; 74 int size = sizeof(str)-1; 75 int i; 76 int length; 77 List list = NULL; 78 List_new(&list); 79 80 for(i=0;i < size;i++) { 81 length = List_push(list,&(str[i])); 82 if(length != i+1) { 83 fail("Bad push"); 84 } 85 } 86 for(i=size-1;i >= 0;i--) { 87 char* cur = (char*) List_pop(list); 88 if(*cur != str[i]) { 89 fail("Bad Pop"); 90 /*fprintf(stderr,"Bad Pop: %c Should be: %c\n", *cur, str[i]);*/ 91 } 92 length = List_length(list); 93 if(length != i) { 94 fail("Bad Length"); 95 /*fprintf(stderr,"Incorrect length: %d Should be: %d\n",length,i);*/ 96 } 97 } 98 List_destroy(list,NULL); 99 list = NULL; 100 } 101 END_TEST 102 103 START_TEST(test_unshift) 104 { 105 char str[] = "ABCDEFG"; 106 int size = sizeof(str)-1; 107 int i; 108 int length; 109 List list = NULL; 110 List_new(&list); 111 112 for(i=0;i < size;i++) { 113 length = List_unshift(list,&(str[i])); 114 if(length != i+1) { 115 fail("Bad push"); 116 } 117 } 118 for(i=size-1;i >= 0;i--) { 119 char* cur = (char*) List_shift(list); 120 if(*cur != str[i]) { 121 fail("Bad Shift"); 122 /*fprintf(stderr,"Bad Shift: %c Should be: %c [%d]\n", *cur, str[i],i);*/ 123 } 124 length = List_length(list); 125 if(length != i) { 126 fail("Bad Length aftershift"); 127 /*fprintf(stderr,"Incorrect length: %d Should be: %d\n",length,i);*/ 128 } 129 } 130 List_destroy(list,NULL); 131 list = NULL; 132 } 133 END_TEST 134 135 START_TEST(test_at) 136 { 137 char str[] = "ABCDEF"; 138 int size = sizeof(str)-1; 139 int i; 140 List list = NULL; 141 List_new(&list); 142 143 for(i=0;i < size;i++) { 144 List_push(list,&(str[i])); 145 } 146 for(i=0;i < size;i++) { 147 char* cur = (char*) List_at(list,i); 148 int length; 149 if(*cur != str[i]) { 150 /*fail("Bad At");*/ 151 fprintf(stderr,"Bad At: %c Should be: %c\n", *cur, str[i]); 152 } 153 length = List_length(list); 154 if(length != size) { 155 fail("Bad Length"); 156 } 157 } 158 List_destroy(list,NULL); 159 list = NULL; 160 } 161 END_TEST 162 163 START_TEST(test_iterate_forward) 164 { 165 char str[] = "ABCDEF"; 166 int size = sizeof(str)-1; 167 int i; 168 List list = NULL; 169 ListIterator it; 170 List_new(&list); 171 172 for(i=0;i < size;i++) { 173 List_push(list,&(str[i])); 174 } 175 it = List_front(list); 176 for(i=0;ListIterator_valid(it);ListIterator_next(&it), i++) { 177 char* cur = (char*) ListIterator_item(it); 178 assert(i < size); 179 if(*cur != str[i]) { 180 /*fprintf(stderr,"Bad Iterator: %c Should be: %c [%d]\n", *cur, str[i],i);*/ 181 fail("Bad Iterator"); 182 } 183 } 184 assert( ListIterator_item(it) == ListItemInvalid ); 185 assert( i == size ); 186 187 List_destroy(list,NULL); 188 list = NULL; 189 } 190 END_TEST 191 192 START_TEST(test_iterate_backward) 193 { 194 char str[] = "ABCDEF"; 195 int size = sizeof(str)-1; 196 int i; 197 List list = NULL; 198 ListIterator it; 199 List_new(&list); 200 201 for(i=0;i < size;i++) { 202 List_push(list,&(str[i])); 203 } 204 it = List_back(list); 205 for(i=size-1;ListIterator_valid(it);ListIterator_prev(&it), i--) { 206 char* cur = (char*) ListIterator_item(it); 207 assert(i < size); 208 if(*cur != str[i]) { 209 /*fprintf(stderr,"Bad Iterator: %c Should be: %c [%d]\n", *cur, str[i],i);*/ 210 fail("Bad Iterator"); 211 } 212 } 213 assert( ListIterator_item(it) == ListItemInvalid ); 214 assert( i == 0 ); 215 216 List_destroy(list,NULL); 217 list = NULL; 218 } 219 END_TEST 220 221 222 START_TEST(test_splice_out_middle) 223 { 224 /* Test extracting every possible "substring" of ABCDEFGHI */ 225 int i, count; 226 char str[] = "ABCDEFGHI"; 227 228 #ifdef _DEBUG 229 fprintf(stderr,"\ntest_splice_out_middle\n"); 230 #endif 231 for(i=0;i<sizeof(str)-1;i++) { 232 int j; 233 for(j=0; j <= i; j++) { 234 int k; 235 for(k=0; k <= (i-j); k++) { 236 count++; 237 Test_splice_outMiddle(str,i,j,k); 238 } 239 } 240 } 241 fprintf(stderr,"List_splice called: %d times\n", count); 242 } 243 END_TEST 244 245 START_TEST(test_splice_in_middle) 246 { 247 /* Test extracting every possible "substring" of ABCDEFGHI */ 248 int str1_len; 249 char str1[] = "ABCDEFG"; 250 char str2[] = "TUVWXYZ"; 251 int count = 0; 252 253 #ifdef _DEBUG 254 fprintf(stderr,"\ntest_splice_in_middle\n"); 255 #endif /* _DEBUG */ 256 for(str1_len=0;str1_len < sizeof(str1)-1;str1_len++) { 257 int offset; 258 for(offset=0; offset <= str1_len; offset++) { 259 int length; 260 for(length=0; length <= (str1_len-offset); length++) { 261 int str2_len; 262 for(str2_len=0; str2_len < sizeof(str2)-1;str2_len++) { 263 count ++; 264 Test_splice_insertMiddle(str1, str1_len, 265 str2, str2_len, 266 offset, length); 267 } 268 } 269 } 270 } 271 fprintf(stderr,"List_splice called: %d times\n", count); 272 } 273 END_TEST 274 275 /* Replaces orig_str with mid_str */ 276 void Test_splice_insertMiddle(char* orig_str, int orig_size, 277 char* mid_str, int mid_size, 278 int offset, int length) { 279 int i; 280 List orig_list = NULL; 281 List mid_list = NULL; 282 List ret_list = NULL; 283 ListIterator it; 284 285 #ifdef _DEBUG 286 fprintf(stderr,"splice_insertMiddle( orig:%d mid:%d offset:%d length:%d )\n", 287 orig_size, mid_size, offset, length); 288 #endif 289 290 /* create a orig_list and mid_list: */ 291 List_new(&orig_list); 292 for(i=0;i < orig_size;i++) { 293 List_push(orig_list,&(orig_str[i])); 294 } 295 296 List_new(&mid_list); 297 for(i=0;i < mid_size;i++) { 298 List_push(mid_list, &(mid_str[i])); 299 } 300 301 /* Do the splice/insert from of the middle */ 302 ret_list = List_splice( orig_list, offset, length, 303 mid_list, NULL ); 304 305 /* verify that orig_list is correct */ 306 for(i = 0, it = List_front(orig_list); 307 ListIterator_valid(it); 308 ListIterator_next(&it),i++) { 309 310 char cur; 311 if( i < offset ) { 312 cur = orig_str[i]; 313 } else if( i >= offset+mid_size ) { 314 cur = orig_str[ i-(mid_size-length) ]; 315 } else { 316 cur = mid_str[ i - offset ]; 317 } 318 assert( (*(char*)ListIterator_item(it)) == cur ); 319 } 320 assert( i == ((orig_size - length) + mid_size) ); 321 322 /* verify that ret_list is correct */ 323 for(i = 0, it = List_front(ret_list); 324 ListIterator_valid(it); 325 ListIterator_next(&it),i++) { 326 327 char cur = orig_str[i+offset]; 328 assert( (*(char*)ListIterator_item(it)) == cur ); 329 } 330 assert( i == length ); 331 332 List_destroy( ret_list, NULL ); 333 ret_list = NULL; 334 List_destroy( orig_list, NULL ); 335 orig_list = NULL; 336 List_destroy( mid_list, NULL ); 337 mid_list = NULL; 338 339 } 340 341 /* Only removes a "substring" of an array */ 342 void Test_splice_outMiddle(char* str, int size, int offset, int length) { 343 int i; 344 List list1 = NULL; 345 List list2 = NULL; 346 ListIterator it; 347 348 #ifdef _DEBUG 349 fprintf(stderr,"splice_outMiddle( size:%d offset:%d length:%d )\n", 350 size, offset, length); 351 #endif 352 353 /* create a new list: */ 354 List_new(&list1); 355 for(i=0;i < size;i++) { 356 List_push(list1,&(str[i])); 357 } 358 359 /* Do the splice out of the middle */ 360 list2 = List_splice( list1, offset, length, 361 NULL, NULL ); 362 363 /* verify that list1 is correct */ 364 for(i = 0, it = List_front(list1); 365 ListIterator_valid(it); 366 ListIterator_next(&it),i++) { 367 368 int index = i; 369 if( i >= offset ) { 370 index += length; 371 } 372 assert( (*(char*)ListIterator_item(it)) == str[index] ); 373 } 374 assert( i == (size - length) ); 375 376 /* verify that list2 is correct */ 377 for(i = 0, it = List_front(list2); 378 ListIterator_valid(it); 379 ListIterator_next(&it),i++) { 380 381 int index = offset + i; 382 assert( (*(char*)ListIterator_item(it)) == str[index] ); 383 } 384 assert( i == length ); 385 386 List_destroy( list1, NULL ); 387 list1 = NULL; 388 List_destroy( list2, NULL ); 389 list2 = NULL; 390 } 391 392 ------------------------------------------------------- RunSuite.c 393 /* Author: Brian Maher <maherb@cc.wwu.edu> 394 * 395 * License: LGPL - Lesser General Public License 396 * See: http://www.gnu.org/copyleft/lesser.html 397 * Quick License: 398 * -Don't claim that you created this code, please give me credit. 399 * -Any good ideas for improvement/fixes/etc, send to the above 400 * author with modifications to source code. 401 * 402 * $Revision$ 403 * $Source$ 404 * 405 */ 406 #include <stdio.h> 407 #include "ListTest.c" 408 409 Suite *ds_suite (void) 410 { 411 Suite *s = suite_create("List-"); 412 TCase *tc_core = tcase_create("Core-"); 413 414 suite_add_tcase (s, tc_core); 415 416 tcase_add_test (tc_core, test_create); 417 tcase_add_test (tc_core, test_length); 418 tcase_add_test (tc_core, test_push); 419 tcase_add_test (tc_core, test_unshift); 420 tcase_add_test (tc_core, test_at); 421 tcase_add_test (tc_core, test_iterate_forward); 422 tcase_add_test (tc_core, test_iterate_backward); 423 tcase_add_test (tc_core, test_splice_out_middle); 424 tcase_add_test (tc_core, test_splice_in_middle); 425 return s; 426 } 427 428 int main (void) 429 { 430 int nf; 431 Suite *s = ds_suite(); 432 SRunner *sr = srunner_create(s); 433 fprintf(stderr,"\n"); 434 #ifdef CK_NORMAL 435 srunner_run_all (sr, CK_NORMAL); 436 #else /* CK_NORMAL */ 437 srunner_run_all (sr, CRNORMAL); 438 #endif /* CK_NORMAL */ 439 440 nf = srunner_ntests_failed(sr); 441 srunner_free(sr); 442 suite_free(s); 443 return (nf == 0) ? 0 : 1; 444 }

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS