diff --git a/src/libCom/ellLib/Makefile b/src/libCom/ellLib/Makefile index b325a453e..9a5cb3e67 100644 --- a/src/libCom/ellLib/Makefile +++ b/src/libCom/ellLib/Makefile @@ -10,3 +10,4 @@ SRC_DIRS += $(LIBCOM)/ellLib INC += ellLib.h Com_SRCS += ellLib.c +Com_SRCS += ellSort.c diff --git a/src/libCom/test/epicsEllTest.c b/src/libCom/test/epicsEllTest.c index b608d1cc3..59a23df0a 100644 --- a/src/libCom/test/epicsEllTest.c +++ b/src/libCom/test/epicsEllTest.c @@ -11,6 +11,7 @@ #include #include "ellLib.h" +#include "dbDefs.h" #include "epicsUnitTest.h" #include "testMain.h" @@ -20,15 +21,13 @@ struct myItem { int num; }; -MAIN(epicsEllTest) +static void testList(void) { ELLLIST list1; ELLLIST list2 = ELLLIST_INIT; int i1 = 1; struct myItem *pitem, *pfirst, *pick; - testPlan(70); - list1.count = 27; list1.node.next = (ELLNODE *) 0x01010101; list1.node.previous = (ELLNODE *) 0x10101010; @@ -192,6 +191,77 @@ MAIN(epicsEllTest) ellFree2(&list1, free); testOk1(ellCount(&list1) == 0); +} + +typedef struct { int A, B; } input_t; + +static int myItemCmp(ELLNODE *a, ELLNODE *b) +{ + struct myItem *A = CONTAINER(a, struct myItem, node), + *B = CONTAINER(b, struct myItem, node); + + if (A->num < B->num) return -1; + else if(A->num > B->num) return 1; + else if(A->list < B->list) return -1; + else if(A->list > B->list) return 1; + else return 0; +} + +static const input_t input[] = { + {-4, 0}, + {-5, 0}, + {0,0}, + {50,0}, + {0,1}, + {5,0}, + {5,1} +}; + +static +void testSort(const input_t *inp, size_t ninp) +{ + unsigned i; + ELLLIST list = ELLLIST_INIT; + struct myItem *alloc = calloc(ninp, sizeof(*alloc)); + + if(!alloc) testAbort("testSort allocation fails"); + + for(i=0; inum = inp[i].A; + it->list= inp[i].B; + + ellAdd(&list, &it->node); + } + + ellSortStable(&list, &myItemCmp); + + testOk(ellCount(&list)==ninp, "output length %u == %u", (unsigned)ellCount(&list), (unsigned)ninp); + if(ellCount(&list)==0) { + testSkip(ninp-1, "all items lost"); + } + + { + struct myItem *prev = CONTAINER(ellFirst(&list), struct myItem, node), + *next; + + for(next = CONTAINER(ellNext(&prev->node), struct myItem, node); + next; + prev = next, next = CONTAINER(ellNext(&next->node), struct myItem, node)) + { + int cond = (prev->numnum) || (prev->num==next->num && prev->listlist); + testOk(cond, "%d:%d < %d:%d", prev->num, prev->list, next->num, next->list); + } + } +} + +MAIN(epicsEllTest) +{ + testPlan(77); + + testList(); + testSort(input, NELEMENTS(input)); return testDone(); }