/*-------------------------------------------------------------------------*/ /* testmystring.c */ /*-------------------------------------------------------------------------*/ #include "mystring.h" #include #include #define MAX_STRING_LENGTH 256 #define BOOLEAN int #define TRUE 1 #define FALSE 0 #define ASSURE(b) assure(b, __LINE__) void assure(BOOLEAN bSuccessful, int iLineNum) { if (! bSuccessful) printf("Test at line %d failed.\n", iLineNum); } void testmystrlen(void) { size_t uiResult; uiResult = mystrlen("Ruth"); ASSURE(uiResult == 4); uiResult = mystrlen("Gehrig"); ASSURE(uiResult == 6); uiResult = mystrlen(""); ASSURE(uiResult == 0); } void testmystrcmp(void) { int iResult; iResult = mystrcmp("Ruth", "Ruth"); ASSURE(iResult == 0); iResult = mystrcmp("Gehrig", "Ruth"); ASSURE(iResult < 0); iResult = mystrcmp("Ruth", "Gehrig"); ASSURE(iResult > 0); iResult = mystrcmp("", "Ruth"); ASSURE(iResult < 0); iResult = mystrcmp("Ruth", ""); ASSURE(iResult > 0); iResult = mystrcmp("", ""); ASSURE(iResult == 0); } void testmystrncmp(void) { int iResult; iResult = mystrncmp("Ruth", "Ruth", 0); ASSURE(iResult == 0); iResult = mystrncmp("Ruth", "Ruth", 4); ASSURE(iResult == 0); iResult = mystrncmp("Ruth", "Ruth", 2); ASSURE(iResult == 0); iResult = mystrncmp("Ruth", "Ruth", 6); ASSURE(iResult == 0); iResult = mystrncmp("Ruth", "RuthGehrig", 4); ASSURE(iResult == 0); iResult = mystrncmp("Ruth", "RuthGehrig", 5); ASSURE(iResult < 0); iResult = mystrncmp("Gehrig", "Ruth", 4); ASSURE(iResult < 0); iResult = mystrncmp("Gehrig", "Ruth", 2); ASSURE(iResult < 0); iResult = mystrncmp("Gehrig", "Ruth", 6); ASSURE(iResult < 0); iResult = mystrncmp("Ruth", "Gehrig", 4); ASSURE(iResult > 0); iResult = mystrncmp("Ruth", "Gehrig", 2); ASSURE(iResult > 0); iResult = mystrncmp("Ruth", "Gehrig", 6); ASSURE(iResult > 0); iResult = mystrncmp("", "Ruth", 4); ASSURE(iResult < 0); iResult = mystrncmp("", "Ruth", 2); ASSURE(iResult < 0); iResult = mystrncmp("", "Ruth", 6); ASSURE(iResult < 0); iResult = mystrncmp("Ruth", "", 4); ASSURE(iResult > 0); iResult = mystrncmp("Ruth", "", 2); ASSURE(iResult > 0); iResult = mystrncmp("Ruth", "", 6); ASSURE(iResult > 0); iResult = mystrncmp("Ruth", "", 0); ASSURE(iResult == 0); iResult = mystrncmp("", "", 2); ASSURE(iResult == 0); iResult = mystrncmp("", "", 0); ASSURE(iResult == 0); } void testmystrcpy(void) { char pc[MAX_STRING_LENGTH]; char *pcResult; pcResult = mystrcpy(pc, "Ruth"); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (strcmp(pc, "Ruth") == 0)); pcResult = mystrcpy(pc, ""); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (strcmp(pc, "") == 0)); } void testmystrncpy(void) { char pc[8]; char *pcResult; char pcX[] = {'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'}; char pcBigRuth[] = {'R', 'u', 't', 'h', '\0', '\0', '\0', '\0'}; char pcBigRuth2[] = {'R', 'u', 't', 'h', '\0', 'x', 'x', 'x'}; char pcSmallRuth[] = {'R', 'u', 't', 'x', 'x', 'x', 'x', 'x'}; char pcNulls[] = {'\0', '\0', '\0', '\0', 'x', 'x', 'x', 'x'}; memcpy(pc, pcX, 8); pcResult = mystrncpy(pc, "Ruth", 5); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (memcmp(pc, pcBigRuth2, 8) == 0)); memcpy(pc, pcX, 8); pcResult = mystrncpy(pc, "Ruth", 8); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (memcmp(pc, pcBigRuth, 8) == 0)); memcpy(pc, pcX, 8); pcResult = mystrncpy(pc, pcBigRuth2, 8); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (memcmp(pc, pcBigRuth, 8) == 0)); memcpy(pc, pcX, 8); pcResult = mystrncpy(pc, "Ruth", 3); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (memcmp(pc, pcSmallRuth, 8) == 0)); memcpy(pc, pcX, 8); pcResult = mystrncpy(pc, "", 4); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (memcmp(pc, pcNulls, 8) == 0)); memcpy(pc, pcX, 8); pcResult = mystrncpy(pc, "Ruth", 0); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (memcmp(pc, pcX, 8) == 0)); memcpy(pc, pcX, 8); pcResult = mystrncpy(pc, "", 0); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (memcmp(pc, pcX, 8) == 0)); } void testmystrcat(void) { char pc[MAX_STRING_LENGTH]; char *pcResult; strcpy(pc, "Ruth"); pcResult = mystrcat(pc, "Gehrig"); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (strcmp(pcResult, "RuthGehrig") == 0)); strcpy(pc, "Ruth"); pcResult = mystrcat(pc, ""); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (strcmp(pcResult, "Ruth") == 0)); strcpy(pc, ""); pcResult = mystrcat(pc, "Ruth"); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (strcmp(pcResult, "Ruth") == 0)); strcpy(pc, ""); pcResult = mystrcat(pc, ""); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (strcmp(pcResult, "") == 0)); } void testmystrncat(void) { char pc[MAX_STRING_LENGTH]; char *pcResult; strcpy(pc, "Ruth"); pcResult = mystrncat(pc, "Gehrig", 4); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (strcmp(pcResult, "RuthGehr") == 0)); strcpy(pc, "Ruth"); pcResult = mystrncat(pc, "Gehrig", 2); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (strcmp(pcResult, "RuthGe") == 0)); strcpy(pc, "Ruth"); pcResult = mystrncat(pc, "Gehrig", 0); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (strcmp(pcResult, "Ruth") == 0)); strcpy(pc, "Ruth"); pcResult = mystrncat(pc, "", 2); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (strcmp(pc, "Ruth") == 0)); strcpy(pc, "Ruth"); pcResult = mystrncat(pc, "", 0); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (strcmp(pc, "Ruth") == 0)); strcpy(pc, ""); pcResult = mystrncat(pc, "Ruth", 4); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (strcmp(pc, "Ruth") == 0)); strcpy(pc, ""); pcResult = mystrncat(pc, "Ruth", 2); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (strcmp(pc, "Ru") == 0)); strcpy(pc, ""); pcResult = mystrncat(pc, "Ruth", 0); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (strcmp(pc, "") == 0)); strcpy(pc, ""); pcResult = mystrncat(pc, "", 2); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (strcmp(pc, "") == 0)); strcpy(pc, ""); pcResult = mystrncat(pc, "", 0); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (strcmp(pc, "") == 0)); } void testmystrchr(void) { char pc[] = "george"; char *pcResult; pcResult = mystrchr(pc, 'g'); ASSURE(pcResult == pc); pcResult = mystrchr(pc, 'e'); ASSURE(pcResult == pc + 1); pcResult = mystrchr(pc, 'n'); ASSURE(pcResult == NULL); pcResult = mystrchr("", 'h'); ASSURE(pcResult == NULL); } void testmystrrchr(void) { char pc[] = "george"; char *pcResult; pcResult = mystrrchr(pc, 'g'); ASSURE(pcResult == pc + 4); pcResult = mystrrchr(pc, 'e'); ASSURE(pcResult == pc + 5); pcResult = mystrrchr(pc, 'n'); ASSURE(pcResult == NULL); pcResult = mystrrchr("", 'h'); ASSURE(pcResult == NULL); } void testmystrspn() { int iResult; iResult = mystrspn("llama", "al"); ASSURE(iResult == 3); iResult = mystrspn("llama", "x"); ASSURE(iResult == 0); iResult = mystrspn("llama", "a"); ASSURE(iResult == 0); iResult = mystrspn("", "a"); ASSURE(iResult == 0); iResult = mystrspn("llama", ""); ASSURE(iResult == 0); iResult = mystrspn("", ""); ASSURE(iResult == 0); } void testmystrcspn() { int iResult; iResult = mystrcspn("llama", "a"); ASSURE(iResult == 2); iResult = mystrcspn("llama", "l"); ASSURE(iResult == 0); iResult = mystrcspn("llama", "x"); ASSURE(iResult == 5); iResult = mystrcspn("", "a"); ASSURE(iResult == 0); iResult = mystrcspn("llama", ""); ASSURE(iResult == 5); iResult = mystrcspn("", ""); ASSURE(iResult == 0); } void testmystrpbrk(void) { char pc[] = "llama"; char *pcResult; pcResult = mystrpbrk(pc, "ma"); ASSURE(pcResult == pc + 2); pcResult = mystrpbrk(pc, "lmx"); ASSURE(pcResult == pc); pcResult = mystrpbrk(pc, "a"); ASSURE(pcResult == pc + 2); pcResult = mystrpbrk(pc, "x"); ASSURE(pcResult == NULL); pcResult = mystrpbrk(pc, ""); ASSURE(pcResult == NULL); pcResult = mystrpbrk("", "lmx"); ASSURE(pcResult == NULL); pcResult = mystrpbrk("", ""); ASSURE(pcResult == NULL); } void testmystrstr() { char pc[] = "llama"; char pcEmpty[] = ""; char *pcResult; pcResult = mystrstr(pc, "am"); ASSURE(pcResult == pc + 2); pcResult = mystrstr(pc, "m"); ASSURE(pcResult == pc + 3); pcResult = mystrstr(pc, "ll"); ASSURE(pcResult == pc); pcResult = mystrstr(pc, "l"); ASSURE(pcResult == pc); pcResult = mystrstr(pc, "llama"); ASSURE(pcResult == pc); pcResult = mystrstr(pc, "lamal"); ASSURE(pcResult == NULL); pcResult = mystrstr(pc, ""); ASSURE(pcResult == pc); pcResult = mystrstr(pcEmpty, "llama"); ASSURE(pcResult == NULL); pcResult = mystrstr(pcEmpty, ""); ASSURE(pcResult == pcEmpty); } void testconst() { const char *cpc = "Ruth"; char pcResult[MAX_STRING_LENGTH]; mystrlen(cpc); mystrcmp(cpc, cpc); mystrncmp(cpc, cpc, 5); mystrcpy(pcResult, cpc); mystrncpy(pcResult, cpc, 5); mystrcat(pcResult, cpc); mystrncat(pcResult, cpc, 5); mystrchr(cpc, 'x'); mystrrchr(cpc, 'x'); mystrspn(cpc, cpc); mystrcspn(cpc, cpc); mystrpbrk(cpc, cpc); mystrstr(cpc, cpc); } int main(int argc, char *argv[]) { testmystrlen(); testmystrcmp(); testmystrncmp(); testmystrcpy(); testmystrncpy(); testmystrcat(); testmystrncat(); testmystrchr(); testmystrrchr(); testmystrspn(); testmystrcspn(); testmystrpbrk(); testmystrstr(); testconst(); printf("End of %s.\n", argv[0]); return 0; }