!
!
!	FILE SYSTEM TESTER 02/04/80
!
!
	PROGRAM ORIGIN :2C00
	DATA ORIGIN :2800
	DIM	CFILES(20), NFILES/20/
	DIM	FILE$[100]
	DIM	OP, I, J, K, L, CHAN, FILE
	DIM	OFILES(5,2), CHANS(5), NCHANS/5/, TCHANS/5/
	DIM	OPS(9), TOPS, NOPS/9/, NOPSTOGO
	DIM	SIZE, TRACE
!	LOCAL VARIABLES
	DIM	R, I2, TOPS2

!	TOP OF DIAGNOSTIC...DO RANDOM ACTION
100	IF NOPSTOGO=0 THEN 120
	FOR	I=1 TO NOPSTOGO
	GOSUB	200
110	NEXT	I

!	ADD PROBABILITY TO EACH OP
120	FOR	I=1 TO NOPS
	OPS(I)	=OPS(I)+1
	NEXT	I
	TOPS	=TOPS+NOPS
	NOPSTOGO=NOPS
	IF	TOPS<>45 THEN PRINT "TOPS=";TOPS;"!!!"
	GOTO	100

200	! RANDOM SCATTER

	R	=RND
	OP	=INT(1+R*TOPS)

	FOR	J=1 TO NOPS
	IF	OPS(J)>=OP THEN 210
	OP	=OP-OPS(J)
	NEXT	J
!	IF WE'RE HERE, SOMETHING GOOFED!
	PRINT	"OOPS!"
	PRINT	"R=";R;"TOPS=";TOPS
	STOP

210	OPS(J)=OPS(J)-1
	TOPS=TOPS-1
	ON J GOTO	1000,2000,3000,4000,5000,6000,7000,8000,9000
	PRINT	"Help! Help! Help! Help! Help!"
	STOP

300	IF TCHANS=0
	THEN	GOSUB POP 0 \
		GOTO 110

	CHAN	=CHANS(1)
	CHANS(1)=CHANS(TCHANS)
	TCHANS=TCHANS-1
	IF	CHAN=0
	THEN	PRINT	"ILLEGAL CHANNEL ASSIGNED! GULP!" \
		STOP

	RETURN

400	! FIND AN EXISTING OR READY TO EXIST FILE
	GOSUB	420

401	FILE	=INT(1+RND*NFILES)
	IF	CFILES(FILE)<0 THEN 401
	RETURN

410	! FIND AN ALREADY EXISTING FILE
	GOSUB	430

411	FILE	=INT(1+RND*NFILES)
	IF	CFILES(FILE)<1 THEN 411
	RETURN

420	! VERIFY THAT AN EXISTING FILE CAN BE FOUND
	IF	NFILES>NCHANS THEN RETURN \ ! NO WAY ALL COULD BE OPEN AT ONCE
	FOR	L=1 TO NFILES
	IF	CFILES(L)>=0 THEN RETURN
	NEXT	L
	GOSUB POP 0
	GOTO	110

430	! VERIFY THAT AN EXISTING FILE CAN BE FOUND
	FOR	L=1 TO NFILES
	IF CFILES(L)>0 THEN RETURN
	NEXT	L
	GOSUB POP 0
	GOTO	110

500	! FIND AN OPEN CHANNEL
	ON ERROR GOTO 599
	IF TCHANS=NCHANS THEN 510 \ ! NO HAY

501	CHAN=INT(RND*NCHANS+1)
	IF	CHAN=0 THEN PRINT "HELP!" \ STOP
	IF	OFILES(CHAN,1)=0 THEN 501
	ON ERROR GOTO 0
	RETURN

510	GOSUB POP 0
	ON ERROR GOTO 0
	GOTO	110


599	! ERROR TRAP FOR ESCAPES
	IF ERR<>1 THEN ERROR
	ON ERROR GOTO 0
	PRINT "TCHANS=";TCHANS;" NCHANS=";NCHANS
	FOR J=1 TO NCHANS
	PRINT	OFILES(J,1);
	NEXT	J
	PRINT
	GOTO	510

600	! CONVERT FILE TO FILE$
	FILE$	=HEX$(FILE)
	FILE$	="RANDOM." CAT FILE$(2,4)
	RETURN

1000	! CREATE A FILE
	GOSUB	400 \ ! ASSIGN A FILE
	GOSUB	300 \ ! ASSIGN A CHANNEL

	SIZE	=INT(1+200*RND)
	CFILES(FILE)=-SIZE
	GOSUB	600
	CREATE	#CHAN,FILE$
	FOR	J=1 TO SIZE
	WRITE	#CHAN,J
	NEXT	J
	IF TRACE
	THEN	PRINT "CREATED ";FILE$;" ON CHANNEL";CHAN;"; SIZE=";SIZE

	OFILES(CHAN,1)=FILE
	OFILES(CHAN,2)=SIZE
	RETURN

2000	! DELETE
	GOSUB 410
	GOSUB	600
	DELETE	FILE$
	CFILES(FILE)=0
	IF TRACE
	THEN	PRINT "DELETED ";FILE$
	RETURN

3000	! OPEN
	GOSUB	410
	GOSUB	300
	GOSUB	600
	OPEN	#CHAN,FILE$
	IF TRACE
	THEN	PRINT "OPENED ";FILE$;" ON CHANNEL ";CHAN

	CFILES(FILE)=-CFILES(FILE)
	OFILES(CHAN,1)=FILE
	OFILES(CHAN,2)=-CFILES(FILE)
	RETURN

4000	! READ
	GOSUB	500
	J=INT(RND*OFILES(CHAN,2))*6
	RESTORE	#CHAN,J
	READ	#CHAN,K
	IF TRACE
	THEN	PRINT "READ AT";J;"FROM CHANNEL";CHAN
	RETURN

5000	! WRITE
	GOSUB	500
	J=INT(RND*OFILES(CHAN,2)*2)
	K=J*6
	RESTORE	#CHAN,K
	WRITE	#CHAN,J
	IF TRACE
	THEN	PRINT "WRITE AT";K;"ON CHANNEL";CHAN
	IF	OFILES(CHAN,2)>=J THEN RETURN
	OFILES(CHAN,2)=J
	CFILES(OFILES(CHAN,1))=-J
	RETURN

6000	! CLOSE

	GOSUB	500
	CLOSE	#CHAN
	IF TRACE
	THEN	PRINT	"CLOSED CHANNEL";CHAN
	CFILES(OFILES(CHAN,1))=OFILES(CHAN,2)
	TCHANS=TCHANS+1
	CHANS(TCHANS)=CHAN
	OFILES(CHAN,1)=0
	RETURN

7000	! RANDOM THRASH
	IF TRACE
	THEN	PRINT "RANDOM THRASH"
	IF	RND>.1
	THEN	GOSUB 4000
	ELSE	GOSUB 5000

	IF	RND>.1 THEN 7000
	IF TRACE
	THEN	PRINT "END RANDOM THRASH"
	RETURN

8000	! OPEN NON EXISTANT FILE

	ON ERROR GOTO 8010
	OPEN	#NCHANS+1,"FILENOTTHERE"
8010	ON ERROR GOTO 0
	IF TRACE
	THEN	PRINT "OPEN NON-EXISTANT FILE 'FILENOTTHERE'"
	RETURN

9000	! CHAIN
	NOPSTOGO=NOPSTOGO-I
	FOR	J=1 TO NCHANS
	IF	OFILES(J,1)=0 THEN 9010
	CFILES(OFILES(J,1))=OFILES(J,2)
	OFILES(J,1)=0

9010	NEXT	J
	FOR	J=1 TO NCHANS
	CHANS(J)=J
	NEXT	J
	TCHANS	=NCHANS
	IF	TRACE
	THEN	PRINT "CHAIN"
	CHAIN	"FTEST"

	END
