;; test/test-suite.scm -- Test framework
;;
;; Copyright (C) 2000 Martin Grabmueller <mgrabmue@cs.tu-berlin.de>
;;
;; This 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, or (at your option)
;; any later version.
;; 
;; This software is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;; 
;; You should have received a copy of the GNU General Public License
;; along with this package; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.  

;; Extend the load path to load the modules from the source tree
(set! %load-path (cons "../data" %load-path ))

(define section-name #f)
(define sub-section-name #f)
(define section-index 0)
(define sub-section-index 0)
(define test-index 0)
(define total-test-count 0)
(define failed-test-count 0)
(define failed-sections '())

(define (pass-if b expected pred?)
  (set! test-index (+ test-index 1))
  (set! total-test-count (+ total-test-count 1))
  (display "    ")
  (display section-index)
  (display ".")
  (display sub-section-index)
  (display ".")
  (display test-index)
  (display ": ")
  (if (pred? b expected)
      (begin (display "ok") (newline))
      (begin
	(set! failed-sections
	      (cons (list section-name sub-section-name test-index)
		    failed-sections))
	(set! failed-test-count (+ failed-test-count 1))
	(display "failed ") (newline))))

(define (begin-section name)
  (set! section-name name)
  (set! sub-section-index 0)
  (set! test-index 0)
  (set! section-index (+ section-index 1))
  (display section-index)
  (display " ")
  (display section-name)
  (newline))

(define (begin-sub-section name)
  (set! sub-section-name name)
  (set! test-index 0)
  (set! sub-section-index (+ sub-section-index 1))
  (display "  ")
  (display section-index)
  (display ".")
  (display sub-section-index)
  (display " ")
  (display name)
  (newline))

(define (test-summary)
  (display "Total tests:   ")
  (display total-test-count)
  (newline)
  (if (null? failed-sections)
      (exit 0)
      (begin
	(display "Failed tests:  ")
	(display failed-test-count)
	(newline)
	(display "Failure ratio: ")
	(display (/ (* failed-test-count 100.0) total-test-count))
	(display "%")
	(newline)
	(display "Failed tests:")
	(newline)
	(for-each (lambda (c)
		    (display "  Section: ")
		    (display (car c))
		    (display ", subsection: ")
		    (display (cadr c))
		    (display ", test: ")
		    (display (caddr c))
		    (newline))
		  (reverse failed-sections))
	(exit 1))))


