/*------------------------------------------------------------------*/ /* testdynarray.c */ /*------------------------------------------------------------------*/ #include "dynarray.h" #include #include /*------------------------------------------------------------------*/ static void printString(void *pvItem, void *pvFormat) /* Print string *pvItem using format pvFormat. */ { char *pcItem = (char*)pvItem; char *pcFormat = (char*)pvFormat; printf(pcFormat, pcItem); } /*------------------------------------------------------------------*/ static int compareString(const void *pvOne, const void *pvTwo) /* Return -1, 0, or 1 depending upon whether *pvOne is less than, equal to, or greater than *pvTwo, respectively. */ { const char *pcOne = (const char*)pvOne; const char *pcTwo = (const char*)pvTwo; return strcmp(pcOne, pcTwo); } /*------------------------------------------------------------------*/ int main(int argc, char *argv[]) { DynArray_T oDynArray; int iLength; char **ppcArray; int i; /* Test DynArray_new. */ oDynArray = DynArray_new(0); /* Test DynArray_add. */ DynArray_add(oDynArray, "Ruth"); DynArray_add(oDynArray, "Gehrig"); DynArray_add(oDynArray, "Mantle"); DynArray_add(oDynArray, "Jeter"); /* Test DynArray_getLength. */ printf("-----------------------------------------------------\n"); iLength = DynArray_getLength(oDynArray); printf("DynArray length: %d\n", iLength); /* Test DynArray_get. */ printf("-----------------------------------------------------\n"); printf("This output should list 4 players\n"); printf("-----------------------------------------------------\n"); printf("%s\n", (char*)DynArray_get(oDynArray, 0)); printf("%s\n", (char*)DynArray_get(oDynArray, 1)); printf("%s\n", (char*)DynArray_get(oDynArray, 2)); printf("%s\n", (char*)DynArray_get(oDynArray, 3)); /* Test DynArray_put. */ DynArray_put(oDynArray, 2, "Berra"); printf("-----------------------------------------------------\n"); printf("This output should list 4 players\n"); printf("-----------------------------------------------------\n"); printf("%s\n", (char*)DynArray_get(oDynArray, 0)); printf("%s\n", (char*)DynArray_get(oDynArray, 1)); printf("%s\n", (char*)DynArray_get(oDynArray, 2)); printf("%s\n", (char*)DynArray_get(oDynArray, 3)); /* Test DynArray_toArray. */ printf("-----------------------------------------------------\n"); printf("This output should list 4 players\n"); printf("-----------------------------------------------------\n"); iLength = DynArray_getLength(oDynArray); ppcArray = (char**)calloc((size_t)iLength, sizeof(char*)); DynArray_toArray(oDynArray, (void**)ppcArray); for (i = 0; i < iLength; ++i) printf("%s\n", ppcArray[i]); free(ppcArray); /* Test DynArray_map. */ printf("-----------------------------------------------------\n"); printf("This output should list 4 players\n"); printf("-----------------------------------------------------\n"); DynArray_map(oDynArray, printString, "%s\n"); /* Test DynArray_sort. */ printf("-----------------------------------------------------\n"); printf("This output should list 4 players in ascending order\n"); printf("-----------------------------------------------------\n"); DynArray_sort(oDynArray, compareString); DynArray_map(oDynArray, printString, "%s\n"); /* Test DynArray_search. */ printf("-----------------------------------------------------\n"); printf("This output should list 1 player\n"); printf("-----------------------------------------------------\n"); i = DynArray_search(oDynArray, "Ruth", compareString); printf("%s\n", (char*)DynArray_get(oDynArray, i)); /* Test DynArray_bsearch. */ printf("-----------------------------------------------------\n"); printf("This output should list 1 player\n"); printf("-----------------------------------------------------\n"); i = DynArray_bsearch(oDynArray, "Ruth", compareString); printf("%s\n", (char*)DynArray_get(oDynArray, i)); /* Test DynArray_free. */ DynArray_free(oDynArray); return 0; }