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