1
+ include RSpec
2
+
3
+ require_relative 'node'
4
+ require_relative 'open_addressing'
5
+
6
+ RSpec . describe OpenAddressing , type : Class do
7
+ let ( :star_wars_movies ) { OpenAddressing . new ( 6 ) }
8
+
9
+ before do
10
+ star_wars_movies [ "Star Wars: The Phantom Menace" ] = "Number One"
11
+ star_wars_movies [ "Star Wars: Attack of the Clones" ] = "Number Two"
12
+ star_wars_movies [ "Star Wars: Revenge of the Sith" ] = "Number Three"
13
+ star_wars_movies [ "Star Wars: A New Hope" ] = "Number Four"
14
+ star_wars_movies [ "Star Wars: The Empire Strikes Back" ] = "Number Five"
15
+ star_wars_movies [ "Star Wars: Return of the Jedi" ] = "Number Six"
16
+ end
17
+
18
+ describe "#index" do
19
+ it "creates a hash key based on the string value passed in" do
20
+ i = star_wars_movies . index ( "Star Wars: A New Hope" , 6 )
21
+ expect ( i ) . to eq 4
22
+ end
23
+ end
24
+
25
+ describe "#key" do
26
+ it "returns the sum of the ascii values of the string value" do
27
+ key = "test"
28
+ expect ( star_wars_movies . index ( key , 6 ) ) . to eq 4
29
+ end
30
+ end
31
+
32
+ describe "#hash[key] = value" do
33
+ it "does not resizes the array when a collision occurs and hash is not full" do
34
+ hash = OpenAddressing . new ( 4 )
35
+ hash [ "key" ] = "value"
36
+ expect ( hash . size ) . to eq 4
37
+ hash [ "key" ] = "second value"
38
+ expect ( hash . size ) . to eq 4
39
+ end
40
+
41
+ it "resizes the array when a collision occurs and hash is full" do
42
+ hash = OpenAddressing . new ( 1 )
43
+ hash [ "key" ] = "value"
44
+ expect ( hash . size ) . to eq 1
45
+ hash [ "key" ] = "second value"
46
+ expect ( hash . size ) . to eq 2
47
+ end
48
+
49
+ it "sets the value of key to value" do
50
+ expect ( star_wars_movies [ "Star Wars: The Phantom Menace" ] ) . to eq "Number One"
51
+ expect ( star_wars_movies [ "Star Wars: Attack of the Clones" ] ) . to eq "Number Two"
52
+ expect ( star_wars_movies [ "Star Wars: Revenge of the Sith" ] ) . to eq "Number Three"
53
+ expect ( star_wars_movies [ "Star Wars: A New Hope" ] ) . to eq "Number Four"
54
+ expect ( star_wars_movies [ "Star Wars: The Empire Strikes Back" ] ) . to eq "Number Five"
55
+ expect ( star_wars_movies [ "Star Wars: Return of the Jedi" ] ) . to eq "Number Six"
56
+ end
57
+ end
58
+
59
+ describe "#next_open_index" do
60
+ it "returns -1 if there are no open indices" do
61
+ inception = OpenAddressing . new ( 1 )
62
+ inception [ "The Original" ] = "The Best Movie Ever"
63
+ expect ( inception . next_open_index ( 0 ) ) . to eq -1
64
+ end
65
+
66
+ end
67
+
68
+ describe "#resize" do
69
+ it "doubles the size of the array when invoked" do
70
+ movies = OpenAddressing . new ( 6 )
71
+ expect ( movies . size ) . to eq 6
72
+ movies . resize
73
+ expect ( movies . size ) . to eq 12
74
+ end
75
+
76
+ it "copies existing values properly when the array is resized" do
77
+ movies = OpenAddressing . new ( 6 )
78
+ movies [ "A New Hope" ] = "Average"
79
+ movies [ "Empire Strikes Back" ] = "Excellent"
80
+ movies [ "Return of the Jedi" ] = "The Best"
81
+ movies . resize
82
+ expect ( movies . size ) . to eq 12
83
+ expect ( movies [ "A New Hope" ] ) . to eq "Average"
84
+ expect ( movies [ "Empire Strikes Back" ] ) . to eq "Excellent"
85
+ expect ( movies [ "Return of the Jedi" ] ) . to eq "The Best"
86
+ end
87
+ end
88
+ end
0 commit comments