static void test_10(void) { sSLList *l1,*l2; size_t oldFree; test_caseStart("Testing sll_clone"); oldFree = heapspace(); l1 = sll_create(); sll_append(l1,(void*)4); sll_append(l1,(void*)3); sll_append(l1,(void*)2); l2 = sll_clone(l1); test_assertSize(sll_length(l2),3); test_assertPtr(sll_get(l2,0),(void*)4); test_assertPtr(sll_get(l2,1),(void*)3); test_assertPtr(sll_get(l2,2),(void*)2); sll_destroy(l1,false); sll_destroy(l2,false); l1 = sll_create(); l2 = sll_clone(l1); test_assertSize(sll_length(l2),0); sll_destroy(l2,false); sll_destroy(l1,false); test_assertSize(heapspace(),oldFree); test_caseSucceeded(); }
unsigned int sll_length(sll *ss) { if(ss == NULL) return 0; else return 1 + sll_length(ss->next); }
static void test_1(void) { ulong x = 0x100; size_t i,len,oldFree; bool res = true; sSLList *list; test_caseStart("Append & check & remove index 0"); oldFree = heapspace(); list = sll_create(); for(i = 0; i < 20; i++) { sll_append(list,(void*)x++); } x = 0x100; for(i = 0; i < 20; i++) { if(sll_get(list,i) != (void*)x++) { res = false; break; } } if(res) { for(i = 0; i < 20; i++) { sll_removeIndex(list,0); } } test_assertTrue(res); len = sll_length(list); test_assertSSize(len,0); sll_destroy(list,false); test_assertSize(heapspace(),oldFree); test_caseSucceeded(); }
static void test_6(void) { ulong x = 0x100; size_t i,oldFree; sSLList *list; bool res = true; test_caseStart("Create & append & set somewhere & destroy"); oldFree = heapspace(); list = sll_create(); for(i = 0; i < 5; i++) { sll_append(list,(void*)x++); } sll_set(list,(void*)0x200,3); if(sll_get(list,3) != (void*)0x200) res = false; sll_set(list,(void*)0x201,2); if(sll_get(list,2) != (void*)0x201) res = false; sll_set(list,(void*)0x202,1); if(sll_get(list,1) != (void*)0x202) res = false; sll_set(list,(void*)0x203,0); if(sll_get(list,0) != (void*)0x203) res = false; sll_set(list,(void*)0x204,4); if(sll_get(list,4) != (void*)0x204) res = false; if(sll_length(list) != 5) res = false; sll_destroy(list,false); test_assertTrue(res); test_assertSize(heapspace(),oldFree); test_caseSucceeded(); }
/* return the number of cities in c's bucket _after_ inserting it */ int htbl_add(htbl *t, city c) { int i; char *s = c->name; // printf("%s", s); int index = hash(s) % t->n_buckets; sll *ss = (t->buckets)[index]; (t->buckets)[index] = sll_cons(c, ss); int num = sll_length((t->buckets)[index]); return num + 1; }
static void test_11(void) { sSLList *l1; size_t oldFree; test_caseStart("Testing sll_removeFirst"); oldFree = heapspace(); l1 = sll_create(); sll_append(l1,(void*)4); sll_append(l1,(void*)3); sll_append(l1,(void*)2); test_assertSize(sll_length(l1),3); test_assertPtr(sll_removeFirst(l1),(void*)4); test_assertSize(sll_length(l1),2); test_assertPtr(sll_removeFirst(l1),(void*)3); test_assertSize(sll_length(l1),1); test_assertPtr(sll_removeFirst(l1),(void*)2); test_assertSize(sll_length(l1),0); test_assertPtr(sll_removeFirst(l1),NULL); sll_destroy(l1,false); test_assertSize(heapspace(),oldFree); test_caseSucceeded(); }
/* print a representation of the linked list to stdout */ void sll_show(sll *ss) { sll *tmp = ss; int i; int sz = sll_length(ss); char c; while (tmp) { printf("\""); for (i = 0; (c = tmp->c->name[i]) != '\0'; i++) printf("%c", c); printf("\" -> "); tmp = tmp->next; } printf(".\n"); }
static void test_2(void) { ulong x = 0x100; size_t i,len,oldFree; sSLList *list; test_caseStart("Append & remove first (NULL)"); oldFree = heapspace(); list = sll_create(); for(i = 0; i < 2; i++) { sll_append(list,(void*)x++); } for(i = 0; i < 2; i++) { sll_removeFirstWith(list,NULL); } len = sll_length(list); test_assertSize(len,0); sll_destroy(list,false); test_assertSize(heapspace(),oldFree); test_caseSucceeded(); }
void execute(char *command) { int n = count_carrots(command); if(n>1){ myError(); return; } // char s[514]; //strcpy(s,command); // char* c = strtok(s, " "); if(!strncmp(command,">",1)){ myError(); return; } command = strtok(command, ">"); if(command==NULL) return; /*if(redir == 2){ myError(); return; }*/ char* fi = strtok(NULL, " > "); // fi = removeleadingspace(fi); // fi = strtok(fi, " "); //fi = strtok(fi, "\n"); if(n == 1){ sll* tempfi = parse_args(fi); if(sll_length(tempfi) > 1){ myError(); sll_free(tempfi); return; } sll_free(tempfi); } int des = 1; if(fi!=NULL) { if(!strncmp(command, "exit",4)|| !strncmp(command, "pwd",3) || !strncmp(command, "cd",2)){ myError(); return; } removeleadingspace(fi); fi = strtok(fi, " "); fi = strtok(fi, "\n"); des = open(fi, O_WRONLY | O_CREAT | O_EXCL, 0666); if(des==-1){ myError(); return; } } /*int bit = redirect(command); char* fname; if(bit == 2){ myError(); return; } else if(bit == 1) { // command to redirect command =strtok(command, ">"); fname = strtok(NULL, ">"); }*/ if(!strncmp(command, "exit",4)){ // myPrint(command); sll* temp = parse_args(command); if(sll_length(temp)==1){ sll_free(temp); exit(0); } else{ sll_free(temp); myError(); return; } } else if(!strncmp(command, " ",1)) { //separate(command, " ", execute); execute(removeleadingspace(command)); return; } else if(!strncmp(command, "cd",2)) { //myPrint(command); //myPrint("\n"); sll* temp = parse_args(command); if(sll_length(temp)>2){ myError(); sll_free(temp); return; } else if(sll_length(temp) == 1 && !strcmp(strtok(temp->s, "\n"),"cd")){ chdir(getenv("HOME")); return; } //char* dir = strtok(command, " "); char*dir = temp->s; // if(strtok(NULL, " ") == NULL){ //myError(); // return; //} if(strlen(dir)!=strlen("cd")&&strcmp(dir,"cd\n")){ myError(); return; } //dir = strtok(NULL, " "); dir = temp->next->s; char* dir2 = strtok(dir, "\n"); if(dir2==NULL) dir2 = getenv("HOME"); if(chdir(dir2)) myError(); return; } else if(!strcmp(command, "\n")) return; else{ pid_t pid = fork(); // child process executes command if(pid==0) { if(!strncmp(command, "pwd",3)){ // myPrint(command); // myPrint("\n"); sll* temp = parse_args(command); if(sll_length(temp) > 1){ sll_free(temp); myError(); exit(0); } sll_free(temp); char dir[514]; //if(!bit) dup2(des, STDOUT_FILENO); myPrint(getcwd(dir, 514)); //else // outp(getcwd(dir, 514),fname); dup2(des, STDOUT_FILENO); myPrint("\n"); exit(0); } // else if(!strncmp(command, " ",1)) { // separate(command, " ", execute); //} else{ sll* list = parse_args(command); char** argz = sll_to_array(list); // myPrint(command); //myPrint("\n"); dup2(des, STDOUT_FILENO); if(execvp(argz[0], argz) == -1){ myError(); exit(0); } sll_free(list); free(argz); } //else //myError(); dup2(des, STDOUT_FILENO); myPrint("\n"); exit(0); } //parent process waits else { int ch; waitpid(pid, &ch, 0); return; } } }