/* Test delete with DeleteRearElement */ void test9(void) { int i, j; printf("Testing DeleteRearElement"); while(LSQ_GetSize(handleAll)) { LSQ_IteratorT iter = LSQ_GetFrontElement(handleAll); assert( iter != NULL); // print_LSQ(handleAll); for(j = 0; j < LSQ_GetSize(handleAll); j++) { // printf("\nFound = %d \t Expecting = %d \t at index = %d", *LSQ_DereferenceIterator(iter), all[j], j); assert( *LSQ_DereferenceIterator(iter) == all[j]); assert( *LSQ_DereferenceIterator(iter) != MAX_ELEMENT_VALUE + 1); LSQ_AdvanceOneElement(iter); } LSQ_DestroyIterator(iter); LSQ_DeleteRearElement(handleAll); } printf("\t\t ---\t OK \n"); for(i = 0; i < MAX_handleALL_LEN; i++) { LSQ_InsertRearElement(handleAll, all[i]); } }
/* Test moving on list with Advance/Rewind */ void test5(void) { int i; LSQ_IteratorT iter = LSQ_GetFrontElement(handleAll); assert( iter != NULL); // print_LSQ(handleAll); printf("Testing Advance"); for(i = 0; i < LSQ_GetSize(handleAll); i++) { assert( *LSQ_DereferenceIterator(iter) == all[i]); assert( *LSQ_DereferenceIterator(iter) != MAX_ELEMENT_VALUE + 1); LSQ_AdvanceOneElement(iter); } printf("\t\t ---\t OK \n"); printf("Testing Rewind"); iter = LSQ_GetPastRearElement(handleAll); for(i = LSQ_GetSize(handleAll)-1; i >= 0; i--) { LSQ_RewindOneElement(iter); assert( *LSQ_DereferenceIterator(iter) == all[i]); assert( *LSQ_DereferenceIterator(iter) != MAX_ELEMENT_VALUE + 1); } printf("\t\t ---\t OK \n"); LSQ_DestroyIterator(iter); }
/* Test moving on list with setPosition */ void test4(void) { int i, j; srand ( time(NULL) ); LSQ_IteratorT iter = LSQ_GetFrontElement(handleAll); assert( iter != NULL); // print_LSQ(handleAll); printf("Testing SetPosition"); for(i = 0; i < MAX_TEST_ATTEMPTS; i++) { j = rand() % 1000; // printf("\n Step = %d \t setPosition = %d ", i, j); LSQ_SetPosition(iter, j); if( j < LSQ_GetSize(handleAll)) { // printf("\n Expecting value = %d \t ", all[j]); // printf("\n Found value = %d \t ", iter->self->value); assert( *LSQ_DereferenceIterator(iter) == all[j]); assert( *LSQ_DereferenceIterator(iter) != MAX_ELEMENT_VALUE + 1); } } printf("\t\t ---\t OK \n"); LSQ_DestroyIterator(iter); }
/* Checking front and rear filling */ void test2(void) { int i; // print_LSQ(handle); LSQ_IteratorT iter = LSQ_GetFrontElement(handle); assert( iter != NULL); for(i = 49; i >= 0; i--) { // printf("front = %d \t index i = %d \t value = %d \n",front[i], i, *LSQ_DereferenceIterator(iter)); assert( *LSQ_DereferenceIterator(iter) == front[i] ); assert( *LSQ_DereferenceIterator(iter) != MAX_ELEMENT_VALUE + 1 ); LSQ_AdvanceOneElement(iter); } LSQ_SetPosition(iter, LSQ_GetSize(handle)-1); assert( iter != NULL); // print_LSQ(handle); // printf("iter = %p \t node = %p \t value = %d \n", iter, iter->self, iter->self->value); for(i = 49; i >= 0; i--) { // printf("rear = %d \t index i = %d \t value = %d \n",rear[i], i, *LSQ_DereferenceIterator(iter)); assert( *LSQ_DereferenceIterator(iter) == rear[i] ); assert( *LSQ_DereferenceIterator(iter) != MAX_ELEMENT_VALUE + 1 ); LSQ_RewindOneElement(iter); } // print_LSQ(handle); LSQ_DestroyIterator(iter); }
/* Test moving on list with ShiftPosition */ void test6(void) { int i, j; LSQ_IteratorT iter; srand ( time(NULL) ); // print_LSQ(handleAll); printf("Testing ShiftPosition forward"); for(i = 0; i < MAX_TEST_ATTEMPTS; i++) { iter = LSQ_GetFrontElement(handleAll); assert( iter != NULL); j = rand() % LSQ_GetSize(handleAll); LSQ_ShiftPosition(iter, j); assert(*LSQ_DereferenceIterator(iter) == all[j]); assert( *LSQ_DereferenceIterator(iter) != MAX_ELEMENT_VALUE + 1); LSQ_DestroyIterator(iter); } printf("\t\t ---\t OK \n"); printf("Testing ShiftPosition backward"); for(i = 0; i < MAX_TEST_ATTEMPTS; i++) { iter = LSQ_GetPastRearElement(handleAll); LSQ_RewindOneElement(iter); assert( iter != NULL); j = rand() % LSQ_GetSize(handleAll); LSQ_ShiftPosition(iter, -j); assert( *LSQ_DereferenceIterator(iter) == all[LSQ_GetSize(handleAll) - 1 - j]); assert( *LSQ_DereferenceIterator(iter) != MAX_ELEMENT_VALUE + 1); LSQ_DestroyIterator(iter); // printf("i = %d \t j = %d\n", i, j); } printf("\t\t ---\t OK \n"); }
/* Test moving on list with GetElementByIndex */ void test7(void) { int i, j; srand ( time(NULL) ); // print_LSQ(handleAll); printf("Testing GetElementByIndex"); for(i = 0; i < MAX_TEST_ATTEMPTS; i++) { j = rand() % LSQ_GetSize(handleAll); // printf("\ni = %d \t j = %d \t size = %d", i , j, LSQ_GetSize(handleAll)); LSQ_IteratorT iter = LSQ_GetElementByIndex(handleAll, j); assert( iter != NULL); // printf("\nFound = %d \t Expecting = %d \t", *LSQ_DereferenceIterator(iter), all[j]); assert( *LSQ_DereferenceIterator(iter) == all[j]); assert( *LSQ_DereferenceIterator(iter) != MAX_ELEMENT_VALUE + 1); LSQ_DestroyIterator(iter); } printf("\t\t ---\t OK \n"); }
void LSQ_DeleteGivenElement(LSQ_IteratorT iterator) { _iterator *iter = iterator; _container *c = NULL; LSQ_BaseTypeT *temp = NULL; if(LSQ_IsIteratorDereferencable(iter)) { c = iter->container; temp = LSQ_DereferenceIterator(iter); memmove(temp, temp + 1, (c->size - iter->index) * sizeof(LSQ_BaseTypeT)); c->size--; c->data = (LSQ_BaseTypeT *)realloc(c->data, sizeof(LSQ_BaseTypeT) * (c->size)); } }
void LSQ_InsertElementBeforeGiven(LSQ_IteratorT iterator, LSQ_BaseTypeT newElement) { _iterator *iter = iterator; _container *c = NULL; LSQ_BaseTypeT *temp = NULL; if(!LSQ_IsIteratorBeforeFirst(iter)) { c = iter->container; c->size++; c->data = (LSQ_BaseTypeT *)realloc(c->data, sizeof(LSQ_BaseTypeT) * (c->size)); temp = LSQ_DereferenceIterator(iter); memmove(temp + 1, temp, (c->size - 1 - iter->index) * sizeof(LSQ_BaseTypeT)); c->data[iter->index] = newElement; //iter->index++; } }