summaryrefslogtreecommitdiff
path: root/spec/quick_find_spec.rb
blob: 629969c64cca8def9f3cf1f15f53c6de77cbc143 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require "spec_helper"

describe QuickFind do
  subject { QuickFind.new(10) }

  it "is not connected by default" do
    10.times do |n|
      expect(subject.connected?(n, n+1)).to be_falsey
    end
  end

  it "is connected" do
    subject.union(0, 1)
    expect(subject.connected?(0, 1)).to be_truthy
  end

  it "unions and connects properly" do
    subject.union(4, 3)
    expect(subject.connected?(4, 3)).to be_truthy
    expect(subject.connected?(3, 4)).to be_truthy

    subject.union(3, 8)
    expect(subject.connected?(3, 8)).to be_truthy

    subject.union(6, 5)
    expect(subject.connected?(6, 5)).to be_truthy

    subject.union(9, 4)
    expect(subject.connected?(9, 4)).to be_truthy

    subject.union(2, 1)
    expect(subject.connected?(2, 1)).to be_truthy

    subject.union(8, 9)
    expect(subject.connected?(8, 9)).to be_truthy

    expect(subject.connected?(5, 0)).to be_falsey
    subject.union(5, 0)
    expect(subject.connected?(5, 0)).to be_truthy
    expect(subject.connected?(6, 0)).to be_truthy
    expect(subject.connected?(5, 6)).to be_truthy

    subject.union(7, 2)
    subject.union(6, 1)

    tuples = [
      [0, 5],
      [5, 6],
      [6, 1],
      [1, 2],
      [2, 7],
      [8, 3],
      [3, 4],
      [4, 9],
    ]
    tuples.each do |tuple|
      expect(subject.connected?(tuple.first, tuple.last)).to be_truthy
    end
  end
end