Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
24 changes: 16 additions & 8 deletions graphoc/src/graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;};
Expand All @@ -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);
Expand All @@ -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++;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
41 changes: 41 additions & 0 deletions tests/test_copy_graph.c
Original file line number Diff line number Diff line change
@@ -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;
}
Loading