summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-09-07 15:20:53 -0600
committermo khan <mo.khan@gmail.com>2020-09-07 15:20:53 -0600
commitc942a5062c9a0b853cd350f0fa3f914efc90935b (patch)
treef932d12627564f41b27d2820131cb2577ac6c25f /src
parent8737272f80feb3d3c477e984568790008f2d6bc0 (diff)
feat: add vertex to graph
Diffstat (limited to 'src')
-rw-r--r--src/03/graph.c15
-rw-r--r--src/03/graph.h7
-rw-r--r--src/03/graph_test.c15
3 files changed, 34 insertions, 3 deletions
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 <stdlib.h>
-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;
}