Regarding the general idea of a test suite (sorry if this is distracting from the immediate float->double topic):
I recently wrote JUnit tests for a slightly modified SimpleEvaluator. I struggled at first with setting up the fixture (ex. should I use param files?), but ended up settling on a combination of A) manual initialization of just the components of EvolutionState that were required for the test, and B) using a bare-bones ParameterDatabase as a
when the class under test needed to create other objects (ex. Evaluator.setup() initializing a Problem).
The resulting fixture (reproduced below) was a lot easier to set up than I'd feared. It is self-contained (using no external ".params" files), and it wasn't hard to get >90% branch coverage for an Evaluator (that's another advantage of JUnit -- coverage metric tools like EclEmma work out of the box). If the class under test references a piece of global state we forgot it needed, we get an NPE (rather than a cheerful, faulty test).
I'd happily contribute tests for a couple classes -- if they were made part of the repository, we could let the suite grow over time.
private final static String PROBLEM_DOUBLE_NAME = "ecjapp.doubles.TestSimpleGroupedProblem";
private final static String BAD_PROBLEM_DOUBLE_NAME = "ecjapp.doubles.TestSimpleProblem";
private EvolutionState state;
private SimpleGroupedEvaluator sut;
@Before
this.state = getFreshState();
this.sut = new SimpleGroupedEvaluator(); // Each test needs to call setup (perhaps after tweaking the parameters)
private static EvolutionState getFreshState() {
final EvolutionState state = new SimpleEvolutionState();
// Set up just the parameters needed for the SUT to initialize itself
state.parameters = getParams();
// We need errors to throw exceptions (rather than exit the program) so we can verify them.
state.output = Evolve.buildOutput();
state.output.setThrowsErrors(true);
state.population = new Population();
state.population.subpops = new Subpopulation[] { new Subpopulation() };
state.population.subpops[0].individuals = getIndividuals();
private static ParameterDatabase getParams() {
final ParameterDatabase parameters = new ParameterDatabase();
// Parameters needed by Evaluator.setup()
parameters.set(new Parameter("eval." + Evaluator.P_PROBLEM), PROBLEM_DOUBLE_NAME);
// Parameters needed by SimpleGroupedEvaluator.setup()
parameters.set(new Parameter("eval." + SimpleGroupedEvaluator.P_CLONE_PROBLEM), "false");
parameters.set(new Parameter("eval." + SimpleGroupedEvaluator.P_NUM_TESTS), "1");
private static Individual[] getIndividuals() {
final Individual[] individuals = new Individual[4];
individuals[0] = new TestIndividual(0);
individuals[1] = new TestIndividual(1);
individuals[2] = new TestIndividual(2);
individuals[3] = new TestIndividual(3);