From ae5767b73231bbc6e8b441203f577484bc0d7566 Mon Sep 17 00:00:00 2001 From: "Marco M. Mosca" Date: Sat, 29 Aug 2026 18:19:38 +0100 Subject: [PATCH] Fix copy_graph while copying edges info --- CHANGELOG | 4 ++++ CMakeLists.txt | 2 +- graphoc/src/graph.c | 24 ++++++++++++++++-------- tests/CMakeLists.txt | 3 +++ tests/test_copy_graph.c | 41 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 tests/test_copy_graph.c diff --git a/CHANGELOG b/CHANGELOG index f643e56..90f2a20 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,9 @@ All changes to the repository will be reported here +## [0.2.3] +### Fixed +- Copying graph edges in `copy_graph` + ## [0.2.2] ### Fixed - Number of nodes allocated in create_graph diff --git a/CMakeLists.txt b/CMakeLists.txt index b056659..1433527 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,7 @@ Author Marco M. Mosca, email: marcomichele.mosca@gmail.com ]] cmake_minimum_required(VERSION 3.5.0) -project(graphoc VERSION 0.2.2 LANGUAGES C) +project(graphoc VERSION 0.2.3 LANGUAGES C) include(CTest) diff --git a/graphoc/src/graph.c b/graphoc/src/graph.c index 74bd627..965ba54 100755 --- a/graphoc/src/graph.c +++ b/graphoc/src/graph.c @@ -159,7 +159,7 @@ graph_t* copy_graph (graph_t* g) graph_t* p; node_t* nodeg, *nodep; char* s; - edge_t* ag, *tail; + edge_t* ag, *tail, *prev; errno=EINVAL; if (g == NULL) {return NULL;}; @@ -176,7 +176,6 @@ graph_t* copy_graph (graph_t* g) { len = strlen((const char*) ((nodeg+i)->label)); s = malloc((len) * sizeof(char)+1); - /** labels[i]=malloc((len+1) * sizeof(char));*/ if (s == NULL) {return NULL;}; strcpy(s,(const char*) ((nodeg+i)->label)); strcpy(labels[i],s); @@ -193,14 +192,23 @@ graph_t* copy_graph (graph_t* g) while((nodeg+i)->label != NULL) { ag = (nodeg+i)->adj; + prev = (nodep+i)->adj; while(ag != NULL) { - tail=malloc(sizeof(edge_t)); - tail->label = ag ->label; - tail->weight = ag ->weight; - tail->next = (nodep+i)->adj; - (nodep+i)->adj = tail; - ag = ag->next; + if ( (tail=malloc(sizeof(edge_t))) == NULL ) { + errno=ENOMEM; + return NULL; + } + tail->label = ag->label; + tail->weight = ag->weight; + tail->next = NULL; + if ( prev == NULL ) { + (nodep+i)->adj = tail; + } else { + prev->next = tail; + } + prev = tail; + ag = ag->next; } i++; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 167db91..ae97d1d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -25,6 +25,7 @@ add_executable (test_get_degree ${CMAKE_CURRENT_SOURCE_DIR}/test_get_degree.c) add_executable (test_check_node_edge ${CMAKE_CURRENT_SOURCE_DIR}/test_check_node_edge.c) add_executable (test_save_graph ${CMAKE_CURRENT_SOURCE_DIR}/test_save_graph.c) add_executable (test_dijkstra ${CMAKE_CURRENT_SOURCE_DIR}/test_dijkstra.c) +add_executable (test_copy_graph ${CMAKE_CURRENT_SOURCE_DIR}/test_copy_graph.c) target_link_libraries (test_create_graph PRIVATE graphoc) target_link_libraries (test_get_node_count PRIVATE graphoc) target_link_libraries (test_get_edge_count PRIVATE graphoc) @@ -33,6 +34,7 @@ target_link_libraries (test_get_degree PRIVATE graphoc) target_link_libraries (test_check_node_edge PRIVATE graphoc) target_link_libraries (test_save_graph PRIVATE graphoc) target_link_libraries (test_dijkstra PRIVATE graphoc) +target_link_libraries (test_copy_graph PRIVATE graphoc) add_test (NAME test_create_graph COMMAND test_create_graph) add_test (NAME test_get_node_count COMMAND test_get_node_count) add_test (NAME test_get_edge_count COMMAND test_get_edge_count) @@ -41,6 +43,7 @@ add_test (NAME test_get_degree COMMAND test_get_degree) add_test (NAME test_check_node_edge COMMAND test_check_node_edge) add_test (NAME test_save_graph COMMAND test_save_graph) add_test (NAME test_dijkstra COMMAND test_dijkstra) +add_test (NAME test_copy_graph COMMAND test_copy_graph) add_executable (test_create_graph_nolabels ${CMAKE_CURRENT_SOURCE_DIR}/test_create_graph_nolabels.c) add_executable (test_create_graph_zerosize ${CMAKE_CURRENT_SOURCE_DIR}/test_create_graph_zerosize.c) diff --git a/tests/test_copy_graph.c b/tests/test_copy_graph.c new file mode 100644 index 0000000..cee04e7 --- /dev/null +++ b/tests/test_copy_graph.c @@ -0,0 +1,41 @@ +#include "graph.h" +#include "string.h" +#include "assert.h" +#include "tests.h" + +void test_copy_graph() +{ + graph_t *graph, *cpy_graph; + unsigned int edge_count, exp_node_count = 5, exp_edge_count = 6; + if ( ( graph = create_graph(5, labels) ) == NULL ) { + perror("test_copy_graph - create graph nodes"); + exit(EXIT_FAILURE); + } + for(char** e=edges; *e!=NULL; e++){ + if ( add_edge(graph,*e) == -1 ) { + perror("test_copy_graph - add edges"); + exit(EXIT_FAILURE); + } + } + + if ( ( cpy_graph = copy_graph(graph) ) == NULL ) { + perror("test_copy_graph - copy graph"); + exit(EXIT_FAILURE); + } + if ( ( edge_count = get_edge_count(cpy_graph) ) == -1 ) { + perror("test_copy_graph - edge count"); + exit(EXIT_FAILURE); + } + assert(cpy_graph->size == exp_node_count); + assert(edge_count == exp_edge_count); + free_graph(&graph); + free_graph(&cpy_graph); + assert(graph == NULL); + assert(cpy_graph == NULL); +} + +int main() +{ + test_copy_graph(); + return 0; +}