📄 integergenetest.java
字号:
/*
* This file is part of JGAP.
*
* JGAP offers a dual license model containing the LGPL as well as the MPL.
*
* For licencing information please see the file license.txt included with JGAP
* or have a look at the top of class org.jgap.Chromosome which representatively
* includes the JGAP license policy applicable for any file delivered with JGAP.
*/
package org.jgap.impl;
import java.util.*;
import org.jgap.*;
import junit.framework.*;
/**
* Tests the IntegerGene class
*
* @author Klaus Meffert
* @since 1.1
*/
public class IntegerGeneTest
extends JGAPTestCase {
/** String containing the CVS revision. Read out via reflection!*/
private final static String CVS_REVISION = "$Revision: 1.42 $";
public static Test suite() {
TestSuite suite = new TestSuite(IntegerGeneTest.class);
return suite;
}
public void testConstruct_0()
throws Exception {
Gene gene = new IntegerGene(conf, 1, 100);
//following should be possible without exception
gene.setAllele(new Integer(101));
}
/**
* @throws Exception
*
* @author Klaus Meffert
* @since 3.1
*/
public void testConstruct_1()
throws Exception {
Genotype.setStaticConfiguration(conf);
Gene gene = new IntegerGene();
assertSame(conf, gene.getConfiguration());
}
public void testToString_0()
throws Exception {
Gene gene = new IntegerGene(conf, 1, 100);
gene.setAllele(new Integer(47));
assertEquals("IntegerGene(1,100)=47", gene.toString());
}
public void testToString_1()
throws Exception {
Gene gene = new IntegerGene(conf, -2, 100);
gene.setAllele(new Integer(99));
assertEquals("IntegerGene(-2,100)=99", gene.toString());
}
public void testGetAllele_0()
throws Exception {
Gene gene = new IntegerGene(conf, 1, 100);
gene.setAllele(new Integer(33));
assertEquals(new Integer(33), gene.getAllele());
}
public void testGetAllele_1()
throws Exception {
Gene gene = new IntegerGene(conf, 1, 100);
gene.setAllele(new Integer(1));
assertEquals(new Integer(1), gene.getAllele());
}
public void testGetAllele_2()
throws Exception {
Gene gene = new IntegerGene(conf, 1, 100);
gene.setAllele(new Integer(100));
assertEquals(new Integer(100), gene.getAllele());
}
public void testEquals_0()
throws Exception {
Gene gene1 = new IntegerGene(conf, 1, 100);
Gene gene2 = new IntegerGene(conf, 1, 100);
assertTrue(gene1.equals(gene2));
assertTrue(gene2.equals(gene1));
}
public void testEquals_1()
throws Exception {
Gene gene1 = new IntegerGene(conf, 1, 100);
assertFalse(gene1.equals(null));
}
public void testEquals_2()
throws Exception {
Gene gene1 = new IntegerGene(conf, 1, 100);
assertFalse(gene1.equals(new BooleanGene(conf)));
}
public void testEquals_3()
throws Exception {
Gene gene1 = new IntegerGene(conf, 1, 100);
assertFalse(gene1.equals(new Vector()));
}
public void testEquals_4()
throws Exception {
Gene gene1 = new IntegerGene(conf, 1, 100);
Gene gene2 = new IntegerGene(conf, 1, 99);
assertTrue(gene1.equals(gene2));
assertTrue(gene2.equals(gene1));
}
public void testEquals_5()
throws Exception {
Gene gene1 = new IntegerGene(conf, 1, 100);
Gene gene2 = new DoubleGene(conf, 1, 99);
assertFalse(gene1.equals(gene2));
assertFalse(gene2.equals(gene1));
}
/**
* Uses subclass of IntegerGene.
* @throws Exception
*
* @author Klaus Meffert
* @since 2.4
*/
public void testEquals_6()
throws Exception {
Gene gene1 = new IntegerGene(conf, 1, 100);
Gene gene2 = new IntegerGene(conf, 1, 100);
gene1.setAllele(new Integer(45));
gene2.setAllele(new Integer(46));
assertFalse(gene1.equals(gene2));
assertFalse(gene2.equals(gene1));
}
/**
* Uses subclass of IntegerGene.
* @throws Exception
*
* @author Klaus Meffert
* @since 2.4
*/
public void testEquals_7()
throws Exception {
Gene gene1 = new IntegerGene(conf, 1, 100);
gene1.setAllele(new Integer(7));
Gene gene2 = new IntegerGene2(conf, 1, 100);
gene2.setAllele(new Integer(7));
assertFalse(gene1.equals(gene2));
assertFalse(gene2.equals(gene1));
}
/**
* Compare with application data set and option for comparation activated.
* @throws Exception
*
* @author Klaus Meffert
* @since 2.4
*/
public void testEquals_8()
throws Exception {
BaseGene gene1 = new IntegerGene(conf, 1, 100);
gene1.setAllele(new Integer(7));
gene1.setApplicationData(new Integer(7));
gene1.setCompareApplicationData(true);
Gene gene2 = new IntegerGene(conf, 1, 100);
gene2.setApplicationData(new Integer(7));
gene2.setAllele(new Integer(7));
gene2.setCompareApplicationData(true);
assertTrue(gene1.equals(gene2));
assertTrue(gene2.equals(gene1));
}
/**
* Compare with application data set and option for comparation activated.
* @throws Exception
*
* @author Klaus Meffert
* @since 2.4
*/
public void testEquals_9()
throws Exception {
BaseGene gene1 = new IntegerGene(conf, 1, 100);
gene1.setAllele(new Integer(7));
gene1.setApplicationData(new Integer(7));
gene1.setCompareApplicationData(true);
Gene gene2 = new IntegerGene(conf, 1, 100);
gene2.setCompareApplicationData(true);
gene2.setApplicationData(new Integer(7));
gene2.setAllele(new Integer(7));
assertTrue(gene1.equals(gene2));
assertTrue(gene2.equals(gene1));
}
/**
* Compare with application data set and option for comparation activated.
* @throws Exception
*
* @author Klaus Meffert
* @since 2.4
*/
public void testEquals_9_2()
throws Exception {
BaseGene gene1 = new IntegerGene(conf, 1, 100);
gene1.setAllele(new Integer(8));
gene1.setApplicationData(new Integer(5));
gene1.setCompareApplicationData(true);
Gene gene2 = new IntegerGene(conf, 1, 100);
gene2.setCompareApplicationData(true);
gene2.setApplicationData(new Integer(7));
gene2.setAllele(new Integer(8));
assertFalse(gene1.equals(gene2));
assertFalse(gene2.equals(gene1));
}
/*
* @throws Exception
* @author Klaus Meffert
* @since 2.4
*/
public void testEquals_10()
throws Exception {
Configuration conf = new ConfigurationForTest();
conf.setRandomGenerator(new RandomGeneratorForTest(5));
Gene gene1 = new IntegerGene(conf, 1, 100);
gene1.setAllele(new Integer(8));
Gene gene2 = new IntegerGene(conf, 1, 99);
gene2.setAllele(new Integer( -8));
assertFalse(gene1.equals(gene2));
assertFalse(gene2.equals(gene1));
}
/**
* Compare with application data set but option for comparation deactivated.
* @throws Exception
*
* @author Klaus Meffert
* @since 2.4
*/
public void testEquals_9_3()
throws Exception {
BaseGene gene1 = new IntegerGene(conf, 1, 100);
gene1.setAllele(new Integer(8));
gene1.setApplicationData(new Integer(5));
gene1.setCompareApplicationData(false);
Gene gene2 = new IntegerGene(conf, 1, 100);
gene2.setCompareApplicationData(false);
gene2.setApplicationData(new Integer(7));
gene2.setAllele(new Integer(8));
assertTrue(gene1.equals(gene2));
assertTrue(gene2.equals(gene1));
}
public void testIntValue_0()
throws Exception {
IntegerGene gene1 = new IntegerGene(conf, 1, 10000);
gene1.setAllele(new Integer(4711));
assertEquals(4711, gene1.intValue());
}
public void testIntValue_1()
throws Exception {
IntegerGene gene1 = new IntegerGene(conf, 1, 10000);
gene1.setAllele(null);
try {
assertEquals(0, gene1.intValue());
fail();
} catch (NullPointerException nullex) {
; //this is OK
}
}
/**
* Set Allele to null, no exception should occur.
*
* @throws Exception
*/
public void testSetAllele_0()
throws Exception {
Gene gene1 = new IntegerGene(conf, 1, 10000);
gene1.setAllele(null);
}
public void testSetAllele_1()
throws Exception {
Gene gene1 = new IntegerGene(conf, 1, 10000);
try {
gene1.setAllele("22");
fail();
} catch (ClassCastException classex) {
; //this is OK
}
}
/**
* Set Allele to value out of bounds with bounds' dimension of 1.
*
* @throws Exception
*
* @author Klaus Meffert
* @since 3.2
*/
public void testSetAllele_2()
throws Exception {
IntegerGene gene1 = new IntegerGene(conf, 0,0);
gene1.setAllele(new Integer(1));
assertEquals(0, gene1.intValue());
}
/**
* Set Allele to value out of bounds with bounds' dimension of 1.
*
* @throws Exception
*
* @author Klaus Meffert
* @since 3.2
*/
public void testSetAllele_3()
throws Exception {
IntegerGene gene1 = new IntegerGene(conf, 1,1);
gene1.setAllele(new Integer(2));
assertEquals(1, gene1.intValue());
}
public void testNewGene_0()
throws Exception {
IntegerGene gene1 = new IntegerGene(conf, 1, 10000);
IGeneConstraintChecker checker = new GeneConstraintChecker();
gene1.setConstraintChecker(checker);
gene1.setAllele(new Integer(4711));
int lower1 = gene1.getLowerBounds();
int upper1 = gene1.getUpperBounds();
IntegerGene gene2 = (IntegerGene) gene1.newGene();
int lower2 = gene2.getLowerBounds();
int upper2 = gene2.getUpperBounds();
assertEquals(lower1, lower2);
assertEquals(upper1, upper2);
assertEquals(checker, gene2.getConstraintChecker());
}
public void testCleanup()
throws Exception {
//cleanup should do nothing!
Gene gene = new IntegerGene(conf, 1, 6);
Gene copy = gene.newGene();
gene.cleanup();
assertEquals(copy, gene);
}
public void testPersistentRepresentation_0()
throws Exception {
Gene gene1 = new IntegerGene(conf, 2, 753);
gene1.setAllele(new Integer(45));
String pres1 = gene1.getPersistentRepresentation();
Gene gene2 = new IntegerGene(conf);
gene2.setValueFromPersistentRepresentation(pres1);
String pres2 = gene2.getPersistentRepresentation();
assertEquals(pres1, pres2);
}
/**
* Should be possible without exception.
* @throws Exception
*
* @author Klaus Meffert
* @since 2.2
*/
public void testPersistentRepresentation_1()
throws Exception {
Gene gene1 = new IntegerGene(conf, 2, 753);
gene1.setAllele(new Integer(45));
gene1.setValueFromPersistentRepresentation(null);
}
/**
* @throws Exception
*
* @author Klaus Meffert
* @since 2.2
*/
public void testPersistentRepresentation_2()
throws Exception {
IntegerGene gene1 = new IntegerGene(conf, 2, 753);
gene1.setAllele(new Integer(45));
gene1.setValueFromPersistentRepresentation("2"
+ IntegerGene.
PERSISTENT_FIELD_DELIMITER
+ "3"
+ IntegerGene.
PERSISTENT_FIELD_DELIMITER
+ "4");
assertEquals(2, ( (Integer) gene1.getAllele()).intValue());
assertEquals(3, gene1.getLowerBounds());
assertEquals(4, gene1.getUpperBounds());
}
/**
* @throws Exception
*
* @author Klaus Meffert
* @since 2.2
*/
public void testPersistentRepresentation_3()
throws Exception {
IntegerGene gene1 = new IntegerGene(conf, 2, 753);
gene1.setAllele(new Integer(45));
gene1.setValueFromPersistentRepresentation("null"
+ IntegerGene.
PERSISTENT_FIELD_DELIMITER
+ "-3"
+ IntegerGene.
PERSISTENT_FIELD_DELIMITER
+ "4");
assertNull(gene1.getAllele());
assertEquals( -3, gene1.getLowerBounds());
assertEquals(4, gene1.getUpperBounds());
}
/**
* @throws Exception
*
* @author Klaus Meffert
* @since 2.2
*/
public void testPersistentRepresentation_4()
throws Exception {
Gene gene1 = new IntegerGene(conf, 2, 753);
gene1.setAllele(new Integer(45));
try {
gene1.setValueFromPersistentRepresentation("null"
+ IntegerGene.
PERSISTENT_FIELD_DELIMITER
+ "3.5"
+ IntegerGene.
PERSISTENT_FIELD_DELIMITER
+ "4");
fail();
} catch (UnsupportedRepresentationException uex) {
; //this is OK
}
}
/**
* @throws Exception
*
* @author Klaus Meffert
* @since 2.2
*/
public void testPersistentRepresentation_5()
throws Exception {
Gene gene1 = new IntegerGene(conf, 2, 753);
gene1.setAllele(new Integer(45));
try {
gene1.setValueFromPersistentRepresentation("null"
+ IntegerGene.
PERSISTENT_FIELD_DELIMITER
+ "3"
+ IntegerGene.
PERSISTENT_FIELD_DELIMITER
+ "a");
fail();
} catch (UnsupportedRepresentationException uex) {
; //this is OK
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -