File: Testing.k

package info (click to toggle)
kaya 0.2.0-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,012 kB
  • ctags: 1,307
  • sloc: cpp: 6,691; haskell: 4,833; sh: 2,868; yacc: 768; makefile: 700; perl: 87
file content (197 lines) | stat: -rw-r--r-- 4,637 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/** -*-C-*-ish
    Kaya standard library
    Copyright (C) 2004-2006 Edwin Brady

    This file is distributed under the terms of the GNU Lesser General
    Public Licence. See COPYING for licence.
*/

/// Support for unit test creation.

module Testing;

import Prelude;
import IO;
import Time;

globals {
    [(Void(), Int, String)] tests;
}

"Check the result of a test.
On failure, throw an exception with the given message."
public Void assert(Bool test, String msg = "Unspecified Assertion Failure")
{
    if (!test) {
	throw(Exception(msg,0));
    }
}

// Functions for generating a variety of random test cases.

"An arbitrary union type.
For creating completely random union values for testing polymorphic functions."
public data Arbitrary 
    = AnInt(Int num)
    | AString(String str)
    | AChar(Char ch)
    | AFloat(Float float)
    | AnArray([Arbitrary] array)
    | Recursive(Arbitrary value)
    | RecPair(Arbitrary value1, Arbitrary value2);

public Arbitrary randomValue()
{
    case rand()%7 of {
	0 -> return AnInt(randomInt);
	| 1 -> return AString(randomString());
	| 2 -> return AChar(randomChar);
	| 3 -> return AFloat(randomFloat);
// keep it quite small, don't want to spend ages generating a value!
	| 4 -> return AnArray(randomArray(@randomValue,0,10)); 
	| 5 -> return Recursive(randomValue);
	| 6 -> return RecPair(randomValue, randomValue);
    }
}

"Return any random integer"
public Int randomInt() 
{
    if (rand()%2 == 0) {
        return randomPositiveInt();
    } else {
        return randomNegativeInt();
    }
}

"Return any random positive integer."
public Int randomPositiveInt()
{
    return rand();
}

"Return any random negative integer."
public Int randomNegativeInt()
{
    return -rand();
}

"Return an even distribution of negative, positive and zero integers"
public Int randomSignOfInt()
{
    case rand()%3 of {
      0 -> return randomPositiveInt();
      | 1 -> return randomNegativeInt();
      | 2 -> return 0;
    }
}

"Return any random float."
public Float randomFloat() 
{
    if (rand()%2 == 0) {
        return randomPositiveFloat();
    } else {
        return randomNegativeFloat();
    }
}

"Return any random positive float."
public Float randomPositiveFloat()
{
    return Float(rand)/Float(rand+1); // No, I'm not convinced either. It'll do.
}

"Return any random negative float."
public Float randomNegativeFloat()
{
    return -Float(rand)/Float(rand+1); // No, I'm not convinced either. It'll do.
}

"Return any random character."
public Char randomChar()
{
    return Char(rand()%256);
}

"Return any random string."
public String randomString(Int minsize = 0, Int maxsize = 160)
{
    num = (rand()%(maxsize-minsize))+minsize;
    str = "";
    for x in [1..num] {
	str += randomChar();
    }
    return str;
}

"Return a random array, with elements given by the generator."
public [a] randomArray(a() generator, Int minsize = 0, Int maxsize = 1000)
{
    num = (rand()%(maxsize-minsize))+minsize;
    arr = [];
    for x in [1..num] {
	push(arr,generator());
    }
    return arr;
}

public [Int] randomIntArray(Int minsize = 0, Int maxsize = 1000)
    = randomArray(@randomInt, minsize, maxsize);

public [Float] randomFloatArray(Int minsize = 0, Int maxsize = 1000)
    = randomArray(@randomFloat, minsize, maxsize);

public [Char] randomCharArray(Int minsize = 0, Int maxsize = 1000)
    = randomArray(@randomChar, minsize, maxsize);

public [String] randomStringArray(Int minsize = 0, Int maxsize = 10000)
    = randomArray(randomString@(minsize,maxsize), minsize, maxsize);

"Initialise the test framework.
Clears all current tests."
public Void init() {
    tests = [];
}

"Add a test function.
The function simply runs a test case, finishing with a call to 'assert'.
Optionally, give the number of times to run the test (useful if it is
generating multiple random values) and a descriptive name."
public Void add(Void() test, Int num = 1, String name = "") {
    push(tests, (@test, num, name));
}

"Run the tests.
Returns true if all tests pass, or false otherwise."
public Bool run() {
    srand(time); 
    failure = 0;
    success = 0;
    for t@testnum in tests {
	(test, num, name) = t;
	Prelude::putStr("Test "+(testnum+1)+" ");
	if (name!="") {
	    Prelude::putStr("("+name+") ");
	}
	try {
	    for x in [1..num] {
		test();
	    }
	    putStrLn("success");
	    success++;
	}
	catch(e) {
	    putStrLn("FAILURE: "+exceptionMessage(e));
	    failure++;
	}
    }
    if (failure==0) {
	putStrLn("\nAll tests pass");
	return true;
    }
    else {
	putStrLn("\n"+failure+" test failure"+(if (failure!=1) "s" else ""));
    }
    return false;
}