-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear_probing_hash_spec.rb
56 lines (42 loc) · 1.36 KB
/
linear_probing_hash_spec.rb
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
require "./linear_probing_hash"
require "pry"
RSpec.describe LinearProbingHash do
it "sets and retrieves some simple keys and values" do
expect(subject["foo"]).to be_nil
subject["foo"] = 3
expect(subject["foo"]).to eq(3)
subject[:bar] = {bop: {}}
expect(subject[:bar]).to eq({bop: {}})
expect(subject["foo"]).to eq(3)
subject[3] = "three"
expect(subject[:bar]).to eq({bop: {}})
expect(subject["foo"]).to eq(3)
expect(subject[3]).to eq("three")
end
it "overwrites a previous value" do
expect(subject["foo"]).to be_nil
subject["foo"] = 3
expect(subject["foo"]).to eq(3)
subject["foo"] = :stewie
expect(subject["foo"]).to eq(:stewie)
end
it "handles collisions" do
expect(subject).to receive(:hash).with(3).and_return(5).at_least(:once)
expect(subject).to receive(:hash).with(13).and_return(5).at_least(:once)
subject[3] = "three"
expect(subject[3]).to eq("three")
subject[13] = "thirteen"
expect(subject[3]).to eq("three")
expect(subject[13]).to eq("thirteen")
end
it "resizes automatically when it runs out of space" do
expect(subject.instance_variable_get("@size")).to eq(10)
11.times do |i|
subject[i] = i.to_s
end
11.times do |i|
expect(subject[i]).to eq(i.to_s)
end
expect(subject.instance_variable_get("@size")).to eq(20)
end
end