001    package aima.test.utiltest;
002    
003    import aima.util.MixedRadixNumber;
004    import junit.framework.TestCase;
005    
006    /**
007     * @author Ciaran O'Reilly
008     * 
009     */
010    public class MixedRadixNumberTest extends TestCase {
011            
012            public void testInvalidRadixs() {
013                    try {
014                            new MixedRadixNumber(100, new int[] {1, 0, -1});
015                            
016                            fail("Should have thrown an Illegal Argument Exception");
017                    } catch (IllegalArgumentException iae) {
018                            // Expected
019                    }
020            }
021            
022            public void testInvalidMaxValue() {
023                    try {
024                            new MixedRadixNumber(100, new int[] {3, 3, 3});
025                            
026                            fail("Should have thrown an Illegal Argument Exception");
027                    } catch (IllegalArgumentException iae) {
028                            // Expected
029                    }
030            }
031            
032            public void testAllowedMaxValue() {
033                    assertEquals(15,  (new MixedRadixNumber(0, new int[] {2, 2, 2, 2}).getMaxAllowedValue()));              
034                    assertEquals(80,  (new MixedRadixNumber(0, new int[] {3, 3, 3, 3}).getMaxAllowedValue()));
035                    assertEquals(5,   (new MixedRadixNumber(0, new int[] {3, 2}).getMaxAllowedValue()));
036                    assertEquals(35,  (new MixedRadixNumber(0, new int[] {3, 3, 2, 2}).getMaxAllowedValue()));
037                    assertEquals(359, (new MixedRadixNumber(0, new int[] {3, 4, 5, 6}).getMaxAllowedValue()));
038                    assertEquals(359, (new MixedRadixNumber(0, new int[] {6, 5, 4, 3}).getMaxAllowedValue()));
039            }
040            
041            public void testIncrement() {
042                    MixedRadixNumber mrn = new MixedRadixNumber(0, new int[] {3, 2});
043                    int i = 0;
044                    while (mrn.increment()) {
045                            i++;
046                    }
047                    assertEquals(i, mrn.getMaxAllowedValue());
048            }
049            
050            public void testDecrement() {
051                    MixedRadixNumber mrn = new MixedRadixNumber(5, new int[] {3, 2});
052                    int i = 0;
053                    while (mrn.decrement()) {
054                            i++;
055                    }
056                    assertEquals(i, mrn.getMaxAllowedValue());
057            }
058            
059            public void testCurrentNumberalValue() {
060                    MixedRadixNumber mrn; 
061                    //
062                    mrn = new MixedRadixNumber(0, new int[] {3, 3, 2, 2});
063                    assertEquals(0, mrn.getCurrentNumeralValue(0));
064                    assertEquals(0, mrn.getCurrentNumeralValue(1));
065                    assertEquals(0, mrn.getCurrentNumeralValue(2));
066                    assertEquals(0, mrn.getCurrentNumeralValue(3));
067                    //
068                    mrn = new MixedRadixNumber(35, new int[] {3, 3, 2, 2});
069                    assertEquals(2, mrn.getCurrentNumeralValue(0));
070                    assertEquals(2, mrn.getCurrentNumeralValue(1));
071                    assertEquals(1, mrn.getCurrentNumeralValue(2));
072                    assertEquals(1, mrn.getCurrentNumeralValue(3));
073                    //
074                    mrn = new MixedRadixNumber(25, new int[] {3, 3, 2, 2});
075                    assertEquals(1, mrn.getCurrentNumeralValue(0));
076                    assertEquals(2, mrn.getCurrentNumeralValue(1));
077                    assertEquals(0, mrn.getCurrentNumeralValue(2));
078                    assertEquals(1, mrn.getCurrentNumeralValue(3));
079                    //
080                    mrn = new MixedRadixNumber(17, new int[] {3, 3, 2, 2});
081                    assertEquals(2, mrn.getCurrentNumeralValue(0));
082                    assertEquals(2, mrn.getCurrentNumeralValue(1));
083                    assertEquals(1, mrn.getCurrentNumeralValue(2));
084                    assertEquals(0, mrn.getCurrentNumeralValue(3));
085                    //
086                    mrn = new MixedRadixNumber(8, new int[] {3, 3, 2, 2});
087                    assertEquals(2, mrn.getCurrentNumeralValue(0));
088                    assertEquals(2, mrn.getCurrentNumeralValue(1));
089                    assertEquals(0, mrn.getCurrentNumeralValue(2));
090                    assertEquals(0, mrn.getCurrentNumeralValue(3));
091                    //
092                    mrn = new MixedRadixNumber(359, new int[] {3, 4, 5, 6});
093                    assertEquals(2, mrn.getCurrentNumeralValue(0));
094                    assertEquals(3, mrn.getCurrentNumeralValue(1));
095                    assertEquals(4, mrn.getCurrentNumeralValue(2));
096                    assertEquals(5, mrn.getCurrentNumeralValue(3));
097                    //
098                    mrn = new MixedRadixNumber(359, new int[] {6, 5, 4, 3});
099                    assertEquals(5, mrn.getCurrentNumeralValue(0));
100                    assertEquals(4, mrn.getCurrentNumeralValue(1));
101                    assertEquals(3, mrn.getCurrentNumeralValue(2));
102                    assertEquals(2, mrn.getCurrentNumeralValue(3));
103            }       
104    }