// test the LRU page replacement strategy void testLRU (void) { // expected results const char *poolContents[] = { // read first five pages and directly unpin them "[0 0],[-1 0],[-1 0],[-1 0],[-1 0]" , "[0 0],[1 0],[-1 0],[-1 0],[-1 0]", "[0 0],[1 0],[2 0],[-1 0],[-1 0]", "[0 0],[1 0],[2 0],[3 0],[-1 0]", "[0 0],[1 0],[2 0],[3 0],[4 0]", // use some of the page to create a fixed LRU order without changing pool content "[0 0],[1 0],[2 0],[3 0],[4 0]", "[0 0],[1 0],[2 0],[3 0],[4 0]", "[0 0],[1 0],[2 0],[3 0],[4 0]", "[0 0],[1 0],[2 0],[3 0],[4 0]", "[0 0],[1 0],[2 0],[3 0],[4 0]", // check that pages get evicted in LRU order "[0 0],[1 0],[2 0],[5 0],[4 0]", "[0 0],[1 0],[2 0],[5 0],[6 0]", "[7 0],[1 0],[2 0],[5 0],[6 0]", "[7 0],[1 0],[8 0],[5 0],[6 0]", "[7 0],[9 0],[8 0],[5 0],[6 0]" }; const int orderRequests[] = {3,4,0,2,1}; const int numLRUOrderChange = 5; int i; int snapshot = 0; BM_BufferPool *bm = MAKE_POOL(); BM_PageHandle *h = MAKE_PAGE_HANDLE(); testName = "Testing LRU page replacement"; CHECK(createPageFile("testbuffer.bin")); createDummyPages(bm, 100); CHECK(initBufferPool(bm, "testbuffer.bin", 5, RS_LRU, NULL)); // reading first five pages linearly with direct unpin and no modifications for(i = 0; i < 5; i++) { pinPage(bm, h, i); unpinPage(bm, h); ASSERT_EQUALS_POOL(poolContents[snapshot++], bm, "check pool content reading in pages"); } // read pages to change LRU order for(i = 0; i < numLRUOrderChange; i++) { pinPage(bm, h, orderRequests[i]); unpinPage(bm, h); ASSERT_EQUALS_POOL(poolContents[snapshot++], bm, "check pool content using pages"); } // replace pages and check that it happens in LRU order for(i = 0; i < 5; i++) { pinPage(bm, h, 5 + i); unpinPage(bm, h); ASSERT_EQUALS_POOL(poolContents[snapshot++], bm, "check pool content using pages"); } // check number of write IOs ASSERT_EQUALS_INT(0, getNumWriteIO(bm), "check number of write I/Os"); ASSERT_EQUALS_INT(10, getNumReadIO(bm), "check number of read I/Os"); CHECK(shutdownBufferPool(bm)); CHECK(destroyPageFile("testbuffer.bin")); free(bm); free(h); TEST_DONE(); }
void testFIFO () { // expected results const char *poolContents[] = { "[0 0],[-1 0],[-1 0]" , "[0 0],[1 0],[-1 0]", "[0 0],[1 0],[2 0]", "[3 0],[1 0],[2 0]", "[3 0],[4 0],[2 0]", "[3 0],[4 1],[2 0]", "[3 0],[4 1],[5x0]", "[6x0],[4 1],[5x0]", "[6x0],[4 1],[0x0]", "[6x0],[4 0],[0x0]", "[6 0],[4 0],[0 0]" }; const int requests[] = {0,1,2,3,4,4,5,6,0}; const int numLinRequests = 5; const int numChangeRequests = 3; int i; BM_BufferPool *bm = MAKE_POOL(); BM_PageHandle *h = MAKE_PAGE_HANDLE(); testName = "Testing FIFO page replacement"; CHECK(createPageFile("testbuffer.bin")); createDummyPages(bm, 100); CHECK(initBufferPool(bm, "testbuffer.bin", 3, RS_FIFO, NULL)); // reading some pages linearly with direct unpin and no modifications for(i = 0; i < numLinRequests; i++) { pinPage(bm, h, requests[i]); unpinPage(bm, h); ASSERT_EQUALS_POOL(poolContents[i], bm, "check pool content"); } // pin one page and test remainder i = numLinRequests; pinPage(bm, h, requests[i]); ASSERT_EQUALS_POOL(poolContents[i],bm,"pool content after pin page"); // read pages and mark them as dirty for(i = numLinRequests + 1; i < numLinRequests + numChangeRequests + 1; i++) { pinPage(bm, h, requests[i]); markDirty(bm, h); unpinPage(bm, h); ASSERT_EQUALS_POOL(poolContents[i], bm, "check pool content"); } // flush buffer pool to disk i = numLinRequests + numChangeRequests + 1; h->pageNum = 4; unpinPage(bm, h); ASSERT_EQUALS_POOL(poolContents[i],bm,"unpin last page"); i++; forceFlushPool(bm); ASSERT_EQUALS_POOL(poolContents[i],bm,"pool content after flush"); // check number of write IOs ASSERT_EQUALS_INT(3, getNumWriteIO(bm), "check number of write I/Os"); ASSERT_EQUALS_INT(8, getNumReadIO(bm), "check number of read I/Os"); CHECK(shutdownBufferPool(bm)); CHECK(destroyPageFile("testbuffer.bin")); free(bm); free(h); TEST_DONE(); }
//Clock[START] void testClock() { // expected results const char *poolContents[] = { "[0 0],[-1 0],[-1 0]", "[0 0],[4 0],[-1 0]", "[0 0],[4 0],[1 0]", "[0 0],[4 0],[1 0]", "[2 0],[4 0],[1 0]", "[2 0],[4 0],[1 0]", "[2 0],[4 0],[3 0]", "[2 0],[4 0],[3 0]", "[2 0],[4 0],[3 0]", "[2 0],[4 0],[3 0]", "[2 0],[4 0],[0 0]", "[2 0],[4 0],[0 0]", "[1 0],[4 0],[0 0]", "[1 0],[4 0],[0 0]", "[1 0],[4 0],[2 0]", "[1 0],[4 0],[2 0]", "[3 0],[4 0],[2 0]", "[3 0],[4 0],[2 0]", "[3 0],[4 0],[2 0]", }; const int requests[] = {0,4,1,4,2,4,3,4,2,4,0,4,1,4,2,4,3,4}; int i; BM_BufferPool *bm = MAKE_POOL(); BM_PageHandle *h = MAKE_PAGE_HANDLE(); testName = "Testing CLOCK page replacement"; CHECK(createPageFile("testbuffer.bin")); createDummyPages(bm, 100); CHECK(initBufferPool(bm, "testbuffer.bin", 3, RS_CLOCK, NULL)); // reading some pages linearly with direct unpin and no modifications for(i = 0; i < 18; i++) { pinPage(bm, h, requests[i]); unpinPage(bm, h); ASSERT_EQUALS_POOL(poolContents[i], bm, "check pool content"); } forceFlushPool(bm); ASSERT_EQUALS_POOL(poolContents[i],bm,"pool content after flush"); // check number of write IOs //0 writes because no dirty pages were present ASSERT_EQUALS_INT(0, getNumWriteIO(bm), "check number of write I/Os"); ASSERT_EQUALS_INT(9, getNumReadIO(bm), "check number of read I/Os"); CHECK(shutdownBufferPool(bm)); CHECK(destroyPageFile("testbuffer.bin")); free(bm); free(h); TEST_DONE(); }
void testLFU() { // expected results const char *poolContents[] = { //Pin and Unpin 3 pages "[0 0],[-1 0],[-1 0]" , "[0 0],[1 0],[-1 0]", "[0 0],[1 0],[2 0]", //Pin the 0th page thrice,1st page twice "[0 3],[1 2],[2 0]", //Now pin a new page say 4. 4 should replace page 2 as page 2 has been Least Freq Used. "[0 3],[1 2],[4 1]", //Pin the 4th page thrice "[0 3],[1 2],[4 4]", //Now pin a new page 5. 5 should replace page 1 as page 1 has been Least Freq Used. "[0 3],[5 1],[4 4]", //Unpin page 0 thrice, page 5 once and page 4 four times. "[0 0],[5 0],[4 0]", }; const int requests[] = {0,1,2,3,4,5,6}; const int numLinRequests = 3; int i; BM_BufferPool *bm = MAKE_POOL(); BM_PageHandle *h = MAKE_PAGE_HANDLE(); testName = "Testing LFU page replacement"; CHECK(createPageFile("testbuffer.bin")); createDummyPages(bm, 100); CHECK(initBufferPool(bm, "testbuffer.bin", 3, RS_LFU, NULL)); // reading some pages linearly with direct unpin and no modifications for(i = 0; i < numLinRequests; i++) { pinPage(bm, h, requests[i]); unpinPage(bm, h); ASSERT_EQUALS_POOL(poolContents[i], bm, "check pool content"); } pinPage(bm, h, 0); pinPage(bm, h, 0); pinPage(bm, h, 0); pinPage(bm, h, 1); pinPage(bm, h, 1); ASSERT_EQUALS_POOL(poolContents[i++],bm,"pool content after pin page"); pinPage(bm, h, 4); ASSERT_EQUALS_POOL(poolContents[i++],bm,"pool content after pin page"); pinPage(bm,h,4); pinPage(bm,h,4); pinPage(bm,h,4); ASSERT_EQUALS_POOL(poolContents[i++],bm,"pool content after pin page"); pinPage(bm,h,5); ASSERT_EQUALS_POOL(poolContents[i++],bm,"pool content after pin page"); h->data = "Page-0"; h->pageNum = 0; unpinPage(bm,h); unpinPage(bm,h); unpinPage(bm,h); h->data = "Page-5"; h->pageNum = 5; unpinPage(bm,h); h->data = "Page-4"; h->pageNum = 4; unpinPage(bm,h); unpinPage(bm,h); unpinPage(bm,h); unpinPage(bm,h); forceFlushPool(bm); ASSERT_EQUALS_POOL(poolContents[i],bm,"pool content after flush"); // check number of write IOs //0 writes because no dirty pages were present ASSERT_EQUALS_INT(0, getNumWriteIO(bm), "check number of write I/Os"); ASSERT_EQUALS_INT(5, getNumReadIO(bm), "check number of read I/Os"); CHECK(shutdownBufferPool(bm)); CHECK(destroyPageFile("testbuffer.bin")); free(bm); free(h); TEST_DONE(); }
void testReadWrite(void) { SM_FileHandle fh; SM_PageHandle ph; int i; ph = (SM_PageHandle) malloc(PAGE_SIZE); testName = "test read and write methods"; TEST_CHECK(createPageFile (TESTPF)); TEST_CHECK(openPageFile (TESTPF, &fh)); ASSERT_TRUE(strcmp(fh.fileName, TESTPF) == 0, "filename correct"); ASSERT_TRUE((fh.totalNumPages == 1), "expect 1 page in new file"); ASSERT_TRUE((fh.curPagePos == 0), "freshly opened file's page position should be 0"); //Writes A, B, C, D, E, F, G, H from 0th page to 7th page for(i = 0; i < 8; i ++) { memset(ph, 'A' + i, PAGE_SIZE); TEST_CHECK(writeBlock (i, &fh, ph)); printf("writing %d th block\n", fh.curPagePos); } // read first page into handle i.e. A TEST_CHECK(readFirstBlock (&fh, ph)); for (i=0; i < PAGE_SIZE; i++) ASSERT_TRUE((ph[i] == 'A'), "expected A"); printf("first block contains A\n"); // read last page into handle i.e. H TEST_CHECK(readLastBlock (&fh, ph)); for (i=0; i < PAGE_SIZE; i++) ASSERT_TRUE((ph[i] == 'H'), "expected H"); printf("last block contains H\n"); // read first page into handle i.e. A readFirstBlock (&fh, ph); // read next page into handle i.e. B TEST_CHECK(readNextBlock (&fh, ph)); for (i=0; i < PAGE_SIZE; i++) ASSERT_TRUE((ph[i] == 'B'), "expected B"); printf("block contains B\n"); readNextBlock (&fh, ph); //C readNextBlock (&fh, ph); //D // read previous page into handle i.e. C TEST_CHECK(readPreviousBlock (&fh, ph)); for (i=0; i < PAGE_SIZE; i++) ASSERT_TRUE((ph[i] == 'C'), "expected C"); printf("block contains C\n"); readNextBlock (&fh, ph); //D readNextBlock (&fh, ph); //E // read current page into handle i.e. E TEST_CHECK(readCurrentBlock (&fh, ph)); for (i=0; i < PAGE_SIZE; i++) ASSERT_TRUE((ph[i] == 'E'), "expected E"); printf("block contains E\n"); readPreviousBlock (&fh, ph); //D //Replace D with Z memset(ph, 'Z', PAGE_SIZE); TEST_CHECK(writeCurrentBlock (&fh, ph)); printf("writing D with Z\n"); // read current page into handle i.e. Z TEST_CHECK(readCurrentBlock (&fh, ph)); for (i=0; i < PAGE_SIZE; i++){ ASSERT_TRUE((ph[i] == 'Z'), "expected Z"); } printf("block contains Z\n"); TEST_CHECK(closePageFile (&fh)); TEST_CHECK(destroyPageFile (TESTPF)); free(ph); TEST_DONE(); }
void testAppendEnsureCapMetaData() { SM_FileHandle fh; SM_PageHandle ph; ph = (SM_PageHandle) malloc(PAGE_SIZE); TEST_CHECK(createPageFile (TESTPF)); TEST_CHECK(openPageFile (TESTPF, &fh)); //Append an empty block to the file. appendEmptyBlock(&fh); //Check whether the appended block has only 4096 '\0' in the currentBlock. readBlock(getBlockPos(&fh),&fh,ph); int i; for (i=0; i < PAGE_SIZE; i++) ASSERT_TRUE((ph[i] == 0), "expected zero byte in first page of freshly initialized page"); printf("Appended Block was empty\n"); //Page File should contain only 2 blocks.first block during createPage and second during appendBlock ASSERT_TRUE((fh.totalNumPages == 2), "Number of Blocks : 2"); //Current Block postion should be 1 ASSERT_TRUE((fh.curPagePos == 1), "Current Page Position is 1"); //add 3 more blocks to the Page File. ensureCapacity(5,&fh); //Verify whether the freshly added 3 blocks are of '\0' characters //[START] readBlock(2,&fh,ph); for (i=0; i < PAGE_SIZE; i++) ASSERT_TRUE((ph[i] == 0), "expected zero byte in first page of freshly initialized page"); readBlock(3,&fh,ph); for (i=0; i < PAGE_SIZE; i++) ASSERT_TRUE((ph[i] == 0), "expected zero byte in first page of freshly initialized page"); readBlock(4,&fh,ph); for (i=0; i < PAGE_SIZE; i++) ASSERT_TRUE((ph[i] == 0), "expected zero byte in first page of freshly initialized page"); printf("Freshly appended 3 blocks are empty\n"); //[END] //Page File should contain only 5 blocks, as we have called ensureCapacity(5) ASSERT_TRUE((fh.totalNumPages == 5), "Number of Blocks : 5"); //Current Block postion should be 4 ASSERT_TRUE((fh.curPagePos == 4), "Current Page Position is 4"); //Store the metaData into the file and close the pagefile. int totalNoOfPages = fh.totalNumPages; char fileName[100]; memset(fileName,'\0',100); strcpy(fileName,fh.fileName); char metaDataFromFile[100]; memset(metaDataFromFile,'\0',100); closePageFile(&fh); //Verify whether the written MetaData is correct or not //[START] char metaDataToBeVerified[100]; memset(metaDataToBeVerified,'\0',100); char returnData[100]; memset(returnData,'\0',100); metaDataToBeVerified[0]= 'P';metaDataToBeVerified[1]= 'S';metaDataToBeVerified[2]= ':';metaDataToBeVerified[3]= '\0'; getString(PAGE_SIZE,returnData); strcat(metaDataToBeVerified,returnData); strcat(metaDataToBeVerified,";"); memset(returnData,'\0',100); strcat(metaDataToBeVerified,"NP:"); getString(totalNoOfPages,returnData); strcat(metaDataToBeVerified,returnData); strcat(metaDataToBeVerified,";"); readMetaDataFromFile(fileName,metaDataFromFile); ASSERT_TRUE((strcmp(metaDataToBeVerified, metaDataFromFile) == 0), "MetaData read from file is correct"); printf("Read Meta Data from file is :: %s\n",metaDataToBeVerified); //[END] TEST_CHECK(destroyPageFile(TESTPF)); free(ph); TEST_DONE(); }