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; }
static bool bpqEmptyTest() { SPBPQueue source, source2; SPListElement e1; source = NULL; ASSERT_TRUE(spBPQueueIsEmpty(source) == true); // check edge case source = spBPQueueCreate(maxSize); ASSERT_TRUE(spBPQueueIsEmpty(source) == true); // check that new queue is empty // insert a new element and check that not empty e1 = spListElementCreate(1, 1.0); source2 = quickQ(1, e1); ASSERT_TRUE(spBPQueueIsEmpty(source2) == false); // remove the element and check that empty spBPQueueDequeue(source2); ASSERT_TRUE(spBPQueueIsEmpty(source) == true); // free memory spBPQueueDestroy(source); spBPQueueDestroy(source2); spListElementDestroy(e1); return true; }
static bool bpqPeekLastTest() { SPBPQueue source, source2; SPListElement e1, e2, e3, e4, last, last2; source = spBPQueueCreate(maxSize); ASSERT_TRUE(spBPQueuePeekLast(source) == NULL); // check edge case // e1, e2, e3, e4 CREATE_4_ELEMENTS() // insert element in unsorted order source2 = quickQ(4, e3, e1, e4, e2); last = spBPQueuePeekLast(source2); last2 = spBPQueuePeekLast(source2); ASSERT_TRUE(spListElementCompare(e4, last) == 0); // check that peekLast is the maximum ASSERT_TRUE(spListElementCompare(last, last2) == 0 && last2 != last); // new copy so not == // free memory spBPQueueDestroy(source); spBPQueueDestroy(source2); spListElementDestroy(last); spListElementDestroy(last2); // e1, e2, e3, e4 DESTROY_4_ELEMENTS() return true; }
static bool bpqMaxValueTest() { SPBPQueue source, source2, source3; SPListElement e1, e2, e3, e4; source = spBPQueueCreate(maxSize); ASSERT_TRUE(spBPQueueMinValue(source) == -1); // check edge case // e1, e2, e3, e4 CREATE_4_ELEMENTS() source2 = quickQ(4, e1, e2, e3, e4); source3 = quickQ(4, e4, e3, e1, e2); double max2 = spBPQueueMaxValue(source2); double max3 = spBPQueueMaxValue(source3); // check that min value is 4 in both cases ASSERT_TRUE(max2 == 4.0); ASSERT_TRUE(max3 == 4.0); // free memory spBPQueueDestroy(source); spBPQueueDestroy(source2); spBPQueueDestroy(source3); // 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; }
int* SPKDTreeKNN(SPKDTreeNode tree, SPPoint p, int k, SP_KDTREE_MSG* msg) { int i; int* res; SPBPQueue bpq; SPListElement head; if(tree == NULL || k <= 0) { *msg = SP_KDTREE_INVALID_ARGUMENT; return NULL; } bpq = spBPQueueCreate(k); if(bpq == NULL) { *msg = SP_KDTREE_ALLOC_FAIL; return NULL; } res = (int*) malloc(k * sizeof(int)); if(res == NULL) { *msg = SP_KDTREE_ALLOC_FAIL; spBPQueueDestroy(bpq); return NULL; } // Call SPKDTreeKNNRecursive to fill the bpq with the k nearest neighbors SPKDTreeKNNRecursive(tree, p, bpq, msg); if (*msg != SP_KDTREE_SUCCESS) { spBPQueueDestroy(bpq); return NULL; } // Cast the bpq to an array for(i = 0; i < k; i++) { head = spBPQueuePeek(bpq); res[i] = spListElementGetIndex(head); spListElementDestroy(head); spBPQueueDequeue(bpq); } spBPQueueDestroy(bpq); *msg = SP_KDTREE_SUCCESS; return res; }
static bool bpqGetMaxSizeTest() { SPBPQueue source = NULL; SPListElement e1; ASSERT_TRUE(-1 == spBPQueueGetMaxSize(source)); //check edge case // check that max size is always maxSize source = quickQ(0); ASSERT_TRUE(maxSize == spBPQueueGetMaxSize(source)); // insert a new element and check max size e1 = spListElementCreate(1, 1.0); spBPQueueEnqueue(source, e1); ASSERT_TRUE(maxSize == spBPQueueGetMaxSize(source)); // insert another element and check max size spListElementSetIndex(e1, 2); spListElementSetValue(e1, 2.0); spBPQueueEnqueue(source, e1); ASSERT_TRUE(maxSize == spBPQueueGetMaxSize(source)); // remove an element and check max size spBPQueueDequeue(source); ASSERT_TRUE(maxSize == spBPQueueGetMaxSize(source)); // free memory spBPQueueDestroy(source); spListElementDestroy(e1); return true; }
//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; }
void destroyCaseData(SPKDTreeNode tree,SPKDArray kdArr,SPPoint* pointsArray,int size, SPBPQueue queue,SPPoint queryPoint){ if (tree) spKDTreeDestroy(tree, false); if (kdArr) spKDArrayDestroy(kdArr); if (pointsArray) destroyPointsArray(pointsArray,size); if (queue) spBPQueueDestroy(queue); if (queryPoint) spPointDestroy(queryPoint); }
// tester for spBPQueueCreate static bool bpqCreateTest() { SPBPQueue source; source = spBPQueueCreate(maxSize); ASSERT_TRUE(source != NULL); spBPQueueDestroy(source); return true; }
void endControlFlow(SPConfig config, SPImageData image, bool isCurrentImageFeaturesArrayAllocated, SPKDTreeNode kdTree, SPBPQueue bpq, int returnValue) { if (returnValue < 0) { printf(MAIN_RETURNED_ERROR); } printf("%s", EXITING); spConfigDestroy(config); freeImageData(image, !isCurrentImageFeaturesArrayAllocated, true); spKDTreeDestroy(kdTree, true); spBPQueueDestroy(bpq); spLoggerDestroy(); }
//null test bool runKnnNullTests(){ SPBPQueue tempQueue = NULL; SPPoint point = generateRandomPoint(4,1); tempQueue = spBPQueueCreate(4); kNearestNeighbors(NULL,tempQueue, point); ASSERT_TRUE(spBPQueueIsEmpty(tempQueue)); spPointDestroy(point); spBPQueueDestroy(tempQueue); return true; }
int* spFindImages(SPPoint* queryFeatures, const int querySize, const SPKDTreeNode root, const SPConfig config, SP_CONFIG_MSG *msg){ int i, knn, numOfSimilarImg, numOfImages; int *imageCounter, *res; SPListElement element; knn = spConfigGetKNN(config, msg); SPBPQueue q = spBPQueueCreate(knn); if(!q){ spLoggerPrintError(ALLOC_FAIL, __FILE__, __FUNCTION__, __LINE__); return NULL; } numOfImages = spConfigGetNumOfImages(config, msg); numOfSimilarImg = spConfigGetNumOfSimilarImages(config, msg); imageCounter = (int*)calloc(numOfImages, sizeof(int)); if(!imageCounter){ spLoggerPrintError(ALLOC_FAIL, __FILE__, __FUNCTION__, __LINE__); spBPQueueDestroy(q); return NULL; } /* count the num of neighbours of every * image for all features of the query */ for(i=0; i<querySize; i++){ spBPQueueClear(q); spKNNSearch(queryFeatures[i], root, q); while((element = spBPQueuePeek(q))!=NULL){ imageCounter[spListElementGetIndex(element)]+=1; spListElementDestroy(element); spBPQueueDequeue(q); } } spBPQueueClear(q); spBPQueueDestroy(q); res = getTopImagesFromArray(imageCounter, numOfImages, numOfSimilarImg); free(imageCounter); return res; }
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; }
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; }
static bool bpqDequeueTest() { SPBPQueue source; SPListElement e; ASSERT_TRUE(SP_BPQUEUE_INVALID_ARGUMENT == spBPQueueDequeue(NULL)); // check edge case source = spBPQueueCreate(maxSize); ASSERT_TRUE(SP_BPQUEUE_INVALID_ARGUMENT == spBPQueueDequeue(source)); // check edge case // insert maxSize elements, then remove them for (int i = 0; i < maxSize; i++) { e = spListElementCreate(i, (double) i); spBPQueueEnqueue(source, e); spListElementDestroy(e); } for (int i = 0; i < maxSize; i++) { ASSERT_TRUE((int )spBPQueueMinValue(source) == i); // check that the minimum was removed on the last dequeue ASSERT_TRUE(spBPQueueDequeue(source) == SP_BPQUEUE_SUCCESS); // check that dequeue succeeded } // free memory spBPQueueDestroy(source); return true; }
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; }
static bool bpqDestroyTest() { spBPQueueDestroy(NULL); // check there's no error when destroying null return true; }