// TestScan.bcpl
// Copyright Xerox Corporation 1979
// Last modified August 28, 1979  9:16 PM by Taft

// Bldr TestScan GP

get "Streams.d"
get "AltoFileSys.d"

external
[
// incoming procedures
SetupReadParam; ReadParam
GetCompleteFa; CreateDiskStream; ReadBlock; FilePos
InitScanStream; GetScanStreamBuffer; FinishScanStream
Ws; Wns; Gets; Closes; Endofs

// incoming statics
dsp; keys
]

//----------------------------------------------------------------------------
let TestScan() be
//----------------------------------------------------------------------------
[
SetupReadParam()
let nBufs = ReadParam($D, "Number of buffers: ")
if nBufs ls 1 then nBufs = 0
if nBufs gr 25 then nBufs = 25
let buffers = vec 25*256
let bufVec = vec 25
for i = 0 to nBufs-1 do bufVec!i = buffers + 256*i

   [
   let name = vec 127
   let slowStream = ReadParam($I, "Input file: ", name, 0, true)
   if slowStream eq -1 break
   Ws("*n"); Ws(name)
   let cfa = vec lCFA
   GetCompleteFa(slowStream, cfa)
   let fastStream = CreateDiskStream(lv cfa>>CFA.fp, ksTypeReadOnly)
   let ssd = InitScanStream(fastStream, bufVec, nBufs)

   let slowBuf = vec 256
   let fastBuf = nil
   let bytes = nil

      [
      bytes = ReadBlock(slowStream, slowBuf, 256) lshift 1
      bytes = bytes - (FilePos(slowStream) & 1)
      fastBuf = GetScanStreamBuffer(ssd)
      if bytes ne ssd>>SSD.numChars then
         ReportError("Byte count wrong", bytes, ssd>>SSD.numChars)
      if bytes ne 0 & fastBuf eq 0 then
         ReportError("Premature eof", bytes, 0)
      for i = 0 to (bytes rshift 1) -1 do
         if slowBuf!i ne fastBuf!i then
            ReportError("Data incorrect", slowBuf!i, fastBuf!i)
      ] repeatuntil bytes ls 512

   fastBuf = GetScanStreamBuffer(ssd)
   if fastBuf ne 0 then
      ReportError("Eof missed", 0, fastBuf)

   FinishScanStream(ssd)
   Closes(fastStream)
   Closes(slowStream)
   ] repeat
]

//----------------------------------------------------------------------------
and ReportError(string, slowVal, fastVal) be
//----------------------------------------------------------------------------
[
Ws("*n")
Ws(string)
Ws(": slow = ")
Wns(dsp, slowVal)
Ws(", fast = ")
Wns(dsp, fastVal)
Ws("   ... more?")
if (Gets(keys) & 137B) eq $N finish
]