test.scm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. ;;; -*-scheme-*-
  2. ;;; GNU Mes --- Maxwell Equations of Software
  3. ;;; Copyright © 2016,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. ;;;
  5. ;;; This file is part of GNU Mes.
  6. ;;;
  7. ;;; GNU Mes is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Mes is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;;; test.mes can be loaded after base.mes. It provides a minimalistic
  21. ;;; test framework: pass-if, pass-if-not, seq?, sequal? and result.
  22. ;;; Code:
  23. (define-module (mes test)
  24. #:use-module (ice-9 rdelim)
  25. #:export (
  26. pass-if
  27. pass-if-equal
  28. pass-if-not
  29. pass-if-eq
  30. pass-if-timeout
  31. result
  32. seq? ; deprecated
  33. sequal? ; deprecated
  34. ))
  35. (cond-expand
  36. (mes
  37. (define (inexact->exact x) x)
  38. (define mes? #t)
  39. (define guile? #f)
  40. (define guile-2? #f)
  41. (define guile-1.8? #f))
  42. (guile-2
  43. (define mes? #f)
  44. (define guile? #t)
  45. (define guile-2? #t)
  46. (define guile-1.8? #f))
  47. (guile
  48. (define mes? #f)
  49. (define guile? #f)
  50. (define guile-2? #f)
  51. (define guile-1.8? #t)))
  52. (define result
  53. ((lambda (pass fail)
  54. (lambda (. t)
  55. (if (or (null? t) (eq? (car t) 'result)) (list pass fail)
  56. (if (eq? (car t) 'report)
  57. (begin
  58. ((lambda (expect)
  59. (begin (display "expect: ") (write expect) (newline))
  60. (newline)
  61. (display "passed: ") (display pass) (newline)
  62. (display "failed: ") (display fail) (newline)
  63. (if (not (eq? expect 0)) (begin (display "expect: ") (write expect) (newline)))
  64. (display "total: ") (display (+ pass fail)) (newline)
  65. (exit (if (eq? expect fail) 0 fail)))
  66. (if (null? (cdr t)) 0 (cadr t))))
  67. (if (car t) (begin (display ": pass") (newline) (set! pass (+ pass 1)))
  68. (begin (display ": fail") (newline) (set! fail (+ fail 1))))))))
  69. 0 0))
  70. (define (seq? expect a) ;;REMOVE ME
  71. (or (eq? a expect)
  72. (begin
  73. (display ": fail")
  74. (newline)
  75. (display "expected: ")
  76. (write expect) (newline)
  77. (display "actual: ")
  78. (write a)
  79. (newline)
  80. #f)))
  81. (define (sequal? expect a) ;;REMOVE ME
  82. (or (equal? a expect)
  83. (begin
  84. (display ": fail")
  85. (newline)
  86. (display "expected: ")
  87. (write expect) (newline)
  88. (display "actual: ")
  89. (write a)
  90. (newline)
  91. #f)))
  92. (define (seq2? a expect)
  93. (or (eq? a expect)
  94. (begin
  95. (display ": fail") (newline)
  96. (display "expected: ") (write expect) (newline)
  97. (display "actual: ") (write a) (newline)
  98. #f)))
  99. (define (sless? a expect)
  100. (or (< a expect)
  101. (begin
  102. (display ": fail") (newline)
  103. (display "expected: ") (write expect) (newline)
  104. (display "actual: ") (write a) (newline)
  105. #f)))
  106. (define (sequal2? actual expect)
  107. (or (equal? actual expect)
  108. (begin
  109. (display ": fail") (newline)
  110. (display "expected: ") (write expect) (newline)
  111. (display "actual: ") (write actual) (newline)
  112. #f)))
  113. (define-macro (pass-if name t)
  114. (list
  115. 'begin
  116. (list display "test: ") (list display name)
  117. (list 'result t))) ;; FIXME
  118. (define-macro (pass-if-eq name expect . body)
  119. (list 'pass-if name (list seq2? (cons 'begin body) expect)))
  120. (define-macro (pass-if-equal name expect . body)
  121. (list 'pass-if name (list sequal2? (cons 'begin body) expect)))
  122. (define-macro (expect-fail name expect . body)
  123. (list 'pass-if name (list not (list sequal2? (cons 'begin body) expect))))
  124. (define-macro (pass-if-not name f)
  125. (list
  126. 'begin
  127. (list display "test: ") (list display name)
  128. (list 'result (list not f)))) ;; FIXME
  129. (define internal-time-units-per-milli-second
  130. (/ internal-time-units-per-second 1000))
  131. (define (test-time thunk)
  132. ((lambda (start)
  133. (begin
  134. (thunk)
  135. (inexact->exact (/ (- (get-internal-run-time) start)
  136. internal-time-units-per-milli-second))))
  137. (get-internal-run-time)))
  138. (define-macro (pass-if-timeout name limit . body)
  139. (list 'pass-if name (list sless? (list test-time (cons* 'lambda '_ body)) limit)))