-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathadjacency_list_graph_test.rb
More file actions
137 lines (113 loc) · 4.63 KB
/
adjacency_list_graph_test.rb
File metadata and controls
137 lines (113 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
require './test/test_helper'
require 'json'
class AdjacencyListGraphTest < Minitest::Test
include VisualGraphs
def setup
@filepath = 'test/resources/test_data.json'
@output_file_path = 'test/resources/output_test_data.json'
File.delete @output_file_path if File.exist? @output_file_path
end
def test_json_file_created_when_graph_was_dumped_to_json
graph = Graph.load_from_json(@filepath)
graph.dump_to_json(@output_file_path)
assert_equal true, File.exist?(@output_file_path), 'json file was not created'
end
def test_dumped_graph_in_json_file_is_correct
graph = Graph.load_from_json(@filepath)
graph.dump_to_json(@output_file_path)
init_file = File.open(@filepath)
dumped_file = File.open(@output_file_path)
initial_graph_json = JSON.load(init_file)
dumped_graph_json = JSON.load(dumped_file)
init_file.close
dumped_file.close
assert_equal initial_graph_json, dumped_graph_json, 'dump_to_json is not working'
end
def test_incorrect_json_file_name_raise_exception
assert_raises InvalidJSONFileNameError do
graph = Graph.load_from_json(@filepath)
graph.dump_to_json('test/incorrect')
end
end
def test_correct_vertices_adjacency_list_initialization
hash_graph = Graph.adjacency_list_init({1 => [2], 2 => [3], 3 => [1]})
assert_equal [1, 2, 3], hash_graph.vertices, 'vertices arrays do not match'
end
def test_correct_edges_adjacency_list_initialization
hash_graph = Graph.adjacency_list_init({1 => [2], 2 => [3], 3 => [1]})
assert_equal [[1, 2], [2, 3], [3, 1]], hash_graph.edges, 'edges arrays do not match'
end
def test_adding_edge_will_add_edge_to_adjacency_list
graph = Graph.load_from_json(@filepath)
graph.insert_edge([1, 5])
assert_equal true, graph.instance_variable_get(:@adjacency_list)[1].include?(5), 'vertex was not added to adjacency_list'
end
def test_existing_edge_will_not_be_inserted
graph = Graph.load_from_json(@filepath)
graph.insert_edge([1, 2])
assert_equal 1, graph.edges.count([1, 2])
end
def test_non_existent_edge_will_be_inserted
graph = Graph.load_from_json(@filepath)
graph.insert_edge([1,4])
assert_equal true, graph.edges.include?([1, 4]), 'new edge was not added'
end
def test_existing_vertice_will_not_be_inserted
graph = Graph.load_from_json(@filepath)
graph.insert_vertex(1)
assert_equal 1, graph.vertices.count(1), 'existing vertex has been added twice'
end
def test_non_existing_vertice_will_be_inserted
graph = Graph.load_from_json(@filepath)
graph.insert_vertex(10)
assert_equal true,graph.vertices.include?(10), 'vertex was not added'
end
def test_non_existent_vertice_will_be_added_while_inserting_edge
graph = Graph.load_from_json(@filepath)
graph.insert_edge([1,4])
assert_equal true, graph.vertices.include?(4), 'new vertex was not added during edge inserting'
end
def test_loop_edge_will_be_added
graph = Graph.load_from_json(@filepath)
graph.insert_edge([1, 1])
assert_equal true, graph.edges.include?([1, 1]), 'looped edge was not added'
end
def test_loop_edge_will_not_add_same_vertex_twice
graph = Graph.load_from_json(@filepath)
graph.insert_edge([4, 4])
assert_equal 1, graph.vertices.count(4), 'more than 1 vertex was added during looped-edge inserting'
end
def test_each_for_edges_array
graph = Graph.load_from_json(@filepath)
test_arr=[]
graph.each { |x| test_arr.append(x) }
assert_equal [[1,2], [2, 3], [3, 1]], test_arr
test_arr.clear
graph.insert_edge([1,4])
graph.each { |x| test_arr.append(x) }
assert_equal [[1,2], [2, 3], [3, 1],[1, 4]], test_arr
test_arr.clear
graph.insert_edge([3,2])
graph.each { |x| test_arr.append(x) }
assert_equal [[1,2], [2, 3], [3, 1],[1, 4], [3, 2]],test_arr
end
def test_each_with_index_for_edges_array
graph = Graph.load_from_json(@filepath)
test_arr=[]
graph.each_with_index{ |x, i| test_arr.append(x, i)}
assert_equal [[1, 2], 0, [2, 3], 1, [3, 1], 2],test_arr
test_arr.clear
graph.insert_edge([4, 4])
graph.each_with_index { |x,i| test_arr.append(x, i)}
assert_equal [[1, 2], 0, [2, 3], 1, [3, 1], 2, [4, 4], 3], test_arr
test_arr.clear
graph.insert_edge([1,4])
graph.insert_edge([2,5])
graph.each_with_index { |x, i| test_arr.append(x, i)}
assert_equal [[1, 2], 0, [2, 3], 1, [3, 1], 2, [4, 4], 3, [1, 4], 4, [2, 5], 5], test_arr
test_arr.clear
graph.insert_edge([3,5])
graph.each_with_index { |x, i| test_arr.append(x, i)}
assert_equal [[1, 2], 0, [2, 3], 1, [3, 1], 2, [4, 4], 3, [1, 4], 4, [2, 5], 5, [3, 5], 6], test_arr
end
end