Skip to content

Done! #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
29 changes: 29 additions & 0 deletions lib/visual_graphs/graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
44 changes: 44 additions & 0 deletions test/equality_test.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/resources/output_graph.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[0,2,1],[3,0,5],[6,4,0]]
1 change: 1 addition & 0 deletions test/resources/output_test_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"1":[2],"2":[3],"3":[1]}