From 47594b35e2d98d8975182d49d3236cf9bd419408 Mon Sep 17 00:00:00 2001 From: Artrit0chka <58296372+Artrit0chka@users.noreply.github.com> Date: Tue, 16 Nov 2021 00:13:40 +0300 Subject: [PATCH 1/3] sdelal! --- lib/visual_graphs/graph.rb | 45 ++++++++++++++++++++++++++++++++++++++ test/equality_test.rb | 44 +++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 test/equality_test.rb diff --git a/lib/visual_graphs/graph.rb b/lib/visual_graphs/graph.rb index a2b0b59..7d1e67f 100644 --- a/lib/visual_graphs/graph.rb +++ b/lib/visual_graphs/graph.rb @@ -79,5 +79,50 @@ def dump_to_json(path) File.open(path, 'w') { |f| f.write(@adjacency_list.to_json) } end + def ==(other_object) + if other_object.is_a? self.class + if not (self.vertices.sort == other_object.vertices.sort) + return false + end + + if not (self.edges.sort == other_object.edges.sort) + return false + end + return true + end + false + 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 + + def equal?(other_object) + if other_object.is_a? self.class + if not (self.vertices.sort.equal? other_object.vertices.sort) + return false + end + + if not (self.edges.sort.equal? 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..8a4f2c7 --- /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 = Graph.adjacency_list_init({2 => [3], 3 => [1], 1 => [2]}) + hash_graph11 = Graph.adjacency_list_init({5 => [3], 7 => [5], 2 => [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 From c46117105188b7dd6e771b331bc48fe52451aaae Mon Sep 17 00:00:00 2001 From: Artrit0chka <58296372+Artrit0chka@users.noreply.github.com> Date: Tue, 16 Nov 2021 00:29:42 +0300 Subject: [PATCH 2/3] Dodelal! --- README.md | 13 +++++++++++++ lib/visual_graphs/graph.rb | 13 ------------- test/equality_test.rb | 4 ++-- test/resources/output_test_data.json | 1 - 4 files changed, 15 insertions(+), 16 deletions(-) delete mode 100644 test/resources/output_test_data.json diff --git a/README.md b/README.md index 211361b..3cd2638 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 7d1e67f..6743748 100644 --- a/lib/visual_graphs/graph.rb +++ b/lib/visual_graphs/graph.rb @@ -111,18 +111,5 @@ def eql?(other_object) false end - def equal?(other_object) - if other_object.is_a? self.class - if not (self.vertices.sort.equal? other_object.vertices.sort) - return false - end - - if not (self.edges.sort.equal? 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 index 8a4f2c7..2e95ca3 100644 --- a/test/equality_test.rb +++ b/test/equality_test.rb @@ -34,8 +34,8 @@ def test_eql def test_equal hash_graph9 = Graph.adjacency_list_init({1 => [2], 2 => [3], 3 => [1]}) - hash_graph10 = Graph.adjacency_list_init({2 => [3], 3 => [1], 1 => [2]}) - hash_graph11 = Graph.adjacency_list_init({5 => [3], 7 => [5], 2 => [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) diff --git a/test/resources/output_test_data.json b/test/resources/output_test_data.json deleted file mode 100644 index 9a0c734..0000000 --- a/test/resources/output_test_data.json +++ /dev/null @@ -1 +0,0 @@ -{"1":[2],"2":[3],"3":[1]} \ No newline at end of file From 3e03e3e0520a08bbd01550582b57f41c302d9eb6 Mon Sep 17 00:00:00 2001 From: Artrit0chka <58296372+Artrit0chka@users.noreply.github.com> Date: Tue, 16 Nov 2021 00:39:23 +0300 Subject: [PATCH 3/3] Tochno Dodelal! --- lib/visual_graphs/graph.rb | 5 +---- test/resources/output_graph.json | 1 + test/resources/output_test_data.json | 1 + 3 files changed, 3 insertions(+), 4 deletions(-) create mode 100644 test/resources/output_test_data.json diff --git a/lib/visual_graphs/graph.rb b/lib/visual_graphs/graph.rb index 6743748..149b883 100644 --- a/lib/visual_graphs/graph.rb +++ b/lib/visual_graphs/graph.rb @@ -80,7 +80,6 @@ def dump_to_json(path) end def ==(other_object) - if other_object.is_a? self.class if not (self.vertices.sort == other_object.vertices.sort) return false end @@ -88,9 +87,7 @@ def ==(other_object) if not (self.edges.sort == other_object.edges.sort) return false end - return true - end - false + true end def ===(other_object) 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