📄 clocktest.java
字号:
package tests;import java.io.IOException;import chainexception.ChainException;import diskmgr.Page;import global.Convert;import global.GlobalConst;import global.PageId;import global.SystemDefs;import bufmgr.*;//Note that in JAVA, methods can't be overridden to be more private.//Therefore, the declaration of all private functions are now declared//protected as opposed to the private type in C++./** * This class provides the functions to test the buffer manager */class ClockDriver extends TestDriver implements GlobalConst { private int TRUE = 1; private int FALSE = 0; private boolean OK = true; private boolean FAIL = false; /** * ClockDriver Constructor, inherited from TestDriver */ public ClockDriver() { super("clocktest"); } /** * calls the runTests function in TestDriver */ public boolean runTests(String replacerArg) { System.out.print("\n" + "Running " + testName() + " tests...." + "\n"); try { SystemDefs sysdef = new SystemDefs(dbpath, NUMBUF + 20, NUMBUF, replacerArg); } catch (Exception e) { Runtime.getRuntime().exit(1); } // Kill anything that might be hanging around String newdbpath; String newlogpath; String remove_logcmd; String remove_dbcmd; String remove_cmd = "/bin/rm -rf "; newdbpath = dbpath; newlogpath = logpath; remove_logcmd = remove_cmd + logpath; remove_dbcmd = remove_cmd + dbpath; // Commands here is very machine dependent. We assume // user are on UNIX system here. If we need to port this // program to other platform, the remove_cmd have to be // modified accordingly. try { Runtime.getRuntime().exec(remove_logcmd); Runtime.getRuntime().exec(remove_dbcmd); } catch (IOException e) { System.err.println("" + e); } remove_logcmd = remove_cmd + newlogpath; remove_dbcmd = remove_cmd + newdbpath; //This step seems redundant for me. But it's in the original //C++ code. So I am keeping it as of now, just in case //I missed something try { Runtime.getRuntime().exec(remove_logcmd); Runtime.getRuntime().exec(remove_dbcmd); } catch (IOException e) { System.err.println("" + e); } //Run the tests. Return type different from C++ boolean _pass = runAllTests(); //Clean up again try { Runtime.getRuntime().exec(remove_logcmd); Runtime.getRuntime().exec(remove_dbcmd); } catch (IOException e) { System.err.println("" + e); } System.out.print("\n" + "..." + testName() + " tests "); System.out.print(_pass == OK ? "completely successfully" : "failed"); System.out.print(".\n\n"); return _pass; } protected boolean runAllTests() { boolean _passAll = OK; //The following runs all the test functions //Running test1() to test6() if (!test1()) { _passAll = FAIL; } if (!test2()) { _passAll = FAIL; } if (!test3()) { _passAll = FAIL; } if (!test4()) { _passAll = FAIL; } if (!test5()) { _passAll = FAIL; } if (!test6()) { _passAll = FAIL; } return _passAll; } /** * overrides the test1 function in TestDriver. It tests some * simple normal buffer manager operations. * * @return whether test1 has passed */ protected boolean test1 () { System.out.print("\n Test 1 exercises the Clock buffer replacement policy\n "); int index; int numPages = NUMBUF; Page pg = new Page(); PageId pid = new PageId(); PageId [] pidsdisk = new PageId[numPages]; PageId [] pids = new PageId[numPages]; boolean status = OK; System.out.print(" - Allocate and dirty some new pages, one at " + "a time, and leave some pinned\n"); // allocate 20 pages and write to disk for (index = 0; status == OK && index < 20; ++index) { try { pid = SystemDefs.JavabaseBM.newPage(pg, 1); } catch (Exception e) { status = FAIL; System.err.print ("*** Could not allocate new page number " + index+1 + "\n"); e.printStackTrace(); } if ( status == OK ) { pidsdisk[index] = pid; } try { SystemDefs.JavabaseBM.unpinPage(pid, true); SystemDefs.JavabaseBM.flushPage(pid); } catch (Exception e) { status = FAIL; System.err.print ("*** Could not flush page " + index+1 + "\n"); e.printStackTrace(); } } // allocate 40 pages and unpin some pages for ( index=0; status == OK && index < 40; ++index ) { try { pid = SystemDefs.JavabaseBM.newPage( pg, 1 ); } catch (Exception e) { status = FAIL; System.err.print ("*** Could not allocate new page number " + index+1 + "\n"); e.printStackTrace(); } if ( status == OK ) { pids[index] = pid; if ( index % 3 == 0 ) { try { SystemDefs.JavabaseBM.unpinPage( pid, true ); } catch (Exception e) { status = FAIL; System.err.print("*** Could not unpin dirty page "+pid.pid+"\n"); } } } } // test replacement policy if ( status == OK ) { System.out.print (" - Read the pages\n"); for (index = 0; index < 10; index++) { pid = pids[index]; if (index % 2 == 0) pid = pidsdisk[index]; try { SystemDefs.JavabaseBM.pinPage( pid, pg, true); } catch (Exception e) { status = FAIL; System.err.print("*** Could not pin page " + pid.pid + "\n"); e.printStackTrace(); } } for (index = 0; index < 20; index++) { pid = pids[index]; if ( index % 10 == 7 ) { try { SystemDefs.JavabaseBM.unpinPage( pid, true ); } catch (Exception e) { status = FAIL; System.err.print("*** Could not unpin dirty page "+pid.pid+"\n"); } } } for (index = 10; index < 20; index++) { pid = pids[index]; if (index % 2 == 1) pid = pidsdisk[index]; try { SystemDefs.JavabaseBM.pinPage( pid, pg, true); } catch (Exception e) { status = FAIL; System.err.print("*** Could not pin page " + pid.pid + "\n"); e.printStackTrace(); } } } int [] ans = {49, 50, 1, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 2, 4, 6, 8, 10, 13, 15, 17, 19, 21, 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}; FrameDesc[] frmeTable = SystemDefs.JavabaseBM.frameTable(); /*for (index = 0; index < numPages; index++) System.out.print(frmeTable[index].pageNo.pid + ", "); System.out.println();*/ for (index = 0; index < numPages; index++) if (ans[index] != frmeTable[index].pageNo.pid) { status = FAIL; break; } if (status == FAIL) { System.err.print(" Error: Wrong Frame Table\n"); } if ( status == OK ) System.out.print(" Test 1 completed successfully.\n"); return status; } protected boolean test2() { return true; } protected boolean test3() { return true; } protected boolean test4() { return true; } protected boolean test5() { return true; } protected boolean test6() { return true; } /** * overrides the testName function in TestDriver * * @return the name of the test */ protected String testName() { return "Clock Replacement Policy"; }}public class ClockTest { public static void main(String argv[]) { ClockDriver bmt = new ClockDriver(); boolean dbstatus; dbstatus = bmt.runTests("Clock"); if (dbstatus != true) { System.err .println("Error encountered during Clock replacement policy tests.\n"); Runtime.getRuntime().exit(0); } Runtime.getRuntime().exit(0); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -