textcursor.sa
Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
class TEST_TEXT_FILE_CURSOR
class TEST_TEXT_FILE_CURSOR is
-- This is a test program to test the file cursor/buffer mechanism for
-- text 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_txttest" ;
const Last_Size : CARD := 6 ;
const Count_in_1060 : CARD := 3189 ;
-- There are 3,189 characters (including 'thousands separators') in
-- the first 1,059 numbers!
main is
Size_End_Line : CARD := LIBCHARS::default.Line_Mark.size ;
Posn_1060 : CARD := Count_in_1060 + (Size_End_Line * (1060 - 1)) ;
class_name("TEXT_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 : TEXT_FILE := TEXT_FILE::create_for_update(file_path.str) ;
test("file creation",(~void(fyle)).str,true.str) ;
if void(fyle) then -- can't do anything else!
finish ;
return
end ;
loop
fyle := fyle + STR::line_mark.separate!(1.upto!(10000).str)
end ;
fyle.flush ;
fyle.close ;
fyle := TEXT_FILE::open_for_update(file_path.str) ;
cursor : TEXT_FILE_CURSOR := fyle.cursor(4100) ;
test("cursor creation",(~void(cursor)).str,true.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(Posn_1060) ;
card_str : STR := cursor.get_upto('\n') ;
test("get_upto",CARD::create(card_str).str,1060.str) ;
test("position",cursor.position.str,(Posn_1060 + 5).str) ;
cursor := cursor.backward(5) ; -- Back to the previous bufferful!
test("get_line after backward move",
CARD::create(cursor.get_line).str,1060.str) ;
-- should be same as last time!
cursor := cursor.position(cursor.size - 1) ;
cursor := cursor.skip(1) ; -- should be at the end!
test("at end",cursor.at_end.str,true.str) ;
cursor := cursor.backward(Last_Size) ;
card_str := cursor.get_upto('\n') ;
test("last number",CARD::create(card_str).str,10000.str) ;
cursor := cursor.position(cursor.size - Last_Size) ; -- last number
places : CARD := (2 * Last_Size) + Size_End_Line ;
loop
cntr : CARD := 10000.downto!(1) ;
tmp : CARD := CARD::create(cursor.get_upto('\n')) ;
case tmp
when 9, 10, 99, 100, 9999, 10000 then
places := places - 1
when 999,1000 then
places := places - 2
else
end ;
if tmp /= cntr then
test("backward error",tmp.str,cntr.str) ;
fyle.close ;
finish ;
return
elsif cntr = 1 then
break!
end ;
cursor := cursor.backward(places * LIBCHARS::default.my_size)
end ;
test("backward scan ends at",cursor.position.str,1.str) ;
cursor.skip_line ;
test("skip_line",cursor.position,(1 + Size_End_Line).str) ;
cursor := cursor.position(10000) ; -- must be mod 4!
text : STR := "\nMary had a little lamb\n" +
"Its fleece was white as snow\n" +
"And everywhere that Mary went\n" +
"The lamb was sure to go.\n" ;
cursor := cursor + text ;
cursor := cursor.position(10000) ;
cursor.skip_line ; -- checks 'single code' line mark
res : STR := STR::create + "\n" ;
loop
4.times! ;
loc_line : STR := cursor.get_line ;
res := res + loc_line + "\n" ;
cursor.skip_line
end ;
test("get_line",text,res) ;
cursor.flush ;
fyle.close ;
-- finally delete the test file!
cwd.delete(File_Name) ;
finish
end ;
end ; -- TEST_TEXT_FILE_CURSOR