Examples of using tags.
[lisp-unit.git] / internal-test / example-tests.lisp
blob448c1f4df36670550815e317757f93c19e4a88a9
1 #|
3 LISP-UNIT Example Tests
5 Copyright (c) 2010-2012, Thomas M. Hermann
6 All rights reserved.
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are
10 met:
12 o Redistributions of source code must retain the above copyright
13 notice, this list of conditions and the following disclaimer.
14 o Redistributions in binary form must reproduce the above copyright
15 notice, this list of conditions and the following disclaimer in
16 the documentation and/or other materials provided with the
17 distribution.
18 o The names of the contributors may not be used to endorse or promote
19 products derived from this software without specific prior written
20 permission.
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
26 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 (in-package :lisp-unit)
38 (defun my-max (x y)
39 "Deliberately wrong"
40 (declare (ignore y))
43 (define-test test-my-max
44 ;; Wrong
45 (assert-equal 5 (my-max 2 5))
46 (assert-equal 5 (my-max 5 2))
47 (assert-equal 10 (my-max 10 10))
48 (assert-equal 0 (my-max -5 0))
49 ;; Error
50 (assert-equal 5 (my-max-err 2 5)))
52 (defun my-sqrt (n)
53 "Not really."
54 (/ n 2))
56 (define-test my-sqrt
57 (dotimes (i 5)
58 (assert-equal i (my-sqrt (* i i)) i)))
60 ;;; Tags
62 (defun add-integer (integer1 integer2)
63 "Add 2 integer numbers"
64 (check-type integer1 integer)
65 (check-type integer2 integer)
66 (+ integer1 integer2))
68 (defun subtract-integer (integer1 integer2)
69 "Subtract 2 integer numbers"
70 (check-type integer1 integer)
71 (check-type integer2 integer)
72 (- integer1 integer2))
74 (define-test add-integer
75 "Test add-integer for values and errors."
76 (:tag :add :integer)
77 (assert-eql 3 (add-integer 1 2))
78 (assert-error 'type-error (add-integer 1.0 2))
79 (assert-error 'type-error (add-integer 1 2.0)))
81 (define-test subtract-integer
82 "Test subtract-integer for values and errors."
83 (:tag :subtract :integer)
84 (assert-eql 1 (subtract-integer 3 2))
85 (assert-error 'type-error (subtract-integer 3.0 2))
86 (assert-error 'type-error (subtract-integer 2 3.0)))
88 (defun add-float (float1 float2)
89 "Add 2 floating point numbers"
90 (check-type float1 float)
91 (check-type float2 float)
92 (+ float1 float2))
94 (defun subtract-float (float1 float2)
95 "Subtract 2 floating point numbers"
96 (check-type float1 float)
97 (check-type float2 float)
98 (- float1 float2))
100 (define-test add-float
101 "Test add-float for values and errors."
102 (:tag :add :float)
103 (assert-eql 3.0 (add-float 1.0 2.0))
104 (assert-error 'type-error (add-float 1.0 2))
105 (assert-error 'type-error (add-float 1 2.0)))
107 (define-test subtract-float
108 "Test subtract-float for values and errors."
109 (:tag :subtract :float)
110 (assert-eql 1.0 (subtract-float 3.0 2.0))
111 (assert-error 'type-error (subtract-float 3.0 2))
112 (assert-error 'type-error (subtract-float 2 3.0)))
114 (defun add-complex (complex1 complex2)
115 "Add 2 complex numbers"
116 (check-type complex1 complex)
117 (check-type complex2 complex)
118 (+ complex1 complex2))
120 (defun subtract-complex (complex1 complex2)
121 "Subtract 2 complex numbers"
122 (check-type complex1 complex)
123 (check-type complex2 complex)
124 (- complex1 complex2))
126 (define-test add-complex
127 "Test add-complex for values and errors."
128 (:tag :add :complex)
129 (assert-eql #C(3 5) (add-complex #C(1 2) #C(2 3)))
130 (assert-error 'type-error (add-integer #C(1 2) 3))
131 (assert-error 'type-error (add-integer 1 #C(2 3))))
133 (define-test subtract-complex
134 "Test subtract-complex for values and errors."
135 (:tag :subtract :complex)
136 (assert-eql #C(1 2) (subtract-complex #C(3 5) #C(2 3)))
137 (assert-error 'type-error (subtract-integer #C(3 5) 2))
138 (assert-error 'type-error (subtract-integer 2 #C(2 3))))