Skip to content

Commit

Permalink
Add a regression test for troydhanson#241.
Browse files Browse the repository at this point in the history
  • Loading branch information
Quuxplusone committed Jul 8, 2022
1 parent 6b54245 commit 0c60fb6
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ PROGS = test1 test2 test3 test4 test5 test6 test7 test8 test9 \
test66 test67 test68 test69 test70 test71 test72 test73 \
test74 test75 test76 test77 test78 test79 test80 test81 \
test82 test83 test84 test85 test86 test87 test88 test89 \
test90 test91 test92 test93 test94 test95 test96
test90 test91 test92 test93 test94 test95 test96 test97
CFLAGS += -I$(HASHDIR)
#CFLAGS += -DHASH_BLOOM=16
#CFLAGS += -O2
Expand Down
1 change: 1 addition & 0 deletions tests/README
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ test93: alt_fatal
test94: utlist with fields named other than 'next' and 'prev'
test95: utstack
test96: HASH_FUNCTION + HASH_KEYCMP
test97: DL_SORT static-analysis false positive (#241)

Other Make targets
================================================================================
Expand Down
1 change: 0 additions & 1 deletion tests/test32.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ int main()
strcpy(name->bname, linebuf);
DL_PREPEND(head, name);
}
/* DL_SORT(head, namecmp); */
DL_FOREACH(head,tmp) {
printf("%s", tmp->bname);
}
Expand Down
1 change: 0 additions & 1 deletion tests/test34.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ int main()
strcpy(name->bname, linebuf);
CDL_PREPEND(head, name);
}
/* CDL_SORT(head, namecmp); */
CDL_FOREACH(head,tmp) {
printf("%s", tmp->bname);
}
Expand Down
Empty file added tests/test97.ans
Empty file.
54 changes: 54 additions & 0 deletions tests/test97.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "utlist.h"
#include <assert.h>
#include <stdlib.h>

struct item {
int x;
struct item *prev;
struct item *next;
};

static int sort_cmp(void *a, void *b)
{
struct item *item1 = (struct item *)a;
struct item *item2 = (struct item *)b;
return item1->x - item2->x;
}

void sort_them(struct item **items)
{
DL_SORT(*items, sort_cmp);
}

int main()
{
struct item *list = NULL;
struct item a = { 2, NULL, NULL };
struct item b = { 1, NULL, NULL };
sort_them(&list);
assert(list == NULL);

DL_APPEND(list, &a);
assert(list == &a);
assert(a.prev == &a);
assert(a.next == NULL);
sort_them(&list);
assert(list == &a);
assert(a.prev == &a);
assert(a.next == NULL);

DL_APPEND(list, &b);
assert(list == &a);
assert(a.prev == &b);
assert(a.next == &b);
assert(b.prev == &a);
assert(b.next == NULL);
sort_them(&list);
assert(list == &b);
assert(b.prev == &a);
assert(b.next == &a);
assert(a.prev == &b);
assert(a.next == NULL);

return 0;
}

0 comments on commit 0c60fb6

Please sign in to comment.