Merge pull request #38 from lukas-linhart/fix-readme-typos
[lisp-unit.git] / internal-test / example-tests.lisp
blob7334fd1aca45d507ec0bbc7d21ec05de9091604f
1 #|
3 LISP-UNIT Example Tests
5 Copyright (c) 2010-2016, Thomas M. Hermann
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:
14 The above copyright notice and this permission notice shall be included
15 in all copies or substantial portions of the Software.
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.
27 (in-package :lisp-unit)
29 (defun my-max (x y)
30 "Deliberately wrong"
31 (declare (ignore y))
34 (define-test test-my-max
35 ;; Wrong
36 (assert-equal 5 (my-max 2 5))
37 (assert-equal 5 (my-max 5 2))
38 (assert-equal 10 (my-max 10 10))
39 (assert-equal 0 (my-max -5 0))
40 ;; Error
41 (assert-equal 5 (my-max-err 2 5)))
43 (defun my-sqrt (n)
44 "Not really."
45 (/ n 2))
47 (define-test my-sqrt
48 (dotimes (i 5)
49 (assert-equal i (my-sqrt (* i i)) i)))
51 (define-test cl-user::my-sqrt
52 (dotimes (i 5)
53 (assert-equal i (my-sqrt (* i i)) i)))
55 ;;; Expand true or false
57 (defun my-less-than-function (arg1 arg2)
58 (< arg1 arg2))
60 (define-test expand-t-or-f-function
61 (let ((val 0))
62 (assert-true (my-less-than-function (incf val) (incf val)))
63 (assert-eql 2 val)
64 (assert-true (my-less-than-function (incf val) (decf val)))
65 (assert-eql 2 val)))
67 (defmacro my-less-than-macro (arg1 arg2)
68 `(< ,arg1 ,arg2))
70 (define-test expand-t-or-f-macro
71 (let ((val 0))
72 (assert-true (my-less-than-macro (incf val) (incf val)))
73 (assert-eql 2 val)
74 (assert-true (my-less-than-macro (incf val) (decf val)))
75 (assert-eql 2 val))
76 (assert-true (and nil (/ 1 0))))
78 ;;; Macro
80 (defmacro my-macro (arg1 arg2)
81 (let ((g1 (gensym))
82 (g2 (gensym)))
83 `(let ((,g1 ,arg1)
84 (,g2 ,arg2))
85 "Start"
86 (+ ,g1 ,g2 3))))
88 (define-test test-macro
89 (assert-expands
90 (let ((#:G1 A) (#:G2 B)) "Start" (+ #:G1 #:G2 3))
91 (my-macro a b)))
93 ;;; Tags
95 (defun add-integer (integer1 integer2)
96 "Add 2 integer numbers"
97 (check-type integer1 integer)
98 (check-type integer2 integer)
99 (+ integer1 integer2))
101 (defun subtract-integer (integer1 integer2)
102 "Subtract 2 integer numbers"
103 (check-type integer1 integer)
104 (check-type integer2 integer)
105 (- integer1 integer2))
107 (define-test add-integer
108 "Test add-integer for values and errors."
109 (:tag :add :integer)
110 (assert-eql 3 (add-integer 1 2))
111 (assert-error 'type-error (add-integer 1.0 2))
112 (assert-error 'type-error (add-integer 1 2.0)))
114 (define-test subtract-integer
115 "Test subtract-integer for values and errors."
116 (:tag :subtract :integer)
117 (assert-eql 1 (subtract-integer 3 2))
118 (assert-error 'type-error (subtract-integer 3.0 2))
119 (assert-error 'type-error (subtract-integer 2 3.0)))
121 (defun add-float (float1 float2)
122 "Add 2 floating point numbers"
123 (check-type float1 float)
124 (check-type float2 float)
125 (+ float1 float2))
127 (defun subtract-float (float1 float2)
128 "Subtract 2 floating point numbers"
129 (check-type float1 float)
130 (check-type float2 float)
131 (- float1 float2))
133 (define-test add-float
134 "Test add-float for values and errors."
135 (:tag :add :float)
136 (assert-eql 3.0 (add-float 1.0 2.0))
137 (assert-error 'type-error (add-float 1.0 2))
138 (assert-error 'type-error (add-float 1 2.0)))
140 (define-test subtract-float
141 "Test subtract-float for values and errors."
142 (:tag :subtract :float)
143 (assert-eql 1.0 (subtract-float 3.0 2.0))
144 (assert-error 'type-error (subtract-float 3.0 2))
145 (assert-error 'type-error (subtract-float 2 3.0)))
147 (defun add-complex (complex1 complex2)
148 "Add 2 complex numbers"
149 (check-type complex1 complex)
150 (check-type complex2 complex)
151 (+ complex1 complex2))
153 (defun subtract-complex (complex1 complex2)
154 "Subtract 2 complex numbers"
155 (check-type complex1 complex)
156 (check-type complex2 complex)
157 (- complex1 complex2))
159 (define-test add-complex
160 "Test add-complex for values and errors."
161 (:tag :add :complex)
162 (assert-eql #C(3 5) (add-complex #C(1 2) #C(2 3)))
163 (assert-error 'type-error (add-integer #C(1 2) 3))
164 (assert-error 'type-error (add-integer 1 #C(2 3))))
166 (define-test subtract-complex
167 "Test subtract-complex for values and errors."
168 (:tag :subtract :complex)
169 (assert-eql #C(1 2) (subtract-complex #C(3 5) #C(2 3)))
170 (assert-error 'type-error (subtract-integer #C(3 5) 2))
171 (assert-error 'type-error (subtract-integer 2 #C(2 3))))