diff --git a/README.md b/README.md index 4a90872..0d446c7 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,19 @@ graph = Graph.load_from_json(path) hash_graph = {1 => [2, 3], 2 => [1, 4]} graph = Graph.adjacency_list_init(hash_graph) +#== +# Сравнивает два графа. + +#=== +# Сравнивает псевдоним ==. + +# eql? +# Сравнивает по хешу. + +# equal? +# Сравнивает по ссылке. Реализован в object + + graph.vertices #return list of vertices graph.edges #return list of edges ``` diff --git a/lib/visual_graphs/graph.rb b/lib/visual_graphs/graph.rb index a2b0b59..149b883 100644 --- a/lib/visual_graphs/graph.rb +++ b/lib/visual_graphs/graph.rb @@ -79,5 +79,34 @@ def dump_to_json(path) File.open(path, 'w') { |f| f.write(@adjacency_list.to_json) } end + def ==(other_object) + if not (self.vertices.sort == other_object.vertices.sort) + return false + end + + if not (self.edges.sort == other_object.edges.sort) + return false + end + true + end + + def ===(other_object) + self == other_object + end + + def eql?(other_object) + if other_object.is_a? self.class + if not (self.vertices.sort.eql? other_object.vertices.sort) + return false + end + + if not (self.edges.sort.eql? other_object.edges.sort) + return false + end + return true + end + false + end + end end \ No newline at end of file diff --git a/test/equality_test.rb b/test/equality_test.rb new file mode 100644 index 0000000..2e95ca3 --- /dev/null +++ b/test/equality_test.rb @@ -0,0 +1,44 @@ +require './test/test_helper' + +class EqualityTest < Minitest::Test + include VisualGraphs + + def test_test + hash_graph = {1 => [2], 2 => [4]} + graph = Graph.adjacency_list_init(hash_graph) + + hash_graph1 = {1 => [2], 2 => [1]} + graph1 = Graph.adjacency_list_init(hash_graph1) + + assert_equal false, graph == graph1 + + end + + def test_test_test + hash_graph3 = Graph.adjacency_list_init({1 => [2], 2 => [3], 3 => [1]}) + hash_graph4 = Graph.adjacency_list_init({2 => [3], 3 => [1], 1 => [2]}) + hash_graph5 = Graph.adjacency_list_init({5 => [3], 7 => [5], 2 => [1]}) + + assert_equal true, hash_graph3 === hash_graph4 + assert_equal false, hash_graph5 === hash_graph4 + end + + def test_eql + hash_graph6 = Graph.adjacency_list_init({1 => [2], 2 => [3], 3 => [1]}) + hash_graph7 = Graph.adjacency_list_init({2 => [3], 3 => [1], 1 => [2]}) + hash_graph8 = Graph.adjacency_list_init({5 => [3], 7 => [5], 2 => [1]}) + + assert_equal true, hash_graph6.eql?(hash_graph7) + assert_equal false, hash_graph7.eql?(hash_graph8) + end + + def test_equal + hash_graph9 = Graph.adjacency_list_init({1 => [2], 2 => [3], 3 => [1]}) + hash_graph10 = hash_graph9 + hash_graph11 = Graph.adjacency_list_init({1 => [2], 2 => [3], 3 => [1]}) + + assert_equal true, hash_graph9.equal?(hash_graph10) + assert_equal false, hash_graph9.equal?(hash_graph11) + end + +end \ No newline at end of file diff --git a/test/resources/output_graph.json b/test/resources/output_graph.json index e69de29..4d5dff6 100644 --- a/test/resources/output_graph.json +++ b/test/resources/output_graph.json @@ -0,0 +1 @@ +[[0,2,1],[3,0,5],[6,4,0]] \ No newline at end of file diff --git a/test/resources/output_test_data.json b/test/resources/output_test_data.json new file mode 100644 index 0000000..9a0c734 --- /dev/null +++ b/test/resources/output_test_data.json @@ -0,0 +1 @@ +{"1":[2],"2":[3],"3":[1]} \ No newline at end of file