-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests for UnboundedArchive, BoundedArchive
- Loading branch information
Felix Reimann
committed
Sep 25, 2018
1 parent
d824484
commit 43987d4
Showing
2 changed files
with
266 additions
and
0 deletions.
There are no files selected for viewing
203 changes: 203 additions & 0 deletions
203
opt4j-core/src/test/java/org/opt4j/core/common/archive/BoundedArchiveTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
package org.opt4j.core.common.archive; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.opt4j.core.Individual; | ||
import org.opt4j.core.IndividualFactory; | ||
import org.opt4j.core.Objective; | ||
import org.opt4j.core.Objectives; | ||
import org.opt4j.core.common.archive.CrowdingArchiveTest.MockProblemModule; | ||
|
||
import com.google.inject.Guice; | ||
import com.google.inject.Injector; | ||
|
||
public class BoundedArchiveTest { | ||
@Test(expected = IllegalArgumentException.class) | ||
public void boundedArchiveIllegalCapacity() { | ||
BoundedArchive archive = new BoundedArchive(-1) { | ||
|
||
@Override | ||
protected boolean updateWithNondominated(Collection<Individual> candidates) { | ||
Assert.fail(); | ||
return false; | ||
} | ||
}; | ||
Assert.fail("should not be creatable: " + archive); | ||
} | ||
|
||
@Test | ||
public void setCapacity() { | ||
BoundedArchive archive = new BoundedArchive(10) { | ||
@Override | ||
protected boolean updateWithNondominated(Collection<Individual> candidates) { | ||
Assert.fail(); | ||
return false; | ||
} | ||
}; | ||
Assert.assertEquals(10, archive.getCapacity()); | ||
archive.setCapacity(20); | ||
Assert.assertEquals(20, archive.getCapacity()); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void setCapacityIllegalCapacity() { | ||
BoundedArchive archive = new BoundedArchive(10) { | ||
@Override | ||
protected boolean updateWithNondominated(Collection<Individual> candidates) { | ||
Assert.fail(); | ||
return false; | ||
} | ||
}; | ||
archive.setCapacity(-1); | ||
} | ||
|
||
@Test | ||
public void addCheckedIndividual() { | ||
Injector injector = Guice.createInjector(new MockProblemModule()); | ||
IndividualFactory factory = injector.getInstance(IndividualFactory.class); | ||
|
||
Objective o0 = new Objective("o0"); | ||
Objective o1 = new Objective("o1"); | ||
|
||
Individual individual1 = factory.create(); | ||
Objectives objectivesA0 = new Objectives(); | ||
objectivesA0.add(o0, 1); | ||
objectivesA0.add(o1, 0); | ||
individual1.setObjectives(objectivesA0); | ||
|
||
BoundedArchive archive = new BoundedArchive(1) { | ||
@Override | ||
protected boolean updateWithNondominated(Collection<Individual> candidates) { | ||
individuals.addAll(candidates); | ||
return true; | ||
} | ||
}; | ||
archive.addCheckedIndividual(individual1); | ||
Assert.assertTrue(archive.contains(individual1)); | ||
Assert.assertEquals(1, archive.size()); | ||
} | ||
|
||
@Test | ||
public void addCheckedIndividualTwice() { | ||
Injector injector = Guice.createInjector(new MockProblemModule()); | ||
IndividualFactory factory = injector.getInstance(IndividualFactory.class); | ||
|
||
Objective o0 = new Objective("o0"); | ||
Objective o1 = new Objective("o1"); | ||
|
||
Individual individual1 = factory.create(); | ||
Objectives objectivesA0 = new Objectives(); | ||
objectivesA0.add(o0, 1); | ||
objectivesA0.add(o1, 0); | ||
individual1.setObjectives(objectivesA0); | ||
|
||
BoundedArchive archive = new BoundedArchive(1) { | ||
@Override | ||
protected boolean updateWithNondominated(Collection<Individual> candidates) { | ||
individuals.addAll(candidates); | ||
return true; | ||
} | ||
}; | ||
archive.addCheckedIndividual(individual1); | ||
archive.addCheckedIndividual(individual1); | ||
Assert.assertTrue(archive.contains(individual1)); | ||
Assert.assertEquals(1, archive.size()); | ||
} | ||
|
||
@Test(expected = IndexOutOfBoundsException.class) | ||
public void addCheckedIndividualDespiteArchiveFull() { | ||
Injector injector = Guice.createInjector(new MockProblemModule()); | ||
IndividualFactory factory = injector.getInstance(IndividualFactory.class); | ||
|
||
Objective o0 = new Objective("o0"); | ||
Objective o1 = new Objective("o1"); | ||
|
||
Individual individual1 = factory.create(); | ||
Objectives objectivesA0 = new Objectives(); | ||
objectivesA0.add(o0, 1); | ||
objectivesA0.add(o1, 0); | ||
individual1.setObjectives(objectivesA0); | ||
|
||
Individual individual2 = factory.create(); | ||
Objectives objectivesA1 = new Objectives(); | ||
objectivesA1.add(o0, 0); | ||
objectivesA1.add(o1, 1); | ||
individual2.setObjectives(objectivesA1); | ||
|
||
BoundedArchive archive = new BoundedArchive(1) { | ||
@Override | ||
protected boolean updateWithNondominated(Collection<Individual> candidates) { | ||
individuals.addAll(candidates); | ||
return true; | ||
} | ||
}; | ||
archive.addCheckedIndividual(individual1); | ||
Assert.assertTrue(archive.contains(individual1)); | ||
Assert.assertEquals(1, archive.size()); | ||
|
||
archive.addCheckedIndividual(individual2); | ||
} | ||
|
||
@Test | ||
public void addCheckedIndividuals() { | ||
Injector injector = Guice.createInjector(new MockProblemModule()); | ||
IndividualFactory factory = injector.getInstance(IndividualFactory.class); | ||
|
||
Objective o0 = new Objective("o0"); | ||
Objective o1 = new Objective("o1"); | ||
|
||
Individual individual1 = factory.create(); | ||
Objectives objectivesA0 = new Objectives(); | ||
objectivesA0.add(o0, 1); | ||
objectivesA0.add(o1, 0); | ||
individual1.setObjectives(objectivesA0); | ||
|
||
BoundedArchive archive = new BoundedArchive(1) { | ||
@Override | ||
protected boolean updateWithNondominated(Collection<Individual> candidates) { | ||
individuals.addAll(candidates); | ||
return true; | ||
} | ||
}; | ||
archive.addCheckedIndividuals(Collections.singleton(individual1)); | ||
Assert.assertTrue(archive.contains(individual1)); | ||
Assert.assertEquals(1, archive.size()); | ||
} | ||
|
||
@Test(expected = IndexOutOfBoundsException.class) | ||
public void addCheckedIndividualsDespiteArchiveFull() { | ||
Injector injector = Guice.createInjector(new MockProblemModule()); | ||
IndividualFactory factory = injector.getInstance(IndividualFactory.class); | ||
|
||
Objective o0 = new Objective("o0"); | ||
Objective o1 = new Objective("o1"); | ||
|
||
Individual individual1 = factory.create(); | ||
Objectives objectivesA0 = new Objectives(); | ||
objectivesA0.add(o0, 1); | ||
objectivesA0.add(o1, 0); | ||
individual1.setObjectives(objectivesA0); | ||
|
||
Individual individual2 = factory.create(); | ||
Objectives objectivesA1 = new Objectives(); | ||
objectivesA1.add(o0, 0); | ||
objectivesA1.add(o1, 1); | ||
individual2.setObjectives(objectivesA1); | ||
|
||
BoundedArchive archive = new BoundedArchive(1) { | ||
@Override | ||
protected boolean updateWithNondominated(Collection<Individual> candidates) { | ||
individuals.addAll(candidates); | ||
return true; | ||
} | ||
}; | ||
archive.addCheckedIndividuals(Collections.singleton(individual1)); | ||
Assert.assertTrue(archive.contains(individual1)); | ||
Assert.assertEquals(1, archive.size()); | ||
|
||
archive.addCheckedIndividuals(Collections.singleton(individual2)); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
opt4j-core/src/test/java/org/opt4j/core/common/archive/UnboundedArchiveTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.opt4j.core.common.archive; | ||
|
||
import java.util.Collections; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.opt4j.core.Individual; | ||
import org.opt4j.core.IndividualFactory; | ||
import org.opt4j.core.Objective; | ||
import org.opt4j.core.Objectives; | ||
import org.opt4j.core.common.archive.CrowdingArchiveTest.MockProblemModule; | ||
|
||
import com.google.inject.Guice; | ||
import com.google.inject.Injector; | ||
|
||
public class UnboundedArchiveTest { | ||
|
||
@Test | ||
public void updateWithNondominated() { | ||
Injector injector = Guice.createInjector(new MockProblemModule()); | ||
IndividualFactory factory = injector.getInstance(IndividualFactory.class); | ||
|
||
Objective o0 = new Objective("o0"); | ||
Objective o1 = new Objective("o1"); | ||
|
||
Individual individual = factory.create(); | ||
Objectives objectivesA0 = new Objectives(); | ||
objectivesA0.add(o0, 0); | ||
objectivesA0.add(o1, 0); | ||
individual.setObjectives(objectivesA0); | ||
|
||
UnboundedArchive archive = new UnboundedArchive(); | ||
Assert.assertFalse(archive.iterator().hasNext()); | ||
Assert.assertTrue(archive.updateWithNondominated(Collections.singleton(individual))); | ||
Assert.assertTrue(archive.iterator().hasNext()); | ||
Assert.assertEquals(archive.iterator().next(), individual); | ||
} | ||
|
||
@Test | ||
public void updateWithExisting() { | ||
Injector injector = Guice.createInjector(new MockProblemModule()); | ||
IndividualFactory factory = injector.getInstance(IndividualFactory.class); | ||
|
||
Objective o0 = new Objective("o0"); | ||
Objective o1 = new Objective("o1"); | ||
|
||
Individual individual = factory.create(); | ||
Objectives objectivesA0 = new Objectives(); | ||
objectivesA0.add(o0, 0); | ||
objectivesA0.add(o1, 0); | ||
individual.setObjectives(objectivesA0); | ||
|
||
UnboundedArchive archive = new UnboundedArchive(); | ||
archive.updateWithNondominated(Collections.singleton(individual)); | ||
Assert.assertFalse(archive.updateWithNondominated(Collections.singleton(individual))); | ||
} | ||
|
||
@Test | ||
public void updateWithNone() { | ||
UnboundedArchive archive = new UnboundedArchive(); | ||
Assert.assertFalse(archive.updateWithNondominated(Collections.emptySet())); | ||
} | ||
} |