Commit c942a50

mo khan <mo.khan@gmail.com>
2020-09-07 21:20:53
feat: add vertex to graph
1 parent 8737272
Changed files (3)
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;
+}
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);
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;
 }