History

random group: better error control; more error tests

Sigrid Solveig Haflínudóttir committed 4 months ago

Commit f90e06e1 parent 6478e522 files · +25 −4Browse files

Unified · Split

2 files
  1. src/random.c +3 −3
  2. test/unit.sl +22 −1
src/random.c+3−3
@@ -43,8 +43,6 @@ static mpint * · 42 unmodified lines
4343 bigrand(pcg_t *c, mpint *n)
4444 {
4545 int bits = mpsignif(n);
46− if(bits == 0)
47− return mpzero;
4846 mpint *b = mpnew(bits);
4947 sl.randstatectx = c;
5048 do
@@ -141,6 +139,8 @@ BUILTIN("random", random) · 90 unmodified lines
141139 }
142140
143141 sl_v v = args[0];
142+ if(sl_unlikely(!sl_isnum(v)))
143+ cthrow(type_error("max", "num", v), v);
144144 soffset max;
145145 if(isfixnum(v)){
146146 max = numval(v);
@@ -169,7 +169,7 @@ badrange: · 22 unmodified lines
169169 v = mk_f64(d * genrand64_real1(&c));
170170 goto done;
171171 }
172− if(sl_unlikely(numeric_compare(v, fixnum(0), true) <= 0))
172+ if(sl_unlikely(numeric_compare(v, fixnum(0), false) <= 0))
173173 goto badrange;
174174 mpint *m, *b;
175175 if(nt == T_BIG){
test/unit.sl+22−1
@@ -1305,6 +1305,14 @@ · 1304 unmodified lines
13051305 (assert (equal? (map 'list identity "hello йцукен") (foldr cons NIL "hello йцукен")))
13061306
13071307 ;; random
1308+(assert-fail (random-state?) arity-error)
1309+(assert-fail (random-state? NIL NIL) arity-error)
1310+(assert (not (random-state? (list 'pcg32x2 0 1 2 3))))
1311+(assert (not (random-state? (cons 'pcg32x2 #arr(u64 0 1 2)))))
1312+(assert (not (random-state? (cons 'pcg32x2 #arr(u32 0 1 2 3)))))
1313+(assert (not (random-state? (cons 'blah #arr(u64 0 1 2 3)))))
1314+(assert-fail (make-random-state 0) type-error)
1315+(assert-fail (make-random-state T 1) arity-error)
13081316 (assert (not *random-state*))
13091317 (assert (not (random-state? *random-state*)))
13101318 (def x (make-random-state T))
@@ -1353,28 +1361,41 @@ · 42 unmodified lines
13531361 (f (random 10.0))
13541362 (f2 (random 10.0f))
13551363 (b (random 10000000000000000000000000000))
1364+ (fix (random +most-positive-fixnum+))
13561365 (s8 (random (s8 5)))
13571366 (f32 (random 1.0f))
1367+ (s64 (random (s64 9223372036854775807)))
1368+ (sfix (random (s64 +most-positive-fixnum+)))
13581369 (z (+ (random (bignum 1)) (random (u64 1)) (random (f32 1)) (random (s32 1))))
13591370 (f64 (random 1.0)))
13601371 (assert (and (not (int? f32)) (>= f32 0.0) (<= f32 1.0)))
13611372 (assert (and (not (int? f64)) (>= f64 0.0) (<= f64 1.0)))
13621373 (assert (and (>= b 0) (or (fixnum? b) (and (bignum? b) (> b +most-positive-fixnum+) (<= b 10000000000000000000000000000)))))
1374+ (assert (and (>= fix 0) (<= fix +most-positive-fixnum+)))
13631375 (assert (and (int? s8) (>= s8 0) (<= s8 5)))
13641376 (assert (and (int? i) (>= i 0) (<= i 10)))
13651377 (assert (and (not (int? f)) (>= f 0.0) (<= f 10.0)))
1366− (assert (<= z 4))
1378+ (assert (and (>= s64 0) (or (fixnum? s64) (and (bignum? s64) (> s64 +most-positive-fixnum+))) (<= s64 9223372036854775807)))
1379+ (assert (and (>= sfix 0) (or (fixnum? sfix) (and (bignum? sfix) (> sfix +most-positive-fixnum+))) (<= sfix +most-positive-fixnum+)))
1380+ (assert (and (>= z 0) (<= z 4)))
13671381 (assert (and (not (int? f2)) (>= f2 0.0) (<= f2 10.0))))))
13681382 (assert-fail (random) arity-error)
1383+(assert-fail (random 1 *random-state* 2) arity-error)
1384+(assert-fail (random NIL) type-error)
1385+(assert-fail (random 'a) type-error)
1386+(assert-fail (random "blah") type-error)
13691387 (assert-fail (random 1 2) type-error)
13701388 (assert-fail (random 1 NIL) type-error)
13711389 (assert-fail (random 1 T) type-error)
13721390 (assert-fail (random 0) arg-error)
13731391 (assert-fail (random 0.0) arg-error)
1392+(assert-fail (random 0.0f) arg-error)
13741393 (assert-fail (random -0.1) arg-error)
1394+(assert-fail (random -0.1f) arg-error)
13751395 (assert-fail (random -1) arg-error)
13761396 (assert-fail (random (bignum -1)) arg-error)
13771397 (assert-fail (random (s8 -1)) arg-error)
1398+(assert-fail (random (s64 -9223372036854775808)) arg-error)
13781399
13791400 (assert (and (bound? T) (bound? NIL)))
13801401

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t