The unit...
#!/opt/local/bin/tclsh
proc fib {n} {
if {$n==1} {return $n}
if {$n==2} {return [expr {$n-1}]}
if {$n==3} {return [expr {$n-1}]}
return [expr { [fib [expr {$n - 1}]] + [fib [expr {$n - 2}]] } ]
}
The test...
#!/opt/local/bin/tclsh
package require tcltest 2.0
namespace import ::tcltest::*
source ../src/fib.tcl
# 1 1 2 3 5 8 13 21 34
test fib-1 { Test first is 1 } {
fib 1
} 1
test fib-2 { Test second is 1 } {
fib 2
} 1
test fib-3 { Test third is 2 } {
fib 3
} 2
test fib-4 { Test fourth is 3 } {
fib 4
} 3
test fib-5 { Test fifth is 5 } {
fib 5
} 5
test fib-6 { Test ninth is 34 } {
fib 9
} 34
test fib-7 { Test fortieth is 102334155 } {
fib 40
} 102334155
I put in the last couple of tests for good luck and for #ian :-)
Oh I must have been bored....
/