📄 dbtest.java
字号:
package tests;import java.io.*;import java.util.*;import java.lang.*;import diskmgr.*;import global.*;class DBDriver extends TestDriver implements GlobalConst { private PageId runStart = new PageId(); private boolean OK = true; private boolean FAIL = false; public DBDriver() { super("dbtest"); } public boolean runTests () { System.out.println ("\n" + "Running " + testName() + " tests...." + "\n"); // 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; 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; try { Runtime.getRuntime().exec(remove_logcmd); Runtime.getRuntime().exec(remove_dbcmd); } catch (IOException e) { System.err.println (""+e); } 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.println("All tests are done."); return _pass; } protected boolean test1() { System.out.println("======="); System.out.println("Test 1: create a new database with 8193 pages long"); System.out.println("======="); // create a new database try { SystemDefs sysdef = new SystemDefs("test.db", 8193, 100, "Clock"); } catch (InvalidPageNumberException e) { } catch (Exception e) { } boolean status = OK; try{ // check the space map Page page = new Page(); PageId page_id = new PageId(); page_id.pid = 1; SystemDefs.JavabaseBM.pinPage(page_id, page, false /* read disk */); dumpSpaceMap("11100000", "00000111", page, 1); SystemDefs.JavabaseBM.unpinPage(page_id, false /* undirty */); SystemDefs.JavabaseDB.DBDestroy(); } catch(Exception e) { status = FAIL; System.err.println(" ERROR: create database failed"); e.printStackTrace(); } System.out.println(" Test 1 is done."); System.out.println(); return status; } protected boolean test2() { System.out.println("======="); System.out.println("Test 2: allocate and deallocate pages"); System.out.println("======="); // create a new database try { SystemDefs sysdef = new SystemDefs("test.db", 8193, 100, "Clock"); } catch (InvalidPageNumberException e) { } catch (Exception e) { } boolean status = OK; try{ System.out.println(" action 1: allocate 10 pages"); System.out.println(" ---------"); PageId start_page_id = new PageId(); SystemDefs.JavabaseDB.allocate_page(start_page_id, 10); // check the space map Page page = new Page(); PageId page_id = new PageId(); page_id.pid = 1; SystemDefs.JavabaseBM.pinPage(page_id, page, false /* read disk */); dumpSpaceMap("11111111 11111000", "11111111 00011111", page, 2); SystemDefs.JavabaseBM.unpinPage(page_id, false /* undirty */); System.out.println(" action 2: deallocate page #3 to #5"); System.out.println(" ---------"); SystemDefs.JavabaseDB.deallocate_page(start_page_id, 3); SystemDefs.JavabaseBM.pinPage(page_id, page, false /* read disk */); dumpSpaceMap("11100011 11111000", "11000111 00011111", page, 2); SystemDefs.JavabaseBM.unpinPage(page_id, false /* undirty */); SystemDefs.JavabaseDB.DBDestroy(); } catch(Exception e) { status = FAIL; System.err.println(" ERROR: allocate/deallocate pages failed"); e.printStackTrace(); } System.out.println(" Test 2 is done."); System.out.println(); return status; } protected boolean test3() { System.out.println("======="); System.out.println("Test 3: insert, look up, and delete file entries"); System.out.println("======="); // create a new database try { SystemDefs sysdef = new SystemDefs("test.db", 8193, 100, "Clock"); } catch (InvalidPageNumberException e) { } catch (Exception e) { } boolean status = OK; try{ System.out.println(" action 1: insert 10 file entries"); System.out.println(" ---------"); PageId start_page_id = new PageId(); // insert 10 file entries for(int i = 4; i < 14; i ++) { start_page_id.pid = i; SystemDefs.JavabaseDB.add_file_entry("entry " + i, start_page_id); } System.out.println(" action 2: look up file entries #5, show its start page id"); System.out.println(" ---------"); System.out.println(" Exact Answer : 5"); // look up file entry #5 start_page_id = SystemDefs.JavabaseDB.get_file_entry("entry 5"); System.out.println(" Your Answer : " + start_page_id); // delete file entry #5 System.out.println(" action 3: delete file entries #5, then try to look up it again"); System.out.println(" ---------"); System.out.println(" Exact Answer : null"); SystemDefs.JavabaseDB.delete_file_entry("entry 5"); start_page_id = SystemDefs.JavabaseDB.get_file_entry("entry 5"); System.out.println(" Your Answer : " + start_page_id); SystemDefs.JavabaseDB.DBDestroy(); } catch(Exception e) { status = FAIL; System.err.println(" ERROR: insert/look up/delete file entries failed"); e.printStackTrace(); } System.out.println(" Test 3 is done."); System.out.println(); return status; } protected void dumpSpaceMap(String leftToRight, String rightToLeft, Page page, long dumpLength) { byte[] buffer = page.getpage(); // print the bit map System.out.println(" space map: "); System.out.println(" Exact Answer (number bit from left to right in a byte): " + leftToRight + " ..."); System.out.println(" Exact Answer (number bit from right to left in a byte): " + rightToLeft + " ..."); System.out.print(" Your Answer: "); for(int i = 0; i < dumpLength; i ++) { StringBuffer bits = new StringBuffer(); byte mask = 1; for(long j = 0; j < 8; j ++) { if((buffer[i] & mask) != 0) { bits.insert(0, "1"); } else { bits.insert(0, "0"); } mask <<= 1; } System.out.print(bits + " "); } System.out.println("..."); } protected String testName () { return "Disk Space Manager"; } protected boolean runAllTests (){ boolean _passAll = OK; if (!test1()) { _passAll = FAIL; } if (!test2()) { _passAll = FAIL; } if (!test3()) { _passAll = FAIL; } return _passAll; }}public class DBTest { public static void main (String argv[]) { DBDriver dbt = new DBDriver(); boolean dbstatus; dbstatus = dbt.runTests(); if(dbstatus != true) { System.err.println("Error encountered during " + dbt.testName() + " tests."); } System.err.println(); System.err.println(); System.err.println(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -