"""Tests to see if pageorg is working correctly. These tests originally came from the U. Wisconsin-Madison Minibase project, which I ported to Java, which I then ported to Python. Note that these tests running to completion does not indicate that your code is correct: you've got to get the output right.""" import pageorg import array import random class TestException(Exception): pass def test1(): page = pageorg.makePage() pageorg.setup(page) print "--- Test 1: Page Initialization Checks ---" print "Available Space: " , pageorg.getAvailableSpace(page) if not pageorg.empty(page): raise TestException("Page should be empty.") print "Page Empty as expected." pageorg.dumpPage(page); def test2(): buffSize = 20 limit = 20 tmpBuf = array.array('c','a'*buffSize) page = pageorg.makePage() pageorg.setup(page) print "--- Test 2: Insert and traversal of records ---" for i in range(limit): rid = pageorg.insertRecord(page,tmpBuf) print "Inserted record, RID", str(rid) rid = pageorg.nextRecord(page,rid) if pageorg.empty(page): raise TestException("The page cannot be empty") rid = pageorg.firstRecord(page) while rid != None: tmpBuf = pageorg.getRecord(page,rid) print "Retrieved record, RID", str(rid) rid = pageorg.nextRecord(page,rid) pageorg.dumpPage(page) def test3(): buffSize = 20 limit = 20 tmpBuf = array.array('c','\0'*buffSize) page = pageorg.makePage() pageorg.setup(page) print "--- Test 3: Insert and delete fixed sized records ---" for i in range(limit): rid = pageorg.insertRecord(page,tmpBuf) print "Inserted record, RID", str(rid) rid = pageorg.nextRecord(page,rid) if pageorg.empty(page): raise TestException("The page cannot be empty"); rid = pageorg.firstRecord(page) deleteList = [] i=0 prevRid = None while rid != None: tmpBuf = pageorg.getRecord(page,rid) if (i%4==0) and (4*i<3*limit): deleteList.append(rid) prevRid = rid rid = pageorg.nextRecord(page,rid) i+=1 deleteList.append(prevRid) # pick up the last one print "Deleting Records" for curRid in deleteList: print "Deleting Rid", str(curRid) if not pageorg.deleteRecord(page,curRid): raise TestException("Deletion of rid" + str(curRid) + \ "failed") print "After Deletion." rid = pageorg.firstRecord(page) while rid != None: print "Retrieving record ", str(rid) tmpBuf = pageorg.getRecord(page,rid) rid = pageorg.nextRecord(page,rid) def test4(): buffSize = 50 limit = 20 string1 = "Alphabet:" string2 = "Junk:" page = pageorg.makePage() pageorg.setup(page) print("--- Test 4: Insert and delete variable-length records ---") for i in range(limit): letter = chr(i+65) if i%2 == 0: string1 = string1 + letter print string1 pageorg.insertRecord(page,array.array('c',string1)) else: string2 = string2 + letter print string2 pageorg.insertRecord(page,array.array('c',string2)) if pageorg.empty(page): raise TestException("The page cannot be empty"); rid = pageorg.firstRecord(page); deleteList = [] i=0; j=0; prevRid = None while rid != None: if (j%4==0) and (4*j<3*limit): deleteList.append(rid) i +=1 # if compaction is taking place, we shall see prevRid = rid rid = pageorg.nextRecord(page,rid) j +=1 deleteList.append(prevRid) # pick up the last one pageorg.dumpPage(page) print "Deleting Records" for curRid in deleteList: print "Deleting Rid", curRid if not pageorg.deleteRecord(page,curRid): raise TestException("Deletion of rid", rid, "failed") rid = pageorg.firstRecord(page) while rid != None: print "Retrieving record ",rid print pageorg.getRecord(page,rid).tostring() rid = pageorg.nextRecord(page,rid) pageorg.dumpPage(page) def test5(): longStr = "" for i in range(2000): longStr = longStr + (chr)(i%26+65) print "--- Test 5: Test some error conditions ---" page = pageorg.makePage() pageorg.setup(page) # Deletion of empty page rid = -6 if pageorg.deleteRecord(page,rid): raise TestException("Empty space deletion.") print "No record is deleted." # FirstRecord of empty page rid = pageorg.firstRecord(page); if rid != None: raise TestException("None should be returned for an empty page.") print "FirstRecord in an empty page is handled correctly." # NextRecord of empty page try: curRid = pageorg.nextRecord(page,rid) raise TestException("nextRecord should not have succeeded(1)") except Exception, e: print "nextRecord handled correctly(1)." try: curRid = -6 curRid = pageorg.nextRecord(page,curRid) raise TestException("nextRecord should not have succeeded(2)") except Exception,e: print "nextRecord handled correctly(2)." # Overfilling pages try: pageorg.insertRecord(page,array.array('c',longStr)) raise TestException("Inserting long string should have failed") except Exception, e: print "Overflow handled correctly" def test6(): print "--- Test 6 ---" page = pageorg.makePage() pageorg.setup(page) initialSpace = pageorg.getAvailableSpace(page) print "Initial available space =",initialSpace maxStr = " "*initialSpace rid = pageorg.insertRecord(page,array.array('c',maxStr)) if not pageorg.deleteRecord(page,rid): raise TestException("Failed to delete maximum size record.") secondSpace = pageorg.getAvailableSpace(page) print "Second available space =", secondSpace if secondSpace != initialSpace: raise TestException("Free space has changed") # insert as many records of random size as will fit records = [] random.seed(90125) longStr = "" for i in range(2000): longStr = longStr + (chr)(i%26+65) try: while True: data = longStr[0:random.randint(1,10)] print 'Inserting',data rid = pageorg.insertRecord(page,array.array('c',data)) records.append((rid,data)) except pageorg.PageFullException,e: pass print "Inserted", len(records),"records" print "Start of deletion" print "space = ",pageorg.getAvailableSpace(page) while len(records) > 0: curRec = random.randrange(0,len(records)) record = pageorg.getRecord(page,records[curRec][0]).tostring() if len(record) != len(records[curRec][1]): print "ERROR: record", curRec, "has length", len(record), \ "and should be", len(records[curRec][1]) if record != records[curRec][1]: print "ERROR: record", curRec, "has value", record, \ "and should be", records[curRec][1] print 'Deleting',record if not pageorg.deleteRecord(page,records[curRec][0]): print "ERROR: failed to delete record",curRec records.pop(curRec) print "End of deletion" finalSpace = pageorg.getAvailableSpace(page) if finalSpace != initialSpace: raise TestException("Final free space does not match initial") def runTest(test): success = True try: test() except TestException, e: print 'Test Exception thrown' success = False print e return success; runTest(test1) runTest(test2) runTest(test3) runTest(test4) runTest(test5) runTest(test6)