%
% Name: adbooktest.dem
% Author: Tim Brecht 
% Date:   Jan 24, 1996
%
% This program is a driver to test the 
% AddressBook class. It makes heavy use
% of assertions, requires no input and
% produces very little output. The goal
% is to write a program that will run
% to completion successfully if and only
% if the underlying implementation of
% the AddressBook class is correct.

import AddressBook 
  in  "adbookln.tu" 

var book1   : ^AddressBook
var book2   : ^AddressBook
var found   : boolean
var entered : boolean
var address : string

address := "unknown"

new book1
assert(book1 not = nil)

book1->Initialize()

% save nothing
book1->Save()

% load nothing
book1->Load()

% ensure nothing is in the book1
book1->LookUp("pig", address, found)
assert(found = false)

% add one entry and look it up
book1->Enter("pig", "pen", entered)
assert(entered = true)
book1->LookUp("pig", address, found)
assert(found = true)
assert(address = "pen")

% delete the only entry and lookup will fail
book1->Delete("pig")
book1->LookUp("pig", address, found)
assert(found = false)
assert(address = "pen")

% add a slightly different version of that entry
book1->Enter("pig", "pig-pen", entered)
assert(entered = true)
book1->LookUp("pig", address, found)
assert(found = true)
assert(address = "pig-pen")

% change the entry and find the new version
book1->Change("pig", "sty")
book1->LookUp("pig", address, found)
assert(found = true)
assert(address = "sty")

% add multiple entries 
book1->Enter("bear", "cave", entered)
assert(entered = true)
book1->Enter("dear", "range", entered)
assert(entered = true)
book1->Enter("car", "garage", entered)
assert(entered = true)
book1->Enter("pillow", "bed", entered)
assert(entered = true)

% look them all up
book1->LookUp("pillow", address, found)
assert(found = true)
assert(address = "bed")
book1->LookUp("car", address, found)
assert(found = true)
assert(address = "garage")
book1->LookUp("dear", address, found)
assert(found = true)
assert(address = "range")
book1->LookUp("bear", address, found)
assert(found = true)
assert(address = "cave")
book1->LookUp("pig", address, found)
assert(found = true)
assert(address = "sty")

% save the current state
book1->Save()

% now delete everything
book1->Delete("pig")
book1->Delete("bear")
book1->Delete("dear")
book1->Delete("car")
book1->Delete("pillow")

% ensure that they are not there
address := "unknown"
book1->LookUp("pillow", address, found)
assert(found = false)
assert(address = "unknown")
book1->LookUp("car", address, found)
assert(found = false)
assert(address = "unknown")
book1->LookUp("dear", address, found)
assert(found = false)
assert(address = "unknown")
book1->LookUp("bear", address, found)
assert(found = false)
assert(address = "unknown")
book1->LookUp("pig", address, found)
assert(found = false)
assert(address = "unknown")

% now reload 
book1->Load()

% and test that each can be found again
book1->LookUp("pillow", address, found)
assert(found = true)
assert(address = "bed")
book1->LookUp("car", address, found)
assert(found = true)
assert(address = "garage")
book1->LookUp("dear", address, found)
assert(found = true)
assert(address = "range")
book1->LookUp("bear", address, found)
assert(found = true)
assert(address = "cave")
book1->LookUp("pig", address, found)
assert(found = true)
assert(address = "sty")

% now change everything
book1->Change("pig", "barn")
book1->Change("bear", "den")
book1->Change("dear", "meadow")
book1->Change("car", "street")
book1->Change("pillow", "floor")

% and test that each can be found again
book1->LookUp("pillow", address, found)
assert(found = true)
assert(address = "floor")
book1->LookUp("car", address, found)
assert(found = true)
assert(address = "street")
book1->LookUp("dear", address, found)
assert(found = true)
assert(address = "meadow")
book1->LookUp("bear", address, found)
assert(found = true)
assert(address = "den")
book1->LookUp("pig", address, found)
assert(found = true)
assert(address = "barn")

% use a second book
new book2
assert(book2 not = nil)

book2->Initialize()

% load up the stuff saved by book1
book2->Load()

% and test that each can be found again
book2->LookUp("pillow", address, found)
assert(found = true)
assert(address = "bed")
book2->LookUp("car", address, found)
assert(found = true)
assert(address = "garage")
book2->LookUp("dear", address, found)
assert(found = true)
assert(address = "range")
book2->LookUp("bear", address, found)
assert(found = true)
assert(address = "cave")
book2->LookUp("pig", address, found)
assert(found = true)
assert(address = "sty")

put "Test completed successfully"
book1->Finalize()
book2->Finalize()