Properly handle the extra results.
[lisp-unit.git] / extensions / rational.lisp
blob43403a6bab71e51ef11abbf374dcbc49d855f9a9
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-
2 #|
4 Rational tests and assertions for LISP-UNIT
6 Copyright (c) 2009-2012, 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 ;;; Symbols exported from the rational extension
32 (export '(rational-equal assert-rational-equal))
34 ;;; Rational predicates and assertions
36 (defgeneric rational-equal (data1 data2)
37 (:documentation
38 "Return true if the rational data is equal."))
40 ;;; (RATIONAL-EQUAL data1 data2) => boolean
41 (defmethod rational-equal ((data1 rational) (data2 rational))
42 "Return true if the rational numbers are equal."
43 (= data1 data2))
45 (defmethod rational-equal ((data1 complex) (data2 complex))
46 "Return true if the complex parts are rational and equal."
47 (if (and (typep data1 '(complex rational))
48 (typep data2 '(complex rational)))
49 (= data1 data2)
50 (error "Rational equality is not applicable to complex values ~
51 with floating point parts.")))
53 (defun %seq-rational-equal (seq1 seq2)
54 "Return true if the sequences are equal length and at each position
55 the corresponding elements are equal."
56 (or
57 (and (null seq1) (null seq2))
58 (and
59 (= (length seq1) (length seq2))
60 (every (lambda (d1 d2) (rational-equal d1 d2)) seq1 seq2))))
62 (defmethod rational-equal ((data1 list) (data2 list))
63 "Return true if the lists are equal in length and element-wise
64 equal."
65 (%seq-rational-equal data1 data2))
67 (defmethod rational-equal ((data1 list) (data2 vector))
68 "Return true if the vector and the list are equal in length and
69 element-wise equal."
70 (%seq-rational-equal data1 data2))
72 (defmethod rational-equal ((data1 vector) (data2 list))
73 "Return true if the vector and the list are equal in length and
74 element-wise equal."
75 (%seq-rational-equal data1 data2))
77 (defmethod rational-equal ((data1 vector) (data2 vector))
78 "Return true if the vectors are equal in length and element-wise
79 equal."
80 (%seq-rational-equal data1 data2))
82 (defmethod rational-equal ((data1 array) (data2 array))
83 "Return true if the arrays are equal in dimension and element-wise
84 equal."
85 (when (equal (array-dimensions data1)
86 (array-dimensions data2))
87 (%seq-rational-equal
88 (make-array (array-total-size data1)
89 :element-type (array-element-type data1)
90 :displaced-to data1)
91 (make-array (array-total-size data2)
92 :element-type (array-element-type data2)
93 :displaced-to data2))))
95 (defmacro assert-rational-equal (expected form &rest extras)
96 `(expand-assert :equal ,form ,form ,expected ,extras :test #'rational-equal))