File: combinatorics.test

package info (click to toggle)
tcllib 1.20%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 68,064 kB
  • sloc: tcl: 216,842; ansic: 14,250; sh: 2,846; xml: 1,766; yacc: 1,145; pascal: 881; makefile: 107; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (323 lines) | stat: -rw-r--r-- 9,761 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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# Tests for combinatorics functions in math library  -*- 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.
#
# Copyright (c) 2001 by Kevin B. Kenny
# All rights reserved.
#
# RCS: @(#) $Id: combinatorics.test,v 1.14 2006/10/09 21:41:41 andreas_kupries Exp $

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

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

testsNeedTcl     8.5
testsNeedTcltest 1.0

testing {
    useLocal math.tcl math
}

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

# Fake [lset] for Tcl releases that don't have it.  We need only
# lset into a flat list.

if { [string compare lset [info commands lset]] } {
    proc K { x y } { set x }
    proc lset { listVar index var } {
	upvar 1 $listVar list
	set list [lreplace [K $list [set list {}]] $index $index $var]
    }
}

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

test combinatorics-1.1 { math::ln_Gamma, wrong num args } {
    catch { math::ln_Gamma } msg
    set msg
} [tcltest::wrongNumArgs math::ln_Gamma x 0]

test combinatorics-1.2 { math::ln_Gamma, main line code } {
    set maxerror 0.
    set f 1.
    for { set i 1 } { $i < 171 } { set i $ip1 } {
	set f [expr { $f * $i }]
	set ip1 [expr { $i + 1 }]
	set f2 [expr { exp( [math::ln_Gamma $ip1] ) }]
	set error [expr { abs( $f2 - $f ) / $f }]
	if { $error > $maxerror } {
	    set maxerror $error
	}
    }
    if { $maxerror > 5e-10 } {
	error "max error of factorials computed using math::ln_Gamma\
               specified to be 5e-10, was $maxerror"
    }
    concat
} {}

test combinatorics-1.3 { math::ln_Gamma, half integer args } {
    set maxerror 0.
    set z 0.5
    set pi 3.1415926535897932
    set g [expr { sqrt( $pi ) }]
    while { $z < 170. } {
	set g2 [expr { exp( [::math::ln_Gamma $z] ) }]
	set error [expr { abs( $g2 - $g ) / $g }]
	if { $error > $maxerror } {
	    set maxerror $error
	}
	set g [expr { $g * $z }]
	set z [expr { $z + 1. }]
    }
    if { $maxerror > 5e-10 } {
	error "max error of half integer gamma computed using math::ln_Gamma\
               specified to be 5e-10, was $maxerror"
    }
    concat
} {}

test combinatorics-1.4 { math::ln_Gamma, bogus arg } {
    catch { math::ln_Gamma bogus } msg
    set msg
} {expected a floating-point number but found "bogus"}

test combinatorics-1.5 { math::ln_Gamma, evaluate at pole } {
    catch { math::ln_Gamma 0.0 } msg
    list $msg $::errorCode
} {{argument to math::ln_Gamma must be positive} {ARITH DOMAIN {argument to math::ln_Gamma must be positive}}}

test combinatorics-1.6 { math::ln_Gamma, exponent overflow } {
    catch { math::ln_Gamma 2.556348163871691e+305 } msg
    list $msg $::errorCode
} {{floating-point value too large to represent} {ARITH OVERFLOW {floating-point value too large to represent}}}

test combinatorics-2.1 { math::factorial, wrong num args } {
    catch { math::factorial } msg
    set msg
} [tcltest::wrongNumArgs math::factorial x 0]

test combinatorics-2.2 { math::factorial 0 } {
    math::factorial 0
} 1

test combinatorics-2.3 { math::factorial, main line } {
    set maxerror 0.
    set f 1.
    for { set i 1 } { $i < 171 } { set i $ip1 } {
	set f [expr { $f * $i }]
	set ip1 [expr { $i + 1 }]
	set f2 [math::factorial $i]
	set error [expr { abs( $f2 - $f ) / $f }]
	if { $error > $maxerror } {
	    set maxerror $error
	}
    }
    if { $maxerror > 1e-16 } {
	error "max error of factorials computed using math::factorial\
               specified to be 1e-16, was $maxerror"
    }
    concat
} {}

test combinatorics-2.4 { math::factorial, half integer args } {
    set maxerror 0.
    set z -0.5
    set pi 3.1415926535897932
    set g [expr { sqrt( $pi ) }]
    while { $z < 169. } {
	set g2 [math::factorial $z]
	set error [expr { abs( $g2 - $g ) / $g }]
	if { $error > $maxerror } {
	    set maxerror $error
	}
	set z [expr { $z + 1. }]
	set g [expr { $g * $z }]
    }
    if { $maxerror > 1e-9 } {
	error "max error of half integer factorial\
               specified to be 1e-9, was $maxerror"
    }
    concat
} {}

test combinatorics-2.5 { math::factorial, bogus arg } {
    catch { math::factorial bogus } msg
    set msg
} {expected a floating-point number but found "bogus"}

test combinatorics-2.6 { math::factorial, evaluate at pole } {
    catch { math::factorial -1.0 } msg
    list $msg $::errorCode
} {{argument to math::factorial must be greater than -1.0} {ARITH DOMAIN {argument to math::factorial must be greater than -1.0}}}

test combinatorics-2.7 { math::factorial, exponent overflow } {
    if {![catch { 
	math::factorial 171 
    } msg]} {
	if { [string equal $msg Infinity] || [string equal $msg Inf] } {
	    set result ok
	} else {
	    set result "result of factorial was [list $msg],\
	                should be Infinity"
	}
    } else {
	if { [string equal [lrange $::errorCode 0 1] {ARITH OVERFLOW}] } {
	    set result ok
	} else {
	    set result "error from factorial was [list $::errorCode],\
                        should be {ARITH IOVERFLOW *}"
	}
    }
    set result
} ok

test combinatorics-2.8 { math::factorial, "" arg } {
    catch { math::factorial "" } msg
    list $msg
} {{expected a floating-point number but found ""}}

test combinatorics-3.1 { math::choose, wrong num args } {
    catch { math::choose } msg
    set msg
} [tcltest::wrongNumArgs math::choose {n k} 0]

test combinatorics-3.2 { math::choose, wrong num args } {
    catch { math::choose 1 } msg
    set msg
} [tcltest::wrongNumArgs math::choose {n k} 1]

test combinatorics-3.3 { math::choose, precomputed table and gamma evals } {
    set maxError 0
    set l {}
    for { set n 0 } { $n < 100 } { incr n } {
	lappend l 1.
	for { set k [expr { $n - 1 }] } { $k > 0 } { set k $km1 } {
	    set km1 [expr { $k - 1 }]
	    set cnk [expr { [lindex $l $k] + [lindex $l $km1] }]
	    lset l $k $cnk
	    set ccnk [math::choose $n $k]
	    set error [expr { abs( $ccnk - $cnk ) / $cnk }]
	    if { $error > $maxError } {
		set maxError $error
	    }
	}
    }
    if { $maxError > 5e-10 } {
	error "max error in math::choose was $maxError, specified to be 5e-10"
    }
    concat
} {}

test combinatorics-3.4 { math::choose, bogus n } {
    catch { math::choose bogus 0 } msg
    set msg
} {expected a floating-point number but found "bogus"}

test combinatorics-3.5 { math::choose bogus k } {
    catch { math::choose 0 bogus } msg
    set msg
} {expected a floating-point number but found "bogus"}

test combinatorics-3.6 { match::choose negative n } {
    catch { math::choose -1 0 } msg
    list $msg $::errorCode
} {{first argument to math::choose must be non-negative} {ARITH DOMAIN {first argument to math::choose must be non-negative}}}

test combinatorics-3.7 { math::choose negative k } {
    math::choose 17 -1
} 0

test combinatorics-3.8 { math::choose excess k } {
    math::choose 17 18
} 0

test combinatorics-3.9 {math::choose negative fraction } {
    catch { math::choose 17 -0.5 } msg
    list $msg $::errorCode
} {{second argument to math::choose must be non-negative, or both must be integers} {ARITH DOMAIN {second argument to math::choose must be non-negative, or both must be integers}}}

test combinatorics-3.10 { math::choose big args } {
    if {![catch { 
	math::choose 1500 750
    } msg]} {
	if { [string equal $msg Infinity] || [string equal $msg Inf] } {
	    set result ok
	} else {
	    set result "result of choose was [list $msg],\
	                should be Infinity"
	}
    } else {
	if { [string equal [lrange $::errorCode 0 1] {ARITH OVERFLOW}] } {
	    set result ok
	} else {
	    set result "error from choose was [list $::errorCode],\
                        should be {ARITH IOVERFLOW *}"
	}
    }
    set result
} ok

test combinatorics-4.1 { math::Beta, wrong num args } {
    catch { math::Beta } msg
    set msg
} [tcltest::wrongNumArgs math::Beta {z w} 0]

test combinatorics-4.2 { math::Beta, wrong num args } {
    catch { math::Beta 1 } msg
    set msg
} [tcltest::wrongNumArgs math::Beta {z w} 1]

test combinatorics-4.3 { math::Beta, bogus z } {
    catch { math::Beta bogus 1 } msg
    set msg
} {expected a floating-point number but found "bogus"}

test combinatorics-4.4 { math::Beta, bogus w } {
    catch { math::Beta 1 bogus } msg
    set msg
} {expected a floating-point number but found "bogus"}

test combinatorics-4.5 { math::Beta, negative z } {
    catch { math::Beta 0 1 } msg
    list $msg $::errorCode
} {{first argument to math::Beta must be positive} {ARITH DOMAIN {first argument to math::Beta must be positive}}}

test combinatorics-4.6 { math::Beta, negative w } {
    catch { math::Beta 1 0 } msg
    list $msg $::errorCode
} {{second argument to math::Beta must be positive} {ARITH DOMAIN {second argument to math::Beta must be positive}}}

test combinatorics-4.7 { math::Beta, test with Pascal } {
    set maxError 0
    set l {}
    for { set n 0 } { $n < 100 } { incr n } {
	lappend l 1.
	for { set k [expr { $n - 1 }] } { $k > 0 } { set k $km1 } {
	    set km1 [expr { $k - 1 }]
	    set cnk [expr { [lindex $l $k] + [lindex $l $km1] }]
	    lset l $k $cnk
	    set w [expr { $k + 1 }]
	    set z [expr { $n - $k + 1 }]
	    set beta [expr { 1.0 / $cnk / ( $z + $w - 1 )}]
	    set cbeta [math::Beta $z $w]
	    set error [expr { abs( $cbeta - $beta ) / $beta }]
	    if { $error > $maxError } {
		set maxError $error
	    }
	}
    }
    if { $maxError > 5e-10 } {
	error "max error in math::Beta was $maxError, specified to be 5e-10"
    }
    concat
} {}

    
testsuiteCleanup