/
skiplist_spec.rb
343 lines (292 loc) · 10.6 KB
/
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
require File.dirname(__FILE__) + '/spec_helper'
describe "Skiplist serialization", :shared => true do
it "should correctly load what it dumped" do
dump1 = @list.dump
newlist = @list.class.load(dump1)
dump2 = newlist.dump
dump1.should == dump2
newlist.to_a.should == @list.to_a
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
Skiplist.with_optimizations(OPTIMIZATIONS) do |lang|
describe "Empty Skiplist [#{lang}]" do
before(:each) do
@maxlevel = 8
@probability = 0.5
@list = Skiplist.new(:maxlevel => @maxlevel, :probability => @probability)
end
it "should be empty" do
@list.should be_empty
end
it "should find nil in a empty skiplist" do
@list.find("xx").should == nil
@list.find("").should == nil
end
it_should_behave_like "Skiplist serialization"
end
describe "Inserting in a skiplist [#{lang}]" do
before(:each) do
@maxlevel = 8
@probability = 0.5
@list = Skiplist.new(: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}"
r = @list.insert(k, k, i)
r.object_id.should == @list.object_id
r.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 "Deleting from skiplist" do
before(:each) do
@list = Skiplist.new
@list.insert "1a", "a"
@list.insert "1b", "b"
@list.insert "1c", "c"
@list.insert "1d", "d"
end
it "should store nil" do
@list.insert("1b", nil)
@list.search("1", "1", 3, 0, false, false).should == ["a", nil, "c"]
end
it "should delete item" do
@list.delete("1b")
@list.search("1", "1", 3, 0, false, false).should == ["a", "c", "d"]
end
end
describe "Big skiplist [#{lang}]" do
before(:each) do
@maxlevel = 8
@probability = 0.5
@list = Skiplist.new(: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 "Skiplist 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 "Skiplist serialization [#{lang}]" do
before(:each) do
@maxlevel = 8
@list = Skiplist.new(:maxlevel => @maxlevel)
@arr = (1..1000).to_a.map{|a| a.to_s}.sort
end
it "should export data to_a correctly" do
@arr.each{|e| @list.insert(e,e) }
@list.to_a.should == @arr.map{|e| [e, e]}
end
it "should load data from_a correctly" do
@list2 = @list.class.from_a(@arr.map{|e| [e, e]}, :maxlevel => @maxlevel)
@list2.to_a.should == @arr.map{|e| [e, e]}
end
end
describe "Skiplist#first_key [#{lang}]" do
before(:each) do
@maxlevel = 8
@probability = 0.5
@list = Skiplist.new(: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 "Skiplist#find_nearest [#{lang}]" do
before(:each) do
@maxlevel = 8
@probability = 0.5
@list = Skiplist.new(: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 "Skiplist#search [#{lang}]" do
before(:each) do
@list = Skiplist.new
@keys = %w[ a aa ab b ba bb pfx1 pfx2 pfx3 x xx xy xyz ]
@values = @keys.map{|v| v + " value"}
@key_values = @keys.map{|v| [v, v]}
@key_values.each do |k, v|
@list.insert(k, v)
end
end
it_should_behave_like "Skiplist serialization"
it "should find all items" do
search_should_yield(@key_values)
end
it "should find all items starting with a prefix" do
search_should_yield(@key_values, :start_key => "a")
search_should_yield(@key_values[1..-1], :start_key => "aa")
search_should_yield(@key_values[2..-1], :start_key => "ab")
search_should_yield(@key_values[3..-1], :start_key => "b")
search_should_yield(@key_values[-1..-1], :start_key => "xyz")
search_should_yield(@key_values[6..-1], :start_key => "pfx")
end
it "should not find any items if prefix not matched" do
search_should_yield([], :start_key => "middle")
search_should_yield([], :start_key => "__prefix")
search_should_yield([], :start_key => "zuffix")
search_should_yield([], :start_key => "pfx0")
end
it "should not find all items before the given end_key (inclusive)" do
search_should_yield(@key_values, :end_key => "xyz")
search_should_yield(@key_values, :end_key => "zuffix")
search_should_yield(@key_values[0..-3], :end_key => "xx")
search_should_yield(@key_values[0..2], :end_key => "a")
search_should_yield(@key_values[0..1], :end_key => "aa")
search_should_yield(@key_values[0..5], :end_key => "b")
end
it "should find items in a range" do
search_should_yield(@key_values[1..5], :start_key => "aa", :end_key => "bb")
search_should_yield(@key_values[0..2], :start_key => "a", :end_key => "a")
search_should_yield(@key_values[3..5], :start_key => "b", :end_key => "b")
search_should_yield(@key_values[6..8], :start_key => "pfx", :end_key => "pfx")
search_should_yield(@key_values[0..5], :start_key => "a", :end_key => "b")
search_should_yield(@key_values[1..5], :start_key => "aa", :end_key => "b")
end
it "should not find items in an invalid range" do
search_should_yield([], :start_key => "b", :end_key => "a")
search_should_yield([], :start_key => "z", :end_key => "a")
search_should_yield([], :start_key => "_", :end_key => "b")
search_should_yield([], :start_key => "ab1", :end_key => "b")
end
it "should search in a reverse order" do
r = search_with_options(@list, :reverse => true, :with_keys => true)
r.should == @key_values.reverse
end
it "should find a range in a reversed order" do
r = search_with_options(@list, :start_key => "ab", :end_key => "aa", :reverse => true)
r.should == %w[ab aa]
r = search_with_options(@list, :start_key => "b", :end_key => "aa", :reverse => true)
r.should == %w[aa ab b ba bb].reverse
r = search_with_options(@list, :start_key => "ba", :end_key => "a", :reverse => true)
r.should == %w[a aa ab b ba].reverse
end
it "should find a value in a reversed order" do
r = search_with_options(@list, :start_key => "a", :end_key => "a", :reverse => true)
r.should == %w[ab aa a]
r = search_with_options(@list, :start_key => "xyz", :end_key => "xyz", :reverse => true)
r.should == %w[xyz]
end
def search_should_yield(results, os = {})
# TODO: added reverse cases
list = @list
os = os.merge(:with_keys => true)
r = search_with_options(list, os)
r.should == results
search_should_use_offsets_and_limits(list, os, r)
os = os.merge(:with_keys => nil)
r = search_with_options(list, os)
r.should == results.map{|k,v| v}
search_should_use_offsets_and_limits(list, os, r)
end
def search_should_use_offsets_and_limits(list, os, r1)
offsets = [-1000, -1, 0, 1, 2, 3, 4, 1000]
limits = [-1000, -1, 0, 1, 2, 3, 4, 1000]
offsets.each do |off|
limits.each do |lim|
r2 = search_with_options(list, os.merge(:offset => off, :limit => lim))
r2.should == (r1[off < 0 ? 0 : off, lim < 0 ? 0 : lim] || [])
end
end
end
def search_with_options(list, os = {})
#puts "OPTIONS: #{os.inspect}"
r = list.search(os[:start_key], os[:end_key], os[:limit], os[:offset], os[:reverse], os[:with_keys])
#puts "R: #{r.inspect}"
r
end
end
end
def raw_list(list)
list.marshal_dump[:raw_list]
end