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
43bigrand(pcg_t *c, mpint *n)43bigrand(pcg_t *c, mpint *n)
44{44{
45 int bits = mpsignif(n);45 int bits = mpsignif(n);
46 if(bits == 0)
47 return mpzero;
48 mpint *b = mpnew(bits);46 mpint *b = mpnew(bits);
49 sl.randstatectx = c;47 sl.randstatectx = c;
50 do48 do
@@ -141,6 +139,8 @@ BUILTIN("random", random) · 90 unmodified lines
141 }139 }
142 140
143 sl_v v = args[0];141 sl_v v = args[0];
142 if(sl_unlikely(!sl_isnum(v)))
143 cthrow(type_error("max", "num", v), v);
144 soffset max;144 soffset max;
145 if(isfixnum(v)){145 if(isfixnum(v)){
146 max = numval(v);146 max = numval(v);
@@ -169,7 +169,7 @@ badrange: · 22 unmodified lines
169 v = mk_f64(d * genrand64_real1(&c));169 v = mk_f64(d * genrand64_real1(&c));
170 goto done;170 goto done;
171 }171 }
172 if(sl_unlikely(numeric_compare(v, fixnum(0), true) <= 0))172 if(sl_unlikely(numeric_compare(v, fixnum(0), false) <= 0))
173 goto badrange;173 goto badrange;
174 mpint *m, *b;174 mpint *m, *b;
175 if(nt == T_BIG){175 if(nt == T_BIG){
test/unit.sl+22−1
@@ -1305,6 +1305,14 @@ · 1304 unmodified lines
1305(assert (equal? (map 'list identity "hello йцукен") (foldr cons NIL "hello йцукен")))1305(assert (equal? (map 'list identity "hello йцукен") (foldr cons NIL "hello йцукен")))
1306 1306
1307;; random1307;; 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)
1308(assert (not *random-state*))1316(assert (not *random-state*))
1309(assert (not (random-state? *random-state*)))1317(assert (not (random-state? *random-state*)))
1310(def x (make-random-state T))1318(def x (make-random-state T))
@@ -1353,28 +1361,41 @@ · 42 unmodified lines
1353 (f (random 10.0))1361 (f (random 10.0))
1354 (f2 (random 10.0f))1362 (f2 (random 10.0f))
1355 (b (random 10000000000000000000000000000))1363 (b (random 10000000000000000000000000000))
1364 (fix (random +most-positive-fixnum+))
1356 (s8 (random (s8 5)))1365 (s8 (random (s8 5)))
1357 (f32 (random 1.0f))1366 (f32 (random 1.0f))
1367 (s64 (random (s64 9223372036854775807)))
1368 (sfix (random (s64 +most-positive-fixnum+)))
1358 (z (+ (random (bignum 1)) (random (u64 1)) (random (f32 1)) (random (s32 1))))1369 (z (+ (random (bignum 1)) (random (u64 1)) (random (f32 1)) (random (s32 1))))
1359 (f64 (random 1.0)))1370 (f64 (random 1.0)))
1360 (assert (and (not (int? f32)) (>= f32 0.0) (<= f32 1.0)))1371 (assert (and (not (int? f32)) (>= f32 0.0) (<= f32 1.0)))
1361 (assert (and (not (int? f64)) (>= f64 0.0) (<= f64 1.0)))1372 (assert (and (not (int? f64)) (>= f64 0.0) (<= f64 1.0)))
1362 (assert (and (>= b 0) (or (fixnum? b) (and (bignum? b) (> b +most-positive-fixnum+) (<= b 10000000000000000000000000000)))))1373 (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+)))
1363 (assert (and (int? s8) (>= s8 0) (<= s8 5)))1375 (assert (and (int? s8) (>= s8 0) (<= s8 5)))
1364 (assert (and (int? i) (>= i 0) (<= i 10)))1376 (assert (and (int? i) (>= i 0) (<= i 10)))
1365 (assert (and (not (int? f)) (>= f 0.0) (<= f 10.0)))1377 (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)))
1367 (assert (and (not (int? f2)) (>= f2 0.0) (<= f2 10.0))))))1381 (assert (and (not (int? f2)) (>= f2 0.0) (<= f2 10.0))))))
1368(assert-fail (random) arity-error)1382(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)
1369(assert-fail (random 1 2) type-error)1387(assert-fail (random 1 2) type-error)
1370(assert-fail (random 1 NIL) type-error)1388(assert-fail (random 1 NIL) type-error)
1371(assert-fail (random 1 T) type-error)1389(assert-fail (random 1 T) type-error)
1372(assert-fail (random 0) arg-error)1390(assert-fail (random 0) arg-error)
1373(assert-fail (random 0.0) arg-error)1391(assert-fail (random 0.0) arg-error)
1392(assert-fail (random 0.0f) arg-error)
1374(assert-fail (random -0.1) arg-error)1393(assert-fail (random -0.1) arg-error)
1394(assert-fail (random -0.1f) arg-error)
1375(assert-fail (random -1) arg-error)1395(assert-fail (random -1) arg-error)
1376(assert-fail (random (bignum -1)) arg-error)1396(assert-fail (random (bignum -1)) arg-error)
1377(assert-fail (random (s8 -1)) arg-error)1397(assert-fail (random (s8 -1)) arg-error)
1398(assert-fail (random (s64 -9223372036854775808)) arg-error)
1378 1399
1379(assert (and (bound? T) (bound? NIL)))1400(assert (and (bound? T) (bound? NIL)))
1380 1401

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