master
 1#include "graph.h"
 2#include <cgreen/cgreen.h>
 3#include <string.h>
 4
 5Ensure(initialize_returns_a_new_graph) {
 6  Graph *graph = graph_initialize();
 7
 8  assert_that(graph, is_not_equal_to(NULL));
 9  for (int i = 0; i < 128; ++i)
10    assert_that(graph->vertices[i], is_equal_to(NULL));
11}
12
13Ensure(add_vertex_returns_a_new_vertex) {
14  Graph *graph = graph_initialize();
15  char label = 'a';
16  Vertex *a = graph_add_vertex(graph, label);
17
18  assert_that(a, is_not_equal_to(NULL));
19  assert_that(graph->vertices[(int)label], is_equal_to(a));
20}
21
22Ensure(add_vertex_adds_max_number_of_verticies_to_graph) {
23  Graph *graph = graph_initialize();
24
25  for (int i = 0; i < 128; ++i) {
26    Vertex *item = graph_add_vertex(graph, (char)i);
27    assert_that(item, is_not_equal_to(NULL));
28    assert_that(graph->vertices[i], is_equal_to(item));
29  }
30}
31
32Ensure(add_edge_connects_two_vertices) {
33  Graph *graph = graph_initialize();
34
35  graph_add_edge(graph, graph_add_vertex(graph, 'a'),
36                 graph_add_vertex(graph, 'b'));
37
38  assert_that(graph->edges['a']['b'], is_equal_to(true));
39  assert_that(graph->edges['b']['a'], is_equal_to(false));
40}
41
42Ensure(has_edge_returns_true) {
43  Graph *graph = graph_initialize();
44  Vertex *a = graph_add_vertex(graph, 'a');
45  Vertex *b = graph_add_vertex(graph, 'b');
46
47  graph_add_edge(graph, a, b);
48
49  assert_that(graph_has_edge(graph, a, b), is_equal_to(true));
50}
51
52Ensure(has_edge_returns_false) {
53  Graph *graph = graph_initialize();
54  Vertex *a = graph_add_vertex(graph, 'a');
55  Vertex *b = graph_add_vertex(graph, 'b');
56  Vertex *c = graph_add_vertex(graph, 'c');
57
58  graph_add_edge(graph, a, b);
59
60  assert_that(graph_has_edge(graph, a, c), is_equal_to(false));
61}
62
63TestSuite *graph_tests() {
64  TestSuite *x = create_test_suite();
65
66  add_test(x, add_edge_connects_two_vertices);
67  add_test(x, add_vertex_adds_max_number_of_verticies_to_graph);
68  add_test(x, add_vertex_returns_a_new_vertex);
69  add_test(x, has_edge_returns_true);
70  add_test(x, has_edge_returns_false);
71  add_test(x, initialize_returns_a_new_graph);
72
73  return x;
74}