For Development HEAD DRAFTSearch (procedure/syntax/module):

11.68 srfi.252 - プロパティテスト

Module: srfi.252

プロパティテストは、テストしたいコンポーネントにランダムな入力を供給して 満たすべき属性(プロパティ)が満たされるかをテストする方法です。 HaskellのQuickCheckによってポピュラーになりました。

このモジュールでは、プロパティテストを実行するためのAPI群と、 ランダムな入力を生成するためのAPI群のふたつを提供します。

テスト手続きはgauche.testと統合されています (gauche.test - 単体テスト参照)。また、このSRFIはSRFI-64と併せて使えるように 設計されており、GaucheでもSRFI-64と併用することができます (srfi.64 - SchemeテストスイートAPI参照)。

ランダムな入力を生成するジェネレータはdata.randomの上に 構築されています (data.random - ランダムデータの生成参照)。 これは簡便のために提供されています。必要なら、独自にジェネレータを作っても構いません。

テストフォーム

Macro: test-property property-check generator-list :optional runs
Macro: test-property-expect-fail property-check generator-list :optional runs
Macro: test-property-skip property-check generator-list :optional runs
Macro: test-property-error property-check generator-list :optional runs
Macro: test-property-error-type error-type property-check generator-list :optional runs

[SRFI-252]{srfi.252} generator-listにあるジェネレータが生成した値を引数として、 property-checkを呼びます。それをruns回繰り返します。

property-checkは手続きに評価される式で、プロパティが満たされたら#tを、 そうでなければ#fを返します。 引数はgenerator-listにリストされた各ジェネレータが生成します。 generator-listproperty-checkが受け取る引数と 同数のジェネレータから構成されていなければなりません。

省略可能なruns引数は非負の整数で、property-checkを何回呼び出すかを 指定します。省略された場合はシステムのデフォルトが使われます (Gaucheでは今のところ30。但し将来変わる可能性はあります)。

test-propertyが最も基本的なもので、全てのproperty-checkの呼び出しが 成功であれば成功します。 test-property-expect-failは逆に、全てのproperyt-check の呼び出しが失敗した場合に成功します (つまり、失敗することを期待したチェックです)。 test-property-skipproperty-checkは呼び出さず、 runs回のテストをスキップしたということだけを記録します。 最後に、test-property-errortest-property-error-typeproperty-checkが常にエラーを投げることを期待します。 test-property-error-typeはそれに加え、投げられるエラーが error-typeコンディションタイプであることを期待します。

SRFIではこれらのテストはSRFI-64のフレームワーク上で実行されることを 想定していますが、srfi.64をロードせずにgauche.testのテストの 一環として実行することも可能です。

Function: property-test-runner

[SRFI-252]{srfi.252} Returns a SRFI-64 test runner object specialized for property testing. In Gauche, this simply returns SRFI-64 simple runner, but other implementations may have some additional features. It is recommended that portable code sets the runner created with this procedure before calling property test procedures.

Generators

These are procedures that create generators of specific types. You can find similar generator procedurs in data.random (see data.random - ランダムデータの生成) and srfi.194 (see srfi.194 - ランダムデータジェネレータ (SRFI)), but the procedures here creates generators with one specific property suitable for testing: They start generating certain pre-defined values–typical, or boundary values–before generating random values.

All created generators captures the value of parameter random-data-random-source (see data.random - ランダムデータの生成) when created. If you want to have reproducible results, you can provide your own random source.

Function: boolean-generator

[SRFI-252]{srfi.252} Returns a generator that yields #t and #f, then a random boolean values in equal probability.

See also booleans$ in data.random (see data.random - ランダムデータの生成).

Function: bytevector-generator

[SRFI-252]{srfi.252} Returns a generator that yields an empty bytevector #u8() first, then bytevectors of random length and random content subsequently.

In Gauche, it is realized as follows (see data.random - ランダムデータの生成, for sequences-of and integers$). The constant sequence-max-size is currently 33 but may be changed.

(gcons* '#u8()
        (sequences-of <u8vector>
                      (integers$ sequence-max-size)
                      (integers$ 256))))
Function: char-generator

[SRFI-252]{srfi.252} Returns a character generator that yields #\null, then random characters subsequently. Currently we sample charactres uniformly from the entire Unicode range, but that causes most of the characters to be very infrequently used ones. We might change it in future.

See also chars$ in data.random (see data.random - ランダムデータの生成).

Function: string-generator

[SRFI-252]{srfi.252} Returns a string generator that yields an empty string "", then strings of random length and random content subsequently.

In Gauche, it is realized as follows (see data.random - ランダムデータの生成, for strings-of and integers$). The constant sequence-max-size is currently 33 but may be changed.

(gcons* ""
  (strings-of (integers$ sequence-max-size) (chars$ char-set:full)))

Note that the chracters are sampled uniformly from the entire Unicode range, which causes almost all strings to consist of very infrequently used characters. We might address this in future.

Function: symbol-generator

[SRFI-252]{srfi.252} Returns a symbol generator that yields a symbol whose name is empty (||), then symbols of random length and random content subsequently. It is basically (gmap string->symbol (string-generator)).

Note that the chracters are sampled uniformly from the entire Unicode range, which causes almost all symbols to consist of very infrequently used characters. We might address this in future.

Function: complex-generator
Function: inexact-complex-generator

[SRFI-252]{srfi.252} Returns a generator that yields the following inexact complex numbers, followed by random complex numbers.

0.0 -0.0 0.5 -0.5 1.0 -1.0
0.0+1.0i 0.0-1.0i -0.0+1.0i -0.0-1.0i
0.5+0.5i 0.5-0.5i -0.5+0.5i -0.5-0.5i
1.0+1.0i 1.0-1.0i -1.0+1.0i -1.0-1.0i
+inf.0+inf.0i +inf.0-inf.0i -inf.0+inf.0i -inf.0-inf.0i
+nan.0+nan.0i
+inf.0 -inf.0 +nan.0

Note: SRFI specifies complex-generator to return both exact and inexact complex numbers if the implementation supports them. Gauche doesn’t have exact complex numbers, so it is the same as inexact-complex-generator.

In Gauche, a random complex number is created by uniformly sampled finite flonums for real and imaginary parts. Note that “uniformly sampled finite flonums” are not the same as “uniformly sampled real numbers”; See finite-flonums$ for the details (see data.random - ランダムデータの生成).

Function: exact-complex-generator
Function: exact-integer-complex-generator

[SRFI-252]{srfi.252} Gauche does not support exact complex numbers, and these procedures raise an error when called.

Function: integer-generator
Function: exact-integer-generator
Function: inexact-integer-generator

[SRFI-252]{srfi.252} Return generators that yields integers, exact integers, and inexact integers, respectively.

The exact integer generator always yield 0, 1, and -1 at the beginning, then random integers follow.

The inexact integer generator always yield 0.0, -0.0, 1.0, and -1.0 at the beginning, then random integers follow.

The general integer generator created by integer-generator samples evenly from an exact integer genratgor and an inexact integer generator.

Since the range of exact integers is only bounded by the memory, uniform sampling from the entire possible range is infeasible; almost all samples would become a huge integer. Instead, we split integer range into 3: relatively small integers, fixnums, and bignums, and sample evenly from these three generators. Small integers and fixnums are sampled uniformly within its range, and bignums are sampled according to the power law; so, we see a lot more bignums closer to fixnum range, with occasional really big numbers sprinkled in.

Function: rational-generator
Function: exact-rational-generator
Function: inexact-rational-generator

[SRFI-252]{srfi.252} Return generators that yields rational numbers, exact rational numbers, and inexact rational numbers, respectively.

The exact rational number generator yields 0, 1, -1, 1/2, and -1/2 at the beginning, then generate (/ a b) where a, b are generated in the same way as exact-integer-generator’s distribution, except 0 is excluded from b.

The inexact rational number generator yields 0.0, -0.0, 0.5, =0.5, 1.0, and -1.0 at the beginning, then proceed to generate random inexact rational numbers. In Gauche, inexact rational numbers are finite flonums, and we uniformly samples from all possible finite flonums (which is not the same as uniform sample from a range of real numbers). See finite-flonums$ in data.random - ランダムデータの生成.

The general rational number generator created by rational-generator evenly samples from an exact rational number generator and an inexact rational number generator.

Function: real-generator
Function: exact-real-generator
Function: inexact-real-generator

[SRFI-252]{srfi.252} Return generators that yields real numbers, exact real numbers, and inexact real numbers, respectively.

In Gauche, the set of exact real numbers are exactly the same as the set of exact rational numbers (infinites and NaNs are inexact), so exact-real-generator is the same as exact-rational-generator.

The inexact real number generator yields 0.0, -0.0, 0.5, -0.5, 1.0, -1.0, +inf.0, -inf.0, and +nan.0 at the beginning, followed by inexact real numbers uniformly sampled from all possible finite flonums (which is not the same as uniform sample from a range of real numbers). See finite-flonums$ in data.random - ランダムデータの生成.

The general real number generator created by real-generator evenly samples from an exact real number generator and an inexact real number generator.

Function: number-generator
Function: exact-number-generator
Function: inexact-number-generator

[SRFI-252]{srfi.252} In Gauche, the set of exact numbers is the same as the set of exact rational numbers (we don’t have exact complex), and the set of inexact numbers is the same as the set of inexact complex numbers. Thus exact-number-generator is the same as exact-rational-generator, and inexact-number-generator is the same as inexact-complex-generator.

The general number generator created by number-generator evenly samples from an exact number generator and an inexact number generator.

Function: list-generator-of elt-generator :optional max-length
Function: vector-generator-of elt-generator :optional mex-length

[SRFI-252]{srfi.252} Return generators that yield a list/vector of random length up to max-length, and elements generated by elt-generator, respectively. Similar to lists-of/vectors-of in data.random.

When max-length is omitted, we leave the length to the default of lists-of/vectors-of, which is determined by the parameter default-sizer in data.random. See data.random - ランダムデータの生成, for the details.

Function: pair-generator-of car-generator :optional cdr-generator

[SRFI-252]{srfi.252} Returns a generator that yields pairs, whose car is generated with car-generator and whose cdr is generated with cdr-generator. If cdr-generator is omitted, car-generator is used.

Function: procedure-generator-of value-generator

[SRFI-252]{srfi.252} Returns a generator that yields procedures, which accepts any number of arguments and returns a value generated with value-generator.



For Development HEAD DRAFTSearch (procedure/syntax/module):
DRAFT