📄 stringgenetest.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 StringGene class.
*
* @author Klaus Meffert
* @since 1.1
*/
public class StringGeneTest
extends JGAPTestCase {
/** String containing the CVS revision. Read out via reflection!*/
private final static String CVS_REVISION = "$Revision: 1.34 $";
public static Test suite() {
TestSuite suite = new TestSuite(StringGeneTest.class);
return suite;
}
/**
* @throws Exception
*
* @author Klaus Meffert
*/
public void testConstruct_0()
throws Exception {
StringGene gene = new StringGene(conf, 1, 100);
//following should be possible without exception
gene.setAllele("ABC");
assertEquals(null, gene.getAlphabet());
assertEquals(1, gene.getMinLength());
assertEquals(100, gene.getMaxLength());
}
/**
* @throws Exception
*
* @author Klaus Meffert
*/
public void testConstruct_1()
throws Exception {
try {
new StringGene(conf, 2, 1);
fail();
}
catch (IllegalArgumentException iex) {
; //this is OK
}
}
/**
* @throws Exception
*
* @author Klaus Meffert
*/
public void testConstruct_2()
throws Exception {
try {
new StringGene(conf, -1, 3);
fail();
}
catch (IllegalArgumentException iex) {
; //this is OK
}
}
/**
* @throws Exception
*
* @author Klaus Meffert
*/
public void testConstruct_3()
throws Exception {
try {
Gene gene = new StringGene(conf, 1, 3);
gene.setAllele("ABCD");
}
catch (IllegalArgumentException iex) {
; //this is OK
}
}
/**
* @throws Exception
*
* @author Klaus Meffert
*/
public void testConstruct_4()
throws Exception {
try {
Gene gene = new StringGene(conf, 1, 3);
gene.setAllele(new Double(2.3d));
}
catch (ClassCastException castex) {
; //this is OK
}
}
/**
*
* @throws Exception
*
* @author Klaus Meffert
*/
public void testSetAlphabet_1()
throws Exception {
StringGene gene = new StringGene(conf, 3, 5);
final String alphabet_const = "1234";
gene.setAlphabet(alphabet_const);
String alphabet = (String) privateAccessor.getField(gene, "m_alphabet");
assertEquals(alphabet, alphabet_const);
assertEquals(alphabet, gene.getAlphabet());
}
/**
* @throws Exception
*
* @author Klaus Meffert
*/
public void testToString_0()
throws Exception {
Gene gene = new StringGene(conf, 3, 7);
gene.setAllele("ABC");
assertEquals("StringGene=ABC", gene.toString());
}
/**
* @throws Exception
*
* @author Klaus Meffert
* @since 2.4
*/
public void testToString_1()
throws Exception {
StringGene gene = new StringGene(conf, 3, 7);
assertEquals("StringGene=null", gene.toString());
}
/**
* @throws Exception
*
* @author Klaus Meffert
* @since 2.4
*/
public void testToString_2()
throws Exception {
StringGene gene = new StringGene(conf, 0, 7);
gene.setAllele("");
assertEquals("StringGene=\"\"", gene.toString());
}
/**
* @throws Exception
*
* @author Klaus Meffert
*/
public void testGetAllele_0()
throws Exception {
Gene gene = new StringGene(conf, 3, 5);
gene.setAllele("BCD");
assertEquals("BCD", gene.getAllele());
}
/**
* @throws Exception
*
* @author Klaus Meffert
*/
public void testEquals_0()
throws Exception {
Gene gene1 = new StringGene(conf, 1, 100);
Gene gene2 = new StringGene(conf, 1, 100);
assertTrue(gene1.equals(gene2));
assertTrue(gene2.equals(gene1));
}
/**
* @throws Exception
*
* @author Klaus Meffert
*/
public void testEquals_1()
throws Exception {
Gene gene1 = new StringGene(conf, 3, 100);
assertFalse(gene1.equals(null));
}
/**
* @throws Exception
*
* @author Klaus Meffert
*/
public void testEquals_2()
throws Exception {
Gene gene1 = new StringGene(conf, 11, 77);
assertTrue(gene1.equals(new StringGene(conf)));
}
/**
* @throws Exception
*
* @author Klaus Meffert
*/
public void testEquals_3()
throws Exception {
Gene gene1 = new StringGene(conf, 11, 17);
assertFalse(gene1.equals(new Vector()));
}
/**
* @throws Exception
*
* @author Klaus Meffert
*/
public void testEquals_4()
throws Exception {
Gene gene1 = new StringGene(conf, 12, 100);
Gene gene2 = new StringGene(conf, 12, 99);
assertTrue(gene1.equals(gene2));
assertTrue(gene2.equals(gene1));
}
/**
* @throws Exception
*
* @author Klaus Meffert
*/
public void testEquals_5()
throws Exception {
Gene gene1 = new StringGene(conf, 2, 5);
Gene gene2 = new StringGene(conf, 1, 5);
gene1.setAllele("ABC");
gene2.setAllele("AB");
assertFalse(gene1.equals(gene2));
assertFalse(gene2.equals(gene1));
}
/**
* @throws Exception
*
* @author Klaus Meffert
*/
public void testEquals_6()
throws Exception {
Gene gene1 = new StringGene(conf, 2, 5);
Gene gene2 = new DoubleGene(conf, 1, 5);
assertFalse(gene1.equals(gene2));
assertFalse(gene2.equals(gene1));
}
/**
* @throws Exception
*
* @author Klaus Meffert
*/
public void testEquals_7()
throws Exception {
Gene gene1 = new StringGene(conf, 2, 5);
Gene gene2 = new BooleanGene(conf);
assertFalse(gene1.equals(gene2));
assertFalse(gene2.equals(gene1));
}
/**
* @throws Exception
*
* @author Klaus Meffert
* @since 2.4
*/
public void testEquals_8()
throws Exception {
Gene gene1 = new StringGene(conf, 2, 6);
gene1.setAllele("hallo");
Gene gene2 = new StringGene(conf, 2, 6);
gene2.setAllele("hello");
assertFalse(gene1.equals(gene2));
assertFalse(gene2.equals(gene1));
gene1.setAllele("hello1");
assertFalse(gene1.equals(gene2));
assertFalse(gene2.equals(gene1));
gene2.setAllele("HELLO1");
assertFalse(gene1.equals(gene2));
assertFalse(gene2.equals(gene1));
}
/**
* Using application data.
* @throws Exception
*
*
* @author Klaus Meffert
* @since 2.4
*/
public void testEquals_9()
throws Exception {
Gene gene1 = new StringGene(conf, 2, 6);
gene1.setAllele("hallo");
gene1.setApplicationData(new Double(2.3d));
Gene gene2 = new StringGene(conf, 2, 6);
gene2.setAllele("hallo");
assertTrue(gene1.equals(gene2));
assertTrue(gene2.equals(gene1));
gene1.setCompareApplicationData(true);
assertFalse(gene1.equals(gene2));
assertTrue(gene2.equals(gene1));
gene2.setCompareApplicationData(true);
assertFalse(gene1.equals(gene2));
assertFalse(gene2.equals(gene1));
gene2.setApplicationData(new Double(2.3d));
assertTrue(gene1.equals(gene2));
assertTrue(gene2.equals(gene1));
}
/**
* Comparation using application data.
* @throws Exception
*
*
* @author Klaus Meffert
* @since 2.4
*/
public void testCompareTo_0()
throws Exception {
Gene gene1 = new StringGene(conf, 2, 6);
gene1.setAllele("hallo");
gene1.setApplicationData(new Double(2.3d));
Gene gene2 = new StringGene(conf, 2, 6);
gene2.setAllele("hallo");
assertEquals(0, gene1.compareTo(gene2));
assertEquals(0, gene2.compareTo(gene1));
gene1.setCompareApplicationData(true);
assertEquals(1, gene1.compareTo(gene2));
assertEquals(0, gene2.compareTo(gene1));
gene2.setCompareApplicationData(true);
assertEquals(1, gene1.compareTo(gene2));
assertEquals( -1, gene2.compareTo(gene1));
gene2.setApplicationData(new Double(2.3d));
assertEquals(0, gene1.compareTo(gene2));
assertEquals(0, gene2.compareTo(gene1));
gene2.setAllele(null);
assertEquals(1, gene1.compareTo(gene2));
gene2.setApplicationData(null);
assertEquals(1, gene1.compareTo(gene2));
}
/**
* class cast exception.
* @throws Exception
*
*
* @author Klaus Meffert
* @since 2.6
*/
public void testCompareTo_1()
throws Exception {
Gene gene1 = new StringGene(conf, 2, 6);
try {
gene1.compareTo(new Chromosome(conf));
fail();
}
catch (ClassCastException cex) {
; // this is OK
}
}
/**
* Comparation using application data and null allele.
* @throws Exception
*
*
* @author Klaus Meffert
* @since 2.6
*/
public void testCompareTo_2()
throws Exception {
Gene gene1 = new StringGene(conf, 2, 6);
gene1.setApplicationData(new Double(2.3d));
Gene gene2 = new StringGene(conf, 2, 6);
assertEquals(0, gene1.compareTo(gene2));
assertEquals(0, gene2.compareTo(gene1));
gene1.setCompareApplicationData(true);
assertEquals(1, gene1.compareTo(gene2));
assertEquals(0, gene2.compareTo(gene1));
gene2.setCompareApplicationData(true);
assertEquals(1, gene1.compareTo(gene2));
assertEquals( -1, gene2.compareTo(gene1));
gene2.setApplicationData(new Double(2.3d));
assertEquals(0, gene1.compareTo(gene2));
assertEquals(0, gene2.compareTo(gene1));
gene2.setApplicationData(null);
assertEquals(1, gene1.compareTo(gene2));
}
/**
* Set Allele to null, no exception should occur.
* @throws Exception
*
* @author Klaus Meffert
*/
public void testSetAllele_0()
throws Exception {
Gene gene1 = new StringGene(conf, 0, 10000);
gene1.setAllele(null);
}
/**
* Allele too short.
* @throws Exception
*
* @author Klaus Meffert
*/
public void testSetAllele_1()
throws Exception {
Gene gene1 = new StringGene(conf, 3, 4);
try {
gene1.setAllele("AB");
fail();
}
catch (IllegalArgumentException iex) {
; //this is OK
}
}
/**
* Allele consists of illegal characters.
* @throws Exception
*
* @author Klaus Meffert
*/
public void testSetAllele_2()
throws Exception {
Gene gene1 = new StringGene(conf, 3, 4, "ABCDEFHI");
try {
gene1.setAllele("ABDG");
fail();
}
catch (IllegalArgumentException iex) {
; //this is OK
}
}
/**
* @throws Exception
*
* @author Klaus Meffert
*/
public void testSetAllele_3()
throws Exception {
Gene gene1 = new StringGene(conf, 3, 4, "EGAL");
try {
// Length of allele to short.
// --------------------------
gene1.setAllele("");
fail();
}
catch (IllegalArgumentException iex) {
; //this is OK
}
}
/**
* @throws Exception
*
* @author Klaus Meffert
*/
public void testSetAllele_4()
throws Exception {
Gene gene1 = new StringGene(conf, 0, 4, "");
//following should be possible
gene1.setAllele("");
}
/**
* @throws Exception
*
* @author Klaus Meffert
* @since 2.2
*/
public void testSetAllele_5()
throws Exception {
StringGene gene1 = new StringGene(conf, 0, 4, "ABC");
//following should be possible
gene1.setAllele("A");
gene1.setConstraintChecker(new IGeneConstraintChecker() {
public boolean verify(Gene a_gene, Object a_alleleValue,
IChromosome a_chrom, int a_index) {
return false;
}
});
gene1.setAllele("B");
assertEquals("A", gene1.stringValue());
}
/**
* @throws Exception
*
* @author Klaus Meffert
* @since 2.2
*/
public void testSetAllele_6()
throws Exception {
StringGene gene1 = new StringGene(conf, 0, 4, "ABC");
//following should be possible
gene1.setAllele("A");
gene1.setConstraintChecker(new IGeneConstraintChecker() {
public boolean verify(Gene a_gene, Object a_alleleValue,
IChromosome a_chrom, int a_index) {
return true;
}
});
gene1.setAllele("B");
assertEquals("B", gene1.stringValue());
}
/**
*
* @throws Exception
*
* @author Klaus Meffert
*/
public void testNewGene_0()
throws Exception {
StringGene gene1 = new StringGene(conf, 1, 4);
IGeneConstraintChecker checker = new GeneConstraintChecker();
gene1.setConstraintChecker(checker);
gene1.setAllele("XYZ");
int minLength1 = gene1.getMinLength();
int maxLength1 = gene1.getMaxLength();
StringGene gene2 = (StringGene) gene1.newGene();
int minLength2 = gene2.getMinLength();
int maxLength2 = gene2.getMaxLength();
assertEquals(minLength1, minLength2);
assertEquals(maxLength1, maxLength2);
assertEquals(checker, gene2.getConstraintChecker());
}
/**
*
* @throws Exception
*
* @author Klaus Meffert
*/
public void testPersistentRepresentation_0()
throws Exception {
Gene gene1 = new StringGene(conf, 2, 10, "ABCDE");
gene1.setAllele("BABE");
String pres1 = gene1.getPersistentRepresentation();
Gene gene2 = new StringGene(conf);
gene2.setValueFromPersistentRepresentation(pres1);
String pres2 = gene2.getPersistentRepresentation();
assertEquals(pres1, pres2);
}
/**
* @throws Exception
*
* @author Klaus Meffert
*/
public void testPersistentRepresentation_1()
throws Exception {
Gene gene1 = new StringGene(conf, 2, 10);
try {
gene1.setAllele("");
fail();
}
catch (IllegalArgumentException iex) {
; //this is OK
}
}
/**
*
* @throws Exception
*
* @author Klaus Meffert
*/
public void testPersistentRepresentation_2()
throws Exception {
Gene gene1 = new StringGene(conf, 0, 10);
gene1.setAllele("");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -