/** * Runs tests on the addAtRear function **/ void addAtRearTest(ListPtr list, int count) { int i; for(i = 0; i < count; i++) { char str[80]; sprintf(str, "%s: %d", "Job", i); JobPtr job = createJob(i, str, i); NodePtr node = createNode(job); addAtRear(list, node); } if(DEBUG > 0) verifyAddAtRearList(list, count); }
void runRandomTests(struct parameters *state, ListPtr list) { int i; int start; int test; NodePtr node; JobPtr job; long int num; srand(state->seed); start=0; if (state->restart) { start = state->done+1; srand(state->randomNum); } for (i=start; i<state->count; i++) { num = rand(); test = num % NUM_TESTS; if ((i > 0) && ((i % CHECKPOINT_COUNT) == 0)) { fprintf(stderr, "checkpointing list, count = %d\n", i); state->done = i-1; state->randomNum = num; checkpointList(list, state->saveFile); checkpointTestSuite(state, stateFile); } switch (test) { case 0: if (DEBUG > 1) fprintf(stderr,"addAtFront\n"); state->n++; job = createJob(state->n, "some info"); node = createNode(job); addAtFront(list, node); break; case 1: if (DEBUG > 1) fprintf(stderr,"addAtRear\n"); state->n++; job = createJob(state->n, "some info"); node = createNode(job); addAtRear(list, node); break; case 2: if (DEBUG > 1) fprintf(stderr,"removeFront\n"); node = removeFront(list); break; case 3: if (DEBUG > 1) fprintf(stderr,"removeRear\n"); node = removeRear(list); break; case 4: if (DEBUG > 1) fprintf(stderr,"removeNode\n"); node = removeNode(list, search(list, i)); break; case 5: if (DEBUG > 1) fprintf(stderr,"reverseList\n"); reverseList(list); default: break; } } }
int main(int argc, char *argv[]) { char *line; char *prompt = "myshell>"; char *envPrompt = "DASH_PROMPT"; char *args[2048]; char *str; char *multiCmd[2048]; int i=0; int flag; int jobid; int code; int fd1,fd2; tcpid = getpgrp(); ObjectPtr myObj; NodePtr node; lst = createList(getKey,toString,freeObject); // Creating a List prompt = getenv(envPrompt); if(prompt == NULL) prompt = "myshell>"; using_history(); if(signal(SIGINT,signalHandling) == SIG_ERR){} if(signal(SIGTSTP,signalHandling) == SIG_ERR){} if(signal(SIGTTOU,SIG_IGN)==SIG_ERR){} if(signal(SIGCHLD,SIG_DFL)==SIG_ERR){} if((argc == 2) && (strcmp(argv[1],"-v")==0)) { printf("%s\n",svn_version()); exit(0); } code = sigsetjmp(env,TRUE); while ((line=readline(prompt))) // Reading Input { if(line==NULL) { printf("\n read line failed \n"); continue; } add_history(line); str = (char *)mymalloc(sizeof(char)*(strlen(line)+1)); strcpy(str,line); if(str[0] == '&' || str[0] == ';') { printf("dash: syntax error near unexpected token %c \n",str[0]); freeVar(line,str,args); continue; } while((pid = waitpid(-1,NULL,WNOHANG)) > 0) { node = search(lst,pid); ((ObjectPtr)(node->obj))->jobStatus = 0; printf("%s\n",(*toString)(node->obj)); removeNode(lst,node); } if(checkMultiCmd(str, multiCmd)== -1) continue; while(multiCmd[i]) { flag = searchAmp(multiCmd[i]); // Function to check if there is an '&' in the Command struct IORedirect *ior = tokenize(multiCmd[i],args); if((int)ior == -1) break; // Parsing the Command that needs to be executed if(exitLogout(multiCmd[i])) // Function to check Exit and Logout Commands { if(checkStoppedJobs()) { //memset(multiCmd,0,sizeof(multiCmd)); break; } else { freeVar(line,str,args); freeList(lst); memset(multiCmd,'\0',sizeof(multiCmd)); myfree(ior); exit(0); } } if(checkEOF(multiCmd[i])) // Function to check EOF { freeVar(line,str,args); memset(multiCmd,'\0',sizeof(multiCmd)); freeList(lst); myfree(ior); exit(0); } if(checkEmptyCmd(multiCmd[i])) // Function to Check if Enter Key is pressed break; if((strcmp(args[0],"cd")==0) && (flag == FALSE)) // Function to check if 'cd' command is used { chgDir(args); break; } if((strcmp(args[0],"jobs")==0) && (flag == FALSE)) // Function to check if 'jobs' command is used { jobsCmd(); break; } if(strcmp(args[0],"history")==0) { printHistory(args, str, flag); break; } if((strcmp(args[0],"bg") == 0) && (flag == FALSE)) { bgCmd(args); break; } if((strcmp(args[0],"fg")==0) && (flag == FALSE)) { fgCmd(args); break; } if(ampCount(multiCmd[i])) { printf(" dash: syntax error near unexpected token '&' \n"); break; } if ( (pid = fork()) < 0) // Forking a Process err_sys("fork error"); else if (pid == 0) { struct stat buf; /* child */ if(flag == TRUE) { if(setpgid(0,0)!=0) perror("setpid() error"); if(tcsetpgrp(0,tcpid)!=0) perror("tcsetpgrp() error"); } if(ior->input != NULL) { if((fd1 = stat(ior->input,&buf))==-1){ printf("dash: %s file does not exist\n",ior->input); break; } if((fd1= access(ior->input,R_OK))==-1){ printf("dash: %s permission denied\n",ior->input); break; } if((fd1 = open(ior->input,O_RDONLY))==-1){ printf("dash: %s inpRedirect opening failed\n",ior->input); break; } else { close(0); dup(fd1); } } if(ior->output != NULL) { /*if((fd1= access(ior->output,W_OK))==-1){ printf("dash: %s permission denied",ior->output); break;}*/ if((fd2=open(ior->output,O_WRONLY|O_TRUNC|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH ))==-1){ printf("dash: %s outRedirect opening failed\n",ior->output); break;} else{ close(1); dup(fd2); } } if (setpgid(0,0) != 0) perror("setpgid() error"); if (tcsetpgrp(0, getpid()) != 0) perror("tcsetpgrp() error"); execvp(args[0],args); err_ret("couldn't execute: %s", line); exit(127); } if(flag == TRUE && (*args[0] !='\0')) { jobid = assignJobId(); myObj = createObject(pid,multiCmd[i],jobid,1); node = createNode(myObj); addAtRear(lst,node); printf("[%d] %d %s &\n",((ObjectPtr)(node->obj))->jobId, ((ObjectPtr)(node->obj))->key, ((ObjectPtr)(node->obj))->data); if ( (pid = waitpid(pid, &status, WNOHANG)) < 0) err_sys("waitpid error"); if((tcsetpgrp(0,tcpid))!=0) perror("tcsetgroup error"); } else { if ( (pid = waitpid(pid, &status, 0|WUNTRACED)) < 0) err_sys("waitpid error"); if((tcsetpgrp(0,tcpid))!=0) perror("tcsetgroup error"); if(WIFSTOPPED(status)) { jobid = assignJobId(); myObj = createObject(pid,line,jobid,2); node = createNode(myObj); addAtRear(lst,node); printf("%s\n",(*toString)(node->obj)); } } i++; } freeVar(line,str,args); memset(multiCmd,'\0',sizeof(multiCmd)); i=0; } freeList(lst); exit(0); }
void runRandomTests(int count, unsigned int seed, int n, ListPtr list) { int i; int test; NodePtr node; ObjectPtr job; ObjectPtr searchJob; int *tests; tests = (int *) malloc(sizeof(int)*NUM_TESTS); for (i=0; i<NUM_TESTS; i++) tests[i]=0; srandom(seed); for (i=0; i<count; i++) { test = (int) (NUM_TESTS * (double) rand()/RAND_MAX); tests[test]++; switch (test) { case 0: if (DEBUG_LEVEL > 1) fprintf(stderr,"addAtFront\n"); n++; job = createObject(n, "some info"); node = createNode(job); addAtFront(list, node); break; case 1: if (DEBUG_LEVEL > 1) fprintf(stderr,"addAtRear\n"); n++; job = createObject(n, "some info"); node = createNode(job); addAtRear(list, node); break; case 2: if (DEBUG_LEVEL > 1) fprintf(stderr,"removeFront\n"); node = removeFront(list); freeNode(node, freeObject); break; case 3: if (DEBUG_LEVEL > 1) fprintf(stderr,"removeRear\n"); node = removeRear(list); freeNode(node, freeObject); break; case 4: if (DEBUG_LEVEL > 1) fprintf(stderr,"removeNode\n"); searchJob = createObject(i,"foo"); node = removeNode(list, search(list, searchJob)); freeNode(node, freeObject); freeObject(searchJob); break; case 5: if (DEBUG_LEVEL > 1) fprintf(stderr,"reverseList\n"); reverseList(list); break; case 6: if (DEBUG_LEVEL > 1) fprintf(stderr,"searchList\n"); searchJob = createObject(i,"foo"); node = search(list, searchJob); freeObject(searchJob); break; default: break; } } print_stats(tests); free(tests); }
/** * Runs edge tests on the current Doubly Linked List implementation **/ void runEdgeCases() { ListPtr l = createList(); NodePtr n = NULL; printf("\n%s\n%s", sep, "Running edge cases with NULL values"); printf("\n%s", "addAtFront"); addAtFront(NULL, NULL); addAtFront(l, NULL); addAtFront(NULL, n); if(l->size == 0 && l->head == NULL && l->tail == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "addAtRear"); addAtRear(NULL, NULL); addAtRear(l, NULL); addAtRear(NULL, n); if(l->size == 0 && l->head == NULL && l->tail == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "removeFront"); n = removeFront(NULL); n = removeFront(l); if(l->size == 0 && l->head == NULL && l->tail == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "removeRear"); n = removeRear(NULL); n = removeRear(l); if(l->size == 0 && l->head == NULL && l->tail == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "removeNode"); n = removeNode(NULL, NULL); n = removeNode(l, NULL); n = removeNode(NULL, n); if(l->size == 0 && n == NULL && l->head == NULL && l->tail == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "reverseList"); reverseList(NULL); reverseList(l); if(l->size == 0 && l->head == NULL && l->tail == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "search"); int i = 0; n = search(NULL, 0); n = search(l, 0); n = search(NULL, i); if(n == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s\n%s", sep, "Running edge cases with Single values"); printf("\n%s", "addAtFront"); JobPtr job = createJob(1, "Single", 1); n = createNode(job); l = createList(); addAtFront(l, n); if(l->size == 1 && l->head == n && l->tail == n && l->head->next == NULL && l->head->prev == NULL && l->tail->next == NULL && l->tail->prev == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "removeFront"); NodePtr removeFrontNode = removeFront(l); if(l->size == 0 && l->head == NULL && l->tail == NULL && removeFrontNode == n) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "addAtRear"); addAtRear(l, n); if(l->size == 1 && l->head == n && l->tail == n && l->head->next == NULL && l->head->prev == NULL && l->tail->next == NULL && l->tail->prev == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s s", "removeRear"); NodePtr removeRearNode = removeRear(l); if(l->size == 0 && l->head == NULL && l->tail == NULL && removeRearNode == n) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "removeNode, empty list"); NodePtr testnode = removeNode(l, n); if(l->size == 0 && l->head == NULL && l->tail == NULL && testnode == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "removeNode, one node, matches"); addAtFront(l, n); testnode = removeNode(l, n); if(l->size == 0 && l->head == NULL && l->tail == NULL && testnode == n) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "removeNode, one node, no match"); JobPtr noMatchJob = createJob(1, "no match", 1); NodePtr noMatch = createNode(noMatchJob); addAtFront(l, noMatch); testnode = removeNode(l, n); if(l->size == 1 && l->head == noMatch && l->tail == noMatch && testnode == NULL && l->head->next == NULL && l->head->prev == NULL && l->tail->next == NULL && l->tail->prev == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "reverseList"); reverseList(l); if(l->size == 1 && l->head == noMatch && l->tail == noMatch && l->head->next == NULL && l->head->prev == NULL && l->tail->next == NULL && l->tail->prev == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "search, matches"); testnode = search(l, 1); if(l->size == 1 && l->head == noMatch && l->tail == noMatch && testnode == noMatch && l->head->next == NULL && l->head->prev == NULL && l->tail->next == NULL && l->tail->prev == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "search, no match"); testnode = search(l, 0); if(l->size == 1 && l->head == noMatch && l->tail == noMatch && testnode == NULL && l->head->next == NULL && l->head->prev == NULL && l->tail->next == NULL && l->tail->prev == NULL) printf("\nSuccess\n"); else printf("\nFail\n"); freeList(l); }