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 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 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;
}
SPBPQueue spBPQueueCopy(SPBPQueue source){
    if(source==NULL) return NULL;
    struct Qnode *n;
    struct Qnode *newNode;
    struct Qnode *prevNode;
    SPBPQueue res = spBPQueueCreate(source->maxSize);

    if (!res){
        return NULL;
     }

    res->size = source->size;
    n = source->first;

    res->first = copyNode(n);
    prevNode = res->first;
    n = n->next;

    while(n){
        newNode = copyNode(n);
        prevNode->next = newNode;

        prevNode = newNode;
        n = n->next;
    }
    res->last = prevNode;

    return res;
}
//edge test case 1
knnTestCaseData initializeKnnEdgeTestCase1(){
	//edge case 1 data
	SPKDTreeNode kNNedgeTestCase1Tree = NULL;
	SPBPQueue kNNedgeTestCase1Queue = NULL;
	SPPoint kNNedgeTestCase1QueryPoint = NULL;
	SPPoint* kNNedgeTestCase1Points = NULL;
	int kNNedgeTestCase1MaxDim = 1;
	int kNNedgeTestCase1Size = 1;
	SP_KDTREE_SPLIT_METHOD kNNedgeTestCase1SplitMethod 	= INCREMENTAL;
	int kNNedgeTestCase1K = 1;
	knnTestCaseData caseData = NULL;
	double data1[1] = {2}, queryData[1] = {0};
	SPPoint p1;
	SPKDArray kdArr = NULL;
	spCalloc(caseData, knn_test_case_data, 1);

	kNNedgeTestCase1Points = (SPPoint*)calloc(sizeof(SPPoint),kNNedgeTestCase1Size);
	if (kNNedgeTestCase1Points == NULL){
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	caseData->points = kNNedgeTestCase1Points;
	caseData->size = kNNedgeTestCase1Size;

	p1 = spPointCreate(data1,kNNedgeTestCase1MaxDim,1);
	if (p1 == NULL){
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	kNNedgeTestCase1Points[0] = p1;


	kNNedgeTestCase1QueryPoint = spPointCreate(queryData,kNNedgeTestCase1MaxDim,2);
	if (kNNedgeTestCase1QueryPoint == NULL){
		spPointDestroy(p1);
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	caseData->queryPoint = kNNedgeTestCase1QueryPoint;

	kNNedgeTestCase1Queue = spBPQueueCreate(kNNedgeTestCase1K);
	if (kNNedgeTestCase1Queue == NULL){
		spPointDestroy(p1);
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	caseData->queue = kNNedgeTestCase1Queue;


	kdArr = Init(kNNedgeTestCase1Points,kNNedgeTestCase1Size);
	kNNedgeTestCase1Tree = InitKDTree(kdArr, kNNedgeTestCase1SplitMethod);
	spKDArrayDestroy(kdArr);

	caseData->k = kNNedgeTestCase1K;
	caseData->split_method = kNNedgeTestCase1SplitMethod;
	caseData->tree = kNNedgeTestCase1Tree;

	return caseData;
}
// tester for spBPQueueCreate
static bool bpqCreateTest() {
	SPBPQueue source;

	source = spBPQueueCreate(maxSize);
	ASSERT_TRUE(source != NULL);
	spBPQueueDestroy(source);

	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;
}
Beispiel #9
0
SPBPQueue spBPQueueCopy(SPBPQueue source){
	assert(source);
	SPBPQueue copy = spBPQueueCreate(source->bound);
	if (copy == NULL){
		return NULL;
	}
	spListDestroy(copy->list);
	copy->list = spListCopy(source->list);
	return copy;
}
// create queue of given size, from given elements
static SPBPQueue quickQ(int size, ...) {
	SPBPQueue source;
	va_list items;
	source = spBPQueueCreate(maxSize);

	va_start(items, size);
	for (int i = 0; i < size; i++) {
		spBPQueueEnqueue(source, va_arg(items, SPListElement));
	}
	va_end(items);
	return source;
}
Beispiel #11
0
bool initializeWorkingImageKDTreeAndBPQueue(const SPConfig config,
		SPImageData* imagesDataList, SPImageData* currentImageData, SPKDTreeNode* kdTree,
		SPBPQueue* bpq, int numOfImages) {
	SP_CONFIG_MSG configMessage = SP_CONFIG_SUCCESS;
	int totalNumOfFeatures, knn;
	SPPoint* allFeaturesArray;
	SP_KDTREE_SPLIT_METHOD splitMethod;

	spVal(spImagesParserStartParsingProcess(config, imagesDataList) == SP_DP_SUCCESS,
			ERROR_PARSING_IMAGES_DATA, false);

	spLoggerSafePrintDebug(DEBUG_IMAGES_PARSER_FINISHED, __FILE__, __FUNCTION__, __LINE__);

	spVal((*currentImageData = initializeWorkingImage()), ERROR_INITIALIZING_QUERY_IMAGE,
			false);

	spLoggerSafePrintDebug(DEBUG_WORKING_IMAGE_INITIALIZED, __FILE__, __FUNCTION__,
			__LINE__);

	totalNumOfFeatures = calculateTotalNumOfFeatures(imagesDataList, numOfImages);

	spLoggerSafePrintDebug(DEBUG_NUMBER_OF_FEATURES_CALCULATED, __FILE__, __FUNCTION__,
			__LINE__);

	spVal((allFeaturesArray = initializeAllFeaturesArray(imagesDataList, numOfImages,
			totalNumOfFeatures)), ERROR_CREATING_FEATURES_ARRAY, false);

	spLoggerSafePrintDebug(DEBUG_FEATURES_ARRAY_INITIALIZED, __FILE__, __FUNCTION__,
			__LINE__);

	splitMethod = spConfigGetSplitMethod(config, &configMessage);
	spValWc(configMessage == SP_CONFIG_SUCCESS, ERROR_READING_SETTINGS,
			free(allFeaturesArray), false);

	spValWc((*kdTree = InitKDTreeFromPoints(allFeaturesArray, totalNumOfFeatures,
			splitMethod)), ERROR_CREATING_KD_TREE, free(allFeaturesArray), false);

	spLoggerSafePrintDebug(DEBUG_KD_TREE_INITIALIZED, __FILE__, __FUNCTION__, __LINE__);

	free(allFeaturesArray);

	knn = spConfigGetKNN(config, &configMessage);

	spVal(configMessage == SP_CONFIG_SUCCESS, ERROR_READING_SETTINGS, false);

	spVal((*bpq = spBPQueueCreate(knn)), ERROR_INITIALIZING_BP_QUEUE, false);

	spLoggerSafePrintDebug(DEBUG_PRIORITY_QUEUE_INITIALIZED, __FILE__, __FUNCTION__,
			__LINE__);

	return true;
}
//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;
}
Beispiel #13
0
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 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;
}
Beispiel #15
0
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 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;
}
//test case 2
knnTestCaseData initializeKnnTestCase2(){
	//test case 2 data
	SPKDTreeNode kNNtestCase2Tree = NULL;
	SPBPQueue kNNtestCase2Queue = NULL;
	SPPoint kNNtestCase2QueryPoint = NULL;
	SPPoint* kNNtestCase2Points = NULL;
	int kNNtestCase2MaxDim = 2;
	int kNNtestCase2Size = 5;
	SP_KDTREE_SPLIT_METHOD kNNtestCase2SplitMethod = INCREMENTAL;
	int kNNtestCase2K = 3;
	knnTestCaseData caseData = NULL;
	double data1[2] = {1,2},data2[2] = {123,70},data3[2] = {2,7},data4[2] = {9,11},
			data5[2] = {3,4}, queryData[2] = {0,0};
	SPPoint p1,p2,p3,p4,p5;
	SPKDArray kdArr = NULL;

	spCalloc(caseData, knn_test_case_data, 1);

	kNNtestCase2Points = (SPPoint*)calloc(sizeof(SPPoint),kNNtestCase2Size);
	if (kNNtestCase2Points == NULL){
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	caseData->points = kNNtestCase2Points;
	caseData->size = kNNtestCase2Size;

	p1 = spPointCreate(data1,kNNtestCase2MaxDim,1);
	if (p1 == NULL){
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	p2 = spPointCreate(data2,kNNtestCase2MaxDim,2);
	if (p2 == NULL){
		spPointDestroy(p1);
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	p3 = spPointCreate(data3,kNNtestCase2MaxDim,3);
	if (p3 == NULL){
		spPointDestroy(p1);
		spPointDestroy(p2);
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	p4 = spPointCreate(data4,kNNtestCase2MaxDim,4);
	if (p4 == NULL){
		spPointDestroy(p1);
		spPointDestroy(p2);
		spPointDestroy(p3);
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	p5 = spPointCreate(data5,kNNtestCase2MaxDim,5);
	if (p5 == NULL){
		spPointDestroy(p1);
		spPointDestroy(p2);
		spPointDestroy(p3);
		spPointDestroy(p4);
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	kNNtestCase2Points[0] = p1;
	kNNtestCase2Points[1] = p2;
	kNNtestCase2Points[2] = p3;
	kNNtestCase2Points[3] = p4;
	kNNtestCase2Points[4] = p5;

	kNNtestCase2QueryPoint= spPointCreate(queryData,kNNtestCase2MaxDim,6);
	if (kNNtestCase2QueryPoint == NULL){
		spPointDestroy(p1);
		spPointDestroy(p2);
		spPointDestroy(p3);
		spPointDestroy(p4);
		spPointDestroy(p5);
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	caseData->queryPoint = kNNtestCase2QueryPoint;

	kNNtestCase2Queue = spBPQueueCreate(kNNtestCase2K);
	if (kNNtestCase2Queue == NULL){
		spPointDestroy(p1);
		spPointDestroy(p2);
		spPointDestroy(p3);
		spPointDestroy(p4);
		spPointDestroy(p5);
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	caseData->queue = kNNtestCase2Queue;

	kdArr = Init(kNNtestCase2Points,kNNtestCase2Size);
	kNNtestCase2Tree = InitKDTree(kdArr, kNNtestCase2SplitMethod);
	spKDArrayDestroy(kdArr);

	caseData->k = kNNtestCase2K;
	caseData->split_method = kNNtestCase2SplitMethod;
	caseData->tree = kNNtestCase2Tree;

	return caseData;
}
//test case 1
knnTestCaseData initializeKnnTestCase1(){
	//test case 1 data
	SPKDTreeNode kNNtestCase1Tree 	= NULL;
	SPBPQueue kNNtestCase1QueueKNN = NULL;
	SPPoint kNNtestCase1QueryPoint	= NULL;
	SPPoint* kNNtestCase1Points = NULL;
	int kNNtestCase1MaxDim = 3;
	int kNNtestCase1Size = 2;
	SP_KDTREE_SPLIT_METHOD kNNtestCase1SplitMethod = MAX_SPREAD;
	int kNNtestCase1K = 1;
	knnTestCaseData caseData = NULL;
	double data1[3] = {1,2,3}, data2[3] = {5,-7,13}, queryData[3] = {0,0,0};
	SPPoint p1,p2;
	SPKDArray kdArr = NULL;

	spCalloc(caseData, knn_test_case_data, 1);

	kNNtestCase1Points = (SPPoint*)calloc(sizeof(SPPoint),kNNtestCase1Size);
	if (kNNtestCase1Points == NULL){
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	caseData->points = kNNtestCase1Points;
	caseData->size = kNNtestCase1Size;

	p1 = spPointCreate(data1,kNNtestCase1MaxDim,1);
	if (p1 == NULL){
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	p2 = spPointCreate(data2,kNNtestCase1MaxDim,2);
	if (p2 == NULL){
		spPointDestroy(p1);
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}

	kNNtestCase1Points[0] = p1;
	kNNtestCase1Points[1] = p2;

	kNNtestCase1QueryPoint = spPointCreate(queryData, kNNtestCase1MaxDim, 3);
	if (kNNtestCase1QueryPoint == NULL){
		spPointDestroy(p1);
		spPointDestroy(p2);
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	caseData->queryPoint = kNNtestCase1QueryPoint;


	kNNtestCase1QueueKNN = spBPQueueCreate(kNNtestCase1K);
	if (kNNtestCase1QueueKNN == NULL){
		spPointDestroy(p1);
		spPointDestroy(p2);
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	caseData->queue = kNNtestCase1QueueKNN;

	kdArr = Init(kNNtestCase1Points,kNNtestCase1Size);
	kNNtestCase1Tree = InitKDTree(kdArr, kNNtestCase1SplitMethod);
	spKDArrayDestroy(kdArr);

	caseData->k = kNNtestCase1K;
	caseData->split_method = kNNtestCase1SplitMethod;
	caseData->tree = kNNtestCase1Tree;

	return caseData;
}
//random tests
bool runRandomKnnTest(){
	int maxDim, size, k;
	bool successFlag = true;
	SPPoint* pointsArray = NULL;
	SPPoint queryPoint = NULL;
	SPBPQueue queue = NULL;
	SPKDArray kdArr = NULL;
	SPKDTreeNode tree = NULL;
	SP_KDTREE_SPLIT_METHOD splitMethod;

	maxDim = 1 + (int)(rand() % RANDOM_TESTS_DIM_RANGE);
	size = 2 + (int)(rand() % RANDOM_TESTS_SIZE_RANGE); //size = 1 is tested at a the edge cases
	k = (int)(rand() % size);
	splitMethod = (int)(rand()%3);

	pointsArray = generateRandomPointsArray(maxDim,size);
	queryPoint = generateRandomPoint(maxDim, size+1);

	queue = spBPQueueCreate(k);

	if (queue == NULL){
		destroyCaseData(tree,kdArr,pointsArray,size,queue,queryPoint);

		spLoggerSafePrintError(COULD_NOT_INITIALIZE_QUERY_POINT,
				__FILE__, __FUNCTION__,
						__LINE__);
		FAIL(COULD_NOT_INITIALIZE_QUERY_POINT);
		return false;
	}

	if (queryPoint == NULL){
		destroyCaseData(tree,kdArr,pointsArray,size,queue,queryPoint);

		spLoggerSafePrintError(COULD_NOT_INITIALIZE_QUERY_POINT,
				__FILE__, __FUNCTION__,
						__LINE__);
		FAIL(COULD_NOT_INITIALIZE_QUERY_POINT);
		return false;
	}

	if (pointsArray == NULL){
		destroyCaseData(tree,kdArr,pointsArray,size,queue,queryPoint);
		spLoggerSafePrintError(COULD_NOT_CREATE_POINTS_ARRAY,
				__FILE__, __FUNCTION__,
						__LINE__);
		FAIL(COULD_NOT_CREATE_POINTS_ARRAY);
		return false;
	}

	kdArr = Init(pointsArray,size);

	if (kdArr == NULL){
		destroyCaseData(tree,kdArr,pointsArray,size,queue,queryPoint);
		spLoggerSafePrintError(COULD_NOT_INITIALIZE_KD_ARRAY,
				__FILE__, __FUNCTION__,
						__LINE__);
		FAIL(COULD_NOT_INITIALIZE_KD_ARRAY);
		return false;
	}

	tree = InitKDTree(kdArr, splitMethod);

	if (tree == NULL){
		destroyCaseData(tree,kdArr,pointsArray,size,queue,queryPoint);
		spLoggerSafePrintError(
				COULD_NOT_INITIALIZE_TREE,
				__FILE__, __FUNCTION__,
						__LINE__);
		FAIL(COULD_NOT_INITIALIZE_TREE);
		return false;
	}


	successFlag = kNearestNeighbors(tree, queue, queryPoint);

	if (!successFlag){
		destroyCaseData(tree,kdArr,pointsArray,size,queue,queryPoint);
		return false;
	}

	successFlag = verifyKNN(queue, k, pointsArray,queryPoint,size);
	destroyCaseData(tree,kdArr,pointsArray,size,queue,queryPoint);

	return successFlag;
}
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;
}