BasicFitness implements Fitness { public int compareTo(Fitness o) { double dO = o.getDouble(); double dT = this.value; if(Double.isNaN(dO) || Double.isInfinite(dO)) { dO = BasicFitness.Max_Double; } if(Double.isNaN(dT) || Double.isInfinite(dT)) { dT = BasicFitness.Max_Double; } if(dT < dO) { return -1; } if(dT > dO) { return 1; } else { return 0; } } BasicFitness();  BasicFitness(double f, Individual i);  BasicFitness(Individual i); int compareTo(Fitness o); double getMaxDoubleFitness(); void setMaxDoubleFitness(double d); double getMinDoubleFitness(); void setMinDoubleFitness(double d); int getMaxIntFitness(); void setMaxIntFitness(int d); int getMinIntFitness(); void setMinIntFitness(int d); void setDefault(); double getDefaultFitness(); double getDouble(); double getDistance(DoubleFitness f); Individual getIndividual(); void setIndividual(Individual i); void setDouble(double f); int getInt(); void setInt(int f); Object getData(); void setData(Object data); static final double DEFAULT_FITNESS; }
@Test public void testCompareTo() { System.out.println("*BasicFitnessTest: compareTo"); Fitness o = null; BasicFitness instance = new BasicFitness(); try { instance.compareTo(o); } catch(NullPointerException e) { assertTrue(true); } o = new BasicFitness(); o.setDouble(Double.NaN); instance.setDouble(Double.NaN); assertEquals(0, instance.compareTo(o)); o.setDouble(Double.NEGATIVE_INFINITY); instance.setDouble(Double.POSITIVE_INFINITY); assertEquals(0, instance.compareTo(o)); o.setDouble(0.0); instance.setDouble(-0.1); assertEquals(-1, instance.compareTo(o)); o.setDouble(0.0); instance.setDouble(0.1); assertEquals(1, instance.compareTo(o)); ArrayList<BasicFitness> alb = new ArrayList<BasicFitness>(4); double[] da = {3.0,2.0,1.0,4.0}; double[] expected = {1.0,2.0,3.0,4.0}; for(double d: da) { alb.add(new BasicFitness(d, null)); } Collections.sort(alb); for (int i = 0; i < expected.length; i++) { assertEquals(expected[i], alb.get(i).getDouble(), 0.0000001); } double[] expected2 = {4.0,3.0,2.0,1.0}; Collections.reverse(alb); for (int i = 0; i < expected2.length; i++) { assertEquals(expected2[i], alb.get(i).getDouble(), 0.0000001); } }