Allow for approx comparison of floats in tests

defexample entries may now include a symbol `~>' instead of `=>' which
uses an approximate comparison instead of `equal' to compare actual and
expected floating-point values.

Also, for completeness, add support for the `should-error' symbol `!!>' in
`examples-to-docs.el'. This is formatted as the comment ";; Error"
master
William West 11 years ago
parent db784f817a
commit 4640a2a51c
  1. 12
      dev/examples-to-docs.el
  2. 2
      dev/examples-to-tests.el
  3. 13
      dev/examples.el

@ -5,9 +5,15 @@
(defvar functions '()) (defvar functions '())
(defun example-to-string (example) (defun example-to-string (example)
(let ((actual (car example)) (-let* (((actual sym expected) example)
(expected (nth 2 example))) (comment
(--> (format "%S ;; => %S" actual expected) (cond
((eq sym '=>) (format "=> %S" expected))
((eq sym '~>) (format "~> %S" expected))
((eq sym '!!>) (format "Error"))
(t (error "Invalid test case: %S" `(,actual ,sym ,expected))))))
(--> comment
(format "%S ;; %s" actual it)
(replace-regexp-in-string "\\\\\\?" "?" it) (replace-regexp-in-string "\\\\\\?" "?" it)
(replace-regexp-in-string "\n" "\\n" it t t) (replace-regexp-in-string "\n" "\\n" it t t)
(replace-regexp-in-string "\t" "\\t" it t t) (replace-regexp-in-string "\t" "\\t" it t t)

@ -3,6 +3,8 @@
(defun example-to-should (actual sym expected) (defun example-to-should (actual sym expected)
(cond ((eq sym '=>) (cond ((eq sym '=>)
`(should (equal ,actual ,expected))) `(should (equal ,actual ,expected)))
((eq sym '~>)
`(should (approx-equal ,actual ,expected)))
((eq sym '!!>) ((eq sym '!!>)
`(should-error (eval ',actual) :type ',expected)) `(should-error (eval ',actual) :type ',expected))
(t (t

@ -9,6 +9,17 @@
(defun square (num) (* num num)) (defun square (num) (* num num))
(defun three-letters () '("A" "B" "C")) (defun three-letters () '("A" "B" "C"))
;; Allow approximate comparison of floating-point results, to work
;; around differences in implementation between systems. Use the `~>'
;; symbol instead of `=>' to test the expected and actual values with
;; `approx-equal'
(defvar epsilon 1e-15)
(defun approx-equal (u v)
(or (= u v)
(< (/ (abs (- u v))
(max (abs u) (abs v)))
epsilon)))
(def-example-group "Maps" (def-example-group "Maps"
"Functions in this category take a transforming function, which "Functions in this category take a transforming function, which
is then applied sequentially to each or selected elements of the is then applied sequentially to each or selected elements of the
@ -977,5 +988,5 @@ new list."
(funcall (-prodfn (-compose f ff) (-compose g gg)) input3)))) => t))) (funcall (-prodfn (-compose f ff) (-compose g gg)) input3)))) => t)))
;; Local Variables: ;; Local Variables:
;; eval: (font-lock-add-keywords nil '(("defexamples\\|def-example-group\\| => \\| !!> " (0 'font-lock-keyword-face)) ("(defexamples[[:blank:]]+\\(.*\\)" (1 'font-lock-function-name-face)))) ;; eval: (font-lock-add-keywords nil '(("defexamples\\|def-example-group\\| => \\| !!> \\| ~>" (0 'font-lock-keyword-face)) ("(defexamples[[:blank:]]+\\(.*\\)" (1 'font-lock-function-name-face))))
;; End: ;; End:

Loading…
Cancel
Save