3 (asdf:oos 'asdf:load-op :FiveAM)
5 (defpackage :it.bese.FiveAM.example
9 (in-package :it.bese.FiveAM.example)
11 ;;; First we need some functions to test.
19 ;;; Now we need to create a test which makes sure that add-2 and add-4
20 ;;; work as specified.
22 ;; we create a test named ADD-2 and supply a short description.
24 "Test the ADD-2 function" ;; a short description
27 (is (= 0 (add-2 -2))))
29 ;; we can already run add-2. This will return the list of test
30 ;; results, it should be a list of two test-passed objects.
34 ;; since we'd like to have some kind of readbale output we'll explain
39 ;; or we could do both at once:
43 ;;; So now we've defined and run a single test. Since we plan on
44 ;;; having more than one test and we'd like to run them together let's
45 ;;; create a simple test suite.
47 (def-suite example-suite :description "The example test suite.")
49 ;; we could explictly specify that every test we create is in the the
50 ;; example-suite suite, but it's easier to just change the default
53 (in-suite example-suite)
55 ;; now we'll create a new test for the add-4 function.
58 (is (= 0 (add-4 -4))))
60 ;; now let's run the test
64 ;; we can get the same effect by running the suite:
68 ;; since we'd like both add-2 and add-4 to be in the same suite, let's
69 ;; redefine add-2 to be in this suite:
71 (test add-2 "Test the ADD-2 function"
73 (is (= 0 (add-2 -2))))
75 ;; now we can run the suite and we'll see that both add-2 and add-4
76 ;; have been run (we know this since we no get 4 checks as opposed to
81 ;; Just for fun let's see what happens when a test fails. Again we'll
82 ;; redefine add-2, but add in a third, failing, check:
84 (test add-2 "Test the ADD-2 function"
89 ;; Finally let's try out the specification based testing.
91 (defun dummy-add (a b)
94 (defun dummy-strcat (a b)
95 (concatenate 'string a b))
98 (for-all ((a (gen-integer))
100 ;; assuming we have an "oracle" to compare our function results to
102 (is (= (+ a b) (dummy-add a b)))
103 ;; if we don't have an oracle (as in most cases) we just ensure
104 ;; that certain properties hold:
105 (is (= (dummy-add a b)
107 (is (= a (dummy-add a 0)))
108 (is (= 0 (dummy-add a (- a))))
109 (is (< a (dummy-add a 1)))
110 (is (= (* 2 a) (dummy-add a a)))))
113 (for-all ((result (gen-string))
114 (split-point (gen-integer :min 0 :max 10000)
115 (< split-point (length result))))
116 (is (string= result (dummy-strcat (subseq result 0 split-point)
117 (subseq result split-point))))))
120 (for-all ((result (gen-integer :min 0 :max 1)))
124 (test fail-guard-conditions
125 (for-all ((x (gen-integer :min 1 :max 1) (= x 0)))