-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_spec.rb
475 lines (453 loc) · 12.5 KB
/
test_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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
describe 'database' do
before do
`rm -rf test.db`
end
def run_script(commands)
raw_output = nil
IO.popen("./db test.db", "r+") do |pipe|
commands.each do |command|
begin
pipe.puts command
rescue Errno::EPIPE
break
end
end
pipe.close_write
# Read entire output
raw_output = pipe.gets(nil)
end
raw_output.split("\n")
end
it 'inserts and retrieves a row' do
result = run_script([
"insert 1 user1 [email protected]",
"select",
".exit",
])
expect(result).to match_array([
"db > Executed.",
"db > (1, user1, [email protected])",
"Executed.",
"db > ",
])
end
it 'keeps data after closing connection' do
result1 = run_script([
"insert 1 user1 [email protected]",
".exit",
])
expect(result1).to match_array([
"db > Executed.",
"db > ",
])
result2 = run_script([
"select",
".exit",
])
expect(result2).to match_array([
"db > (1, user1, [email protected])",
"Executed.",
"db > ",
])
end
it 'prints error message when table is full' do
script = (1..1401).map do |i|
"insert #{i} user#{i} person#{i}@example.com"
end
script << ".exit"
result = run_script(script)
expect(result.last(2)).to match_array([
"db > Executed.",
"db > ",
])
end
it 'allows inserting strings that are the maximum length' do
long_username = "a"*32
long_email = "a"*255
script = [
"insert 1 #{long_username} #{long_email}",
"select",
".exit",
]
result = run_script(script)
expect(result).to match_array([
"db > Executed.",
"db > (1, #{long_username}, #{long_email})",
"Executed.",
"db > ",
])
end
it 'prints error message if strings are too long' do
long_username = "a"*33
long_email = "a"*256
script = [
"insert 1 #{long_username} #{long_email}",
"select",
".exit",
]
result = run_script(script)
expect(result).to match_array([
"db > String is too long.",
"db > Executed.",
"db > ",
])
end
it 'prints an error message if id is negative' do
script = [
"insert -1 cstack [email protected]",
"select",
".exit",
]
result = run_script(script)
expect(result).to match_array([
"db > ID must be positive.",
"db > Executed.",
"db > ",
])
end
it 'prints an error message if there is a duplicate id' do
script = [
"insert 1 user1 [email protected]",
"insert 1 user1 [email protected]",
"select",
".exit",
]
result = run_script(script)
expect(result).to match_array([
"db > Executed.",
"db > Error: Duplicate key.",
"db > (1, user1, [email protected])",
"Executed.",
"db > ",
])
end
it 'allows printing out the structure of a one-node btree' do
script = [3, 1, 2].map do |i|
"insert #{i} user#{i} person#{i}@example.com"
end
script << ".btree"
script << ".exit"
result = run_script(script)
expect(result).to match_array([
"db > Executed.",
"db > Executed.",
"db > Executed.",
"db > Tree:",
"- leaf (size 3)",
" - 1",
" - 2",
" - 3",
"db > "
])
end
it 'allows printing out the structure of a 3-leaf-node btree' do
script = (1..14).map do |i|
"insert #{i} user#{i} person#{i}@example.com"
end
script << ".btree"
script << "insert 15 user15 [email protected]"
script << ".exit"
result = run_script(script)
expect(result[14...(result.length)]).to match_array([
"db > Tree:",
"- internal (size 1)",
" - leaf (size 7)",
" - 1",
" - 2",
" - 3",
" - 4",
" - 5",
" - 6",
" - 7",
" - key 7",
" - leaf (size 7)",
" - 8",
" - 9",
" - 10",
" - 11",
" - 12",
" - 13",
" - 14",
"db > Executed.",
"db > ",
])
end
it 'allows printing out the structure of a 4-leaf-node btree' do
script = [
"insert 18 user18 [email protected]",
"insert 7 user7 [email protected]",
"insert 10 user10 [email protected]",
"insert 29 user29 [email protected]",
"insert 23 user23 [email protected]",
"insert 4 user4 [email protected]",
"insert 14 user14 [email protected]",
"insert 30 user30 [email protected]",
"insert 15 user15 [email protected]",
"insert 26 user26 [email protected]",
"insert 22 user22 [email protected]",
"insert 19 user19 [email protected]",
"insert 2 user2 [email protected]",
"insert 1 user1 [email protected]",
"insert 21 user21 [email protected]",
"insert 11 user11 [email protected]",
"insert 6 user6 [email protected]",
"insert 20 user20 [email protected]",
"insert 5 user5 [email protected]",
"insert 8 user8 [email protected]",
"insert 9 user9 [email protected]",
"insert 3 user3 [email protected]",
"insert 12 user12 [email protected]",
"insert 27 user27 [email protected]",
"insert 17 user17 [email protected]",
"insert 16 user16 [email protected]",
"insert 13 user13 [email protected]",
"insert 24 user24 [email protected]",
"insert 25 user25 [email protected]",
"insert 28 user28 [email protected]",
".btree",
".exit",
]
result = run_script(script)
expect(result[30...(result.length)]).to match_array([
"db > Tree:",
"- internal (size 3)",
" - leaf (size 7)",
" - 1",
" - 2",
" - 3",
" - 4",
" - 5",
" - 6",
" - 7",
" - key 7",
" - leaf (size 8)",
" - 8",
" - 9",
" - 10",
" - 11",
" - 12",
" - 13",
" - 14",
" - 15",
" - key 15",
" - leaf (size 7)",
" - 16",
" - 17",
" - 18",
" - 19",
" - 20",
" - 21",
" - 22",
" - key 22",
" - leaf (size 8)",
" - 23",
" - 24",
" - 25",
" - 26",
" - 27",
" - 28",
" - 29",
" - 30",
"db > ",
])
end
it 'allows printing out the structure of a 7-leaf-node btree' do
script = [
"insert 58 user58 [email protected]",
"insert 56 user56 [email protected]",
"insert 8 user8 [email protected]",
"insert 54 user54 [email protected]",
"insert 77 user77 [email protected]",
"insert 7 user7 [email protected]",
"insert 25 user25 [email protected]",
"insert 71 user71 [email protected]",
"insert 13 user13 [email protected]",
"insert 22 user22 [email protected]",
"insert 53 user53 [email protected]",
"insert 51 user51 [email protected]",
"insert 59 user59 [email protected]",
"insert 32 user32 [email protected]",
"insert 36 user36 [email protected]",
"insert 79 user79 [email protected]",
"insert 10 user10 [email protected]",
"insert 33 user33 [email protected]",
"insert 20 user20 [email protected]",
"insert 4 user4 [email protected]",
"insert 35 user35 [email protected]",
"insert 76 user76 [email protected]",
"insert 49 user49 [email protected]",
"insert 24 user24 [email protected]",
"insert 70 user70 [email protected]",
"insert 48 user48 [email protected]",
"insert 39 user39 [email protected]",
"insert 15 user15 [email protected]",
"insert 47 user47 [email protected]",
"insert 30 user30 [email protected]",
"insert 86 user86 [email protected]",
"insert 31 user31 [email protected]",
"insert 68 user68 [email protected]",
"insert 37 user37 [email protected]",
"insert 66 user66 [email protected]",
"insert 63 user63 [email protected]",
"insert 40 user40 [email protected]",
"insert 78 user78 [email protected]",
"insert 19 user19 [email protected]",
"insert 46 user46 [email protected]",
"insert 14 user14 [email protected]",
"insert 81 user81 [email protected]",
"insert 72 user72 [email protected]",
"insert 6 user6 [email protected]",
"insert 50 user50 [email protected]",
"insert 85 user85 [email protected]",
"insert 67 user67 [email protected]",
"insert 2 user2 [email protected]",
"insert 55 user55 [email protected]",
"insert 69 user69 [email protected]",
"insert 5 user5 [email protected]",
"insert 65 user65 [email protected]",
"insert 52 user52 [email protected]",
"insert 1 user1 [email protected]",
"insert 29 user29 [email protected]",
"insert 9 user9 [email protected]",
"insert 43 user43 [email protected]",
"insert 75 user75 [email protected]",
"insert 21 user21 [email protected]",
"insert 82 user82 [email protected]",
"insert 12 user12 [email protected]",
"insert 18 user18 [email protected]",
"insert 60 user60 [email protected]",
"insert 44 user44 [email protected]",
".btree",
".exit",
]
result = run_script(script)
expect(result[64...(result.length)]).to match_array([
"db > Tree:",
"- internal (size 1)",
" - internal (size 2)",
" - leaf (size 7)",
" - 1",
" - 2",
" - 4",
" - 5",
" - 6",
" - 7",
" - 8",
" - key 8",
" - leaf (size 11)",
" - 9",
" - 10",
" - 12",
" - 13",
" - 14",
" - 15",
" - 18",
" - 19",
" - 20",
" - 21",
" - 22",
" - key 22",
" - leaf (size 8)",
" - 24",
" - 25",
" - 29",
" - 30",
" - 31",
" - 32",
" - 33",
" - 35",
" - key 35",
" - internal (size 3)",
" - leaf (size 12)",
" - 36",
" - 37",
" - 39",
" - 40",
" - 43",
" - 44",
" - 46",
" - 47",
" - 48",
" - 49",
" - 50",
" - 51",
" - key 51",
" - leaf (size 11)",
" - 52",
" - 53",
" - 54",
" - 55",
" - 56",
" - 58",
" - 59",
" - 60",
" - 63",
" - 65",
" - 66",
" - key 66",
" - leaf (size 7)",
" - 67",
" - 68",
" - 69",
" - 70",
" - 71",
" - 72",
" - 75",
" - key 75",
" - leaf (size 8)",
" - 76",
" - 77",
" - 78",
" - 79",
" - 81",
" - 82",
" - 85",
" - 86",
"db > ",
])
end
it 'prints constants' do
script = [
".constants",
".exit",
]
result = run_script(script)
expect(result).to match_array([
"db > Constants:",
"ROW_SIZE: 293",
"COMMON_NODE_HEADER_SIZE: 6",
"LEAF_NODE_HEADER_SIZE: 14",
"LEAF_NODE_CELL_SIZE: 297",
"LEAF_NODE_SPACE_FOR_CELLS: 4082",
"LEAF_NODE_MAX_CELLS: 13",
"db > ",
])
end
it 'prints all rows in a multi-level tree' do
script = []
(1..15).each do |i|
script << "insert #{i} user#{i} person#{i}@example.com"
end
script << "select"
script << ".exit"
result = run_script(script)
expect(result[15...result.length]).to match_array([
"db > (1, user1, [email protected])",
"(2, user2, [email protected])",
"(3, user3, [email protected])",
"(4, user4, [email protected])",
"(5, user5, [email protected])",
"(6, user6, [email protected])",
"(7, user7, [email protected])",
"(8, user8, [email protected])",
"(9, user9, [email protected])",
"(10, user10, [email protected])",
"(11, user11, [email protected])",
"(12, user12, [email protected])",
"(13, user13, [email protected])",
"(14, user14, [email protected])",
"(15, user15, [email protected])",
"Executed.", "db > ",
])
end
end