abs.lsp 3.55 KB
 pfdietz committed Sep 02, 2003 1 2 3 4 5 ``````;-*- Mode: Lisp -*- ;;;; Author: Paul Dietz ;;;; Created: Mon Sep 1 20:16:42 2003 ;;;; Contains: Tests of ABS `````` Daniel Kochmański committed Sep 01, 2015 6 `````` `````` pfdietz committed Sep 02, 2003 7 `````` `````` Daniel Kochmański committed Aug 31, 2015 8 `````` `````` pfdietz committed Sep 02, 2003 9 10 `````` (deftest abs.error.1 `````` pfdietz committed Jan 16, 2004 11 12 `````` (signals-error (abs) program-error) t) `````` pfdietz committed Sep 02, 2003 13 14 `````` (deftest abs.error.2 `````` pfdietz committed Jan 16, 2004 15 16 `````` (signals-error (abs 0 0) program-error) t) `````` pfdietz committed Sep 02, 2003 17 18 `````` (deftest abs.error.3 `````` pfdietz committed Jan 16, 2004 19 20 `````` (signals-error (abs 0 nil nil) program-error) t) `````` pfdietz committed Sep 02, 2003 21 22 23 `````` (deftest abs.1 (loop for x in *numbers* `````` sds committed Aug 12, 2009 24 25 `````` for a = (abs x) always (and (realp a) (not (minusp a)))) `````` pfdietz committed Sep 02, 2003 26 27 28 29 `````` t) (deftest abs.2 (loop for x = (random-fixnum) `````` sds committed Aug 12, 2009 30 31 32 33 `````` for a = (abs x) repeat 10000 unless (if (plusp x) (eql x a) (eql (- x) a)) collect (list x a)) `````` pfdietz committed Sep 02, 2003 34 35 36 37 38 `````` nil) (deftest abs.3 (let ((bound (ash 1 300))) (loop for x = (random-from-interval bound) `````` sds committed Aug 12, 2009 39 40 41 42 `````` for a = (abs x) repeat 10000 unless (if (plusp x) (eql x a) (eql (- x) a)) collect (list x a))) `````` pfdietz committed Sep 02, 2003 43 44 45 46 `````` nil) (deftest abs.4 (loop for num = (random-fixnum) `````` sds committed Aug 12, 2009 47 48 49 50 51 52 53 `````` for den = (random-fixnum) for den2 = (if (zerop den) 1 den) for r = (/ num den) for a = (abs r) repeat 10000 unless (if (>= r 0) (eql r a) (eql (- r) a)) collect (list num den2 r a)) `````` pfdietz committed Sep 02, 2003 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 `````` nil) (deftest abs.5 (let ((bound (ash 1 210))) (loop for num = (random-from-interval bound) for den = (random-from-interval bound) for den2 = (if (zerop den) 1 den) for r = (/ num den) for a = (abs r) repeat 10000 unless (if (>= r 0) (eql r a) (eql (- r) a)) collect (list num den2 r a))) nil) (deftest abs.6 (let ((bound (float (ash 1 11) 1.0s0))) (loop for x = (random-from-interval bound) for a = (abs x) repeat 10000 unless (if (minusp x) `````` sds committed Aug 12, 2009 76 77 `````` (eql (- x) a) (eql x a)) `````` pfdietz committed Sep 02, 2003 78 79 80 81 82 83 84 85 86 87 `````` collect (list x a))) nil) (deftest abs.7 (let ((bound (float (ash 1 22) 1.0f0))) (loop for x = (random-from-interval bound) for a = (abs x) repeat 10000 unless (if (minusp x) `````` sds committed Aug 12, 2009 88 89 `````` (eql (- x) a) (eql x a)) `````` pfdietz committed Sep 02, 2003 90 91 92 93 94 95 96 97 98 99 `````` collect (list x a))) nil) (deftest abs.8 (let ((bound (float (ash 1 48) 1.0d0))) (loop for x = (random-from-interval bound) for a = (abs x) repeat 10000 unless (if (minusp x) `````` sds committed Aug 12, 2009 100 101 `````` (eql (- x) a) (eql x a)) `````` pfdietz committed Sep 02, 2003 102 103 104 105 106 107 108 109 110 111 `````` collect (list x a))) nil) (deftest abs.9 (let ((bound (float (ash 1 48) 1.0l0))) (loop for x = (random-from-interval bound) for a = (abs x) repeat 10000 unless (if (minusp x) `````` sds committed Aug 12, 2009 112 113 `````` (eql (- x) a) (eql x a)) `````` pfdietz committed Sep 02, 2003 114 115 116 117 118 119 120 121 122 123 124 `````` collect (list x a))) nil) ;;; The example on the abs page says that (abs -0.0) should be -0,0. ;;; However, FABS on the x86 returns 0.0 for that. Since the examples ;;; in the hyperspec are not normative, the following four tests ;;; have been commented out. ;;; (deftest abs.10 ;;; (abs -0.0s0) ;;; -0.0s0) `````` sds committed Aug 12, 2009 125 ``````;;; `````` pfdietz committed Sep 02, 2003 126 127 128 ``````;;; (deftest abs.11 ;;; (abs -0.0f0) ;;; -0.0f0) `````` sds committed Aug 12, 2009 129 ``````;;; `````` pfdietz committed Sep 02, 2003 130 131 132 ``````;;; (deftest abs.12 ;;; (abs -0.0d0) ;;; -0.0d0) `````` sds committed Aug 12, 2009 133 ``````;;; `````` pfdietz committed Sep 02, 2003 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 ``````;;; (deftest abs.13 ;;; (abs -0.0l0) ;;; -0.0l0) ;;; Complex numbers (deftest abs.14 (let ((result (abs #c(3 4)))) (=t result 5)) t) (deftest abs.15 (let ((result (abs #c(-3 4)))) (=t result 5)) t) (deftest abs.16 (let ((result (abs #c(3 -4)))) (=t result 5)) t) (deftest abs.17 (let ((result (abs #c(-3 -4)))) (=t result 5)) t) (deftest abs.18 (abs #c(3.0s0 4.0s0)) 5.0s0) (deftest abs.19 (abs #c(3.0f0 -4.0f0)) 5.0f0) (deftest abs.20 (abs #c(-3.0d0 4.0d0)) 5.0d0) (deftest abs.21 (abs #c(-3.0l0 4.0l0)) 5.0l0) `````` pfdietz committed Jan 23, 2006 175 176 177 `````` (deftest abs.22 (macrolet ((%m (z) z)) `````` sds committed Aug 12, 2009 178 `````` (abs (expand-in-current-env (%m -4)))) `````` pfdietz committed Jan 23, 2006 179 `` 4)``