Files
mercury/extras/quickcheck/tutes/use62.m
Simon Mei e56d7cc495 qcheck is an autotesting tool similar to Haskell's Quickcheck.
Estimated hours taken : 250

qcheck is an autotesting tool similar to Haskell's Quickcheck.

A collection of web pages are written as a user guide for quickcheck.
It covers the syntax and features supported by the current version of
quickcheck.

RELEASE_NOTES:
compiler/notes/authors.html:
extras/README:
        Modified to mention quickcheck.

extras/quickcheck/qcheck.m:
        New file that contains the source code for qcheck.

extras/quickcheck/rnd.m:
        New file written by conway. Its functions are similar
        to those in library random.m. The random numbers generated
        with random.m functions show strong correlation in lower
        bits; rnd.m does not seem have such problems.

extras/quickcheck/tutes/T1.html:
extras/quickcheck/tutes/T2.html:
extras/quickcheck/tutes/T3.html:
extras/quickcheck/tutes/T4.html:
extras/quickcheck/tutes/T5.html:
extras/quickcheck/tutes/T6.html:
extras/quickcheck/tutes/T7.html:
extras/quickcheck/tutes/T8.html:
extras/quickcheck/tutes/T9.html:
extras/quickcheck/tutes/T10.html:
        New files, each html covers a topic.

extras/quickcheck/tutes/index.html:
        New file, the index.

extras/quickcheck/tutes/mymax.m:
extras/quickcheck/tutes/nrev.m:
extras/quickcheck/tutes/nrev2.m:
        New files, contains mymax/2, nrev/1 and nrev/2.

extras/quickcheck/tutes/use.m:
extras/quickcheck/tutes/use1.m:
extras/quickcheck/tutes/use11.m:
extras/quickcheck/tutes/use20.m:
extras/quickcheck/tutes/use21.m:
extras/quickcheck/tutes/use22.m:
extras/quickcheck/tutes/use31.m:
extras/quickcheck/tutes/use33.m:
extras/quickcheck/tutes/use51.m:
extras/quickcheck/tutes/use62.m:
extras/quickcheck/tutes/use71.m:
extras/quickcheck/tutes/use81.m:
extras/quickcheck/tutes/use91.m:
        New files, contains examples shown in each tutorial.
2001-05-31 02:08:49 +00:00

100 lines
2.4 KiB
Mathematica

%---------------------------------------------------------------------------%
% Copyright (C) 2001 The University of Melbourne.
% This file may only be copied under the terms of the GNU General
% Public License - see the file COPYING in the Mercury distribution.
%---------------------------------------------------------------------------%
:- module use62.
:- interface.
:- use_module io.
:- pred main(io__state, io__state).
:- mode main(di, uo) is det.
%---------------------------------------------------------------------------%
:- implementation.
:- import_module int, list, string.
:- import_module qcheck, rnd.
%---------------------------------------------------------------------------%
% arbitrary user-defined types for testing purposes
%---------------------------------------------------------------------------%
:- type bullet
---> good(color)
; inaccurate(color)
; defective(color).
:- type color
---> black
; white.
%---------------------------------------------------------------------------%
main -->
{ freq_B(B) },
{ freq_W(W) },
qcheck(qcheck__f(prop2), "bullet fight", 10000, [[],B,W], []).
:- pred freq_B(list(frequency)).
:- mode freq_B(out) is det.
freq_B(Out) :-
Out = [ {50, [ [ {100, []}, {0, []} ] ] },
{10, [ [ {100, []}, {0, []} ] ] },
{40, [ [ {100, []}, {0, []} ] ] }
].
:- pred freq_W(list(frequency)).
:- mode freq_W(out) is det.
freq_W(Out) :-
Out = [ {40, [ [ {0, []}, {100, []} ] ] },
{30, [ [ {0, []}, {100, []} ] ] },
{30, [ [ {0, []}, {100, []} ] ] }
].
:- func prop2(int, bullet, bullet) = property.
prop2(Seed, B, W) = fight(Seed, B, W) `>>>`
({"ComB",B} `>>>`
({"ComW", W} `>>>` [yes])
).
:- func fight(int, bullet, bullet) = string.
:- mode fight(in, in, in) = out is det.
fight(Seed, B, W) = String :-
rnd__init(Seed, RS0),
B_hit = is_hit(B, RS0, RS1),
W_hit = is_hit(W, RS1, _),
(if B_hit = W_hit
then
String = "draw"
else if B_hit > W_hit
then
String = "B win"
else
String = "W win"
).
:- func is_hit(bullet, rnd, rnd) = int.
:- mode is_hit(in, in, out) = out is det.
is_hit(Bullet, RS0, RS) = Int :-
Temp = rand_allint(RS0, RS) rem 2,
(
Bullet = good(_),
Int = 1
;
Bullet = inaccurate(_),
(if Temp = 0
then
Int = 1
else
Int = 0
)
;
Bullet = defective(_),
Int = 0
).