001    package aima.test.search.nqueens;
002    
003    import java.util.Set;
004    
005    import junit.framework.TestCase;
006    
007    import aima.search.nqueens.NQueensBoard;
008    import aima.search.nqueens.NQueensFitnessFunction;
009    
010    /**
011     * @author Ciaran O'Reilly
012     * 
013     */
014    public class NQueensFitnessFunctionTest extends TestCase {
015    
016            NQueensFitnessFunction fitnessFunction;
017    
018            @Override
019            public void setUp() {
020                    fitnessFunction = new NQueensFitnessFunction();
021            }
022    
023            public void test_getValue() {
024                    assertTrue(0.0 == fitnessFunction.getValue("00000000"));
025                    assertTrue(0.0 == fitnessFunction.getValue("01234567"));
026                    assertTrue(0.0 == fitnessFunction.getValue("76543210"));
027    
028                    assertTrue(23.0 == fitnessFunction.getValue("56136477"));
029                    assertTrue(28.0 == fitnessFunction.getValue("04752613"));
030            }
031    
032            public void test_isGoalState() {
033                    assertTrue(fitnessFunction.isGoalState("04752613"));
034                    assertFalse(fitnessFunction.isGoalState("00000000"));
035                    assertFalse(fitnessFunction.isGoalState("56136477"));
036            }
037    
038            public void test_getBoardForIndividual() {
039                    NQueensBoard board = fitnessFunction.getBoardForIndividual("56136477");
040                    assertEquals(" -  -  -  -  -  -  -  - \n"
041                                    + " -  -  Q  -  -  -  -  - \n" + " -  -  -  -  -  -  -  - \n"
042                                    + " -  -  -  Q  -  -  -  - \n" + " -  -  -  -  -  Q  -  - \n"
043                                    + " Q  -  -  -  -  -  -  - \n" + " -  Q  -  -  Q  -  -  - \n"
044                                    + " -  -  -  -  -  -  Q  Q \n", board.getBoardPic());
045    
046                    assertEquals("--------\n" + "--Q-----\n" + "--------\n" + "---Q----\n"
047                                    + "-----Q--\n" + "Q-------\n" + "-Q--Q---\n" + "------QQ\n",
048                                    board.toString());
049            }
050    
051            public void test_generateRandomIndividual() {
052                    for (int i = Character.MIN_RADIX; i <= Character.MAX_RADIX; i++) {
053                            String individual = fitnessFunction.generateRandomIndividual(i);
054                            assertEquals(i, individual.length());
055                    }
056            }
057    
058            public void test_getFiniteAlphabet() {
059                    for (int i = Character.MIN_RADIX; i <= Character.MAX_RADIX; i++) {
060                            Set<Character> fab = fitnessFunction
061                                            .getFiniteAlphabetForBoardOfSize(i);
062                            assertEquals(i, fab.size());
063                    }
064            }
065    }