1. David Krentzlin
  2. data-generators

Commits

certainty  committed 82fb006

fixed bounded fixnum generators. Adjusted %random-fixnum to use random-integer internally so that we can actually generate 64bit numbers on every system. Added dependency to numbers.

  • Participants
  • Parent commits 8e4579d
  • Branches default

Comments (0)

Files changed (4)

File data-generators-impl.scm

View file
   (unless (<= lo hi)
     (error '%random-fixnum "upper bound must be <= lower bound" lo hi))
   (let ((range (- hi lo -1)))
-    (+ (bsd:random-fixnum range) lo)))
+    (inexact->exact (+ (bsd:random-integer range) lo))))
 
-(define (%gen-fixnum/fixed-range size #!optional (start 0))
-  (lambda ()
-    (+ (bsd:random-fixnum size) start)))
+;; since these generators
+;; will most likely be used to feed some foreign code
+;; the ranges have been selected to conform to those present on most platforms
 
-;; these procedures can not be sized
-(define gen-int8   (%gen-fixnum/fixed-range 256 -128))
-(define gen-uint8  (%gen-fixnum/fixed-range 256))
-(define gen-int16  (%gen-fixnum/fixed-range 65536 -32768))
-(define gen-uint16 (%gen-fixnum/fixed-range 65536))
-(define gen-int32  (%gen-fixnum/fixed-range (expt 2 32) (- (expt 2 31))))
-(define gen-uint32 (%gen-fixnum/fixed-range (expt 2 32)))
-(define gen-int64  (%gen-fixnum/fixed-range (expt 2 64) (- (expt 2 64))))
-(define gen-uint64 (%gen-fixnum/fixed-range (expt 2 64)))
+(define gen-int8   (cut %random-fixnum -127 127))
+(define gen-uint8  (cut %random-fixnum 0 255))
+(define gen-int16  (cut %random-fixnum -32767 32767))
+(define gen-uint16 (cut %random-fixnum 0 65535))
+(define gen-int32  (cut %random-fixnum -2147483647 2147483647))
+(define gen-uint32 (cut %random-fixnum 0 4294967295))
+(define gen-int64  (cut %random-fixnum -9223372036854775807 9223372036854775807))
+(define gen-uint64 (cut %random-fixnum 0 18446744073709551615))
+
 
 ;; generic generator for fixnum allows to size the resulting number
 (define make-sizer cons)
 
 (define (%clamp val lower upper)
   (cond
-   ((>= val upper) upper)
-   ((and (>= val lower) (<= val upper)) val)
+   ((> val upper) upper)
+   ((and (> val lower) (<= val upper)) val)
    (else lower)))
 
 ;; doesn't currently work correctly

File data-generators.meta

View file
  (author "David Krentzlin")
  (category data)
  (license "BSD")
- (depends random-bsd)
+ (depends random-bsd numbers)
  (test-depends test))

File data-generators.scm

View file
   (use (prefix random-bsd bsd:)
        (only ports with-output-to-string)
        (only srfi-1 iota list-tabulate list-ref)
-       srfi-14 srfi-69)
+       srfi-14 srfi-69 numbers)
 
   (include "data-generators-impl.scm"))

File tests/run.scm

View file
 (define (in? x ls) (not (null? (member x ls))))
 (define (between? a x y) (and (>= a x) (<= a y)))
 
+(define-syntax test-fixed-range
+  (syntax-rules ()
+    ((_  gen lower upper)
+     (test-group (symbol->string (quote gen))
+       (test-assert
+        (between? (gen) lower upper))))))
+
 (test-group "gen-bool"
   (test-assert
     (in? (gen-bool) (list #t #f))))
     (test-assert
      (<=  (at-most gen-fixnum 2) 2)))
 
+(test-fixed-range gen-int8 -127 127)
+(test-fixed-range gen-uint8 0 255)
+(test-fixed-range gen-int16 -32767 32767)
+(test-fixed-range gen-uint16 0 65535)
+(test-fixed-range gen-int32 -2147483647 2147483647)
+(test-fixed-range gen-uint32 0 4294967295)
+(test-fixed-range gen-int64 -9223372036854775807 9223372036854775807)
+(test-fixed-range gen-uint64 0 18446744073709551615)
+
 (test-group "gen-real"
     (test-assert
      (between? (gen-real) 0.0 1.0))