소스 검색

Add unit test for new *_APPEND_ELEM macros and test changed behaviour of *_PREPEND_ELEM macros for NULL elt pointers

Thilo Schulz 9 년 전
부모
커밋
360d2a968a
4개의 변경된 파일377개의 추가작업 그리고 2개의 파일을 삭제
  1. 1 1
      tests/Makefile
  2. 1 1
      tests/README
  3. 79 0
      tests/test86.ans
  4. 296 0
      tests/test86.c

+ 1 - 1
tests/Makefile

@@ -13,7 +13,7 @@ PROGS = test1 test2 test3 test4 test5 test6 test7 test8 test9   \
         test58 test59 test60 test61 test62 test63 test64 test65 \
         test66 test67 test68 test69 test70 test71 test72 test73 \
         test74 test75 test76 test77 test78 test79 test80 test81 \
-        test82 test83 test84 test85
+        test82 test83 test84 test85 test86
 CFLAGS += -I$(HASHDIR)
 #CFLAGS += -DHASH_BLOOM=16
 #CFLAGS += -O2

+ 1 - 1
tests/README

@@ -87,7 +87,7 @@ test82: test utarray_inserta past end of array
 test83: test HASH_REPLACE_STR with char[] key
 test84: test HASH_REPLACE_STR with char* key
 test85: test HASH_OVERHEAD on null and non null hash
-
+test86: test *_APPEND_ELEM / *_PREPEND_ELEM (Thilo Schulz)
 
 Other Make targets
 ================================================================================

+ 79 - 0
tests/test86.ans

@@ -0,0 +1,79 @@
+CDL appends
+a b c 
+count = 3
+Test CDL_PREPEND_ELEM d with elt NULL
+a b c d 
+Test CDL_PREPEND_ELEM e before item b
+a e b c d 
+Test CDL_APPEND_ELEM f with elt NULL
+f a e b c d 
+Test CDL_APPEND_ELEM g after item b
+f a e b g c d 
+count = 7
+advancing head pointer
+a e b g c d f 
+a e b g c d f a e b g c d f a e b g c d 
+a f d c g b e a f d 
+deleting (b)
+a e g c d f 
+deleting (a)
+e g c d f 
+deleting (c)
+e g d f 
+deleting (g)
+e d f 
+deleting (e)
+d f 
+deleting (d)
+f deleting (f)
+
+DL appends
+a b c 
+count = 3
+Test DL_PREPEND_ELEM d with elt NULL
+a b c d 
+Test DL_PREPEND_ELEM e before item b
+a e b c d 
+Test DL_APPEND_ELEM f with elt NULL
+f a e b c d 
+Test DL_APPEND_ELEM g after item b
+f a e b g c d 
+count = 7
+deleting (b)
+f a e g c d 
+deleting (a)
+f e g c d 
+deleting (c)
+f e g d 
+deleting (g)
+f e d 
+deleting (e)
+f d 
+deleting (d)
+f deleting (f)
+
+LL appends
+a b c 
+count = 3
+Test LL_PREPEND_ELEM d with elt NULL
+a b c d 
+Test LL_PREPEND_ELEM e before item b
+a e b c d 
+Test LL_APPEND_ELEM f with elt NULL
+f a e b c d 
+Test LL_APPEND_ELEM g after item b
+f a e b g c d 
+count = 7
+deleting (b)
+f a e g c d 
+deleting (a)
+f e g c d 
+deleting (c)
+f e g d 
+deleting (g)
+f e d 
+deleting (e)
+f d 
+deleting (d)
+f deleting (f)
+

+ 296 - 0
tests/test86.c

@@ -0,0 +1,296 @@
+#include <stdio.h>
+#include "utlist.h"
+
+typedef struct el {
+    int id;
+    struct el *next, *prev;
+} el;
+
+int main(int argc, char *argv[])
+{
+    int i;
+    int count;
+    el els[10], *e;
+    el *head = NULL;
+    el *nullptr = NULL;
+    for(i=0; i<10; i++) {
+        els[i].id=(int)'a'+i;
+    }
+
+    /* test CDL macros */
+    printf("CDL appends\n");
+    CDL_APPEND(head,&els[0]);
+    CDL_APPEND(head,&els[1]);
+    CDL_APPEND(head,&els[2]);
+    CDL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+    CDL_COUNT(head,e, count);
+    printf("count = %d\n", count);
+
+    printf("Test CDL_PREPEND_ELEM %c with elt NULL\n", els[3].id);
+    CDL_PREPEND_ELEM(head, nullptr, &els[3]);
+    CDL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+
+    printf("Test CDL_PREPEND_ELEM %c before item %c\n", els[4].id, els[1].id);
+    CDL_PREPEND_ELEM(head, &els[1], &els[4]);
+    CDL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+
+    printf("Test CDL_APPEND_ELEM %c with elt NULL\n", els[5].id);
+    CDL_APPEND_ELEM(head, nullptr, &els[5]);
+    CDL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+
+    printf("Test CDL_APPEND_ELEM %c after item %c\n", els[6].id, els[1].id);
+    CDL_APPEND_ELEM(head, &els[1], &els[6]);
+    CDL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+    CDL_COUNT(head,e, count);
+    printf("count = %d\n", count);
+
+    /* point head to head->next */
+    printf("advancing head pointer\n");
+    head = head->next;
+    CDL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+
+    /* follow circular loop a few times */
+    for(i=0,e=head; e && i<20; i++,e=e->next) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+
+    /* follow circular loop backwards a few times */
+    for(i=0,e=head; e && i<10; i++,e=e->prev) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+
+    printf("deleting (b)\n");
+    CDL_DELETE(head,&els[1]);
+    CDL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+    printf("deleting (a)\n");
+    CDL_DELETE(head,&els[0]);
+    CDL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+    printf("deleting (c)\n");
+    CDL_DELETE(head,&els[2]);
+    CDL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+    printf("deleting (g)\n");
+    CDL_DELETE(head,&els[6]);
+    CDL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+    printf("deleting (e)\n");
+    CDL_DELETE(head,&els[4]);
+    CDL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+    printf("deleting (d)\n");
+    CDL_DELETE(head,&els[3]);
+    CDL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("deleting (f)\n");
+    CDL_DELETE(head,&els[5]);
+    CDL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+
+    /* test DL macros */
+    printf("DL appends\n");
+
+    DL_APPEND(head,&els[0]);
+    DL_APPEND(head,&els[1]);
+    DL_APPEND(head,&els[2]);
+    DL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+    DL_COUNT(head,e, count);
+    printf("count = %d\n", count);
+
+    printf("Test DL_PREPEND_ELEM %c with elt NULL\n", els[3].id);
+    DL_PREPEND_ELEM(head, nullptr, &els[3]);
+    DL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+
+    printf("Test DL_PREPEND_ELEM %c before item %c\n", els[4].id, els[1].id);
+    DL_PREPEND_ELEM(head, &els[1], &els[4]);
+    DL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+
+    printf("Test DL_APPEND_ELEM %c with elt NULL\n", els[5].id);
+    DL_APPEND_ELEM(head, nullptr, &els[5]);
+    DL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+
+    printf("Test DL_APPEND_ELEM %c after item %c\n", els[6].id, els[1].id);
+    DL_APPEND_ELEM(head, &els[1], &els[6]);
+    DL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+    DL_COUNT(head,e, count);
+    printf("count = %d\n", count);
+
+    printf("deleting (b)\n");
+    DL_DELETE(head,&els[1]);
+    DL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+    printf("deleting (a)\n");
+    DL_DELETE(head,&els[0]);
+    DL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+    printf("deleting (c)\n");
+    DL_DELETE(head,&els[2]);
+    DL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+    printf("deleting (g)\n");
+    DL_DELETE(head,&els[6]);
+    DL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+    printf("deleting (e)\n");
+    DL_DELETE(head,&els[4]);
+    DL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+    printf("deleting (d)\n");
+    DL_DELETE(head,&els[3]);
+    DL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("deleting (f)\n");
+    DL_DELETE(head,&els[5]);
+    DL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+
+
+    /* test LL macros */
+    printf("LL appends\n");
+
+    LL_APPEND(head,&els[0]);
+    LL_APPEND(head,&els[1]);
+    LL_APPEND(head,&els[2]);
+    LL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+    LL_COUNT(head,e, count);
+    printf("count = %d\n", count);
+
+    printf("Test LL_PREPEND_ELEM %c with elt NULL\n", els[3].id);
+    LL_PREPEND_ELEM(head, nullptr, &els[3]);
+    LL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+
+    printf("Test LL_PREPEND_ELEM %c before item %c\n", els[4].id, els[1].id);
+    LL_PREPEND_ELEM(head, &els[1], &els[4]);
+    LL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+
+    printf("Test LL_APPEND_ELEM %c with elt NULL\n", els[5].id);
+    LL_APPEND_ELEM(head, nullptr, &els[5]);
+    LL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+
+    printf("Test LL_APPEND_ELEM %c after item %c\n", els[6].id, els[1].id);
+    LL_APPEND_ELEM(head, &els[1], &els[6]);
+    LL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+    LL_COUNT(head,e, count);
+    printf("count = %d\n", count);
+
+    printf("deleting (b)\n");
+    LL_DELETE(head,&els[1]);
+    LL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+    printf("deleting (a)\n");
+    LL_DELETE(head,&els[0]);
+    LL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+    printf("deleting (c)\n");
+    LL_DELETE(head,&els[2]);
+    LL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+    printf("deleting (g)\n");
+    LL_DELETE(head,&els[6]);
+    LL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+    printf("deleting (e)\n");
+    LL_DELETE(head,&els[4]);
+    LL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+    printf("deleting (d)\n");
+    LL_DELETE(head,&els[3]);
+    LL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("deleting (f)\n");
+    LL_DELETE(head,&els[5]);
+    LL_FOREACH(head,e) {
+        printf("%c ", e->id);
+    }
+    printf("\n");
+
+    return 0;
+}