bincursor.sa


Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
 

class TEST_BIN_FILE_CURSOR

class TEST_BIN_FILE_CURSOR is -- This is a test program to test the file cursor/buffer mechanism for -- binary files. Note that it is not portable for simplicity in building -- a local library. -- Version 1.0 Nov 97. Copyright K Hopper, U of Waikato -- Development History -- ------------------- -- Date Who By Detail -- ---- ------ ------ -- 19 Nov 97 kh Original include TEST ; const File_Name : STR := "yy_bintest" ; main is class_name("BIN_FILE_CURSOR") ; cwd : DIRECTORY := DIRECTORY::current ; -- first delete test file if it exists! cwd.delete(File_Name) ; -- now create a big file with known data! file_path : FILE_PATH := cwd.dirname.append(File_Name) ; fyle : BIN_FILE := BIN_FILE::create_for_update(file_path.str) ; test("create file for update",(~void(fyle)).str,true.str) ; if void(fyle) then finish ; return end ; loop fyle := fyle + 1.upto!(1000000).binstr end ; fyle.flush ; fyle.close ; fyle := BIN_FILE::open_for_update(file_path.str) ; test("open file for update",(~void(fyle)).str,true.str) ; if void(fyle) then finish ; return end ; cursor : BIN_FILE_CURSOR := fyle.cursor(4100) ; test("cursor creation",void(cursor).str,false.str) ; if void(cursor) then -- can't do anything else! finish ; return end ; test("size " + fyle.size.str,(fyle.size = cursor.size).str,true.str) ; test("position",cursor.position.str,0.str) ; cursor := cursor.position(4098) ; test("get",CARD::create(cursor.get(3).binstr).hex_str,"00040100") ; test("position after get",cursor.position.str,4101.str) ; cursor := cursor.backward(3) ; -- Back to the previous bufferful! test("seek 3 back then repeat get", CARD::create(cursor.get(3).binstr).hex_str,"00040100") ; -- should be same as last time! cursor := cursor.forward(3995898) ; -- the last item dummy : FBINSTR := cursor.get(1) ; -- make sure buffer is filled! test("at end is",cursor.at_end.str,true.str) ; cursor := cursor.backward(4) ; test("cursor position for last number is",cursor.position.str, (cursor.size - 4).str) ; test("last number is",CARD::create(cursor.get(4).binstr).str,1000000.str) ; cursor := cursor.position(cursor.size - 4) ; -- before the last number! loop -- reading backwards! cntr : CARD := 1000000.downto!(1) ; tmp : CARD := CARD::create(cursor.get(4).binstr) ; if tmp /= cntr then test("backward error for ",tmp.str,cntr.str) ; fyle.close ; finish ; return elsif cntr = 1 then break! else cursor := cursor.backward(8) end end ; test("backward scan ends at",cursor.position.str,4.str) ; -- Now to reverse the file contents (number by number)! front : CARD := 0 ; back : CARD := cursor.size - 4 ; -- The two indicators loop cursor := cursor.position(front) ; tmp_front : BINSTR := cursor.get(4).binstr ; cursor := cursor.position(back) ; tmp_back : BINSTR := cursor.get(4).binstr ; cursor := cursor.backward(4) ; cursor := cursor + tmp_front ; back := back - 4 ; cursor := cursor.position(front) ; cursor := cursor + tmp_back ; front := front + 4 ; if front >= back then -- finished reversal break! end end ; -- and then test that reversal is OK! cursor := cursor.position(cursor.size - 4) ; loop cntr : CARD := 1.upto!(1000000) ; tmp : CARD := CARD::create(cursor.get(4).binstr) ; if tmp /= cntr then test("forward error for",tmp.str,cntr.str) ; fyle.close ; finish ; return elsif cntr = 1000000 then break! else cursor := cursor.backward(8) end end ; cursor.flush ; fyle.close ; test("forward scan backward(!) ends at",cursor.position.str,4.str) ; -- finally delete the test file! cwd.delete(File_Name) ; finish end ; end ; -- TEST_BIN_FILE_CURSOR