-- TestMonitoredQueue.Mesa
-- written by Paxton.  December 1981
-- last written by Paxton.  9-Dec-81  9:50:07

DIRECTORY
MonitoredQueue;

TestMonitoredQueue: PROGRAM  
	IMPORTS MonitoredQueue =
BEGIN OPEN MonitoredQueue;

source: MQ ← Create[];
input: REF READONLY TEXT ← "abcdefghijklmnopqrstuvwxyz";
output: REF TEXT ← NEW[TEXT[26]];

Produce: PROC = { -- adds to source
	FOR i:NAT IN [0..input.length) DO Add[NEW[CHAR ← input[i]],source]; ENDLOOP;
	Close[source] };

Consume: PROC = { -- moves from source to output
	DO	x: REF ANY ← Remove[source ! EndOfQueue => EXIT]; 
		WITH x SELECT FROM
		xx: REF CHAR => {
			output[output.length] ← xx↑;
			output.length ← output.length+1 };
		ENDCASE => ERROR;
		ENDLOOP };

Test: PROC = {
	p1: PROCESS ← FORK Produce;
	p2: PROCESS ← FORK Consume;
	ok: BOOLEAN;
	JOIN p1;
	JOIN p2;
	ok ← FALSE;
	Add[NIL, source ! QueueClosed => { ok ← TRUE; CONTINUE }];
	IF ~ok THEN ERROR;
	IF input.length # output.length THEN ERROR;
	FOR i:NAT IN [0..input.length) DO
		IF input[i] # output[i] THEN ERROR; ENDLOOP;
	-- make sure can reuse the queue
	Reset[source];
	output.length ← 0;
	p1 ← FORK Produce;
	p2 ← FORK Consume;
	JOIN p1;
	JOIN p2;
	ok ← FALSE;
	Add[NIL, source ! QueueClosed => { ok ← TRUE; CONTINUE }];
	IF ~ok THEN ERROR;
	IF input.length # output.length THEN ERROR;
	FOR i:NAT IN [0..input.length) DO
		IF input[i] # output[i] THEN ERROR; ENDLOOP;
	};

Test;
	
END.