📄 fixedbinarygenetest.java
字号:
}
try {
gene1.compareTo(new Integer(3));
fail();
}
catch (Exception e) {
/*Should compare only FixedBinary Gene's*/
}
}
/**
* @author Klaus Meffert
* @since 2.2
*/
public void testCompareTo_4() {
FixedBinaryGene gene1 = new FixedBinaryGene(3);
FixedBinaryGene gene2 = new FixedBinaryGene(4);
assertEquals( -1, gene1.compareTo(gene2));
assertEquals(1, gene2.compareTo(gene1));
}
/***
* Apply Mutation (index,percentage). if >0 make 1(0) if <0 make 0(1)
* @author vamsi
*/
public void testApplyMutation_0() {
FixedBinaryGene gene = new FixedBinaryGene(4);
gene.setAllele(new int[] {0, 0, 1, 1});
gene.applyMutation(0, 0.0d);
assertEquals("[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("[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) {
/*Fine*/
}
}
/**
* @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("[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("[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("[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("[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("[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));
}
}
public void testNewGene_0() {
FixedBinaryGene gene1 = new FixedBinaryGene(7);
FixedBinaryGene gene2 = (FixedBinaryGene) gene1.newGene();
assertTrue(gene1.equals(gene2));
assertTrue(gene2.equals(gene1));
}
/**
* @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*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -