File: random.test

package info (click to toggle)
tcllib 2.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,560 kB
  • sloc: tcl: 306,798; ansic: 14,272; sh: 3,035; xml: 1,766; yacc: 1,157; pascal: 881; makefile: 124; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (273 lines) | stat: -rw-r--r-- 6,961 bytes parent folder | download | duplicates (2)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
## -*- tcl -*-
# Tests for the PRNG procedures -*- tcl -*-
#
# This file contains a collection of tests for one or more of the Tcllib
# procedures.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# $Id: random.test,v 1.1 2011/06/17 06:40:14 arjenmarkus Exp $
#
# Copyright (c) 2011 by Arjen Markus
# All rights reserved.
#
# -------------------------------------------------------------------------

source [file join \
        [file dirname [file dirname [file join [pwd] [info script]]]] \
        devtools testutilities.tcl]

testsNeedTcl     8.5
testsNeedTcltest 2.1

#support {
#    useLocal random.tcl     simulation::random
#}
testing {
    useLocal random.tcl simulation::random
}

# -------------------------------------------------------------------------

#
# As the values were given with four digits, an absolute
# error is most appropriate
#
proc matchNumbers {expected actual} {
    set match 1
    foreach a $actual e $expected {
        if {abs($a-$e) > 0.1e-4} {
            set match 0
            break
        }
    }
    return $match
}

customMatch numbers matchNumbers

# -------------------------------------------------------------------------

test "Bernoulli-1.0" "Bernoulli generator with p=0" \
    -body {
    set p [::simulation::random::prng_Bernoulli 0.0]
    set count 0
    for {set i 0} {$i < 1000} {incr i} {
        set rnd [$p]
        if { $rnd > 0.0 } {
            incr count
        }
    }
    set count
} -result 0

test "Bernoulli-1.1" "Bernoulli generator with p=1" \
    -body {
    set p [::simulation::random::prng_Bernoulli 1.0]
    set count 0
    for {set i 0} {$i < 1000} {incr i} {
        set rnd [$p]
        if { $rnd > 0.0 } {
            incr count
        }
    }
    set count
} -result 1000

test "Uniform-1.0" "Uniform generator with number between -1.0 and 1.0" \
    -body {
    set p [::simulation::random::prng_Uniform -1.0 1.0]
    set nearminus1 0
    set nearplus1 0
    set outside 0
    for {set i 0} {$i < 1000} {incr i} {
        set rnd [$p]
        if { $rnd > 0.9 } {
            incr nearplus1
        }
        if { $rnd < -0.9 } {
            incr nearminus1
        }
        if { $rnd < -1.0 || $rnd > 1.0 } {
            incr outside
        }
    }

    #
    # It is very unlikely that all 1000 numbers stay within the range -0.9 -- 0.9
    #
    set result [expr {$nearplus1 > 0 && $nearminus1 > 0 && $outside == 0}]
} -result 1

test "Exponential-1.0" "Exponential generator with minimum -1.0 and mean 1" \
    -body {
    set p [::simulation::random::prng_Exponential -1.0 1.0]
    set outside 0
    set mean    0.0
    for {set i 0} {$i < 1000} {incr i} {
        set rnd [$p]
        if { $rnd < -1.0 } {
            incr outside
        }
        set mean [expr {$mean + $rnd}]
    }
    set mean [expr {$mean / 1000.0}]

    #
    # We use a rough estimate for the deviation in the mean
    #
    set result [expr {$outside == 0 && abs($mean - 1.0) < 0.5}]
} -result 1

test "Discrete-1.0" "Discrete generator with numbers 0, 1, 2 and 3" \
    -body {
    set p [::simulation::random::prng_Discrete 4]
    set outside 0
    for {set i 0} {$i < 1000} {incr i} {
        set rnd [$p]
        switch -- $rnd {
            0 - 1 - 2 - 3 {
               # Nothing to do
            }
            default {
               incr outside
            }
        }
    }
    set result [expr {$outside == 0}]
} -result 1

test "Poisson-1.0" "Poisson generator with mean 10" \
    -body {
    set p [::simulation::random::prng_Poisson 10]
    set noninteger 0
    set mean       0.0
    for {set i 0} {$i < 1000} {incr i} {
        set rnd [$p]
        if { ![string is integer -strict $rnd] } {
            incr noninteger
        }
        set mean [expr {$mean + $rnd}]
    }
    set mean [expr {$mean / 1000.0}]

    #
    # We use a rough estimate for the deviation in the mean
    #
    set result [expr {$noninteger == 0 && abs($mean - 10.0) < 0.5}]
} -result 1

test "Normal-1.0" "Normal generator with mean 1 and standard deviation 1" \
    -body {
    set p [::simulation::random::prng_Normal 1 1]
    set mean       0.0
    set stdev      0.0
    for {set i 0} {$i < 1000} {incr i} {
        set rnd [$p]
        set mean  [expr {$mean  + $rnd}]
        set stdev [expr {$stdev + $rnd * $rnd}]
    }
    set mean  [expr {$mean  / 1000.0}]
    set stdev [expr {sqrt($stdev / 1000.0)}]

    #
    # We use a rough estimate for the deviation in the mean and stdev
    # Main effect of test: is the procedure syntactically correct?
    #
    set result [expr {abs($mean - 1.0) < 0.5 && abs($stdev - 1.0) < 0.5}]
} -result 1

#
# TODO: These tests merely check that the generated procedure "works"
#
test "Pareto-1.0" "Pareto generator with minimum 1 and steepness 2" \
    -body {
    set p [::simulation::random::prng_Pareto 1 2]
    set rnd [$p]
    set result 1
} -result 1

test "Gumbel-1.0" "Gumbel generator with minimum 1 and scale factor 3" \
    -body {
    set p [::simulation::random::prng_Gumbel 1 3]
    set rnd [$p]
    set result 1
} -result 1

test "ChiSquared-1.0" "chi-squared generator with 4 degrees of freedom" \
    -body {
    set p [::simulation::random::prng_chiSquared 4]
    set rnd [$p]
    set result 1
} -result 1

test "Disk-1.0" "disk generator with radius 2" \
    -body {
    set p [::simulation::random::prng_Disk 2]
    set rnd [$p]
    set result [llength $rnd]
} -result 2

test "Ball-1.0" "ball generator with radius 2" \
    -body {
    set p [::simulation::random::prng_Ball 2]
    set rnd [$p]
    set result [llength $rnd]
} -result 3

test "Sphere-1.0" "sphere generator with radius 2.5" \
    -body {
    set p [::simulation::random::prng_Sphere 2.5]
    set rnd [$p]
    set result [llength $rnd]
} -result 3

test "Rectangle-1.0" "rectangle generator with sides 10 and 0.1" \
    -body {
    set p [::simulation::random::prng_Rectangle 10 0.1]
    set rnd [$p]
    set result [llength $rnd]
} -result 2

test "Block-1.0" "block generator with sides 10, 0.1 and 2.5" \
    -body {
    set p [::simulation::random::prng_Block 10 0.1 2.5]
    set rnd [$p]
    set result [llength $rnd]
} -result 3

test "Triangle-1.0" "triangularly distributed numbers between -1.0 and 1.0" \
    -body {
    set p [::simulation::random::prng_Triangle -1.0 1.0]

    set okay 1
    for {set i 0} {$i < 1000} {incr i} {
        set rnd [$p]

        if { $rnd < -1.0 || $rnd > 1.0 } {
            set okay 0
            break
        }
    }

    set okay
} -result 1

test "Triangle-1.1" "triangularly distributed numbers between -1.0 and 1.0 (alternative)" \
    -body {
    set p [::simulation::random::prng_Triangle 1.0 -1.0]

    set okay 1
    for {set i 0} {$i < 1000} {incr i} {
        set rnd [$p]

        if { $rnd < -1.0 || $rnd > 1.0 } {
            set okay 0
            break
        }
    }

    set okay
} -result 1

# End of test cases
testsuiteCleanup