Skip to content

Commit

Permalink
tests (#54)
Browse files Browse the repository at this point in the history
Co-authored-by: annegog <annegog>
  • Loading branch information
annegog authored Jan 14, 2024
1 parent d9f48f1 commit 25adbdb
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 63 deletions.
17 changes: 12 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ CFLAGS = -Wall -Werror -g -I$(INCLUDE)
LDFLAGS = -lm

# .o files
OBJS = $(MAIN)/main.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODULES)/Dimension.o $(MODULES)/MathematicalFunctions.o $(MODULES)/FindAllRightNeighborsAlgorithm.o $(MODULES)/knn.o $(MODULES)/knn_improvements.o $(MODULES)/Cost.o
OBJS = $(MAIN)/main.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODULES)/Dimension.o $(MODULES)/MathematicalFunctions.o $(MODULES)/FindAllRightNeighborsAlgorithm.o $(MODULES)/knn.o $(MODULES)/knn_improvements.o $(MODULES)/Cost.o $(MODULES)/projection_tree.o

# Test objects
OBJSKNN_IMP = $(TESTS)/knn_improvements_test.o $(MODULES)/Cost.o $(MODULES)/knn_improvements.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODULES)/Dimension.o $(MODULES)/MathematicalFunctions.o $(MODULES)/knn.o
OBJSKNN_IMP = $(TESTS)/knn_improvements_test.o $(MODULES)/Cost.o $(MODULES)/knn_improvements.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODULES)/Dimension.o $(MODULES)/MathematicalFunctions.o $(MODULES)/knn.o $(MODULES)/projection_trees.o
OBJSKNN = $(TESTS)/knn_test.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODULES)/Dimension.o $(MODULES)/MathematicalFunctions.o $(MODULES)/FindAllRightNeighborsAlgorithm.o $(MODULES)/knn.o
OBJSG = $(TESTS)/graph_test.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODULES)/Dimension.o $(MODULES)/MathematicalFunctions.o $(MODULES)/knn.o
OBJSN = $(TESTS)/nodes_test.o $(MODULES)/Node.o $(MODULES)/Dimension.o $(MODULES)/Neighbors.o $(MODULES)/MathematicalFunctions.o
OBJSD = $(TESTS)/dimension_test.o $(MODULES)/Node.o $(MODULES)/Dimension.o $(MODULES)/Neighbors.o $(MODULES)/MathematicalFunctions.o
OBJSNEI = $(TESTS)/neighbours_test.o $(MODULES)/Neighbors.o $(MODULES)/Node.o $(MODULES)/Dimension.o $(MODULES)/MathematicalFunctions.o
OBJSMATH = $(TESTS)/mathematical_test.o $(MODULES)/MathematicalFunctions.o $(MODULES)/Dimension.o
OBJSTREE = $(TESTS)/projection_trees_tests.o $(MODULES)/projection_trees.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODULES)/Dimension.o $(MODULES)/MathematicalFunctions.o $(MODULES)/knn.o

# Executables
EXEC = program
Expand All @@ -36,6 +37,7 @@ EXECN = nodes_test
EXECD = dimension_test
EXECNEI = neighbours_test
EXECMATH = mathFunctions_test
EXECTREES = projection_trees_tests

# Args for examples
ARGSEUCL = $(DATASETS)/00001000-4.bin 100 399 euclidean 0.2 0.001
Expand Down Expand Up @@ -68,15 +70,17 @@ runNEI: $(EXECNEI)
runMath: $(EXECMATH)
./$(EXECMATH)

runTrees: $(EXECTREES)
./$(EXECTREES)

# for every .c file
all: run-euclidean run-manhattan runKNN_imp runKNN runG runN runD runNEI runMath
all: run-euclidean run-manhattan runKNN_imp runKNN runG runN runD runNEI runMath runTrees

# for 2 "main"
main-all: run-euclidean run-manhattan

# for all tests
run-all-tests: runKNN_imp runKNN runG runN runD runNEI runMath
run-all-tests: runKNN_imp runKNN runG runN runD runNEI runMath runTrees

$(EXEC): $(OBJS)
$(CC) $(OBJS) -o $(EXEC) $(LDFLAGS)
Expand All @@ -103,6 +107,9 @@ $(EXECNEI): $(OBJSNEI)
$(EXECMATH): $(OBJSMATH)
$(CC) $(OBJSMATH) -o $(EXECMATH) $(LDFLAGS)

$(EXECTREES): $(OBJSTREE)
$(CC) $(OBJSTREE) -o $(EXECTREES) $(LDFLAGS)

clean:
rm -f $(OBJS) $(EXEC) $(OBJSG) $(OBJSN) $(OBJSD) $(OBJSKNN) $(OBJSMATH) $(OBJSNEI) $(EXECG) $(EXECN) $(EXECD) $(EXECKNN) $(EXECNEI) $(EXECMATH) $(OBJSKNNIMP) $(EXECMATH) $(EXECKNN_IMP) $(OBJSKNN_IMP)
rm -f $(OBJS) $(EXEC) $(OBJSG) $(OBJSN) $(OBJSD) $(OBJSKNN) $(OBJSMATH) $(OBJSNEI) $(EXECG) $(EXECN) $(EXECD) $(EXECKNN) $(EXECNEI) $(EXECMATH) $(OBJSKNNIMP) $(EXECMATH) $(EXECKNN_IMP) $(OBJSKNN_IMP) $(EXECTREES) $(OBJSTREE)
rm *.txt
4 changes: 0 additions & 4 deletions include/projection_trees.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,4 @@ void remainingRandomNodes(Graph** graph, Node* currentNode, int* usedNumbers, in

void freeTree(TreeNode* root);

void freeNodeProj(Node* node);

void freeDimensionProj(Dimension* dimension);

#endif
24 changes: 0 additions & 24 deletions modules/projection_trees.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,27 +238,3 @@ void freeTree(TreeNode *root) {
free(root);
}
}


// void freeNodeProj(Node* node) {
// while (node != NULL) {
// Node* temp = node;
// node = node->next;

// // Free the memory associated with the dimension linked list
// // freeDimensionProj(temp->dimension);

// // Free any other memory associated with the node (e.g., cost, neighbors, etc.)
// // Add additional free calls if needed

// free(temp);
// }
// }

// void freeDimensionProj(Dimension* dimension) {
// while (dimension != NULL) {
// Dimension* temp = dimension;
// dimension = dimension->next;
// free(temp);
// }
// }
18 changes: 15 additions & 3 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ CFLAGS = -Wall -Werror -g -I$(INCLUDE)
LDFLAGS = -lm

# .o files
OBJSKNN_IMP = $(TESTS)/knn_improvements_test.o $(MODULES)/Cost.o $(MODULES)/knn_improvements.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODULES)/Dimension.o $(MODULES)/MathematicalFunctions.o $(MODULES)/knn.o
OBJSKNN_IMP = $(TESTS)/knn_improvements_test.o $(MODULES)/Cost.o $(MODULES)/knn_improvements.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODULES)/Dimension.o $(MODULES)/MathematicalFunctions.o $(MODULES)/knn.o $(MODULES)/projection_trees.o

OBJSKNN = $(TESTS)/knn_test.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODULES)/Dimension.o $(MODULES)/MathematicalFunctions.o $(MODULES)/knn.o

Expand All @@ -25,6 +25,7 @@ OBJSNEI = $(TESTS)/neighbours_test.o $(MODULES)/Neighbors.o $(MODULES)/Node.o $(

OBJSMATH = $(TESTS)/mathematical_test.o $(MODULES)/MathematicalFunctions.o $(MODULES)/Dimension.o

OBJSTREE = $(TESTS)/projection_trees_tests.o $(MODULES)/projection_trees.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODULES)/Dimension.o $(MODULES)/MathematicalFunctions.o $(MODULES)/knn.o

OBJSK = $(TESTS)/knn_test.o $(MODULES)/Graph.o $(MODULES)/Node.o $(MODULES)/Neighbors.o $(MODULES)/Dimension.o $(MODULES)/MathematicalFunctions.o $(MODULES)/FindAllRightNeighborsAlgorithm.o $(MODULES)/knn.o

Expand All @@ -43,6 +44,8 @@ EXECNEI = neighbours_test

EXECMATH = mathFunctions_test

EXECTREES = projection_trees_tests

$(EXECKNN_IMP): $(OBJSKNN_IMP)
$(CC) $(OBJSKNN_IMP) -o $(EXECKNN_IMP) $(LDFLAGS)

Expand All @@ -64,8 +67,11 @@ $(EXECNEI): $(OBJSNEI)
$(EXECMATH): $(OBJSMATH)
$(CC) $(OBJSMATH) -o $(EXECMATH) $(LDFLAGS)

$(EXECTREES): $(OBJSTREE)
$(CC) $(OBJSTREE) -o $(EXECTREES) $(LDFLAGS)

clean:
rm -f $(OBJSG) $(OBJSN) $(OBJSD) $(OBJSKNN) $(OBJSKNN_IMP) $(OBJSMATH) $(OBJSNEI) $(EXECG) $(EXECN) $(EXECD) $(EXECKNN) $(EXECNEI) $(EXECMATH) $(EXECKNN_IMP)
rm -f $(OBJSG) $(OBJSN) $(OBJSD) $(OBJSKNN) $(OBJSKNN_IMP) $(OBJSMATH) $(OBJSNEI) $(EXECG) $(EXECN) $(EXECD) $(EXECKNN) $(EXECNEI) $(EXECMATH) $(EXECKNN_IMP) $(EXECTREES) $(OBJSTREE)

runKNN_imp: $(EXECKNN_IMP)
./$(EXECKNN_IMP)
Expand All @@ -88,7 +94,10 @@ runNEI: $(EXECNEI)
runMath: $(EXECMATH)
./$(EXECMATH)

run-all: runMath runG runN runD runNEI runKNN runKNN_imp
runTrees: $(EXECTREES)
./$(EXECTREES)

run-all: runMath runG runN runD runNEI runKNN runKNN_imp runTrees

valgrind-graph: $(EXECG)
valgrind --error-exitcode=1 --leak-check=full --show-leak-kinds=all ./$(EXECG)
Expand All @@ -107,3 +116,6 @@ valgrind-knn: $(EXECKNN)

valgrind-knn-imp: $(EXECKNN_IMP)
valgrind --error-exitcode=1 --leak-check=full --show-leak-kinds=all ./$(EXECKNN_IMP)

valgrind-trees: $(EXECTREES)
valgrind --error-exitcode=1 --leak-check=full --show-leak-kinds=all ./$(EXECTREES)
6 changes: 3 additions & 3 deletions tests/knn_improvements_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ void testIncrementalSearchFailScenario3() {

void testSampling() {
// Create a list of neighbors
Node node1 = {1, NULL, NULL, NULL, NULL};
Node node2 = {2, NULL, NULL, NULL, NULL};
Node node3 = {3, NULL, NULL, NULL, NULL};
Node node1 = {1, 0.0, NULL, NULL, NULL};
Node node2 = {2, 0.0, NULL, NULL, NULL};
Node node3 = {3, 0.0, NULL, NULL, NULL};

NodeNeighborsLinkedList neighbor1 = {&node1, 1.0, 1, NULL};
NodeNeighborsLinkedList neighbor2 = {&node2, 2.0, 0, NULL};
Expand Down
9 changes: 4 additions & 5 deletions tests/knn_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ void testCheck(){
head1->next = head2;
head2->next = NULL;

TEST_ASSERT(check(0, head1, 0, 1.5) == 1); //source's and neighbor's name are the same
TEST_ASSERT(check(1, head1, 0, 1.5) == 1); //neighbor's name already in the head
TEST_ASSERT(check(2, head1, 2, 1.5) == 1); //source's and neighbor's name are the same and neighbor's name already in the head
TEST_ASSERT(check(node1->nodeNameInt, head1, node1, 1.5) == 1); //source's and neighbor's name are the same
TEST_ASSERT(check(node2->nodeNameInt, head1, node2, 1.5) == 1); //neighbor's name already in the head
TEST_ASSERT(check(node2->nodeNameInt, head1, node2, 1.5) == 1); //source's and neighbor's name are the same and neighbor's name already in the head

TEST_ASSERT(check(3, head1, 0, 2.5) == 1); //source's and neighbor's name are the different and neighbor's name not in the head but cost is higher
TEST_ASSERT(check(3, head1, 0, 1.5) == 0); //source's and neighbor's name are the different and neighbor's name not in the head and cost is lower
TEST_ASSERT(check(3, head1, node1, 2.5) == 1); //source's and neighbor's name are different, and neighbor's name not in the head, but cost is higher

free(node1);
free(node2);
Expand Down
19 changes: 0 additions & 19 deletions tests/mathematical_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,10 @@ void testManhattanDistance2() {
freeDimensions(dimension2);
}


void testDistanceFunc() {
Dimension *dimension1 = NULL;
addDimension(&dimension1, 1, 1.000);

Dimension *dimension2 = NULL;
addDimension(&dimension2, 1, 0.500);

double cost = distance(dimension1, dimension2, "manhattan");
TEST_ASSERT(cost == 0.5);

cost = distance(dimension1, dimension2, "euclidean");
TEST_ASSERT(cost == 0.5);

freeDimensions(dimension1);
freeDimensions(dimension2);
}

TEST_LIST = {
{"testEuclideanDistance", testEuclideanDistance},
{"testEuclideanDistance2", testEuclideanDistance2},
{"testManhattanDistance", testManhattanDistance},
{"testManhattanDistance2", testManhattanDistance2},
{"testDistanceFunc", testDistanceFunc},
{NULL, NULL}
};
72 changes: 72 additions & 0 deletions tests/projection_trees_tests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/////////////////////////////////////////////////////////////////
//
//
// Unit tests for projection trees
//
//
//////////////////////////////////////////////////////////////////
#include "acutest.h"
#include "../include/projection_trees.h"

void testGenerateRandomProjection() {
int dimension = 10;
int* projection = generateRandomProjection(dimension);

TEST_CHECK(projection != NULL);

for (int i = 0; i < dimension; i++) {
TEST_CHECK(projection[i] == 1 || projection[i] == -1);
}

free(projection);
}

void testComputeProjection() {
Node node;
Dimension dim1 = {1, 1.0, NULL};
Dimension dim2 = {2, 2.0, NULL};
Dimension dim3 = {3, 3.0, NULL};
node.dimension = &dim1;
dim1.next = &dim2;
dim2.next = &dim3;

// Create a projection vector
int projection[3] = {2, -1, 3};

double result = computeProjection(&node, projection);
TEST_CHECK(result == 9.00);
}

void testAddData() {
Node* headData = NULL;
Node node1 = {1, 1.00, NULL, NULL, NULL};
Node node2 = {2, 2.00, NULL, NULL, NULL};
Node node3 = {3, 3.00, NULL, NULL, NULL};

addData(&headData, &node1);
addData(&headData, &node2);
addData(&headData, &node3);

// Check if the nodes are added in the correct order
TEST_CHECK(headData != NULL);
TEST_CHECK(headData->nodeNameInt == 1);
TEST_CHECK(headData->next != NULL);
TEST_CHECK(headData->next->nodeNameInt == 2);
TEST_CHECK(headData->next->next != NULL);
TEST_CHECK(headData->next->next->nodeNameInt == 3);

Node* current = headData;
while (current != NULL) {
Node* next = current->next;
free(current);
current = next;
}
}


TEST_LIST = {
{"testGenerateRandomProjection", testGenerateRandomProjection},
{"testComputeProjection", testComputeProjection},
{"testAddData", testAddData},
{NULL, NULL}
};

0 comments on commit 25adbdb

Please sign in to comment.