samples/composition/src-test/api/FactorialTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 10:04:51 +0200
changeset 209 1c999569643b
parent 207 675393d7093a
child 210 acf2c31e22d4
permissions -rw-r--r--
to the end of daily_life
jtulach@17
     1
/*
jtulach@17
     2
 * Žluťoučký kůň je naše hříbátko.
jtulach@17
     3
 * and open the template in the editor.
jtulach@17
     4
 */
jtulach@17
     5
jtulach@17
     6
package api;
jtulach@17
     7
jtulach@17
     8
import junit.framework.TestCase;
jtulach@17
     9
jtulach@17
    10
/**
jtulach@17
    11
 *
jtulach@17
    12
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@17
    13
 */
jtulach@17
    14
public class FactorialTest extends TestCase {
jtulach@17
    15
    
jtulach@17
    16
    public FactorialTest(String testName) {
jtulach@17
    17
        super(testName);
jtulach@17
    18
    }            
jtulach@17
    19
jtulach@17
    20
    @Override
jtulach@17
    21
    protected void setUp() throws Exception {
jtulach@17
    22
        super.setUp();
jtulach@17
    23
    }
jtulach@17
    24
jtulach@17
    25
    @Override
jtulach@17
    26
    protected void tearDown() throws Exception {
jtulach@17
    27
        super.tearDown();
jtulach@17
    28
    }
jtulach@17
    29
    
jtulach@17
    30
    public void testFactorial3() {
jtulach@17
    31
        assertEquals(6, Factorial.factorial(3));
jtulach@17
    32
    }
jtulach@17
    33
    
jtulach@20
    34
    public void testFactorial4() {
jtulach@20
    35
        assertEquals(24, Factorial.factorial(4));
jtulach@20
    36
    }
jtulach@20
    37
    
jtulach@17
    38
    public void testFactorial5() {
jtulach@17
    39
        assertEquals(120, Factorial.factorial(5));
jtulach@17
    40
    }
jtulach@209
    41
jtulach@209
    42
    /** Class showing inventive, non-expected use of 
jtulach@209
    43
     * Arithmetica methods to do multiplication instead of
jtulach@209
    44
     * addition.
jtulach@209
    45
     */
jtulach@209
    46
    //BEGIN: design.composition.arith.factorial
jtulach@209
    47
    public static final class Factorial extends Arithmetica {
jtulach@209
    48
        public static int factorial(int n) {
jtulach@209
    49
            return new Factorial().sumRange(1, n);
jtulach@209
    50
        }
jtulach@209
    51
        @Override
jtulach@209
    52
        public int sumTwo(int one, int second) {
jtulach@209
    53
            return one * second;
jtulach@209
    54
        }
jtulach@209
    55
    }
jtulach@209
    56
    //END: design.composition.arith.factorial
jtulach@209
    57
jtulach@209
    58
    
jtulach@17
    59
}