1
1
require_relative './quick_union'
2
2
3
3
RSpec . describe QuickUnion do
4
+ let ( :quick_union ) { QuickUnion . new ( 10 ) }
5
+
4
6
it "Create array with values assigned to its index" do
5
- qf = QuickUnion . new ( 10 )
6
- expect ( qf . size ) . to eq ( 10 )
7
- expect ( qf . values . size ) . to eq ( 10 )
7
+ expect ( quick_union . size ) . to eq ( 10 )
8
+ expect ( quick_union . values . size ) . to eq ( 10 )
9
+ end
10
+
11
+ it "Check if two elements are connected" do
12
+ expect ( quick_union . connected ( 0 , 1 ) ) . to eq ( false )
13
+ end
14
+
15
+ it "Test the algorithm" do
16
+ quick_union . union ( 4 , 3 )
17
+ expect ( quick_union . values ) . to eq ( [ 0 , 1 , 2 , 4 , 4 , 5 , 6 , 7 , 8 , 9 ] )
18
+ quick_union . union ( 9 , 8 )
19
+ expect ( quick_union . values ) . to eq ( [ 0 , 1 , 2 , 4 , 4 , 5 , 6 , 7 , 9 , 9 ] )
20
+ quick_union . union ( 9 , 7 )
21
+ expect ( quick_union . values ) . to eq ( [ 0 , 1 , 2 , 4 , 4 , 5 , 6 , 9 , 9 , 9 ] )
22
+ quick_union . union ( 0 , 1 )
23
+ expect ( quick_union . values ) . to eq ( [ 0 , 0 , 2 , 4 , 4 , 5 , 6 , 9 , 9 , 9 ] )
24
+ quick_union . union ( 6 , 7 )
25
+ expect ( quick_union . values ) . to eq ( [ 0 , 0 , 2 , 4 , 4 , 5 , 6 , 9 , 9 , 6 ] )
26
+ end
27
+
28
+ it "Get the root element of an element" do
29
+ quick_union . union ( 6 , 7 )
30
+ expect ( quick_union . root ( 7 ) ) . to eq ( 6 )
31
+ end
32
+
33
+ it "Check if two elements are connected" do
34
+ quick_union . union ( 0 , 1 )
35
+ quick_union . union ( 1 , 2 )
36
+ quick_union . union ( 2 , 3 )
37
+ expect ( quick_union . connected ( 0 , 3 ) ) . to eq ( true )
8
38
end
9
39
end
0 commit comments