From c942a5062c9a0b853cd350f0fa3f914efc90935b Mon Sep 17 00:00:00 2001 From: mo khan Date: Mon, 7 Sep 2020 15:20:53 -0600 Subject: feat: add vertex to graph --- src/03/graph.c | 15 ++++++++++++++- src/03/graph.h | 7 ++++++- src/03/graph_test.c | 15 ++++++++++++++- 3 files changed, 34 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/03/graph.c b/src/03/graph.c index ed244ba..fbd8677 100644 --- a/src/03/graph.c +++ b/src/03/graph.c @@ -1,8 +1,21 @@ #include "graph.h" #include -Vertex *graph_initialize(char label) { +Vertex *vertex_initialize(char label) { Vertex *item = malloc(sizeof(Vertex)); item->label = label; return item; }; + +Graph *graph_initialize(void) { + Graph *item = malloc(sizeof(Graph)); + for (int i = 0; i < 256; ++i) + item->vertices[i] = NULL; + return item; +} + +Vertex *graph_add_vertex(Graph *graph, char label) { + Vertex *item = vertex_initialize(label); + graph->vertices[(int)label] = item; + return item; +} diff --git a/src/03/graph.h b/src/03/graph.h index 26fb7c6..65f01f5 100644 --- a/src/03/graph.h +++ b/src/03/graph.h @@ -2,4 +2,9 @@ typedef struct { char label; } Vertex; -Vertex *graph_initialize(char label); +typedef struct { + Vertex *vertices[256]; +} Graph; + +Graph *graph_initialize(void); +Vertex *graph_add_vertex(Graph *graph, char label); diff --git a/src/03/graph_test.c b/src/03/graph_test.c index ecc47bb..44a8715 100644 --- a/src/03/graph_test.c +++ b/src/03/graph_test.c @@ -7,9 +7,18 @@ Ensure(three_equals_three) { } Ensure(initialize_returns_a_new_vertex) { - Vertex *a = graph_initialize('a'); + Graph *graph = graph_initialize(); + + assert_that(graph, is_not_equal_to(NULL)); +} + +Ensure(add_vertex_returns_a_new_vertex) { + Graph *graph = graph_initialize(); + char label = 'a'; + Vertex *a = graph_add_vertex(graph, label); assert_that(a, is_not_equal_to(NULL)); + assert_that(graph->vertices[(int)label], is_equal_to(a)); } TestSuite *graph_tests() { @@ -17,5 +26,9 @@ TestSuite *graph_tests() { add_test(x, three_equals_three); + add_test(x, initialize_returns_a_new_vertex); + + add_test(x, add_vertex_returns_a_new_vertex); + return x; } -- cgit v1.2.3