📄 clocktestsimple.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 ClockDriverSimple 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 ClockDriverSimple() { super("clocktest"); } /** * calls the runTests function in TestDriver */ public boolean runTests(String replacerArg) { System.out.print("\n" + "Running " + testName() + " tests...." + "\n"); int numOfBuffer = 5; try { SystemDefs sysdef = new SystemDefs(dbpath, 50, numOfBuffer, 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 = 5; Page pg = new Page(); PageId pid = new PageId(); boolean status = OK; System.out.print(" - Allocate 4 new pages, one at a time, and leave them pinned\n"); // allocate 5 pages for (index = 0; status == OK && index < 4; ++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(); } } System.out.println(" frame table: 4, 5, 1, 2, 3"); int[] ans1 = {4, 5, 1, 2, 3}; FrameDesc[] frmeTable = SystemDefs.JavabaseBM.frameTable(); for (index = 0; index < numPages; index++) if (ans1[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 completed successfully.\n\n"); //unpin page 3 and 4 System.out.print(" - Unpin page 3 and 4\n"); pid = new PageId(3); try { SystemDefs.JavabaseBM.unpinPage(pid, true); } catch (Exception e) { status = FAIL; System.err.print("*** Could not flush page " + index + 1 + "\n"); e.printStackTrace(); } pid = new PageId(4); try { SystemDefs.JavabaseBM.unpinPage(pid, true); } catch (Exception e) { status = FAIL; System.err.print("*** Could not flush page " + index + 1 + "\n"); e.printStackTrace(); } status = OK; System.out.println(" frame table: 4, 5, 1, 2, 3"); int[] ans2 = {4, 5, 1, 2, 3}; for (index = 0; index < numPages; index++) if (ans2[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 completed successfully.\n\n"); // allocate 1 new page System.out.print(" - Allocate the 5th page\n"); 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(); } System.out.println(" frame table: 4, 5, 6, 2, 3"); int[] ans3 = {4, 5, 6, 2, 3}; status = OK; for (index = 0; index < numPages; index++) if (ans3[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 completed successfully.\n\n"); // allocate 1 new page System.out.print(" - Allocate the 6th page\n"); 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(); } System.out.println(" frame table: 7, 5, 6, 2, 1"); int[] ans4 = {7, 5, 6, 2, 1}; status = OK; for (index = 0; index < numPages; index++) if (ans4[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 completed successfully.\n\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 "Simple Clock Replacement Policy"; }}public class ClockTestSimple { public static void main(String argv[]) { ClockDriverSimple bmt = new ClockDriverSimple(); 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 + -