// DateTest.bcpl -- test OS date handling
// Copyright Xerox Corporation 1979
// Last modified February 18, 1979  5:01 PM by Boggs

// Bldr DateTest TimeConvB TimeConvA TimeIO Template

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

external
[
// Incoming procedures
OpenFile; DeleteFile; ActOnDiskPages
Closes; Puts; Gets; Resets
CallSwat; Zero; SetBlock; MoveBlock; ReadCalendar
Wss; UNPACKDT; WRITEUDT; PutTemplate

// Incoming statics
keys; dsp; sysDisk
]

//----------------------------------------------------------------------------
let DateTest() be
//----------------------------------------------------------------------------
[
let log = vec lST; SetBlock(log, CallSwat, lST)
log>>ST.par1 = OpenFile("DateTest.log", ksTypeWriteOnly, charItem)
log>>ST.par2 = dsp
log>>ST.puts = SplitPuts

Wss(log, "*NDateTest of February 18, 1979  5:06 PM")

let fp = vec lFP; let dates = vec 6

Wss(log, "*N*NDeleteFile(*"DateTest.1*")")
DeleteFile("DateTest.1")

Zero(fp, lFP)
Wss(log, "*N*Nfile = OpenFile(*"DateTest.1*", ksTypeWriteOnly)")
let file = OpenFile("DateTest.1", ksTypeWriteOnly, 0, 0, fp)
Wait()
Wss(log, "*NCloses(file)")
Closes(file)
DoDates(log, fp, dates)

Wait()
Zero(fp, lFP)
Wss(log, "*N*Nfile = OpenFile(*"DateTest.1*", ksTypeWriteOnly)")
let file = OpenFile("DateTest.1", ksTypeWriteOnly, 0, 0, fp)
Wait()
Wss(log, "*NCloses(file)")
Closes(file)
DoDates(log, fp, dates, false, false, false)

Wait()
Zero(fp, lFP)
Wss(log, "*N*Nfile = OpenFile(*"DateTest.1*", ksTypeWriteOnly)")
let file = OpenFile("DateTest.1", ksTypeWriteOnly, 0, 0, fp)
Wss(log, "*NPuts(file, 0)")
Puts(file, 0)
Wait()
Wss(log, "*NCloses(file)")
Closes(file)
DoDates(log, fp, dates, true, false, true)

Wait()
Zero(fp, lFP)
Wss(log, "*N*Nfile = OpenFile(*"DateTest.1*", ksTypeReadOnly)")
let file = OpenFile("DateTest.1", ksTypeReadOnly, 0, 0, fp)
Wait()
Wss(log, "*NCloses(file)")
Closes(file)
DoDates(log, fp, dates, false, true, false)

Wait()
Zero(fp, lFP)
Wss(log, "*N*Nfile = OpenFile(*"DateTest.1*", ksTypeReadOnly)")
let file = OpenFile("DateTest.1", ksTypeReadOnly, 0, 0, fp)
Wss(log, "*NGets(file)")
Gets(file)
Wait()
Wss(log, "*NCloses(file)")
Closes(file)
DoDates(log, fp, dates, false, true, false)

Wait()
Zero(fp, lFP)
Wss(log, "*N*Nfile = OpenFile(*"DateTest.1*", ksTypeReadWrite)")
let file = OpenFile("DateTest.1", ksTypeReadWrite, 0, 0, fp)
Wait()
Wss(log, "*NCloses(file)")
Closes(file)
DoDates(log, fp, dates, false, true, false)

Wait()
Zero(fp, lFP)
Wss(log, "*N*Nfile = OpenFile(*"DateTest.1*", ksTypeReadWrite)")
let file = OpenFile("DateTest.1", ksTypeReadWrite, 0, 0, fp)
Wss(log, "*NPuts(file, 0)")
Puts(file, 0)
Wait()
Wss(log, "*NCloses(file)")
Closes(file)
DoDates(log, fp, dates, true, true, true)

Wait()
Zero(fp, lFP)
Wss(log, "*N*Nfile = OpenFile(*"DateTest.1*", ksTypeReadWrite)")
let file = OpenFile("DateTest.1", ksTypeReadWrite, 0, 0, fp)
Wss(log, "*NGets(file)")
Gets(file)
Wait()
Wss(log, "*NCloses(file)")
Closes(file)
DoDates(log, fp, dates, false, true, false)

Wait()
Zero(fp, lFP)
Wss(log, "*N*Nfile = OpenFile(*"DateTest.1*", ksTypeReadWrite)")
let file = OpenFile("DateTest.1", ksTypeReadWrite, 0, 0, fp)
Wss(log, "*NPuts(file, 0)")
Puts(file, 0)
Wss(log, "*NResets(file)")
Resets(file)
Wss(log, "*NGets(file)")
Gets(file)
Wait()
Wss(log, "*NCloses(file)")
Closes(file)
DoDates(log, fp, dates, true, true, true)

// Cleanup and go away
Closes(log>>ST.par1)
]

//----------------------------------------------------------------------------
and SplitPuts(st, char) be
//----------------------------------------------------------------------------
[
Puts(st>>ST.par1, char)
Puts(st>>ST.par2, char)
]

//----------------------------------------------------------------------------
and DoDates(log, fp, dates, create, read, write; numargs na) be
//----------------------------------------------------------------------------
[
let ld, DAs = vec 256, vec 3
SetBlock(DAs, fillInDA, 3); DAs!1 = fp>>FP.leaderVirtualDa
ActOnDiskPages(sysDisk, lv ld, DAs+1, fp, 0, 0, DCreadD)

let createdUDT = vec 9; UNPACKDT(lv ld>>LD.created, createdUDT)
let readUDT = vec 9; UNPACKDT(lv ld>>LD.read, readUDT)
let writtenUDT = vec 9; UNPACKDT(lv ld>>LD.written, writtenUDT)
PutTemplate(log, "*NCreate: $P, Read: $P, Write: $P",
 WRITEUDT, createdUDT, WRITEUDT, readUDT, WRITEUDT, writtenUDT)

if na gr 3 then
   [
   CheckDate(log, dates, lv ld>>LD.created, create, "Create")
   CheckDate(log, dates+2, lv ld>>LD.read, read, "Read")
   CheckDate(log, dates+4, lv ld>>LD.written, write, "Write")
   ]

MoveBlock(dates, lv ld>>LD.created, 2)
MoveBlock(dates+2, lv ld>>LD.read, 2)
MoveBlock(dates+4, lv ld>>LD.written, 2)
]

//----------------------------------------------------------------------------
and CheckDate(log, date1, date2, shouldChange, name) be
//----------------------------------------------------------------------------
[
let changed = date1!0 ne date2!0 % date1!1 ne date2!1
if changed ne shouldChange then
   PutTemplate(log, "*N****** $S date $S have changed ******",
    name, (shouldChange? "should", "shouldn't"))
]

//----------------------------------------------------------------------------
and Wait() be	// wait for the seconds to change
//----------------------------------------------------------------------------
[
let now, future = vec 1, vec 1
ReadCalendar(now)
ReadCalendar(future) repeatwhile now!1 eq future!1
]