static bool bpqClearTest() { SPBPQueue source, source2; SPListElement e1, e2, e3, e4; // e1, e2, e3, e4 CREATE_4_ELEMENTS() source = quickQ(4, e1, e2, e3, e4); spBPQueueClear(source); ASSERT_TRUE(0 == spBPQueueSize(source)); // check that size is 0 after clearing source2 = spBPQueueCreate(maxSize); spBPQueueClear(source2); ASSERT_TRUE(0 == spBPQueueSize(source)); // check that size is 0 after clearing // free memory spBPQueueDestroy(source); spBPQueueDestroy(source2); // e1, e2, e3, e4 DESTROY_4_ELEMENTS() return true; }
// tester for spBPQueueCopy static bool bpqCopyTest() { SPBPQueue source, source2, copy, copy2; SPListElement e1, e2, e3, e4, epeek; ASSERT_TRUE(spBPQueueCopy(NULL) == NULL); // check edge case source = spBPQueueCreate(10); copy = spBPQueueCopy(source); ASSERT_TRUE(copy != NULL); ASSERT_TRUE(0 == spBPQueueSize(copy)); // e1, e2, e3, e4 CREATE_4_ELEMENTS() spBPQueueEnqueue(source, e1); ASSERT_TRUE(0 == spBPQueueSize(copy)); // ensure the copy is a NEW COPY source2 = quickQ(4, e1, e2, e3, e4); copy2 = spBPQueueCopy(source2); ASSERT_TRUE(4 == spBPQueueSize(copy2)); // check that size of copy is correct // check that all elements copied correctly epeek = spBPQueuePeek(copy2); ASSERT_TRUE(spListElementCompare(e1, epeek) == 0); spBPQueueDequeue(copy2); spListElementDestroy(epeek); // free the element // repeat epeek = spBPQueuePeek(copy2); ASSERT_TRUE(spListElementCompare(e2, epeek) == 0); spBPQueueDequeue(copy2); spListElementDestroy(epeek); epeek = spBPQueuePeek(copy2); ASSERT_TRUE(spListElementCompare(e3, epeek) == 0); spBPQueueDequeue(copy2); spListElementDestroy(epeek); epeek = spBPQueuePeek(copy2); ASSERT_TRUE(spListElementCompare(e4, epeek) == 0); spBPQueueDequeue(copy2); spListElementDestroy(epeek); epeek = spBPQueuePeek(copy2); ASSERT_TRUE(epeek == NULL); // free all remaining memory spListElementDestroy(epeek); spBPQueueDestroy(source); spBPQueueDestroy(source2); spBPQueueDestroy(copy); spBPQueueDestroy(copy2); // e1, e2, e3, e4 DESTROY_4_ELEMENTS() return true; }
SP_BPQUEUE_MSG spBPQueueDequeue(SPBPQueue source) { SPListElement first; SP_LIST_MSG actionStatus; spMinimalVerifyArguments(source != NULL, SP_BPQUEUE_INVALID_ARGUMENT); if (spBPQueueIsEmpty(source)) return SP_BPQUEUE_EMPTY; first = spListGetFirst(source->queue); if (first == NULL) return SP_BPQUEUE_EMPTY; // if we have 1 items -> last is first -> we should free its pointer if (spBPQueueSize(source) == 1) { spListElementDestroy(source->maxElement); source->maxElement = NULL; } actionStatus = spListRemoveCurrent(source->queue); if (actionStatus != SP_LIST_SUCCESS) return SP_BPQUEUE_EMPTY; return SP_BPQUEUE_SUCCESS; }
SP_BPQUEUE_MSG spBPQueueInsertNotEmptyNotLast(SPBPQueue source, SPListElement element) { SP_LIST_MSG retVal; retVal = spListInsertBeforeCurrent(source->queue, element); if (retVal == SP_LIST_OUT_OF_MEMORY) return SP_BPQUEUE_OUT_OF_MEMORY; if (spBPQueueSize(source) > spBPQueueGetMaxSize(source)) return spBPQueueHandleFullCapacity(source); return SP_BPQUEUE_SUCCESS; }
//general test bool verifyKNN(SPBPQueue rsltQueue, int k,SPPoint* pointsArray,SPPoint queryPoint, int numOfPoints){ SPBPQueue workingQueue = NULL; SPListElement tempElement = NULL; SP_BPQUEUE_MSG msg; bool emptyFlag; int i, queueSize; int* rsltsArray; if (rsltQueue == NULL || pointsArray == NULL || queryPoint == NULL) return false; workingQueue = spBPQueueCopy(rsltQueue); ASSERT_TRUE(workingQueue != NULL); rsltsArray = getRealRsltsArray(k,pointsArray,queryPoint,numOfPoints); if (rsltsArray == NULL){ spBPQueueDestroy(workingQueue); return false; } queueSize = spBPQueueSize(workingQueue); for (i = 0; i< queueSize ; i++){ tempElement = spBPQueuePeek(workingQueue); if (spListElementGetIndex(tempElement) != rsltsArray[i]){ free(rsltsArray); spBPQueueDestroy(workingQueue); return false; } spListElementDestroy(tempElement); msg = spBPQueueDequeue(workingQueue); if (msg != SP_BPQUEUE_SUCCESS){ free(rsltsArray); spBPQueueDestroy(workingQueue); return false; } } emptyFlag = spBPQueueIsEmpty(workingQueue); spBPQueueDestroy(workingQueue); free(rsltsArray); return emptyFlag; }
static bool bpqFullTest() { SPBPQueue source; SPListElement e1; source = NULL; ASSERT_TRUE(spBPQueueIsFull(source) == false); // check edge case source = spBPQueueCreate(maxSize); // insert maxSize element and check that full at the end while (spBPQueueSize(source) < maxSize) { ASSERT_TRUE(spBPQueueIsFull(source) == false); // check that not full in the process e1 = spListElementCreate(1, 1.0); spBPQueueEnqueue(source, e1); spListElementDestroy(e1); } ASSERT_TRUE(spBPQueueIsFull(source) == true); // free memory spBPQueueDestroy(source); return true; }
SP_BPQUEUE_MSG spBPQueueHandleFullCapacity(SPBPQueue source) { int i; SPListElement currElemInQueue, prevElemInQueue; currElemInQueue = spListGetFirst(source->queue); prevElemInQueue = currElemInQueue; for (i=1;i<spBPQueueSize(source);i++) { prevElemInQueue = currElemInQueue; currElemInQueue = spListGetNext(source->queue); } // if we get here we assume we never reached the end of the list (meaning null) // because we assume spBPQueueSize is valid spListRemoveCurrent(source->queue); spListElementDestroy(source->maxElement); source->maxElement = spListElementCopy(prevElemInQueue); if (source->maxElement == NULL) return SP_BPQUEUE_OUT_OF_MEMORY; return SP_BPQUEUE_SUCCESS; }
static bool bpqGetSizeTest() { SPBPQueue source = NULL; SPListElement e1, e; ASSERT_TRUE(-1 == spBPQueueSize(source)); //check edge case source = quickQ(0); ASSERT_TRUE(0 == spBPQueueSize(source)); // insert a new element and check size e1 = spListElementCreate(1, 1.0); spBPQueueEnqueue(source, e1); ASSERT_TRUE(1 == spBPQueueSize(source)); // make sure that inserting same element twice works spListElementSetIndex(e1, 2); spListElementSetValue(e1, 2.0); spBPQueueEnqueue(source, e1); ASSERT_TRUE(2 == spBPQueueSize(source)); // remove an element and check size spBPQueueDequeue(source); ASSERT_TRUE(1 == spBPQueueSize(source)); // insert more then maxSize elements and check that size is always less then maxSize for (int i = 0; i < 2 * maxSize; i++) { ASSERT_TRUE(spBPQueueSize(source) <= maxSize); e = spListElementCreate(i, 1.0); spBPQueueEnqueue(source, e); spListElementDestroy(e); } // free memory spBPQueueDestroy(source); spListElementDestroy(e1); return true; }
bool spBPQueueIsFull(SPBPQueue source) { assert(source != NULL); return spBPQueueSize(source) == spBPQueueGetMaxSize(source); }
bool spBPQueueIsEmpty(SPBPQueue source) { assert(source != NULL); return spBPQueueSize(source) == 0; }
static bool bpqEnqueueTest() { SPBPQueue source, source2; SPListElement e, e1, e2, e3, e4, e5, peek, peekLast; ASSERT_TRUE(SP_BPQUEUE_INVALID_ARGUMENT == spBPQueueEnqueue(NULL, NULL)); // check edge case CREATE_4_ELEMENTS() // e1, e2, e3, e4 e5 = spListElementCreate(5, 4.0); source = quickQ(3, e2, e1, e4); ASSERT_TRUE(SP_BPQUEUE_SUCCESS == spBPQueueEnqueue(source, e3)); // check that enqueue succeeded ASSERT_TRUE(SP_BPQUEUE_SUCCESS == spBPQueueEnqueue(source, e5)); ASSERT_TRUE(5 == spBPQueueSize(source)); // check that enqueue inserts in order peek = spBPQueuePeek(source); peekLast = spBPQueuePeekLast(source); ASSERT_TRUE(spListElementCompare(e1, peek) == 0); // make sure queue sorts by value and then by index ASSERT_TRUE(spListElementCompare(e5, peekLast) == 0); // e1, e2, e3, e4 DESTROY_4_ELEMENTS() spListElementDestroy(e5); // create new queue with maxSize source2 = spBPQueueCreate(maxSize); // insert 2*maxSize elements from lowest to highest value and check that min and max are correct for (int i = 0; i < maxSize; i++) { e = spListElementCreate(i, (double) i); ASSERT_TRUE(SP_BPQUEUE_SUCCESS == spBPQueueEnqueue(source2, e)); spListElementDestroy(e); } for (int i = maxSize; i < 2 * maxSize; i++) { e = spListElementCreate(i, (double) i); ASSERT_TRUE(SP_BPQUEUE_FULL == spBPQueueEnqueue(source2, e)); // check full when inserting more then maxSize elements spListElementDestroy(e); } ASSERT_TRUE(spBPQueueMinValue(source2) == 0.0); // check that all elements with value too high are not in the queue ASSERT_TRUE((int )spBPQueueMaxValue(source2) == maxSize - 1); spBPQueueClear(source2); // insert 2*maxSize elements from highest to lowest value and check that min and max are correct and same as before for (int i = 2 * maxSize - 1; i >= 0; i--) { e = spListElementCreate(i, (double) i); spBPQueueEnqueue(source2, e); spListElementDestroy(e); } // check min value is correct ASSERT_TRUE(spBPQueueMinValue(source2) == 0.0); ASSERT_TRUE((int )spBPQueueMaxValue(source2) == maxSize - 1); // free memory spBPQueueDestroy(source); spBPQueueDestroy(source2); spListElementDestroy(peek); spListElementDestroy(peekLast); return true; }
bool spBPQueueIsEmpty(SPBPQueue source) { assert(source); return (spBPQueueSize(source) == 0); }