KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > gp > impl > GPConfigurationTest


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.gp.impl;
11
12 import java.util.*;
13 import junit.framework.*;
14 import org.jgap.*;
15 import org.jgap.gp.*;
16
17 /**
18  * Tests the GPConfiguration class.
19  *
20  * @author Klaus Meffert
21  * @since 3.0
22  */

23 public class GPConfigurationTest
24     extends GPTestCase {
25   /** String containing the CVS revision. Read out via reflection!*/
26   private final static String JavaDoc CVS_REVISION = "$Revision: 1.5 $";
27
28   public static Test suite() {
29     TestSuite suite = new TestSuite(GPConfigurationTest.class);
30     return suite;
31   }
32
33   public void setUp() {
34     super.setUp();
35   }
36
37   /**
38    * @throws Exception
39    *
40    * @author Klaus Meffert
41    * @since 3.0
42    */

43   public void testSerialize_0()
44       throws Exception JavaDoc {
45     GPConfiguration.reset();
46     GPConfiguration conf = new GPConfiguration();
47     GPConfiguration conf2 = (GPConfiguration) doSerialize(conf);
48     assertEquals(conf, conf2);
49   }
50
51   /**
52    * @throws Exception
53    *
54    * @author Klaus Meffert
55    * @since 3.0
56    */

57   public void testMemory_0()
58       throws Exception JavaDoc {
59     GPConfiguration.reset();
60     GPConfiguration conf = new GPConfiguration();
61     conf.storeInMemory("name1", "test1");
62     assertEquals("test1", conf.readFromMemory("name1"));
63   }
64
65   /**
66    * @throws Exception
67    *
68    * @author Klaus Meffert
69    * @since 3.0
70    */

71   public void testMemory_1()
72       throws Exception JavaDoc {
73     GPConfiguration.reset();
74     GPConfiguration conf = new GPConfiguration();
75     try {
76       conf.readFromMemory("name1");
77       fail();
78     } catch (IllegalArgumentException JavaDoc iex) {
79       ; //this is OK
80
}
81   }
82
83   /**
84    * @throws Exception
85    *
86    * @author Klaus Meffert
87    * @since 3.0
88    */

89   public void testMemory_2()
90       throws Exception JavaDoc {
91     GPConfiguration.reset();
92     GPConfiguration conf = new GPConfiguration();
93     conf.storeInMemory("name1", "test1");
94     conf.storeInMemory("name2", "test2");
95     conf.storeInMemory("name3", "test3");
96     assertEquals("test2", conf.readFromMemory("name2"));
97     // Read repeatedly.
98
// ----------------
99
assertEquals("test2", conf.readFromMemory("name2"));
100     // Ensure vaues are not exchanged.
101
// -------------------------------
102
assertEquals("test1", conf.readFromMemory("name1"));
103     assertEquals("test3", conf.readFromMemory("name3"));
104   }
105
106   /**
107    * @throws Exception
108    *
109    * @author Klaus Meffert
110    * @since 3.0
111    */

112   public void testMemory_3()
113       throws Exception JavaDoc {
114     GPConfiguration.reset();
115     GPConfiguration conf = new GPConfiguration();
116     conf.storeInMemory("name1", "test1");
117     conf.clearMemory();
118     try {
119       conf.readFromMemory("name1");
120       fail();
121     } catch (IllegalArgumentException JavaDoc iex) {
122       ; //this is OK
123
}
124   }
125
126   /**
127    * @throws Exception
128    *
129    * @author Klaus Meffert
130    * @since 3.0
131    */

132   public void testStack_0()
133       throws Exception JavaDoc {
134     GPConfiguration.reset();
135     GPConfiguration conf = new GPConfiguration();
136     assertEquals(0, conf.stackSize());
137     try {
138       assertNull(conf.peekStack());
139       fail();
140     } catch (EmptyStackException eex) {
141       ; //this is OK
142
}
143   }
144
145   /**
146    * @throws Exception
147    *
148    * @author Klaus Meffert
149    * @since 3.0
150    */

151   public void testStack_1()
152       throws Exception JavaDoc {
153     GPConfiguration.reset();
154     GPConfiguration conf = new GPConfiguration();
155     conf.pushToStack("test1");
156     assertEquals(1, conf.stackSize());
157     conf.pushToStack("test2");
158     assertEquals(2, conf.stackSize());
159     assertEquals("test2", conf.popFromStack());
160     assertEquals(1, conf.stackSize());
161     assertEquals("test1", conf.popFromStack());
162     assertEquals(0, conf.stackSize());
163   }
164
165   /**
166    * @throws Exception
167    *
168    * @author Klaus Meffert
169    * @since 3.0
170    */

171   public void testStack_2()
172       throws Exception JavaDoc {
173     GPConfiguration.reset();
174     GPConfiguration conf = new GPConfiguration();
175     Vector obj = new Vector();
176     conf.pushToStack(obj);
177     assertSame(obj, conf.peekStack());
178     assertSame(obj, conf.popFromStack());
179   }
180
181   /**
182    * @throws Exception
183    *
184    * @author Klaus Meffert
185    * @since 3.0
186    */

187   public void testStack_3()
188       throws Exception JavaDoc {
189     GPConfiguration.reset();
190     GPConfiguration conf = new GPConfiguration();
191     try {
192       conf.popFromStack();
193       fail();
194     } catch (EmptyStackException eex) {
195       ; //this is OK
196
}
197   }
198
199   /**
200    * @throws Exception
201    *
202    * @author Klaus Meffert
203    * @since 3.0
204    */

205   public void testStack_4()
206       throws Exception JavaDoc {
207     GPConfiguration.reset();
208     GPConfiguration conf = new GPConfiguration();
209     conf.pushToStack("test1");
210     conf.clearStack();
211     assertEquals(0, conf.stackSize());
212   }
213
214   /**
215    * @throws Exception
216    *
217    * @author Klaus Meffert
218    * @since 3.1
219    */

220   public void testSetSelectionMethod_0()
221       throws Exception JavaDoc {
222     GPConfiguration.reset();
223     GPConfiguration conf = new GPConfiguration();
224     try {
225       conf.setSelectionMethod(null);
226       fail();
227     } catch (IllegalArgumentException JavaDoc iex) {
228       ; //this is OK
229
}
230   }
231
232   /**
233    * @throws Exception
234    *
235    * @author Klaus Meffert
236    * @since 3.1
237    */

238   public void testSetSelectionMethod_1()
239       throws Exception JavaDoc {
240     GPConfiguration.reset();
241     GPConfiguration conf = new GPConfiguration();
242     INaturalGPSelector sel = new FitnessProportionateSelection();
243     conf.setSelectionMethod(sel);
244     assertSame(sel, conf.getSelectionMethod());
245   }
246
247   /**
248    * @throws Exception
249    *
250    * @author Klaus Meffert
251    * @since 3.1
252    */

253   public void testSetCrossoverMethod_0()
254       throws Exception JavaDoc {
255     GPConfiguration.reset();
256     GPConfiguration conf = new GPConfiguration();
257     try {
258       conf.setCrossoverMethod(null);
259       fail();
260     } catch (IllegalArgumentException JavaDoc iex) {
261       ; //this is OK
262
}
263   }
264
265   /**
266    * @throws Exception
267    *
268    * @author Klaus Meffert
269    * @since 3.1
270    */

271   public void testSetCrossoverMethod_1()
272       throws Exception JavaDoc {
273     GPConfiguration.reset();
274     GPConfiguration conf = new GPConfiguration();
275     CrossMethod cross = new BranchTypingCross(conf);
276     conf.setCrossoverMethod(cross);
277     assertSame(cross, conf.getCrossMethod());
278   }
279
280   /**
281    * @throws Exception
282    *
283    * @author Klaus Meffert
284    * @since 3.1
285    */

286   public void testConstructor_0()
287       throws Exception JavaDoc {
288     GPConfiguration.reset();
289     GPConfiguration conf = new GPConfiguration();
290     assertEquals(BranchTypingCross.class, conf.getCrossMethod().getClass());
291     assertEquals(FitnessProportionateSelection.class,
292                  conf.getSelectionMethod().getClass());
293     assertEquals(DefaultGPFitnessEvaluator.class,
294                  conf.getGPFitnessEvaluator().getClass());
295   }
296
297   /**
298    * @throws Exception
299    *
300    * @author Klaus Meffert
301    * @since 3.1
302    */

303   public void testConstructor_1()
304       throws Exception JavaDoc {
305     GPConfiguration.reset();
306     FitnessProportionateSelection sel = new FitnessProportionateSelection();
307     GPConfiguration conf = new GPConfiguration(sel);
308     assertSame(sel, conf.getSelectionMethod());
309   }
310 }
311
Popular Tags