/
search_skiplist_spec.rb
222 lines (188 loc) · 5.85 KB
/
search_skiplist_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
require File.dirname(__FILE__) + '/spec_helper'
describe "SearchSkiplist serialization", :shared => true do
it "should correctly load what it dumped" do
dump1 = @list.marshal_dump
newlist = @list.class.allocate
dump2 = newlist.marshal_load(dump1).marshal_dump
dump1.should == dump2
end
it "should correctly load to_a results" do
arr1 = @list.to_a
newlist = @list.class.from_a(arr1)
arr2 = newlist.to_a
arr1.should == arr2
end
end
SearchSkiplist.with_optimizations(OPTIMIZATIONS) do |lang|
describe "Empty SearchSkiplist [#{lang}]" do
before(:each) do
@maxlevel = 8
@probability = 0.5
@list = SearchSkiplist.new(nil, :maxlevel => @maxlevel, :probability => @probability)
end
it "should be empty" do
@list.should be_empty
end
it "should be serialized with marshal_dump" do
@list.marshal_dump.should == {
:options => {
:probability => @probability,
:maxlevel => @maxlevel
},
:raw_list => [
[[nil]*@maxlevel, nil, nil]
]
}
end
it "should find nil in a empty skiplist" do
@list.find("xx").should == nil
@list.find("").should == nil
end
it_should_behave_like "SearchSkiplist serialization"
end
describe "Inserting in a skiplist [#{lang}]" do
before(:each) do
@maxlevel = 8
@probability = 0.5
@list = SearchSkiplist.new(nil, :maxlevel => @maxlevel, :probability => @probability)
end
it "should insert empty key in place of default head" do
@list.insert("", 42, 1).should == @list
@list.find("").should == 42
@list.find("-").should == nil
@list.to_a.should == [["", 42]]
end
it "should insert non-empty key" do
@list.insert("x", 42, 1).should == @list
@list.find("").should == nil
@list.find("x").should == 42
@list.find("-").should == nil
end
it "should correctly insert keys in an ascending level order" do
1.upto(@maxlevel) do |i|
k = "x#{i}"
@list.insert(k, k, i).should == @list
@list.find("").should == nil
@list.find(k).should == k
@list.find("-").should == nil
end
# repeat
1.upto(@maxlevel) do |i|
k = "x#{i}"
@list.find(k).should == k
end
end
it "should correctly insert keys in a descending level order" do
@maxlevel.downto(1) do |i|
k = "x#{@maxlevel-i}"
@list.insert(k, k, i).should == @list
@list.find("").should == nil
@list.find(k).should == k
@list.find("-").should == nil
end
# repeat
@maxlevel.downto(1) do |i|
k = "x#{@maxlevel-i}"
@list.find(k).should == k
end
end
end
describe "Big skiplist [#{lang}]" do
before(:each) do
@maxlevel = 8
@probability = 0.5
@list = SearchSkiplist.new(nil, :maxlevel => @maxlevel, :probability => @probability)
1000.times do
k = rand(2**64).to_s
v = k
@list.insert(k, v)
end
end
it_should_behave_like "SearchSkiplist serialization"
it "should support to_a with sorted key-value pairs" do
ary = @list.to_a
ary.should == ary.sort{|a,b| a[0] <=> b[0] }
ary.size.should == 1000
end
it "should have iterate pairs with #each" do
c = []
@list.each do |key, value|
c << [key, value]
end
c.should have_at_least(10).items
c[0..10].each do |a|
a[0].should == a[1] # key == value
end
c.should == c.sort{|a, b| a[0] <=> b[0] } # sorted order
end
end
describe "SearchSkiplist#first_key [#{lang}]" do
before(:each) do
@maxlevel = 8
@probability = 0.5
@list = SearchSkiplist.new(nil, :maxlevel => @maxlevel, :probability => @probability)
end
it "should return nil for empty skiplist" do
@list.first_key.should == nil
end
it "should return key for non-empty skiplist" do
@list.insert("b", "data1")
@list.first_key.should == "b"
@list.insert("c", "data2")
@list.first_key.should == "b"
end
end
describe "SearchSkiplist#find_nearest [#{lang}]" do
before(:each) do
@maxlevel = 8
@probability = 0.5
@list = SearchSkiplist.new(nil, :maxlevel => @maxlevel, :probability => @probability)
end
it "should find nil in empty skiplist" do
@list.find_nearest("a").should == nil
@list.find_nearest("").should == nil
@list.find_nearest(nil).should == nil
end
it "should find exact value if it is present" do
@list.insert("b", "B")
@list.insert("f", "F")
@list.find_nearest("b").should == "B"
@list.find_nearest("f").should == "F"
end
it "should find nearest value or nil" do
@list.insert("b", "B")
@list.insert("f", "F")
@list.find_nearest("a").should == nil
@list.find_nearest("c").should == "B"
@list.find_nearest("g").should == "F"
end
it "should always find empty-string key if nothing found" do
@list.insert("", "Empty")
@list.insert("b", "B")
@list.insert("f", "F")
@list.find_nearest("a").should == "Empty"
@list.find_nearest("c").should == "B"
@list.find_nearest("g").should == "F"
end
end
describe "SearchSkiplist#search" do
before(:each) do
@list = SearchSkiplist.new
@keys = %w[ a aa ab b ba bb x xx xy xyz ]
@values = @keys.map{|v| v + " value"}
@key_values = @keys.map{|v| [v, v + " value"]}
@key_values.each do |k, v|
@list.insert(k, v)
end
end
it "should find all items" do
@list.search(nil, nil, nil, nil, nil, nil).should == @values
end
it "should find all items with keys" do
@list.search(nil, nil, nil, nil, nil, true).should == @key_values
end
end
end
def raw_list(list)
list.marshal_dump[:raw_list]
end