-
Notifications
You must be signed in to change notification settings - Fork 8
/
money.lisp
35 lines (28 loc) · 1006 Bytes
/
money.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
; Copyright 2010 Andrew Pennebaker under the terms of the MIT X license
; found at http://www.opensource.org/licenses/mit-license.html
;
; Copyright 2004 Darius Bacon
;
; Example from Kent Beck, _Test-Driven Development_
(use-package :cl-quickcheck)
(defstruct money amount currency)
(defun dollars (n)
(make-money :amount n :currency 'usd))
(defun francs (n)
(make-money :amount n :currency 'franc))
(defun money* (n money)
(make-money :amount (* n (money-amount money))
:currency (money-currency money)))
(defun money= (money1 money2)
(and (= (money-amount money1) (money-amount money2))
(eql (money-currency money1) (money-currency money2))))
(when *testing*
(for-all (m)
(isnt money= (dollars m) (francs m)) ; deliberately failing
(is money= (dollars m) (dollars m)))
(for-all (m n)
(only-if (/= m n)
(isnt money= (dollars m) (dollars n))) ; deliberately failing
(is money=
(money* m (francs n))
(francs (* m n)))))