#! /usr/bin/env groovy class Factorial_memoized_Test extends GroovyTestCase { def data = [ [ 0 , 1 ] , [ 1 , 1 ] , [ 2 , 2 ] , [ 3 , 6 ] , [ 4 , 24 ] , [ 5 , 120 ] , [ 6 , 720 ] , [ 7 , 5040 ] , [ 8 , 40320 ] , [ 9 , 362880 ] , [ 10 , 3628800 ] , [ 11 , 39916800 ] , [ 12 , 479001600 ] , [ 13 , 6227020800G ] , // Have to force the correct type here for some reason. [ 14 , 87178291200G ] , // Have to force the correct type here for some reason. [ 20 , 2432902008176640000G ] , // Have to force the correct type here for some reason. [ 30 , 265252859812191058636308480000000 ] , [ 40 , 815915283247897734345611269596115894272000000000 ] ] void test_correctIterative ( ) { data.each { datum -> assertEquals ( datum[1] , Factorial_memoized.iterative ( datum[0] ) ) } } void test_correctRecursive ( ) { data.each { datum -> assertEquals ( datum[1] , Factorial_memoized.recursive ( datum[0] ) ) } } void test_negativeIterative ( ) { ( -20 .. -1 ).each { i -> shouldFail { Factorial_memoized.iterative ( i ) } } } void test_negativeRecursive ( ) { ( -20 .. -1 ).each { i -> shouldFail { Factorial_memoized.recursive ( i ) } } } void test_floatingPointIterative ( ) { ( -12 .. 2 ).each { i -> shouldFail { Factorial_memoized.iterative ( i + 0.5 ) } } } void test_floatingPointRecursive ( ) { ( -12 .. 2 ).each { i -> shouldFail { Factorial_memoized.recursive ( i + 0.5 ) } } } }