% File "main.t" % The file contains a driver program whose purpose is merely to test % the rope class (and indirectly, to test the Node, internalNode and % leafNode classes. % To run this entire program, one simply runs main.t % The testing provided here is extremely limited. % Imports % ------------------------------------------------------------------ % This program (directly) uses only the rope class. Indirectly, % it uses also Node, internalNode, and leafNode. import rope in "rope.tu" % Constants % ------------------------------------------------------------------ % These special stream numbers refer to the standard input and % the standard output. const stdoutStream : int := - 1 const stdinStream : int := - 2 % countChars % ------------------------------------------------------------------ % Counts the number of characters in the file whose stream number is % `inFile', which must already be open. This count is needed before % calling rope->Get. If called, rope->Get also has to read the file, % which means that it will be read twice in total. The extra cost % of reading the file twice buys us the ability to build a balanced % tree. % An alternative approach would be to read the file only once, % creating an imbalanced tree, and then to go back and shuffle the % tree around to balance it. Another approach would be to require % the input to begin with a count of the number of characters in the % long string. % Note that this procedure should not be called if input is % to come from the keyboard. The user would have to type the input % once to be counted, and once again to be read by rope->Get. procedure countChars (inFile : int, var numChars : int) var c : string (1) numChars := 0 loop exit when eof (inFile) get : inFile, c : 1 if c not= "\n" then numChars += 1 end if end loop end countChars % Main Program % ------------------------------------------------------------------ % I repeat: this testing is extremely limited! put "A rope stored in a file" put repeat ("-", 60) var bigRope : ^rope var filename : string var inFile : int var numChars : int new bigRope assert bigRope not= nil put "Filename: " .. get filename open : inFile, filename, get assert inFile not= 0 countChars (inFile, numChars) close : inFile open : inFile, filename, get assert inFile not= 0 bigRope -> Get (inFile, numChars) close : inFile bigRope -> Put (stdoutStream, 60) put "Height is ", bigRope -> Height put "Size is ", (bigRope -> Size) var s1, s2, s3, s4, s5 : ^rope new s1 new s2 new s3 new s4 new s5 assert s1 not= nil and s2 not= nil and s3 not= nil and s4 not= nil and s5 not= nil put skip, "Some concatenations" put repeat ("-", 60) s1 -> SetValue ("Hello ") s2 -> SetValue ("There ") s3 -> Concatenate (s1, s2) s1 -> SetValue ("Camper") s3 -> Concatenate (s3, s1) s3 -> Put (stdoutStream, 50) put skip, "Same rope, printed with smaller line width (4 characters)" put repeat ("-", 60) s3 -> Put (stdoutStream, 4) put skip, "A subrope out of the middle" put repeat ("-", 60) s1 := s3 -> SubRope (7, 15) s1 -> Put (stdoutStream, 50) put skip, "A subrope consisting of the whole rope" put repeat ("-", 60) s2 := s3 -> SubRope (1, s3 -> Size) s2 -> Put (stdoutStream, 50) put skip, "A null subrope" put repeat ("-", 60) s4 -> SetValue ("Happy") s5 := s4 -> SubRope (2, 1) s5 -> Put (stdoutStream, 50)