#! /usr/bin/env groovy class Factorial_J_BigInteger_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 correctTest ( function ) { data.each { datum -> assertEquals ( datum[1] , function ( datum[0] ) ) } } void test_correctIterative ( ) { correctTest ( Factorial_J_BigInteger.&iterative ) } void test_correctRecursive ( ) { correctTest ( Factorial_J_BigInteger.&recursive ) } void test_correctTailRecursive ( ) { correctTest ( Factorial_J_BigInteger.&tailRecursive ) } void negativeTest ( function ) { ( -20 .. -1 ).each { i -> shouldFail ( FactorialIllegalArgumentException ) { function ( i ) } } } void test_negativeIterative ( ) { negativeTest ( Factorial_J_BigInteger.&iterative ) } void test_negativeRecursive ( ) { negativeTest ( Factorial_J_BigInteger.&recursive ) } void test_negativeTailRecursive ( ) { negativeTest ( Factorial_J_BigInteger.&tailRecursive ) } void test_iterativeEnormousSucceeds ( ) { Factorial_J_BigInteger.iterative ( 10000 ) ; } void test_recursiveEnormousFails ( ) { shouldFail ( StackOverflowError ) { Factorial_J_BigInteger.recursive ( 8200 ) ; } } void test_tailRecursiveEnormousFails ( ) { shouldFail ( StackOverflowError ) { Factorial_J_BigInteger.tailRecursive ( 8700 ) ; } } void floatingPointTest ( function ) { ( -12 .. 2 ).each { i -> shouldFail ( MissingMethodException ) { function ( i + 0.5 ) } } } void test_floatingPointIterative ( ) { floatingPointTest ( Factorial_J_BigInteger.&iterative ) } void test_floatingPointRecursive ( ) { floatingPointTest ( Factorial_J_BigInteger.&recursive ) } void test_floatingPointTailRecursive ( ) { floatingPointTest ( Factorial_J_BigInteger.&tailRecursive ) } }