From ec218a9222552e6c156a867d93fdddcd6c20bf9f Mon Sep 17 00:00:00 2001 From: annegog Date: Sun, 14 Jan 2024 17:45:22 +0200 Subject: [PATCH] tests --- Makefile | 17 +++++--- include/projection_trees.h | 4 -- modules/projection_trees.c | 24 ------------ tests/Makefile | 18 +++++++-- tests/knn_improvements_test.c | 6 +-- tests/knn_test.c | 9 ++--- tests/mathematical_test.c | 19 --------- tests/projection_trees_tests.c | 72 ++++++++++++++++++++++++++++++++++ 8 files changed, 106 insertions(+), 63 deletions(-) create mode 100644 tests/projection_trees_tests.c diff --git a/Makefile b/Makefile index 4edc583..5ce72b8 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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 @@ -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) @@ -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 \ No newline at end of file diff --git a/include/projection_trees.h b/include/projection_trees.h index 5b140dc..4cf57b9 100644 --- a/include/projection_trees.h +++ b/include/projection_trees.h @@ -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 diff --git a/modules/projection_trees.c b/modules/projection_trees.c index 197da47..d917ce1 100644 --- a/modules/projection_trees.c +++ b/modules/projection_trees.c @@ -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); -// } -// } diff --git a/tests/Makefile b/tests/Makefile index 5bf716a..ecc5599 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -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 @@ -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 @@ -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) @@ -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) @@ -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) @@ -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) \ No newline at end of file diff --git a/tests/knn_improvements_test.c b/tests/knn_improvements_test.c index 9aad000..4e8304f 100644 --- a/tests/knn_improvements_test.c +++ b/tests/knn_improvements_test.c @@ -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}; diff --git a/tests/knn_test.c b/tests/knn_test.c index 52f4c05..6070611 100644 --- a/tests/knn_test.c +++ b/tests/knn_test.c @@ -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); diff --git a/tests/mathematical_test.c b/tests/mathematical_test.c index 7623c2d..625718c 100644 --- a/tests/mathematical_test.c +++ b/tests/mathematical_test.c @@ -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} }; diff --git a/tests/projection_trees_tests.c b/tests/projection_trees_tests.c new file mode 100644 index 0000000..e3e34ac --- /dev/null +++ b/tests/projection_trees_tests.c @@ -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} +};