#!/usr/bin/perl -w
#
# autotest: generate a stack checking C program,
#	    from a mini language, in which "20 30 - -" means
#	    push 20, push 30, pop, pop, doing all obvious tests
#	    as we go.  In particular, we need to simulate the
#	    stack ourselves to know what the correct answer is!
#
use strict;

die "Usage: autotest opstr maxdepth\n" unless @ARGV == 2;
my $opstr = shift;
my $maxdepth = shift;

#
# preamble($outfh): Append the standard C checking preamble to $outfh
#
sub preamble ($)
{
	my( $outfh ) = @_;
	print $outfh <<EOF;
#include <stdio.h>
#include <stdlib.h>
#include "bool.h"
#include "check.h"
#include "stack.h"

#define CHECK_ELEMENT CHECK_INT

int main( void )
{
	stack s = empty_stack();       \tcheck_stack(s,"[]");
EOF
}

#
# postamble( $outfh ): Append the standard C postamble to $outfh
#
sub postamble ($)
{
	my( $outfh ) = @_;

	print $outfh <<EOF;
	destroy_stack(s);
	return 0;
}
EOF
}

my $name = "atest";
open( my $outfh, '>', "$name.c" ) ||
	die "autotest: can't create $name.c, $!\n";
preamble( $outfh );
postamble( $outfh );
close( $outfh );
print "look at $name.c now\n";

exit 0;
