#!/bin/sh
#
# Common - Elements shared by all regression tests for poly2d
#
# Written 2010, 2011 by Werner Almesberger
# Copyright 2010, 2011 Werner Almesberger
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#


compile_and_run()
{
	CFLAGS="-Wall -Werror -g -std=gnu99 -I.."
	LIBS="-lpoly2d -lCGAL -lCGAL_Core -lboost_thread"
	LIBS="$LIBS -lstdc++ -lmpfr -lgmp -lm"

	cat <<EOF >_.c
#include <poly2d.h>
#include "p2d_hsort.h"


static void recurse_hier(const struct p2d_hier *h, int level)
{
	const struct v2d *v;

	while (h) {
		printf("%*s", level*2, "");
		v = h->p.v;
		while (v) {
			printf("%s%g %g", v == h->p.v ? "" : "  ", v->x, v->y);
			v = v->next;
			if (v == h->p.v)
				break;
		}
		printf("\n");
		recurse_hier(h->holes, level+1);
		h = p2d_to_hier(h->p.next);
	}
}


static void __attribute__((unused)) print_hier(const struct p2d_hier *h)
{
	recurse_hier(h, 0);
}


int main(void)
{
`cat _in`
	return 0;
}
EOF
	gcc $CFLAGS _.c -L.. $LIBS || return
	$VALGRIND ./a.out
}


tst()
{
    echo -n "$1: " 1>&2
    shift
    cat >_in
    compile_and_run "$@" >_out 2>&1 || {
	echo FAILED "($SCRIPT)" 1>&2
	cat _out
	exit 1
    }
}


tst_fail()
{
    echo -n "$1: " 1>&2
    shift
    cat >_in
    compile_and_run "$@" >_out 2>&1 && {
	echo FAILED "($SCRIPT)" 1>&2
	cat _out
	exit 1
    }
    rm -f _in _.c a.out
}


expect()
{
    diff -u - "$@" _out >_diff || {
	echo FAILED "($SCRIPT)" 1>&2
	cat _diff 1>&2
	exit 1
    }
    echo PASSED 1>&2
    rm -f _in _out _diff _.c a.out
    passed=`expr ${passed:-0} + 1`
}