[1656e5]: contrib / sb-gmp / tests.lisp Maximize Restore History

Download this file

tests.lisp    198 lines (170 with data), 7.8 kB

  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
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
(defpackage "SB-GMP-TESTS"
(:use "COMMON-LISP" "SB-GMP" "SB-RT"))
(in-package "SB-GMP-TESTS")
(defparameter *state* (make-gmp-rstate))
(rand-seed *state* 1234)
(defmacro defgenerator (name arguments &body body)
`(defun ,name ,arguments
(lambda () ,@body)))
(defgenerator gen-mpz (&key (limbs 5) sign nonzero)
(let ((integer (random-bitcount *state*
(* limbs sb-vm:n-word-bits))))
(when (and nonzero (zerop integer))
(setf integer 1))
(ecase sign
((+ nil) integer)
(- (- integer))
((t random) (if (zerop (random 2))
integer
(- integer))))))
(defun gen-mpq (&key (limbs 5) sign nonzero)
(let ((numerator (gen-mpz :limbs limbs :sign sign
:nonzero nonzero))
(denominator (gen-mpz :limbs limbs :nonzero t)))
(lambda ()
(/ (funcall numerator) (funcall denominator)))))
(defun maybe-apply (maybe-function &optional arguments)
(if (typep maybe-function '(or function symbol))
(apply maybe-function arguments)
maybe-function))
(defun test-one-case (base tested &rest arguments)
(let* ((initial-hashes (mapcar #'sxhash arguments))
(base-values (let ((*gmp-disabled* t))
(multiple-value-list (maybe-apply base arguments))))
(test-values (let ((*gmp-disabled* nil))
(multiple-value-list (apply tested arguments))))
(final-hashes (mapcar #'sxhash arguments)))
(unless (and (= (length base-values) (length test-values))
(every #'eql base-values test-values))
(error "Failed test: ~S returned ~S; expected ~S"
(cons tested arguments) test-values base-values))
(unless (every #'eql initial-hashes final-hashes)
(error "Failed test: ~S modified arguments ~{~A~^, ~} ~
(printed modified values)"
(cons tested arguments)
(loop for i upfrom 0
for initial in initial-hashes
for final in final-hashes
unless (eql initial final)
collect i))))
nil)
;; Really just the most basic smoke test, otherwise
;; build times ballon up a bit on slow machines.
(defvar *iteration-count* 3)
(defun test-n-cases (base tested &rest argument-generators)
(let ((*random-state* (sb-ext:seed-random-state 54321)))
(loop repeat *iteration-count* do
(apply 'test-one-case base tested
(mapcar #'maybe-apply argument-generators)))))
(defmacro define-gmp-test ((name &key (repeat 1) limbs (gmp-seed 1234))
&body body)
`(deftest ,name
(let ((*random-state* (sb-ext:seed-random-state 54321)))
(rand-seed *state* ,gmp-seed)
(handler-case
(dotimes (i ,repeat)
;; try to get small failures first
(let ((limbs (case i
(0 ,(subst `(lambda (x)
x 0)
'random
limbs))
(1 ,(subst `(lambda (x)
(if (> x 1) 1 0))
'random
limbs))
(t ,limbs))))
(declare (ignorable limbs))
,@body))
(error (c)
(format t "~&~A~%" c)
nil)
(:no-error (&rest _) _ t)))
t))
(define-gmp-test (mpz-add :repeat 7 :limbs (+ (random #xFFFFF) 2))
(test-n-cases '+ 'mpz-add
(gen-mpz :limbs limbs :sign t)
(gen-mpz :limbs limbs :sign t)))
(define-gmp-test (mpz-sub :repeat 7 :limbs (+ (random #x1FFFF) 2))
(test-n-cases '- 'mpz-sub
(gen-mpz :limbs limbs :sign t)
(gen-mpz :limbs limbs :sign t)))
(define-gmp-test (mpz-mul :repeat 7 :limbs (+ (random #x253F) 2))
(test-n-cases '* 'mpz-mul
(gen-mpz :limbs limbs :sign t)
(gen-mpz :limbs limbs :sign t)))
(define-gmp-test (mpz-tdiv :repeat 7 :limbs (+ (random #x253F) 2))
(test-n-cases 'truncate 'mpz-tdiv
(gen-mpz :limbs limbs :sign t)
(gen-mpz :limbs limbs :sign t :nonzero t)))
(define-gmp-test (mpz-fdiv :repeat 7 :limbs (+ (random #x253F) 2))
(test-n-cases 'floor 'mpz-fdiv
(gen-mpz :limbs limbs :sign t)
(gen-mpz :limbs limbs :sign t :nonzero t)))
(define-gmp-test (mpz-cdiv :repeat 7 :limbs (+ (random #x253F) 2))
(test-n-cases 'ceiling 'mpz-cdiv
(gen-mpz :limbs limbs :sign t)
(gen-mpz :limbs limbs :sign t :nonzero t)))
(define-gmp-test (mpz-gcd :repeat 7 :limbs (+ (random #x253F) 2))
(test-n-cases 'gcd 'mpz-gcd
(gen-mpz :limbs limbs :sign t)
(gen-mpz :limbs limbs :sign t)))
(define-gmp-test (mpz-lcm :repeat 7 :limbs (+ (random #x253F) 2))
(test-n-cases 'lcm 'mpz-lcm
(gen-mpz :limbs limbs :sign t)
(gen-mpz :limbs limbs :sign t)))
(define-gmp-test (isqrt :repeat 7 :limbs (+ (random #x253F) 2))
(test-n-cases 'isqrt 'mpz-sqrt (gen-mpz :limbs limbs)))
(define-gmp-test (mpz-mod :repeat 7 :limbs (1+ (random #x253F)))
(test-n-cases 'mod 'mpz-mod
(gen-mpz :limbs limbs :sign t)
(gen-mpz :limbs limbs :sign t :nonzero t)))
(define-gmp-test (mpz-powm :repeat 7 :limbs (1+ (random #x253F)))
(test-n-cases (lambda (base exponent mod)
(let ((*gmp-disabled* nil)) ; atrociously slow otherwise
(mod (expt base exponent) mod)))
'mpz-powm
(gen-mpz :limbs limbs :sign t)
(lambda ()
(1+ (random 40)))
(gen-mpz :limbs (ceiling limbs 2) :nonzero t)))
;; bugs that have been fixed
(define-gmp-test (sign-conversion)
(test-one-case '+ 'mpz-add #x7FFFFFFFFFFFFFFF #x7FFFFFFFFFFFFFFF))
(define-gmp-test (truncate-1)
(test-one-case 'truncate 'mpz-tdiv
30951488519636377404900619671461408624764773310745985021994671444676860083493
200662724990805535745252242839121922075))
(define-gmp-test (truncate-2)
(test-one-case 'truncate 'mpz-tdiv
320613729464106236061704728914573914390
-285049280629101090500613812618405407883))
(define-gmp-test (mpz-nextprime :repeat 7
:gmp-seed 6234
:limbs (1+ (random #x2F)))
(let ((a (gen-mpz :limbs limbs)))
(dotimes (i *iteration-count*)
(let* ((a (funcall a))
(p (mpz-nextprime a)))
(assert (>= p a))
(assert (plusp (mpz-probably-prime-p p)))))))
(define-gmp-test (mpq-add :repeat 7 :limbs (1+ (random #x3FF))
:gmp-seed 1235)
(test-n-cases '+ 'mpq-add
(gen-mpq :limbs limbs :sign t)
(gen-mpq :limbs limbs :sign t)))
(define-gmp-test (mpq-sub :repeat 7 :limbs (1+ (random #x1FF))
:gmp-seed 1235)
(test-n-cases '- 'mpq-sub
(gen-mpq :limbs limbs :sign t)
(gen-mpq :limbs limbs :sign t)))
(define-gmp-test (mpq-mul :repeat 7 :limbs (1+ (random #x5FF))
:gmp-seed 6235)
(test-n-cases '* 'mpq-mul
(gen-mpq :limbs limbs :sign t)
(gen-mpq :limbs limbs :sign t)))
(define-gmp-test (mpq-div :repeat 7 :limbs (1+ (random #x3FF))
:gmp-seed 7235)
(test-n-cases '/ 'mpq-div
(gen-mpq :limbs limbs :sign t)
(gen-mpq :limbs limbs :sign t)))