0001: /*
0002: * This file is part of JGAP.
0003: *
0004: * JGAP offers a dual license model containing the LGPL as well as the MPL.
0005: *
0006: * For licensing information please see the file license.txt included with JGAP
0007: * or have a look at the top of class org.jgap.Chromosome which representatively
0008: * includes the JGAP license policy applicable for any file delivered with JGAP.
0009: */
0010: package org.jgap.impl;
0011:
0012: import org.jgap.*;
0013: import junit.framework.*;
0014:
0015: /**
0016: * Tests the FixedBinaryGene class.
0017: *
0018: * @author Klaus Meffert
0019: * @author vamsi
0020: * @since 2.0
0021: */
0022: public class FixedBinaryGeneTest extends JGAPTestCase {
0023: /** String containing the CVS revision. Read out via reflection!*/
0024: private final static String CVS_REVISION = "$Revision: 1.33 $";
0025:
0026: public static Test suite() {
0027: TestSuite suite = new TestSuite(FixedBinaryGeneTest.class);
0028: return suite;
0029: }
0030:
0031: /**
0032: *
0033: * @author Klaus Meffert
0034: * @since 2.0
0035: * @throws Exception
0036: */
0037: public void testConstruct_0() throws Exception {
0038: // following should be possible without exception
0039: new FixedBinaryGene(conf, 1);
0040: new FixedBinaryGene(conf, 10);
0041: new FixedBinaryGene(conf, 1000);
0042: new FixedBinaryGene(conf, 100000);
0043: }
0044:
0045: /**
0046: *
0047: * @author Klaus Meffert
0048: * @throws Exception
0049: */
0050: public void testConstruct_1() throws Exception {
0051: try {
0052: new FixedBinaryGene(conf, 0);
0053: fail();
0054: } catch (IllegalArgumentException iex) {
0055: ; //this is OK
0056: }
0057: }
0058:
0059: /**
0060: *
0061: * @author Klaus Meffert
0062: * @throws Exception
0063: */
0064: public void testConstruct_2() throws Exception {
0065: try {
0066: new FixedBinaryGene(conf, -5);
0067: fail();
0068: } catch (IllegalArgumentException iex) {
0069: ; //this is OK
0070: }
0071: }
0072:
0073: /**
0074: *
0075: * @author vamsi
0076: * @throws Exception
0077: */
0078: public void testConstruct_3() throws Exception {
0079: int i = 0;
0080: FixedBinaryGene gene = new FixedBinaryGene(conf, 5);
0081: for (i = 0; i < 4; i++) {
0082: //assert that we have
0083: assertFalse(gene.getBit(i));
0084: }
0085: assertEquals("FixedBinaryGene[0,0,0,0,0]", gene.toString());
0086: }
0087:
0088: /**
0089: *
0090: * @author vamsi
0091: * @throws Exception
0092: */
0093: public void testConstruct_4() throws Exception {
0094: FixedBinaryGene gene = new FixedBinaryGene(conf, 6);
0095: assertEquals(1, gene.size());
0096: assertEquals(1, (gene.getValue()).length);
0097: assertEquals("FixedBinaryGene[0,0,0,0,0,0]", gene.toString());
0098: }
0099:
0100: /**
0101: * Buffer allocation test case.
0102: *
0103: * @author vamsi
0104: * @throws Exception
0105: */
0106: public void testConstruct_5() throws Exception {
0107: FixedBinaryGene gene;
0108: gene = new FixedBinaryGene(conf, 32);
0109: assertEquals(1, gene.size());
0110: gene = new FixedBinaryGene(conf, 81);
0111: assertEquals(3, gene.size());
0112: }
0113:
0114: /**
0115: *
0116: * @author Klaus Meffert
0117: * @since 2.2
0118: * @throws Exception
0119: */
0120: public void testConstruct_6() throws Exception {
0121: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 1);
0122: new FixedBinaryGene(conf, gene1);
0123: }
0124:
0125: /**
0126: *
0127: * @author vamsi
0128: * @throws Exception
0129: */
0130: public void testToString_0() throws Exception {
0131: Gene gene = new FixedBinaryGene(conf, 1);
0132: gene.setAllele(new int[] { 1 });
0133: assertEquals("FixedBinaryGene[1]", gene.toString());
0134: }
0135:
0136: /**
0137: *
0138: * @author vamsi
0139: * @throws Exception
0140: */
0141: public void testToString_1() throws Exception {
0142: Gene gene = new FixedBinaryGene(conf, 3);
0143: gene.setAllele(new int[] { 1, 0, 1 });
0144: assertEquals("FixedBinaryGene[1,0,1]", gene.toString());
0145: }
0146:
0147: /**
0148: *
0149: * @author Klaus Meffert
0150: * @since 2.2
0151: * @throws Exception
0152: */
0153: public void testToString_2() throws Exception {
0154: Gene gene = new FixedBinaryGene(conf, 3);
0155: assertEquals("FixedBinaryGene[0,0,0]", gene.toString());
0156: }
0157:
0158: /**
0159: *
0160: * @author vamsi
0161: * @throws Exception
0162: */
0163: public void testGetAllele_0() throws Exception {
0164: Gene gene = new FixedBinaryGene(conf, 1);
0165: int[] value = new int[] { 0 };
0166: gene.setAllele(value);
0167: assertEquals(value.length, ((int[]) gene.getAllele()).length);
0168: for (int i = 0; i < value.length; i++) {
0169: assertEquals(value[i], ((int[]) gene.getAllele())[i]);
0170: }
0171: }
0172:
0173: /**
0174: *
0175: * @author vamsi
0176: * @throws Exception
0177: */
0178: public void testGetAllele_1() throws Exception {
0179: Gene gene = new FixedBinaryGene(conf, 2);
0180: try {
0181: gene.setAllele(new Integer(100));
0182: fail();
0183: } catch (ClassCastException classex) {
0184: ; //this is OK
0185: }
0186: }
0187:
0188: /**
0189: *
0190: * @author Klaus Meffert
0191: * @since 2.2
0192: * @throws Exception
0193: */
0194: public void testGetAllele_2() throws Exception {
0195: Gene gene = new FixedBinaryGene(conf, 1);
0196: int[] value = new int[] { 1 };
0197: gene.setAllele(value);
0198: assertEquals(value.length, ((int[]) gene.getAllele()).length);
0199: for (int i = 0; i < value.length; i++) {
0200: assertEquals(value[i], ((int[]) gene.getAllele())[i]);
0201: }
0202: }
0203:
0204: /**
0205: *
0206: * @author vamsi
0207: * @throws Exception
0208: */
0209: public void testEquals_0() throws Exception {
0210: Gene gene1 = new FixedBinaryGene(conf, 1);
0211: Gene gene2 = new FixedBinaryGene(conf, 1);
0212: assertTrue(gene1.equals(gene2));
0213: }
0214:
0215: /**
0216: *
0217: * @author vamsi
0218: * @throws Exception
0219: */
0220: public void testEquals_1() throws Exception {
0221: Gene gene1 = new FixedBinaryGene(conf, 1);
0222: assertFalse(gene1.equals(null));
0223: }
0224:
0225: /**
0226: *
0227: * @author vamsi
0228: * @throws Exception
0229: */
0230: public void testEquals_2() throws Exception {
0231: Gene gene1 = new FixedBinaryGene(conf, 2);
0232: gene1.setAllele(new int[] { 1, 0 });
0233: Gene gene2 = new FixedBinaryGene(conf, 2);
0234: gene2.setAllele(new int[] { 0, 1 });
0235: assertFalse(gene1.equals(gene2));
0236: assertFalse(gene2.equals(gene1));
0237: }
0238:
0239: /**
0240: *
0241: * @author vamsi
0242: * @throws Exception
0243: */
0244: public void testEquals_3() throws Exception {
0245: Gene gene1 = new FixedBinaryGene(conf, 5);
0246: assertFalse(gene1.equals(new IntegerGene(conf)));
0247: }
0248:
0249: public void testEquals_4() throws Exception {
0250: Gene gene1 = new FixedBinaryGene(conf, 1);
0251: Gene gene2 = new IntegerGene(conf);
0252: assertFalse(gene1.equals(gene2));
0253: assertFalse(gene2.equals(gene1));
0254: }
0255:
0256: public void testEquals_5() throws Exception {
0257: Gene gene1 = new FixedBinaryGene(conf, 1);
0258: Gene gene2 = new DoubleGene(conf);
0259: assertFalse(gene1.equals(gene2));
0260: assertFalse(gene2.equals(gene1));
0261: }
0262:
0263: public void testEquals_6() throws Exception {
0264: Gene gene1 = new FixedBinaryGene(conf, 1);
0265: Gene gene2 = new BooleanGene(conf);
0266: assertFalse(gene1.equals(gene2));
0267: assertFalse(gene2.equals(gene1));
0268: }
0269:
0270: public void testEquals_7() throws Exception {
0271: Gene gene1 = new FixedBinaryGene(conf, 1);
0272: Gene gene2 = new StringGene(conf);
0273: assertFalse(gene1.equals(gene2));
0274: assertFalse(gene2.equals(gene1));
0275: }
0276:
0277: /**
0278: *
0279: * @author Klaus Meffert
0280: * @since 2.4
0281: * @throws Exception
0282: */
0283: public void testEquals_8() throws Exception {
0284: Gene gene1 = new FixedBinaryGene(conf, 2);
0285: gene1.setAllele(new int[] { 0, 1 });
0286: Gene gene2 = new FixedBinaryGene(conf, 2);
0287: gene2.setAllele(new int[] { 0, 0 });
0288: assertFalse(gene1.equals(gene2));
0289: assertFalse(gene2.equals(gene1));
0290: }
0291:
0292: /**
0293: *
0294: * @author vamsi
0295: * @throws Exception
0296: */
0297: public void testIntValues_0() throws Exception {
0298: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 4);
0299: assertFalse(gene1.getIntValues() == null);
0300: }
0301:
0302: /**
0303: *
0304: * @author vamsi
0305: * @throws Exception
0306: */
0307: public void testIntValues_1() throws Exception {
0308: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 2);
0309: int[] values = gene1.getIntValues();
0310: int i;
0311: for (i = 0; i < values.length; i++) {
0312: assertEquals(0, values[i]);
0313: }
0314: }
0315:
0316: /**
0317: *
0318: * @author vamsi
0319: * @throws Exception
0320: */
0321: public void testIntValues_2() throws Exception {
0322: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
0323: gene1.setAllele(new int[] { 0, 1, 0 });
0324: assertEquals(false, gene1.getBit(0));
0325: assertEquals(true, gene1.getBit(1));
0326: assertEquals(false, gene1.getBit(2));
0327: assertEquals(3, gene1.getLength());
0328: }
0329:
0330: /**
0331: * Allele is null.
0332: *
0333: * @author vamsi
0334: * @throws Exception
0335: */
0336: public void testSetAllele_0() throws Exception {
0337: Gene gene1 = new FixedBinaryGene(conf, 1);
0338: try {
0339: gene1.setAllele(null);
0340: fail();
0341: } catch (IllegalArgumentException iex) {
0342: ; //this is OK
0343: }
0344: }
0345:
0346: /**
0347: * Allele is of wrong type.
0348: *
0349: * @author vamsi
0350: * @throws Exception
0351: */
0352: public void testSetAllele_1() throws Exception {
0353: Gene gene1 = new FixedBinaryGene(conf, 1);
0354: try {
0355: gene1.setAllele("22");
0356: fail();
0357: } catch (ClassCastException classex) {
0358: ; //this is OK
0359: }
0360: }
0361:
0362: /**
0363: * Set Allele to int values, no exception should occur.
0364: *
0365: * @author vamsi
0366: * @throws Exception
0367: */
0368: public void testSetAllele_2() throws Exception {
0369: Gene gene1 = new FixedBinaryGene(conf, 3);
0370: gene1.setAllele(new int[] { 0, 0, 1 });
0371: }
0372:
0373: /**
0374: * The implementation should throw an exception if the alle size is more than
0375: * the size of the created gene.
0376: *
0377: * @author vamsi
0378: * @throws Exception
0379: */
0380: public void testSetAllele_3() throws Exception {
0381: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
0382: try {
0383: gene1.setAllele(new int[] { 0, 1, 1, 1, 1, 1 });
0384: fail();
0385: } catch (Exception e) {
0386: ; //this is OK
0387: }
0388: }
0389:
0390: /**
0391: * Allele contains illegal characters.
0392: *
0393: * @author vamsi
0394: * @throws Exception
0395: */
0396: public void testSetAllele_4() throws Exception {
0397: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 4);
0398: try {
0399: gene1.setAllele(new int[] { 0, 3, 1, 4 });
0400: fail();
0401: } catch (Exception e) {
0402: ; //this is OK
0403: }
0404: }
0405:
0406: /**
0407: * Allele is of wrong length.
0408: *
0409: * @author vamsi
0410: * @throws Exception
0411: */
0412: public void testSetAllele_5() throws Exception {
0413: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 4);
0414: try {
0415: gene1.setAllele(new int[] { 0, 0 });
0416: fail();
0417: } catch (Exception e) {
0418: ; //this is OK
0419: }
0420: }
0421:
0422: /**
0423: *
0424: * @author Klaus Meffert
0425: * @since 2.2
0426: * @throws Exception
0427: */
0428: public void testSetAllele_6() throws Exception {
0429: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
0430: gene1.setConstraintChecker(new IGeneConstraintChecker() {
0431: public boolean verify(Gene a_gene, Object a_alleleValue,
0432: IChromosome a_chrom, int a_index) {
0433: return false;
0434: }
0435: });
0436: gene1.setAllele(new int[] { 0, 0, 1 });
0437: assertFalse(gene1.getBit(0));
0438: assertFalse(gene1.getBit(1));
0439: assertFalse(gene1.getBit(2));
0440: }
0441:
0442: /**
0443: *
0444: * @author Klaus Meffert
0445: * @since 2.2
0446: * @throws Exception
0447: */
0448: public void testSetAllele_7() throws Exception {
0449: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
0450: gene1.setConstraintChecker(new IGeneConstraintChecker() {
0451: public boolean verify(Gene a_gene, Object a_alleleValue,
0452: IChromosome a_chrom, int a_index) {
0453: return true;
0454: }
0455: });
0456: gene1.setAllele(new int[] { 0, 0, 1 });
0457: assertFalse(gene1.getBit(0));
0458: assertFalse(gene1.getBit(1));
0459: assertTrue(gene1.getBit(2));
0460: }
0461:
0462: /**
0463: *
0464: * @author Klaus Meffert
0465: * @since 2.2
0466: * @throws Exception
0467: */
0468: public void testSetConstraintChecker_0() throws Exception {
0469: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
0470: assertNull(gene1.getConstraintChecker());
0471: gene1.setConstraintChecker(new IGeneConstraintChecker() {
0472: public boolean verify(Gene a_gene, Object a_alleleValue,
0473: IChromosome a_chrom, int a_index) {
0474: return false;
0475: }
0476: });
0477: assertNotNull(gene1.getConstraintChecker());
0478: }
0479:
0480: /**
0481: * Comparison should return 0 if same, -1 if less 1 if more.
0482: *
0483: * @author vamsi
0484: * @throws Exception
0485: */
0486: public void testCompareTo_0() throws Exception {
0487: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 4);
0488: FixedBinaryGene gene2 = new FixedBinaryGene(conf, 4);
0489: gene1.setAllele(new int[] { 1, 0, 1, 0 });
0490: gene2.setAllele(new int[] { 1, 1, 0, 1 });
0491: assertEquals(1, gene1.compareTo(null));
0492: assertEquals(-1, gene1.compareTo(gene2));
0493: assertEquals(1, gene2.compareTo(gene1));
0494: }
0495:
0496: /**
0497: *
0498: * @author vamsi
0499: * @throws Exception
0500: */
0501: public void testCompareTo_1() throws Exception {
0502: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
0503: FixedBinaryGene gene2 = new FixedBinaryGene(conf, 3);
0504: assertEquals(0, gene1.compareTo(gene2));
0505: assertEquals(0, gene2.compareTo(gene1));
0506: }
0507:
0508: /**
0509: *
0510: * @author vamsi
0511: * @throws Exception
0512: */
0513: public void testCompareTo_2() throws Exception {
0514: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
0515: FixedBinaryGene gene2 = new FixedBinaryGene(conf, 3);
0516: gene1.setAllele(new int[] { 1, 1, 1 });
0517: gene2.setAllele(new int[] { 1, 1, 1 });
0518: assertEquals(0, gene1.compareTo(gene2));
0519: assertEquals(0, gene2.compareTo(gene1));
0520: gene1.setAllele(new int[] { 0, 0, 0 });
0521: gene2.setAllele(new int[] { 0, 0, 0 });
0522: assertEquals(0, gene1.compareTo(gene2));
0523: assertEquals(0, gene2.compareTo(gene1));
0524: }
0525:
0526: /**
0527: *
0528: * @author vamsi
0529: * @throws Exception
0530: */
0531: public void testCompareTo_3_1() throws Exception {
0532: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
0533: BooleanGene gene2 = new BooleanGene(conf);
0534: try {
0535: gene1.compareTo(gene2);
0536: fail();
0537: } catch (Exception e) {
0538: ; //this is OK (should compare only FixedBinaryGene's)
0539: }
0540: }
0541:
0542: /**
0543: *
0544: * @author vamsi
0545: * @throws Exception
0546: */
0547: public void testCompareTo_3_2() throws Exception {
0548: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
0549: try {
0550: gene1.compareTo(new Integer(3));
0551: fail();
0552: } catch (Exception e) {
0553: ; //this is OK (should compare only FixedBinaryGene's)
0554: }
0555: }
0556:
0557: /**
0558: *
0559: * @author Klaus Meffert
0560: * @since 2.2
0561: * @throws Exception
0562: */
0563: public void testCompareTo_4() throws Exception {
0564: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
0565: FixedBinaryGene gene2 = new FixedBinaryGene(conf, 4);
0566: assertEquals(-1, gene1.compareTo(gene2));
0567: assertEquals(1, gene2.compareTo(gene1));
0568: }
0569:
0570: /**
0571: * Apply Mutation (index,percentage). if >0 make 1(0) if <0 make 0(1)
0572: *
0573: * @author vamsi
0574: * @throws Exception
0575: */
0576: public void testApplyMutation_0() throws Exception {
0577: FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
0578: gene.setAllele(new int[] { 0, 0, 1, 1 });
0579: gene.applyMutation(0, 0.0d);
0580: assertEquals("FixedBinaryGene[0,0,1,1]", gene.toString());
0581: }
0582:
0583: /**
0584: *
0585: * @author vamsi
0586: * @throws Exception
0587: */
0588: public void testApplyMutation_1() throws Exception {
0589: FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
0590: gene.setAllele(new int[] { 0, 0, 1, 0 });
0591: gene.applyMutation(1, 0.000001d);
0592: assertEquals("FixedBinaryGene[0,1,1,0]", gene.toString());
0593: }
0594:
0595: /**
0596: *
0597: * @author vamsi
0598: * @throws Exception
0599: */
0600: public void testApplyMutation_2() throws Exception {
0601: FixedBinaryGene gene = new FixedBinaryGene(conf, 5);
0602: gene.setAllele(new int[] { 1, 0, 1, 0, 1 });
0603: try {
0604: //index size is greater
0605: gene.applyMutation(333, -0.000001d);
0606: fail();
0607: } catch (Exception e) {
0608: ; //this is OK
0609: }
0610: }
0611:
0612: /**
0613: *
0614: * @author vamsi
0615: * @throws Exception
0616: */
0617: public void testApplyMutation_3() throws Exception {
0618: FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
0619: gene.setAllele(new int[] { 1, 1, 0, 1 });
0620: gene.applyMutation(0, -1.0d);
0621: assertEquals("FixedBinaryGene[0,1,0,1]", gene.toString());
0622: }
0623:
0624: /**
0625: *
0626: * @author vamsi
0627: * @throws Exception
0628: */
0629: public void testApplyMutation_4() throws Exception {
0630: FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
0631: gene.setAllele(new int[] { 0, 1, 0, 1 });
0632: gene.applyMutation(0, -2.0d);
0633: gene.applyMutation(3, 2.0d);
0634: gene.applyMutation(1, -4.0d);
0635: assertEquals("FixedBinaryGene[0,0,0,1]", gene.toString());
0636: }
0637:
0638: /**
0639: *
0640: * @author vamsi
0641: * @throws Exception
0642: */
0643: public void testApplyMutation_5() throws Exception {
0644: FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
0645: gene.setAllele(new int[] { 1, 1, 1, 1 });
0646: gene.applyMutation(0, 2.0d);
0647: gene.applyMutation(1, 2.0d);
0648: gene.applyMutation(2, 2.0d);
0649: gene.applyMutation(3, 2.0d);
0650: assertEquals("FixedBinaryGene[1,1,1,1]", gene.toString());
0651: }
0652:
0653: /**
0654: *
0655: * @author vamsi
0656: * @since 2.0
0657: * @throws Exception
0658: */
0659: public void testSetValueFromPersistentRepresentation_0()
0660: throws Exception {
0661: FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
0662: try {
0663: gene.setValueFromPersistentRepresentation(null);
0664: fail();
0665: } catch (UnsupportedRepresentationException uex) {
0666: ; //this is OK
0667: }
0668: }
0669:
0670: /**
0671: * @since 2.0
0672: * @throws Exception
0673: */
0674: public void testSetValueFromPersistentRepresentation_1()
0675: throws Exception {
0676: FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
0677: try {
0678: gene.setValueFromPersistentRepresentation("null");
0679: fail();
0680: } catch (UnsupportedRepresentationException uex) {
0681: ; //this is OK
0682: }
0683: }
0684:
0685: /**
0686: * @throws Exception
0687: *
0688: * @author vamsi
0689: * @since 2.0
0690: */
0691: public void testSetValueFromPersistentRepresentation_2()
0692: throws Exception {
0693: FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
0694: gene.setValueFromPersistentRepresentation("[1,1,1,1]");
0695: assertTrue(gene.getBit(0));
0696: assertTrue(gene.getBit(1));
0697: assertTrue(gene.getBit(2));
0698: assertTrue(gene.getBit(3));
0699: }
0700:
0701: /**
0702: * @throws Exception
0703: *
0704: * @author vamsi
0705: * @since 2.0
0706: */
0707: public void testSetValueFromPersistentRepresentation_3()
0708: throws Exception {
0709: FixedBinaryGene gene = new FixedBinaryGene(conf, 4);
0710: gene.setValueFromPersistentRepresentation("[0,0,0,0]");
0711: assertFalse(gene.getBit(0));
0712: assertFalse(gene.getBit(1));
0713: assertFalse(gene.getBit(2));
0714: assertFalse(gene.getBit(3));
0715: }
0716:
0717: /**
0718: * @throws Exception
0719: *
0720: * @author vamsi
0721: * @since 2.0
0722: */
0723: public void testSetValueFromPersistentRepresentation_4()
0724: throws Exception {
0725: FixedBinaryGene gene = new FixedBinaryGene(conf, 5);
0726: gene.setValueFromPersistentRepresentation("[0,1,1,0,0]");
0727: assertFalse(gene.getBit(0));
0728: assertTrue(gene.getBit(1));
0729: assertTrue(gene.getBit(2));
0730: assertFalse(gene.getBit(3));
0731: assertFalse(gene.getBit(4));
0732: }
0733:
0734: /**
0735: * @throws Exception
0736: *
0737: * @since 2.0
0738: * @author Klaus Meffert
0739: */
0740: public void testSetValueFromPersistentRepresentation_5()
0741: throws Exception {
0742: FixedBinaryGene gene = new FixedBinaryGene(conf, 5);
0743: try {
0744: gene.setValueFromPersistentRepresentation("[0,1,1,0]");
0745: fail();
0746: } catch (UnsupportedRepresentationException uex) {
0747: ; //this is OK
0748: }
0749: }
0750:
0751: /**
0752: *
0753: * @author vamsi
0754: * @since 2.0
0755: * @throws Exception
0756: */
0757: public void testSetValueFromPersistentRepresentation_6()
0758: throws Exception {
0759: FixedBinaryGene gene = new FixedBinaryGene(conf, 1);
0760: try {
0761: gene.setValueFromPersistentRepresentation("X");
0762: fail();
0763: } catch (UnsupportedRepresentationException uex) {
0764: ; //this is OK
0765: }
0766: }
0767:
0768: /**
0769: *
0770: * @author vamsi
0771: * @throws Exception
0772: */
0773: public void testGetPersistentRepresentation_0() throws Exception {
0774: FixedBinaryGene gene = new FixedBinaryGene(conf, 3);
0775: gene.setAllele(new int[] { 1, 0, 1 });
0776: String s = gene.getPersistentRepresentation();
0777: assertEquals("FixedBinaryGene[1,0,1]", s);
0778: }
0779:
0780: /**
0781: * @throws Exception
0782: *
0783: * @author vamsi
0784: * @since 2.0
0785: */
0786: public void testGetPersistentRepresentation_1() throws Exception {
0787: FixedBinaryGene gene = new FixedBinaryGene(conf, 3);
0788: try {
0789: gene.setValueFromPersistentRepresentation(null);
0790: fail();
0791: } catch (UnsupportedRepresentationException uex) {
0792: ; //this is OK
0793: }
0794: }
0795:
0796: /**
0797: * @throws Exception
0798: *
0799: * @author Klaus Meffert
0800: * @since 2.0
0801: */
0802: public void testGetPersistentRepresentation_2() throws Exception {
0803: FixedBinaryGene gene = new FixedBinaryGene(conf, 3);
0804: String s = gene.getPersistentRepresentation();
0805: assertEquals("FixedBinaryGene[0,0,0]", s);
0806: }
0807:
0808: /**
0809: *
0810: * @author Klaus Meffert
0811: * @since 2.2
0812: * @throws Exception
0813: */
0814: public void testClone_0() throws Exception {
0815: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 1);
0816: FixedBinaryGene gene2 = (FixedBinaryGene) gene1.clone();
0817: assertEquals(gene1, gene2);
0818: }
0819:
0820: /**
0821: *
0822: * @author Klaus Meffert
0823: * @since 2.2
0824: * @throws Exception
0825: */
0826: public void testClone_1() throws Exception {
0827: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 3);
0828: FixedBinaryGene gene2 = (FixedBinaryGene) gene1.clone();
0829: assertEquals(gene1, gene2);
0830: }
0831:
0832: /**
0833: *
0834: * @author Klaus Meffert
0835: * @since 2.2
0836: * @throws Exception
0837: */
0838: public void testSetBit_0() throws Exception {
0839: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
0840: gene1.setAllele(new int[] { 1, 1, 0, 0, 1, 0, 1 });
0841: assertTrue(gene1.getBit(0));
0842: gene1.setBit(0, false);
0843: assertFalse(gene1.getBit(0));
0844: gene1.setBit(1, true);
0845: assertTrue(gene1.getBit(1));
0846: gene1.setBit(4, false);
0847: assertFalse(gene1.getBit(0));
0848: }
0849:
0850: /**
0851: *
0852: * @author Klaus Meffert
0853: * @since 2.2
0854: * @throws Exception
0855: */
0856: public void testSetBit_1() throws Exception {
0857: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
0858: gene1.setAllele(new int[] { 1, 1, 0, 0, 1, 0, 1 });
0859: gene1.setBit(2, 4, true);
0860: assertTrue(gene1.getBit(0));
0861: assertTrue(gene1.getBit(1));
0862: assertTrue(gene1.getBit(2));
0863: assertTrue(gene1.getBit(3));
0864: }
0865:
0866: /**
0867: *
0868: * @author Klaus Meffert
0869: * @since 2.2
0870: * @throws Exception
0871: */
0872: public void testSetBit_2() throws Exception {
0873: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
0874: gene1.setAllele(new int[] { 1, 1, 0, 0, 1, 0, 1 });
0875: try {
0876: gene1.setBit(2, 2);
0877: fail();
0878: } catch (IllegalArgumentException iex) {
0879: ; //this is OK
0880: }
0881: }
0882:
0883: /**
0884: *
0885: * @author Klaus Meffert
0886: * @since 2.2
0887: * @throws Exception
0888: */
0889: public void testSetBit_3() throws Exception {
0890: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
0891: gene1.setAllele(new int[] { 1, 1, 0, 0, 1, 0, 1 });
0892: try {
0893: gene1.setBit(2, -1);
0894: fail();
0895: } catch (IllegalArgumentException iex) {
0896: ; //this is OK
0897: }
0898: }
0899:
0900: /**
0901: *
0902: * @author Klaus Meffert
0903: * @since 2.2
0904: * @throws Exception
0905: */
0906: public void testSetBit_4() throws Exception {
0907: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
0908: gene1.setAllele(new int[] { 1, 1, 0, 0, 1, 0, 1 });
0909: try {
0910: gene1.setBit(2, 1, false);
0911: fail();
0912: } catch (IllegalArgumentException iex) {
0913: ; //this is OK
0914: }
0915: }
0916:
0917: /**
0918: *
0919: * @author Klaus Meffert
0920: * @since 2.6
0921: * @throws Exception
0922: */
0923: public void testSetBit_5() throws Exception {
0924: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
0925: gene1.setAllele(new int[] { 1, 1, 0, 0, 1, 0, 1 });
0926: FixedBinaryGene gene2 = new FixedBinaryGene(conf, 9);
0927: gene2.setBit(2, 6, gene1);
0928: assertTrue(gene2.getBit(2));
0929: assertTrue(gene2.getBit(3));
0930: assertTrue(gene2.getBit(6));
0931: assertFalse(gene2.getBit(0));
0932: assertFalse(gene2.getBit(1));
0933: assertFalse(gene2.getBit(4));
0934: assertFalse(gene2.getBit(5));
0935: assertFalse(gene2.getBit(7));
0936: assertFalse(gene2.getBit(8));
0937: }
0938:
0939: /**
0940: *
0941: * @author Klaus Meffert
0942: * @since 2.6
0943: * @throws Exception
0944: */
0945: public void testSetBit_6() throws Exception {
0946: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 4);
0947: gene1.setAllele(new int[] { 1, 1, 0, 0 });
0948: FixedBinaryGene gene2 = new FixedBinaryGene(conf, 7);
0949: gene2.setBit(2, 6, gene1);
0950: assertTrue(gene2.getBit(2));
0951: assertTrue(gene2.getBit(3));
0952: assertTrue(gene2.getBit(6));
0953: assertFalse(gene2.getBit(0));
0954: assertFalse(gene2.getBit(1));
0955: assertFalse(gene2.getBit(4));
0956: assertFalse(gene2.getBit(5));
0957: }
0958:
0959: /**
0960: *
0961: * @author Klaus Meffert
0962: * @since 2.2
0963: * @throws Exception
0964: */
0965: public void testSubbString_0() throws Exception {
0966: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
0967: gene1.setAllele(new int[] { 1, 1, 0, 0, 1, 0, 1 });
0968: FixedBinaryGene gene2 = gene1.substring(0, 4);
0969: assertEquals(5, gene2.getLength());
0970: assertEquals(1, gene2.size());
0971: assertTrue(gene2.getBit(0));
0972: assertTrue(gene2.getBit(1));
0973: assertFalse(gene2.getBit(2));
0974: assertFalse(gene2.getBit(3));
0975: assertTrue(gene2.getBit(4));
0976: }
0977:
0978: /**
0979: *
0980: * @author Klaus Meffert
0981: * @since 2.2
0982: * @throws Exception
0983: */
0984: public void testSubString_1() throws Exception {
0985: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
0986: gene1.setAllele(new int[] { 1, 1, 0, 0, 1, 0, 1 });
0987: try {
0988: gene1.substring(0, 7);
0989: fail();
0990: } catch (IndexOutOfBoundsException iex) {
0991: ; //this is OK
0992: }
0993: }
0994:
0995: /**
0996: *
0997: * @author Klaus Meffert
0998: * @since 2.2
0999: * @throws Exception
1000: */
1001: public void testFlip_0() throws Exception {
1002: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
1003: gene1.setAllele(new int[] { 1, 1, 0, 0, 1, 0, 1 });
1004: gene1.flip(0);
1005: assertFalse(gene1.getBit(0));
1006: gene1.flip(6);
1007: assertFalse(gene1.getBit(6));
1008: gene1.flip(2);
1009: assertTrue(gene1.getBit(2));
1010: }
1011:
1012: /**
1013: *
1014: * @author Klaus Meffert
1015: * @since 2.2
1016: * @throws Exception
1017: */
1018: public void testFlip_1() throws Exception {
1019: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
1020: gene1.setAllele(new int[] { 1, 1, 0, 0, 1, 0, 1 });
1021: try {
1022: gene1.flip(7);
1023: fail();
1024: } catch (IndexOutOfBoundsException iex) {
1025: ; //this is OK
1026: }
1027: }
1028:
1029: /**
1030: *
1031: * @author Klaus Meffert
1032: * @since 2.2
1033: * @throws Exception
1034: */
1035: public void testSetToRandomValue_0() throws Exception {
1036: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
1037: try {
1038: gene1.setToRandomValue(null);
1039: fail();
1040: } catch (IllegalArgumentException iex) {
1041: ; //this is OK
1042: }
1043: }
1044:
1045: /**
1046: *
1047: * @author Klaus Meffert
1048: * @since 2.2
1049: * @throws Exception
1050: */
1051: public void testSetToRandomValue_1() throws Exception {
1052: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
1053: gene1.setToRandomValue(new StockRandomGenerator());
1054: }
1055:
1056: /**
1057: *
1058: * @author Klaus Meffert
1059: * @since 2.2
1060: * @throws Exception
1061: */
1062: public void testSetToRandomValue_2() throws Exception {
1063: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
1064: gene1.setToRandomValue(new RandomGeneratorForTesting(false));
1065: for (int i = 0; i < 7; i++) {
1066: assertFalse(gene1.getBit(i));
1067: }
1068: }
1069:
1070: /**
1071: *
1072: * @author Klaus Meffert
1073: * @since 2.2
1074: * @throws Exception
1075: */
1076: public void testSetToRandomValue_3() throws Exception {
1077: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
1078: gene1.setToRandomValue(new RandomGeneratorForTesting(true));
1079: for (int i = 0; i < 7; i++) {
1080: assertTrue(gene1.getBit(i));
1081: }
1082: }
1083:
1084: /**
1085: *
1086: * @author Klaus Meffert
1087: * @throws Exception
1088: */
1089: public void testNewGene_0() throws Exception {
1090: FixedBinaryGene gene1 = new FixedBinaryGene(conf, 7);
1091: IGeneConstraintChecker checker = new GeneConstraintChecker();
1092: gene1.setConstraintChecker(checker);
1093: FixedBinaryGene gene2 = (FixedBinaryGene) gene1.newGene();
1094: assertTrue(gene1.equals(gene2));
1095: assertTrue(gene2.equals(gene1));
1096: assertEquals(checker, gene2.getConstraintChecker());
1097: }
1098:
1099: /**
1100: *
1101: * @author Klaus Meffert
1102: * @since 2.2
1103: * @throws Exception
1104: */
1105: public void testHashCode_0() throws Exception {
1106: FixedBinaryGene gene = new FixedBinaryGene(conf, 6);
1107: gene.hashCode();
1108: gene.setBit(0, 5, false);
1109: gene.hashCode();
1110: gene.setBit(0, 5, true);
1111: gene.hashCode();
1112: /**@todo implement checks for uniqueness*/
1113: }
1114:
1115: /**
1116: *
1117: * @author Klaus Meffert
1118: * @since 2.5
1119: * @throws Exception
1120: */
1121: public void testHashCode_1() throws Exception {
1122: Gene gene1 = new FixedBinaryGene(conf, 2);
1123: Gene gene2 = new FixedBinaryGene(conf, 2);
1124: int[] v1 = (int[]) gene1.getAllele();
1125: int[] v2 = (int[]) gene1.getAllele();
1126: int h1 = v1.hashCode();
1127: int h2 = v2.hashCode();
1128: assertEquals(gene1.hashCode(), gene2.hashCode());
1129: }
1130:
1131: /**
1132: *
1133: * @author Klaus Meffert
1134: * @since 2.5
1135: * @throws Exception
1136: */
1137: public void testHashCode_2() throws Exception {
1138: Gene gene1 = new FixedBinaryGene(conf, 3);
1139: gene1.setAllele(new int[] { 0, 1, 0 });
1140: Gene gene2 = new FixedBinaryGene(conf, 3);
1141: gene2.setAllele(new int[] { 0, 1, 0 });
1142: assertEquals(gene1.hashCode(), gene2.hashCode());
1143: }
1144:
1145: /**
1146: *
1147: * @author Klaus Meffert
1148: * @since 2.5
1149: * @throws Exception
1150: */
1151: public void testHashCode_3() throws Exception {
1152: Gene gene1 = new FixedBinaryGene(conf, 2);
1153: gene1.setAllele(new int[] { 1, 1 });
1154: Gene gene2 = new FixedBinaryGene(conf, 2);
1155: gene1.setAllele(new int[] { 1, 0 });
1156: assertFalse(gene1.hashCode() == gene2.hashCode());
1157: assertTrue(gene1.hashCode() == gene1.hashCode());
1158: assertTrue(gene2.hashCode() == gene2.hashCode());
1159: }
1160:
1161: /**
1162: *
1163: * @author Klaus Meffert
1164: * @since 2.4
1165: * @throws Exception
1166: */
1167: public void testSetEnergy_0() throws Exception {
1168: BaseGene gene = new FixedBinaryGene(conf, 2);
1169: assertEquals(0.0, gene.getEnergy(), DELTA);
1170: }
1171:
1172: /**
1173: *
1174: * @author Klaus Meffert
1175: * @since 2.4
1176: * @throws Exception
1177: */
1178: public void testSetEnergy_1() throws Exception {
1179: BaseGene gene = new FixedBinaryGene(conf, 3);
1180: gene.setEnergy(2.3);
1181: assertEquals(2.3, gene.getEnergy(), DELTA);
1182: gene.setEnergy(-55.8);
1183: assertEquals(-55.8, gene.getEnergy(), DELTA);
1184: gene.setEnergy(0.5);
1185: gene.setEnergy(0.8);
1186: assertEquals(0.8, gene.getEnergy(), DELTA);
1187: }
1188:
1189: class GeneConstraintChecker implements IGeneConstraintChecker {
1190: public boolean verify(Gene a_gene, Object a_alleleValue,
1191: IChromosome a_chrom, int a_index) {
1192: return true;
1193: }
1194: }
1195:
1196: /**@todo test compareTo with applicationcata*/
1197: }
|