void *thr_time(void *arg__) { ArgTime *arg= arg__; tstamp t0 = nanotime(arg->clock), t0epoch = nanoepoch(), lastt = t0, curt; Tok *t; while (!inQ->EOT) { t = ll_pop(inQ); nlock_lock(t->edit); ll_push(timeQ, t); curt = nanotime(arg->clock); t->sstart = curt - t0; t->slast = curt - lastt; t->epoch = t->sstart - t0epoch; nlock_unlock(t->edit); lastt = curt; } timeQ->EOT = true; return 0; }
void run_linked_list_tests() { char* inputs[] = { "item1", "item2", "item3", }; int num_inputs = sizeof(inputs) / sizeof(char*); char* data; // after init LinkedList *ll = ll_new(); assert("Starts empty", ll_is_empty(ll)); assert("Initial size is 0", ll_size(ll)==0); assert("Peek returns null", ll_peek(ll)==NULL); // after pushing one item ll_push(ll, inputs[0]); assert("Is not empty after pushing an item", !ll_is_empty(ll)); assert("Has size of 1 after pushing an item", ll_size(ll)==1); assert("Peeking returns the item we pushed", ll_peek(ll)==inputs[0]); // after two items ll_push(ll, inputs[1]); assert("Is not empty after pushing a second item", !ll_is_empty(ll)); assert("Has size of 2 after pushing a second item", ll_size(ll)==2); assert("Peeking returns the second item", ll_peek(ll)==inputs[1]); // after three items ll_push(ll, inputs[2]); assert("Is not empty after pushing a third item", !ll_is_empty(ll)); assert("Has size of 3 after pushing a third item", ll_size(ll)==3); assert("Peeking returns the third item", ll_peek(ll)==inputs[2]); // iterating through the items int index = num_inputs; int all_match = 1; ll_each(ll, char* input, ({ all_match &= (inputs[--index] == input); })); assert("It iterates the correct number of times", index==0); assert("The item provided matched each time", all_match); // popping an item data = ll_pop(ll); assert("It is not empty after popping the third item", !ll_is_empty(ll)); assert("Has size of 2 after popping the third item", ll_size(ll)==2); assert("Peeking returns the second item", ll_peek(ll)==inputs[1]); // cleanup ll_free(ll); }
void *thr_print(void *arg__) { ArgPrint *args = arg__; Tok *t; while (!inQ->EOT) { t = ll_pop(timeQ); nlock_wait(t->edit); fprintf(args->f, "%" PRIu64 " %" PRIu64 " %" PRIu64 " ", t->epoch, t->sstart, t->slast); putItok(args->f, t->str); fputc(args->delim, args->f); deleteTok(t); } return 0; }
PT *ll_remcurr(LL *list){ if(list == NULL) return NULL; PT* out = list->curr; if(out == list->head){ return ll_pop(list); } else { if(out != NULL) { if(out->next != NULL){ out->next->prev = out->prev; } if(out->prev != NULL){ out->prev->next = out->next; } list->curr = out->next; list->length--; } return out; } }
/* Remove item from stack without returning it */ void ll_del(){ struct ll* st = ll_pop(); if(st) free(st); }
int main(int argc, char** argv) { llist *l = malloc(sizeof(llist)); const int reference[11] = {20, 19, 18, 14, 12, 10, 8, 6, 4, 2, -1}; const int size = sizeof(reference) / sizeof(int); int error = 1; // Add numbers 20, 18, 16, 14, ... 2, 0 to the list. { int i; for (i = 20; i >= 0; i -= 2) { ll_push(l, i); } } if (ll_size(l) != 11) { return error; } ll_print(l); error++; ll_pop(l); ll_push(l, -1); ll_insert(l, 1, 19); ll_remove(l, 3); printf("Phase 2 complete (pop, push, insert, remove).\n"); // Check first and last index. if (l -> first -> value != reference[0]) { return error; } printf("Phase 3A complete (check first value).\n"); error++; if (l -> last -> value != reference[size - 1]) { return error; } printf("Phase 3B complete (check last value).\n"); error++; // Check by traversing. { node *n = l -> first; int index = 0; while (n != NULL) { if (n -> value == reference[index++]) { n = n -> next; } else { return error; } } } printf("Phase 4A complete (traverse forward).\n"); error++; // Check by backward traversing. { node *n = l -> last; int index = size - 1; while (n != NULL) { if (n -> value == reference[index--]) { n = n -> prev; } else { return error; } } } printf("Phase 4B complete (traverse backward).\n"); error++; // Check by indexing. { int i; for (i = 0; i < ll_size(l); i++) { if (reference[i] != ll_get(l, i)) { return error; } } } printf("Phase 5 complete (iterate).\n"); error++; printf("Completed without error.\n"); return 0; }
void ll_pop_free(LL *list){ PT* p = ll_pop(list); if(p != NULL) free(p); }