Changeset 30555 in project


Ignore:
Timestamp:
03/12/14 20:34:16 (7 years ago)
Author:
certainty
Message:

data-generators: added docs for new generators

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/4/data-generators

    r30552 r30555  
    4242
    4343(define (gen-json-string)
    44   (with-size (0 . 20)
     44  (with-size (range 0 20)
    4545    (gen-string-of (gen-char char-set:letter+digit))))
    4646
    4747(define (gen-json-object nesting)
    48   (with-size (0 . 5)
     48  (with-size (range 0 5)
    4949    (gen-vector-of (gen-pair-of (gen-json-string) (gen-json-value (sub1 nesting))))))
    5050
     
    5656
    5757(define (gen-json-array nesting)
    58   (with-size (0 . 5)
     58  (with-size (range 0 5)
    5959    (gen-list-of (gen-json-value (sub1 nesting)))))
    6060
     
    8686Invokes the generator '''gen''' once and returns that value.
    8787
    88 <procedure>(<-* n gen)</procedure>
    89 Invokes the generator '''n'''-times and returns a list of '''n''' values.
     88<procedure>(<- amount gen)</procedure>
     89Invokes the generator '''gen''' '''amount''' times and returns a list of values
    9090
    9191<procedure>(gen-for-each n proc gen)</procedure>
    9292Invokes the generator '''gen''' '''n''' times and applies '''proc''' to each value in turn.
     93
     94==== Ranges
     95Many generators accept a range argument that specifies the upper and lower bounds.
     96You can use the following procedures to work with ranges.
     97
     98<procedure>(range lower upper)</procedure>
     99Creates a range that covers values from '''lower''' to '''upper'''.
     100There are three variations of this:
     101
     102* lower is #f and upper is not #f  -> creates a range from (gen-current-fixnum-min) till upper
     103* lower is not #f and upper is #f  -> creates a range from lower till (gen-current-fixnum-max)
     104* lower is not #f and upper is not #f -> creates a range from lower till upper
     105
     106==== Generic generator
     107The library provides a generic generator procedure that is able to build the correct generator based on the type of
     108the supplied arguments.
     109
     110<procedure>(gen lower upper)</procedure>
     111Creates a generator that can be created for the given argument types.
     112Currently supported types are:
     113
     114* fixnum
     115* flonum
     116* char
     117
     118<procedure>(gen range)</procedure>
     119Creates a generator that can be created for the given range. It dispatches on the type of the lower bound.
     120Currently supported types are:
     121
     122* fixnum
     123* flonum
     124* char
     125
     126<procedure>(register-generator-for-type! type? gen)</procedure>
     127Registers a type-predicate with a generator. When the generic generator procedure '''gen''' is invoked, it will dispatch
     128on the first argument using the registered type-predicates.
    93129
    94130==== Parameters
     
    110146Generator for a fixnum between '''gen-current-fixnum-min''' '''and upper-bound'''
    111147
     148<procedure>(gen-fixnum range)</procedure>
     149Generator for a fixnum within the given '''range'''
     150
    112151<procedure>(gen-fixnum lower-bound upper-bound)</procedure>
    113152Generator for a fixnum between '''lower-bound''' and '''upper-bound'''
    114153
     154<procedure>(gen-odd-fixnum ...)</procedure>
     155This generator supports all the variations for '''gen-fixnum''' but it will only yield odd numbers
     156
     157<procedure>(gen-even-fixnum ...)</procedure>
     158This generator supports all the variations for '''gen-fixnum''' but it will only yield even numbers
     159
    115160<procedure>(gen-int8)</procedure>
    116161Generator for a fixnum between -127 and 127.
     
    144189Generator for a real number between 0.0 and '''upper-bound'''.
    145190
     191<procedure>(gen-real range)</procedure>
     192Generator for a real number in the given '''range'''.
     193
    146194<procedure>(gen-real lower-bound upper-bound)</procedure>
    147195Generator for a real number between '''lower-bound''' and '''upper-bound'''.
     
    151199
    152200<procedure>(gen-char charset)</procedure>
    153 Generator for a character from the char-set '''charset'''
     201Generator for a character from the char-set '''charset'''.
     202
     203<procedure>(gen-char range)</procedure>
     204Generator for a character in the given '''range'''.
    154205
    155206<procedure>(gen-char lower-bound upper-bound)</procedure>
     
    168219Generator that draws a random sample from the list of '''candidates'''.
    169220 
    170 
    171221==== Combinators
    172222
     
    175225This defaults to (gen-uint8).
    176226
     227<procedure>(gen-values-of . gens)</procedure>
     228Generator that returns multiple values. Each value is generated with the given generators
     229
    177230<procedure>(gen-pair-of car-gen cdr-gen)</procedure>
    178231Generator for a pair where the car is generated with '''car-gen''' and the cdr is generated with with '''cdr-gen'''.
     
    183236<procedure>(gen-tuple-of . gens)</procedure>
    184237Generator for a tuple where each element is generated by the given generators.
    185 
    186238
    187239<procedure>(gen-string-of char-gen size #!optional (size (gen-current-default-size)))</procedure>
     
    213265Generator for a record that is created with the given '''ctor''' where each slot is generated with the given '''slot-gens'''.
    214266
     267<procedure>(gen-transform transformer gen)</procedure>
     268Generator that retrieves values from '''gen''' and applies '''transformer''' to this value.
     269
     270
    215271<syntax>(with-size size-spec ...)</syntax>
    216272Use this to constrain the size of the data generated by the combinators.
    217 '''size-spec''' is either a fixnum or a pair of fixnums. If it is a
     273'''size-spec''' is either a fixnum a range or a generator. If it is a
    218274fixnum all data will be generated with
    219 the size equal to that fixnum. If it is a pair, all data will be
    220 generated with a size that lies between the car of the pair
    221 and the cdr of the pair.
    222 
     275the size equal to that fixnum. If it is a range, all data will be
     276generated with a size that lies within that range. If it's a generator, this
     277generator is expected to return the size.
    223278
    224279==== Defining your own generators
Note: See TracChangeset for help on using the changeset viewer.