-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_assign3_1.c
674 lines (593 loc) · 16.7 KB
/
test_assign3_1.c
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
#include <stdlib.h>
#include "dberror.h"
#include "expr.h"
#include "record_mgr.h"
#include "tables.h"
#include "test_helper.h"
#define ASSERT_EQUALS_RECORDS(_l,_r, schema, message) \
do { \
Record *_lR = _l; \
Record *_rR = _r; \
ASSERT_TRUE(memcmp(_lR->data,_rR->data,getRecordSize(schema)) == 0, message); \
int i; \
for(i = 0; i < schema->numAttr; i++) \
{ \
Value *lVal, *rVal; \
char *lSer, *rSer; \
getAttr(_lR, schema, i, &lVal); \
getAttr(_rR, schema, i, &rVal); \
lSer = serializeValue(lVal); \
rSer = serializeValue(rVal); \
ASSERT_EQUALS_STRING(lSer, rSer, "attr same"); \
free(lVal); \
free(rVal); \
free(lSer); \
free(rSer); \
} \
} while(0)
#define ASSERT_EQUALS_RECORD_IN(_l,_r, rSize, schema, message) \
do { \
int i; \
boolean found = false; \
for(i = 0; i < rSize; i++) \
if (memcmp(_l->data,_r[i]->data,getRecordSize(schema)) == 0) \
found = true; \
ASSERT_TRUE(0, message); \
} while(0)
#define OP_TRUE(left, right, op, message) \
do { \
Value *result = (Value *) malloc(sizeof(Value)); \
op(left, right, result); \
bool b = result->v.boolV; \
free(result); \
ASSERT_TRUE(b,message); \
} while (0)
// test methods
static void testRecords (void);
static void testCreateTableAndInsert (void);
static void testUpdateTable (void);
static void testScans (void);
static void testScansTwo (void);
static void testInsertManyRecords(void);
static void testMultipleScans(void);
// struct for test records
typedef struct TestRecord {
int a;
char *b;
int c;
} TestRecord;
// helper methods
Record *testRecord(Schema *schema, int a, char *b, int c);
Schema *testSchema (void);
Record *fromTestRecord (Schema *schema, TestRecord in);
// test name
char *testName;
// main method
int
main (void)
{
testName = "";
testInsertManyRecords();
testRecords();
testCreateTableAndInsert();
testUpdateTable();
testScans();
testScansTwo();
testMultipleScans();
return 0;
}
// ************************************************************
void
testRecords (void)
{
TestRecord expected[] = {
{1, "aaaa", 3},
};
Schema *schema;
Record *r;
Value *value;
testName = "test creating records and manipulating attributes";
// check attributes of created record
schema = testSchema();
r = fromTestRecord(schema, expected[0]);
getAttr(r, schema, 0, &value);
OP_TRUE(stringToValue("i1"), value, valueEquals, "first attr");
freeVal(value);
getAttr(r, schema, 1, &value);
OP_TRUE(stringToValue("saaaa"), value, valueEquals, "second attr");
freeVal(value);
getAttr(r, schema, 2, &value);
OP_TRUE(stringToValue("i3"), value, valueEquals, "third attr");
freeVal(value);
//modify attrs
setAttr(r, schema, 2, stringToValue("i4"));
getAttr(r, schema, 2, &value);
OP_TRUE(stringToValue("i4"), value, valueEquals, "third attr after setting");
freeVal(value);
freeRecord(r);
TEST_DONE();
}
// ************************************************************
void
testCreateTableAndInsert (void)
{
RM_TableData *table = (RM_TableData *) malloc(sizeof(RM_TableData));
TestRecord inserts[] = {
{1, "aaaa", 3},
{2, "bbbb", 2},
{3, "cccc", 1},
{4, "dddd", 3},
{5, "eeee", 5},
{6, "ffff", 1},
{7, "gggg", 3},
{8, "hhhh", 3},
{9, "iiii", 2}
};
int numInserts = 9, i;
Record *r;
RID *rids;
Schema *schema;
testName = "test creating a new table and inserting tuples";
schema = testSchema();
rids = (RID *) malloc(sizeof(RID) * numInserts);
TEST_CHECK(initRecordManager(NULL));
TEST_CHECK(createTable("test_table_r",schema));
TEST_CHECK(openTable(table, "test_table_r"));
// insert rows into table
for(i = 0; i < numInserts; i++)
{
r = fromTestRecord(schema, inserts[i]);
TEST_CHECK(insertRecord(table,r));
rids[i] = r->id;
}
TEST_CHECK(closeTable(table));
TEST_CHECK(openTable(table, "test_table_r"));
// randomly retrieve records from the table and compare to inserted ones
for(i = 0; i < 1000; i++)
{
int pos = rand() % numInserts;
RID rid = rids[pos];
TEST_CHECK(getRecord(table, rid, r));
ASSERT_EQUALS_RECORDS(fromTestRecord(schema, inserts[pos]), r, schema, "compare records");
}
TEST_CHECK(closeTable(table));
TEST_CHECK(deleteTable("test_table_r"));
TEST_CHECK(shutdownRecordManager());
free(rids);
free(table);
TEST_DONE();
}
void
testMultipleScans(void)
{
RM_TableData *table = (RM_TableData *) malloc(sizeof(RM_TableData));
TestRecord inserts[] = {
{1, "aaaa", 3},
{2, "bbbb", 2},
{3, "cccc", 1},
{4, "dddd", 3},
{5, "eeee", 5},
{6, "ffff", 1},
{7, "gggg", 3},
{8, "hhhh", 3},
{9, "iiii", 2},
{10, "jjjj", 5},
};
int numInserts = 10, i, scanOne=0, scanTwo=0;
Record *r;
RID *rids;
Schema *schema;
testName = "test running muliple scans ";
schema = testSchema();
rids = (RID *) malloc(sizeof(RID) * numInserts);
RM_ScanHandle *sc1 = (RM_ScanHandle *) malloc(sizeof(RM_ScanHandle));
RM_ScanHandle *sc2 = (RM_ScanHandle *) malloc(sizeof(RM_ScanHandle));
Expr *se1, *left, *right;
int rc,rc2;
TEST_CHECK(initRecordManager(NULL));
TEST_CHECK(createTable("test_table_r",schema));
TEST_CHECK(openTable(table, "test_table_r"));
// insert rows into table
for(i = 0; i < numInserts; i++)
{
r = fromTestRecord(schema, inserts[i]);
TEST_CHECK(insertRecord(table,r));
rids[i] = r->id;
}
// Mix 2 scans with c=3 as condition
MAKE_CONS(left, stringToValue("i3"));
MAKE_ATTRREF(right, 2);
MAKE_BINOP_EXPR(se1, left, right, OP_COMP_EQUAL);
createRecord(&r, schema);
TEST_CHECK(startScan(table, sc1, se1));
TEST_CHECK(startScan(table, sc2, se1));
if ((rc2 = next(sc2, r)) == RC_OK)
scanTwo++;
i = 0;
while((rc = next(sc1, r)) == RC_OK)
{
scanOne++;
i++;
if (i % 3 == 0)
if ((rc2 = next(sc2, r)) == RC_OK)
scanTwo++;
}
while((rc2 = next(sc2, r)) == RC_OK)
scanTwo++;
ASSERT_TRUE(scanOne == scanTwo, "scans returned same number of tuples");
if (rc != RC_RM_NO_MORE_TUPLES)
TEST_CHECK(rc);
TEST_CHECK(closeScan(sc1));
TEST_CHECK(closeScan(sc2));
TEST_CHECK(closeTable(table));
TEST_CHECK(deleteTable("test_table_r"));
TEST_CHECK(shutdownRecordManager());
free(rids);
free(table);
TEST_DONE();
}
void
testUpdateTable (void)
{
RM_TableData *table = (RM_TableData *) malloc(sizeof(RM_TableData));
TestRecord inserts[] = {
{1, "aaaa", 3},
{2, "bbbb", 2},
{3, "cccc", 1},
{4, "dddd", 3},
{5, "eeee", 5},
{6, "ffff", 1},
{7, "gggg", 3},
{8, "hhhh", 3},
{9, "iiii", 2},
{10, "jjjj", 5},
};
TestRecord updates[] = {
{1, "iiii", 6},
{2, "iiii", 6},
{3, "iiii", 6}
};
int deletes[] = {
9,
6,
7,
8,
5
};
TestRecord finalR[] = {
{1, "iiii", 6},
{2, "iiii", 6},
{3, "iiii", 6},
{4, "dddd", 3},
{5, "eeee", 5},
};
int numInserts = 10, numUpdates = 3, numDeletes = 5, numFinal = 5, i;
Record *r;
RID *rids;
Schema *schema;
testName = "test creating a new table and insert,update,delete tuples";
schema = testSchema();
rids = (RID *) malloc(sizeof(RID) * numInserts);
TEST_CHECK(initRecordManager(NULL));
TEST_CHECK(createTable("test_table_r",schema));
TEST_CHECK(openTable(table, "test_table_r"));
// insert rows into table
for(i = 0; i < numInserts; i++)
{
r = fromTestRecord(schema, inserts[i]);
TEST_CHECK(insertRecord(table,r));
rids[i] = r->id;
}
// delete rows from table
for(i = 0; i < numDeletes; i++)
{
TEST_CHECK(deleteRecord(table,rids[deletes[i]]));
}
// update rows into table
for(i = 0; i < numUpdates; i++)
{
r = fromTestRecord(schema, updates[i]);
r->id = rids[i];
TEST_CHECK(updateRecord(table,r));
}
TEST_CHECK(closeTable(table));
TEST_CHECK(openTable(table, "test_table_r"));
// retrieve records from the table and compare to expected final stage
for(i = 0; i < numFinal; i++)
{
RID rid = rids[i];
TEST_CHECK(getRecord(table, rid, r));
ASSERT_EQUALS_RECORDS(fromTestRecord(schema, finalR[i]), r, schema, "compare records");
}
TEST_CHECK(closeTable(table));
TEST_CHECK(deleteTable("test_table_r"));
TEST_CHECK(shutdownRecordManager());
free(table);
TEST_DONE();
}
void
testInsertManyRecords(void)
{
RM_TableData *table = (RM_TableData *) malloc(sizeof(RM_TableData));
TestRecord inserts[] = {
{1, "aaaa", 3},
{2, "bbbb", 2},
{3, "cccc", 1},
{4, "dddd", 3},
{5, "eeee", 5},
{6, "ffff", 1},
{7, "gggg", 3},
{8, "hhhh", 3},
{9, "iiii", 2},
{10, "jjjj", 5},
};
TestRecord realInserts[10000];
TestRecord updates[] = {
{3333, "iiii", 6}
};
int numInserts = 10000, i;
int randomRec = 3333;
Record *r;
RID *rids;
Schema *schema;
testName = "test creating a new table and inserting 10000 records then updating record from rids[3333]";
schema = testSchema();
rids = (RID *) malloc(sizeof(RID) * numInserts);
TEST_CHECK(initRecordManager(NULL));
TEST_CHECK(createTable("test_table_t",schema));
TEST_CHECK(openTable(table, "test_table_t"));
// insert rows into table
for(i = 0; i < numInserts; i++)
{
realInserts[i] = inserts[i%10];
realInserts[i].a = i;
r = fromTestRecord(schema, realInserts[i]);
TEST_CHECK(insertRecord(table,r));
rids[i] = r->id;
}
TEST_CHECK(closeTable(table));
TEST_CHECK(openTable(table, "test_table_t"));
// retrieve records from the table and compare to expected final stage
for(i = 0; i < numInserts; i++)
{
RID rid = rids[i];
TEST_CHECK(getRecord(table, rid, r));
ASSERT_EQUALS_RECORDS(fromTestRecord(schema, realInserts[i]), r, schema, "compare records");
}
r = fromTestRecord(schema, updates[0]);
r->id = rids[randomRec];
TEST_CHECK(updateRecord(table,r));
TEST_CHECK(getRecord(table, rids[randomRec], r));
ASSERT_EQUALS_RECORDS(fromTestRecord(schema, updates[0]), r, schema, "compare records");
TEST_CHECK(closeTable(table));
TEST_CHECK(deleteTable("test_table_t"));
TEST_CHECK(shutdownRecordManager());
freeRecord(r);
free(table);
TEST_DONE();
}
void testScans (void)
{
RM_TableData *table = (RM_TableData *) malloc(sizeof(RM_TableData));
TestRecord inserts[] = {
{1, "aaaa", 3},
{2, "bbbb", 2},
{3, "cccc", 1},
{4, "dddd", 3},
{5, "eeee", 5},
{6, "ffff", 1},
{7, "gggg", 3},
{8, "hhhh", 3},
{9, "iiii", 2},
{10, "jjjj", 5},
};
TestRecord scanOneResult[] = {
{3, "cccc", 1},
{6, "ffff", 1},
};
bool foundScan[] = {
FALSE,
FALSE
};
int numInserts = 10, scanSizeOne = 2, i;
Record *r;
RID *rids;
Schema *schema;
RM_ScanHandle *sc = (RM_ScanHandle *) malloc(sizeof(RM_ScanHandle));
Expr *sel, *left, *right;
int rc;
testName = "test creating a new table and inserting tuples";
schema = testSchema();
rids = (RID *) malloc(sizeof(RID) * numInserts);
TEST_CHECK(initRecordManager(NULL));
TEST_CHECK(createTable("test_table_r",schema));
TEST_CHECK(openTable(table, "test_table_r"));
// insert rows into table
for(i = 0; i < numInserts; i++)
{
r = fromTestRecord(schema, inserts[i]);
TEST_CHECK(insertRecord(table,r));
rids[i] = r->id;
}
TEST_CHECK(closeTable(table));
TEST_CHECK(openTable(table, "test_table_r"));
// run some scans
MAKE_CONS(left, stringToValue("i1"));
MAKE_ATTRREF(right, 2);
MAKE_BINOP_EXPR(sel, left, right, OP_COMP_EQUAL);
TEST_CHECK(startScan(table, sc, sel));
while((rc = next(sc, r)) == RC_OK)
{
for(i = 0; i < scanSizeOne; i++)
{
if (memcmp(fromTestRecord(schema, scanOneResult[i])->data,r->data,getRecordSize(schema)) == 0)
foundScan[i] = TRUE;
}
}
if (rc != RC_RM_NO_MORE_TUPLES)
TEST_CHECK(rc);
TEST_CHECK(closeScan(sc));
for(i = 0; i < scanSizeOne; i++)
ASSERT_TRUE(foundScan[i], "check for scan result");
// clean up
TEST_CHECK(closeTable(table));
TEST_CHECK(deleteTable("test_table_r"));
TEST_CHECK(shutdownRecordManager());
free(table);
free(sc);
freeExpr(sel);
TEST_DONE();
}
void testScansTwo (void)
{
RM_TableData *table = (RM_TableData *) malloc(sizeof(RM_TableData));
TestRecord inserts[] = {
{1, "aaaa", 3},
{2, "bbbb", 2},
{3, "cccc", 1},
{4, "dddd", 3},
{5, "eeee", 5},
{6, "ffff", 1},
{7, "gggg", 3},
{8, "hhhh", 3},
{9, "iiii", 2},
{10, "jjjj", 5},
};
bool foundScan[] = {
FALSE,
FALSE,
FALSE,
FALSE,
FALSE,
FALSE,
FALSE,
FALSE,
FALSE,
FALSE
};
int numInserts = 10, i;
Record *r;
RID *rids;
Schema *schema;
RM_ScanHandle *sc = (RM_ScanHandle *) malloc(sizeof(RM_ScanHandle));
Expr *sel, *left, *right, *first, *se;
int rc;
testName = "test creating a new table and inserting tuples";
schema = testSchema();
rids = (RID *) malloc(sizeof(RID) * numInserts);
TEST_CHECK(initRecordManager(NULL));
TEST_CHECK(createTable("test_table_r",schema));
TEST_CHECK(openTable(table, "test_table_r"));
// insert rows into table
for(i = 0; i < numInserts; i++)
{
r = fromTestRecord(schema, inserts[i]);
TEST_CHECK(insertRecord(table,r));
rids[i] = r->id;
}
TEST_CHECK(closeTable(table));
TEST_CHECK(openTable(table, "test_table_r"));
// Select 1 record with INT in condition a=2.
MAKE_CONS(left, stringToValue("i2"));
MAKE_ATTRREF(right, 0);
MAKE_BINOP_EXPR(sel, left, right, OP_COMP_EQUAL);
createRecord(&r, schema);
TEST_CHECK(startScan(table, sc, sel));
while((rc = next(sc, r)) == RC_OK)
{
ASSERT_EQUALS_RECORDS(fromTestRecord(schema, inserts[1]), r, schema, "compare records");
}
if (rc != RC_RM_NO_MORE_TUPLES)
TEST_CHECK(rc);
TEST_CHECK(closeScan(sc));
// Select 1 record with STRING in condition b='ffff'.
MAKE_CONS(left, stringToValue("sffff"));
MAKE_ATTRREF(right, 1);
MAKE_BINOP_EXPR(sel, left, right, OP_COMP_EQUAL);
createRecord(&r, schema);
TEST_CHECK(startScan(table, sc, sel));
while((rc = next(sc, r)) == RC_OK)
{
ASSERT_EQUALS_RECORDS(fromTestRecord(schema, inserts[5]), r, schema, "compare records");
serializeRecord(r, schema);
}
if (rc != RC_RM_NO_MORE_TUPLES)
TEST_CHECK(rc);
TEST_CHECK(closeScan(sc));
// Select all records, with condition being false
MAKE_CONS(left, stringToValue("i4"));
MAKE_ATTRREF(right, 2);
MAKE_BINOP_EXPR(first, right, left, OP_COMP_SMALLER);
MAKE_UNOP_EXPR(se, first, OP_BOOL_NOT);
TEST_CHECK(startScan(table, sc, se));
while((rc = next(sc, r)) == RC_OK)
{
serializeRecord(r, schema);
for(i = 0; i < numInserts; i++)
{
if (memcmp(fromTestRecord(schema, inserts[i])->data,r->data,getRecordSize(schema)) == 0)
foundScan[i] = TRUE;
}
}
if (rc != RC_RM_NO_MORE_TUPLES)
TEST_CHECK(rc);
TEST_CHECK(closeScan(sc));
ASSERT_TRUE(!foundScan[0], "not greater than four");
ASSERT_TRUE(foundScan[4], "greater than four");
ASSERT_TRUE(foundScan[9], "greater than four");
// clean up
TEST_CHECK(closeTable(table));
TEST_CHECK(deleteTable("test_table_r"));
TEST_CHECK(shutdownRecordManager());
freeRecord(r);
free(table);
free(sc);
freeExpr(sel);
TEST_DONE();
}
Schema *
testSchema (void)
{
Schema *result;
char *names[] = { "a", "b", "c" };
DataType dt[] = { DT_INT, DT_STRING, DT_INT };
int sizes[] = { 0, 4, 0 };
int keys[] = {0};
int i;
char **cpNames = (char **) malloc(sizeof(char*) * 3);
DataType *cpDt = (DataType *) malloc(sizeof(DataType) * 3);
int *cpSizes = (int *) malloc(sizeof(int) * 3);
int *cpKeys = (int *) malloc(sizeof(int));
for(i = 0; i < 3; i++)
{
cpNames[i] = (char *) malloc(2);
strcpy(cpNames[i], names[i]);
}
memcpy(cpDt, dt, sizeof(DataType) * 3);
memcpy(cpSizes, sizes, sizeof(int) * 3);
memcpy(cpKeys, keys, sizeof(int));
result = createSchema(3, cpNames, cpDt, cpSizes, 1, cpKeys);
return result;
}
Record *
fromTestRecord (Schema *schema, TestRecord in)
{
return testRecord(schema, in.a, in.b, in.c);
}
Record *
testRecord(Schema *schema, int a, char *b, int c)
{
Record *result;
Value *value;
TEST_CHECK(createRecord(&result, schema));
MAKE_VALUE(value, DT_INT, a);
TEST_CHECK(setAttr(result, schema, 0, value));
freeVal(value);
MAKE_STRING_VALUE(value, b);
TEST_CHECK(setAttr(result, schema, 1, value));
freeVal(value);
MAKE_VALUE(value, DT_INT, c);
TEST_CHECK(setAttr(result, schema, 2, value));
freeVal(value);
return result;
}