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