Expanded the predicate and assertion interface.
[lisp-unit.git] / rational.lisp
blobe31e74a047adff6239cc22a54f60e1a42dcb4e65
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-
2 ;;;
3 ;;; Floating tests and assertions for LISP-UNIT
4 ;;;
5 ;;; Copyright (c) 2009 Thomas M. Hermann
6 ;;;
7 ;;; Permission is hereby granted, free of charge, to any person obtaining
8 ;;; a copy of this software and associated documentation files (the "Software"),
9 ;;; to deal in the Software without restriction, including without limitation
10 ;;; the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 ;;; and/or sell copies of the Software, and to permit persons to whom the
12 ;;; Software is furnished to do so, subject to the following conditions:
13 ;;;
14 ;;; The above copyright notice and this permission notice shall be included
15 ;;; in all copies or substantial portions of the Software.
16 ;;;
17 ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 ;;; OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 ;;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 ;;; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 ;;; OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 ;;; ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 ;;; OTHER DEALINGS IN THE SOFTWARE.
24 ;;;
26 (common-lisp:in-package :lisp-unit)
28 (defgeneric rational-equal (data1 data2)
29 (:documentation
30 "Return true if the rational data is equal."))
32 ;;; (RATIONAL-EQUAL data1 data2) => boolean
33 (defmethod rational-equal ((data1 rational) (data2 rational))
34 "Return true if the rational numbers are equal."
35 (= data1 data2))
37 (defmethod rational-equal ((data1 complex) (data2 complex))
38 "Return true if the complex parts are rational and equal."
39 (if (and (typep data1 '(complex rational))
40 (typep data2 '(complex rational)))
41 (= data1 data2)
42 (error "Rational equality is not applicable to complex values ~
43 with floating point parts.")))
45 (defun %seq-rational-equal (seq1 seq2)
46 "Return true if the sequences are equal length and at each position
47 the corresponding elements are equal."
48 (or
49 (and (null seq1) (null seq2))
50 (and
51 (= (length seq1) (length seq2))
52 (every (lambda (d1 d2) (rational-equal d1 d2)) seq1 seq2))))
54 (defmethod rational-equal ((data1 list) (data2 list))
55 "Return true if the lists are equal in length and element-wise
56 equal."
57 (%seq-rational-equal data1 data2))
59 (defmethod rational-equal ((data1 list) (data2 vector))
60 "Return true if the vector and the list are equal in length and
61 element-wise equal."
62 (%seq-rational-equal data1 data2))
64 (defmethod rational-equal ((data1 vector) (data2 list))
65 "Return true if the vector and the list are equal in length and
66 element-wise equal."
67 (%seq-rational-equal data1 data2))
69 (defmethod rational-equal ((data1 vector) (data2 vector))
70 "Return true if the vectors are equal in length and element-wise
71 equal."
72 (%seq-rational-equal data1 data2))
74 (defmethod rational-equal ((data1 array) (data2 array))
75 "Return true if the arrays are equal in dimension and element-wise
76 equal."
77 (when (equal (array-dimensions data1)
78 (array-dimensions data2))
79 (%seq-rational-equal
80 (make-array (array-total-size data1)
81 :element-type (array-element-type data1)
82 :displaced-to data1)
83 (make-array (array-total-size data2)
84 :element-type (array-element-type data2)
85 :displaced-to data2))))
87 (defmacro assert-rational-equal (expected form &rest extras)
88 (expand-assert :equal form form expected extras :test #'rational-equal))