📄 fixedbinarygenetest.java
字号:
public void testApplyMutation_0() {
FixedBinaryGene gene = new FixedBinaryGene(4);
gene.setAllele(new int[] {0, 0, 1, 1});
gene.applyMutation(0, 0.0d);
assertEquals("FixedBinaryGene[0,0,1,1]", gene.toString());
}
/**
* @author vamsi
*/
public void testApplyMutation_1() {
FixedBinaryGene gene = new FixedBinaryGene(4);
gene.setAllele(new int[] {0, 0, 1, 0});
gene.applyMutation(1, 0.000001d);
assertEquals("FixedBinaryGene[0,1,1,0]", gene.toString());
}
/**
* @author vamsi
*/
public void testApplyMutation_2() {
FixedBinaryGene gene = new FixedBinaryGene(5);
gene.setAllele(new int[] {1, 0, 1, 0, 1});
try {
//index size is greater
gene.applyMutation(333, -0.000001d);
fail();
}
catch (Exception E) {
; //this is OK
}
}
/**
* @author vamsi
*/
public void testApplyMutation_3() {
FixedBinaryGene gene = new FixedBinaryGene(4);
gene.setAllele(new int[] {1, 1, 0, 1});
gene.applyMutation(0, -1.0d);
assertEquals("FixedBinaryGene[0,1,0,1]", gene.toString());
}
/**
* @author vamsi
*/
public void testApplyMutation_4() {
FixedBinaryGene gene = new FixedBinaryGene(4);
gene.setAllele(new int[] {0, 1, 0, 1});
gene.applyMutation(0, -2.0d);
gene.applyMutation(3, 2.0d);
gene.applyMutation(1, -4.0d);
assertEquals("FixedBinaryGene[0,0,0,1]", gene.toString());
}
/**
* @author vamsi
*/
public void testApplyMutation_5() {
FixedBinaryGene gene = new FixedBinaryGene(4);
gene.setAllele(new int[] {1, 1, 1, 1});
gene.applyMutation(0, 2.0d);
gene.applyMutation(1, 2.0d);
gene.applyMutation(2, 2.0d);
gene.applyMutation(3, 2.0d);
assertEquals("FixedBinaryGene[1,1,1,1]", gene.toString());
}
/**
* @author vamsi
* @since 2.0
*/
public void testSetValueFromPersistentRepresentation_0() {
FixedBinaryGene gene = new FixedBinaryGene(4);
try {
gene.setValueFromPersistentRepresentation(null);
fail();
}
catch (UnsupportedRepresentationException uex) {
; //this is OK
}
}
/**
* @since 2.0
* @throws Exception
*/
public void testSetValueFromPersistentRepresentation_1()
throws Exception {
FixedBinaryGene gene = new FixedBinaryGene(4);
try {
gene.setValueFromPersistentRepresentation("null");
fail();
}
catch (UnsupportedRepresentationException uex) {
; //this is OK
}
}
/**
* @author vamsi
* @since 2.0
* @throws Exception
*/
public void testSetValueFromPersistentRepresentation_2()
throws Exception {
FixedBinaryGene gene = new FixedBinaryGene(4);
gene.setValueFromPersistentRepresentation("[1,1,1,1]");
assertTrue(gene.getBit(0));
assertTrue(gene.getBit(1));
assertTrue(gene.getBit(2));
assertTrue(gene.getBit(3));
}
/**
* @author vamsi
* @since 2.0
* @throws Exception
*/
public void testSetValueFromPersistentRepresentation_3()
throws Exception {
FixedBinaryGene gene = new FixedBinaryGene(4);
gene.setValueFromPersistentRepresentation("[0,0,0,0]");
assertFalse(gene.getBit(0));
assertFalse(gene.getBit(1));
assertFalse(gene.getBit(2));
assertFalse(gene.getBit(3));
}
/**
* @author vamsi
* @since 2.0
* @throws Exception
*/
public void testSetValueFromPersistentRepresentation_4()
throws Exception {
FixedBinaryGene gene = new FixedBinaryGene(5);
gene.setValueFromPersistentRepresentation("[0,1,1,0,0]");
assertFalse(gene.getBit(0));
assertTrue(gene.getBit(1));
assertTrue(gene.getBit(2));
assertFalse(gene.getBit(3));
assertFalse(gene.getBit(4));
}
/**
* @since 2.0
* @author Klaus Meffert
* @throws Exception
*/
public void testSetValueFromPersistentRepresentation_5()
throws Exception {
FixedBinaryGene gene = new FixedBinaryGene(5);
try {
gene.setValueFromPersistentRepresentation("[0,1,1,0]");
fail();
}
catch (UnsupportedRepresentationException uex) {
; //this is OK
}
}
/**
* @author vamsi
* @since 2.0
*/
public void testSetValueFromPersistentRepresentation_6() {
FixedBinaryGene gene = new FixedBinaryGene(1);
try {
gene.setValueFromPersistentRepresentation("X");
fail();
}
catch (UnsupportedRepresentationException uex) {
; //this is OK
}
}
/**
* @author vamsi
*/
public void testGetPersistentRepresentation_0() {
FixedBinaryGene gene = new FixedBinaryGene(3);
gene.setAllele(new int[] {1, 0, 1});
String s = gene.getPersistentRepresentation();
assertEquals("FixedBinaryGene[1,0,1]", s);
}
/**
* @throws Exception
* @author vamsi
/**
* @throws Exception
* @author vamsi
* @since 2.0
*/
public void testGetPersistentRepresentation_1()
throws Exception {
FixedBinaryGene gene = new FixedBinaryGene(3);
try {
gene.setValueFromPersistentRepresentation(null);
fail();
}
catch (UnsupportedRepresentationException uex) {
; //this is OK
}
}
/**
* @throws Exception
* @author Klaus Meffert
* @since 2.0
*/
public void testGetPersistentRepresentation_2()
throws Exception {
FixedBinaryGene gene = new FixedBinaryGene(3);
String s = gene.getPersistentRepresentation();
assertEquals("FixedBinaryGene[0,0,0]", s);
}
/**
* @author Klaus Meffert
* @since 2.2
*/
public void testClone_9() {
FixedBinaryGene gene1 = new FixedBinaryGene(1);
FixedBinaryGene gene2 = (FixedBinaryGene) gene1.clone();
assertEquals(gene1, gene2);
}
/**
* @author Klaus Meffert
* @since 2.2
*/
public void testSetBit_0() {
FixedBinaryGene gene1 = new FixedBinaryGene(7);
gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
assertTrue(gene1.getBit(0));
gene1.setBit(0, false);
assertFalse(gene1.getBit(0));
gene1.setBit(1, true);
assertTrue(gene1.getBit(1));
gene1.setBit(4, false);
assertFalse(gene1.getBit(0));
}
/**
* @author Klaus Meffert
* @since 2.2
*/
public void testSetBit_1() {
FixedBinaryGene gene1 = new FixedBinaryGene(7);
gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
gene1.setBit(2, 4, true);
assertTrue(gene1.getBit(0));
assertTrue(gene1.getBit(0));
assertTrue(gene1.getBit(0));
assertTrue(gene1.getBit(0));
}
/**
* @author Klaus Meffert
* @since 2.2
*/
public void testSetBit_2() {
FixedBinaryGene gene1 = new FixedBinaryGene(7);
gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
try {
gene1.setBit(2, 2);
fail();
}
catch (IllegalArgumentException iex) {
; //this is OK
}
}
/**
* @author Klaus Meffert
* @since 2.2
*/
public void testSetBit_3() {
FixedBinaryGene gene1 = new FixedBinaryGene(7);
gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
try {
gene1.setBit(2, -1);
fail();
}
catch (IllegalArgumentException iex) {
; //this is OK
}
}
/**
* @author Klaus Meffert
* @since 2.2
*/
public void testSetBit_4() {
FixedBinaryGene gene1 = new FixedBinaryGene(7);
gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
try {
gene1.setBit(2, 1, false);
fail();
}
catch (IllegalArgumentException iex) {
; //this is OK
}
}
/**
* @author Klaus Meffert
* @since 2.2
*/
public void testSubbString_0() {
FixedBinaryGene gene1 = new FixedBinaryGene(7);
gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
FixedBinaryGene gene2 = gene1.substring(0, 4);
assertEquals(5, gene2.getLength());
assertEquals(1, gene2.size());
assertTrue(gene2.getBit(0));
assertTrue(gene2.getBit(1));
assertFalse(gene2.getBit(2));
assertFalse(gene2.getBit(3));
assertTrue(gene2.getBit(4));
}
/**
* @author Klaus Meffert
* @since 2.2
*/
public void testSubString_1() {
FixedBinaryGene gene1 = new FixedBinaryGene(7);
gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
try {
gene1.substring(0, 7);
fail();
}
catch (IndexOutOfBoundsException iex) {
; //this is OK
}
}
/**
* @author Klaus Meffert
* @since 2.2
*/
public void testFlip_0() {
FixedBinaryGene gene1 = new FixedBinaryGene(7);
gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
gene1.flip(0);
assertFalse(gene1.getBit(0));
gene1.flip(6);
assertFalse(gene1.getBit(6));
gene1.flip(2);
assertTrue(gene1.getBit(2));
}
/**
* @author Klaus Meffert
* @since 2.2
*/
public void testFlip_1() {
FixedBinaryGene gene1 = new FixedBinaryGene(7);
gene1.setAllele(new int[] {1, 1, 0, 0, 1, 0, 1});
try {
gene1.flip(7);
fail();
}
catch (IndexOutOfBoundsException iex) {
; //this is OK
}
}
/**
* @author Klaus Meffert
* @since 2.2
*/
public void testSetToRandomValue_0() {
FixedBinaryGene gene1 = new FixedBinaryGene(7);
try {
gene1.setToRandomValue(null);
fail();
}
catch (IllegalArgumentException iex) {
; //this is OK
}
}
/**
* @author Klaus Meffert
* @since 2.2
*/
public void testSetToRandomValue_1() {
FixedBinaryGene gene1 = new FixedBinaryGene(7);
gene1.setToRandomValue(new StockRandomGenerator());
}
/**
* @author Klaus Meffert
* @since 2.2
*/
public void testSetToRandomValue_2() {
FixedBinaryGene gene1 = new FixedBinaryGene(7);
gene1.setToRandomValue(new RandomGeneratorForTest(false));
for (int i = 0; i < 7; i++) {
assertFalse(gene1.getBit(i));
}
}
/**
* @author Klaus Meffert
* @since 2.2
*/
public void testSetToRandomValue_3() {
FixedBinaryGene gene1 = new FixedBinaryGene(7);
gene1.setToRandomValue(new RandomGeneratorForTest(true));
for (int i = 0; i < 7; i++) {
assertTrue(gene1.getBit(i));
}
}
/**
* @author Klaus Meffert
*/
public void testNewGene_0() {
FixedBinaryGene gene1 = new FixedBinaryGene(7);
IGeneConstraintChecker checker = new GeneConstraintChecker();
gene1.setConstraintChecker(checker);
FixedBinaryGene gene2 = (FixedBinaryGene) gene1.newGene();
assertTrue(gene1.equals(gene2));
assertTrue(gene2.equals(gene1));
assertEquals(checker, gene2.getConstraintChecker());
}
/**
* @author Klaus Meffert
* @since 2.2
*/
public void testHashCode_0() {
FixedBinaryGene gene = new FixedBinaryGene(6);
gene.hashCode();
gene.setBit(0, 5, false);
gene.hashCode();
gene.setBit(0, 5, true);
gene.hashCode();
/**@todo implement checks for uniqueness*/
}
/**
* @author Klaus Meffert
* @since 2.5
*/
public void testHashCode_1() {
Gene gene1 = new FixedBinaryGene(2);
Gene gene2 = new FixedBinaryGene(2);
int[] v1 = (int[])gene1.getAllele();
int[] v2 = (int[])gene1.getAllele();
int h1 = v1.hashCode();
int h2 = v2.hashCode();
assertEquals(gene1.hashCode(), gene2.hashCode());
}
/**
* @author Klaus Meffert
* @since 2.5
*/
public void testHashCode_2() {
Gene gene1 = new FixedBinaryGene(3);
gene1.setAllele(new int[]{0,1,0});
Gene gene2 = new FixedBinaryGene(3);
gene2.setAllele(new int[]{0,1,0});
assertEquals(gene1.hashCode(), gene2.hashCode());
}
/**
* @author Klaus Meffert
* @since 2.5
*/
public void testHashCode_3() {
Gene gene1 = new FixedBinaryGene(2);
gene1.setAllele(new int[]{1,1});
Gene gene2 = new FixedBinaryGene(2);
gene1.setAllele(new int[]{1,0});
assertFalse(gene1.hashCode() == gene2.hashCode());
}
/**
* @author Klaus Meffert
* @since 2.4
*/
public void testSetEnergy_0() {
BaseGene gene = new FixedBinaryGene(2);
assertEquals(0.0, gene.getEnergy(), DELTA);
}
/**
* @author Klaus Meffert
* @since 2.4
*/
public void testSetEnergy_1() {
BaseGene gene = new FixedBinaryGene(3);
gene.setEnergy(2.3);
assertEquals(2.3, gene.getEnergy(), DELTA);
gene.setEnergy(-55.8);
assertEquals(-55.8, gene.getEnergy(), DELTA);
gene.setEnergy(0.5);
gene.setEnergy(0.8);
assertEquals(0.8, gene.getEnergy(), DELTA);
}
class GeneConstraintChecker implements IGeneConstraintChecker {
public boolean verify(Gene a_gene, Object a_alleleValue)
throws RuntimeException {
return true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -