From: gmungoc Date: Tue, 16 Dec 2014 10:46:46 +0000 (+0000) Subject: JAL-845 move main method to 'test' class (needs further work); X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=e1ca456711764eabb0d91f49754a6dc4620368dc;p=jalview.git JAL-845 move main method to 'test' class (needs further work); formatting --- diff --git a/src/jalview/util/MapList.java b/src/jalview/util/MapList.java index eb414b9..4c62500 100644 --- a/src/jalview/util/MapList.java +++ b/src/jalview/util/MapList.java @@ -20,7 +20,8 @@ */ package jalview.util; -import java.util.*; +import java.util.Enumeration; +import java.util.Vector; /** * MapList Simple way of bijectively mapping a non-contiguous linear range to @@ -39,31 +40,41 @@ public class MapList public boolean equals(MapList obj) { if (obj == this) + { return true; + } if (obj != null && obj.fromRatio == fromRatio && obj.toRatio == toRatio && obj.fromShifts != null && obj.toShifts != null) { int i, iSize = fromShifts.size(), j, jSize = obj.fromShifts.size(); if (iSize != jSize) + { return false; + } for (i = 0, iSize = fromShifts.size(), j = 0, jSize = obj.fromShifts .size(); i < iSize;) { int[] mi = (int[]) fromShifts.elementAt(i++); int[] mj = (int[]) obj.fromShifts.elementAt(j++); if (mi[0] != mj[0] || mi[1] != mj[1]) + { return false; + } } iSize = toShifts.size(); jSize = obj.toShifts.size(); if (iSize != jSize) + { return false; + } for (i = 0, j = 0; i < iSize;) { int[] mi = (int[]) toShifts.elementAt(i++); int[] mj = (int[]) obj.toShifts.elementAt(j++); if (mi[0] != mj[0] || mi[1] != mj[1]) + { return false; + } } return true; } @@ -157,9 +168,13 @@ public class MapList private void ensureRange(int[] limits, int pos) { if (limits[0] > pos) + { limits[0] = pos; + } if (limits[1] < pos) + { limits[1] = pos; + } } public MapList(int from[], int to[], int fromRatio, int toRatio) @@ -499,7 +514,9 @@ public class MapList int fromEnd[] = shiftTo(end); // needs to be inclusive of end of symbol // position if (fromStart == null || fromEnd == null) + { return null; + } int iv[] = getIntervals(fromShifts, fromStart, fromEnd, fromRatio); return iv; } @@ -519,7 +536,9 @@ public class MapList int toStart[] = shiftFrom(start); int toEnd[] = shiftFrom(end); if (toStart == null || toEnd == null) + { return null; + } int iv[] = getIntervals(toShifts, toStart, toEnd, toRatio); return iv; } @@ -612,7 +631,9 @@ public class MapList i++; } if (fs == fe && fe == -1) + { return null; + } Vector ranges = new Vector(); if (fs <= fe) { @@ -623,7 +644,9 @@ public class MapList iv = new int[] { iv[0], iv[1] };// clone if (i == fs) + { iv[0] = startpos; + } while (i != fe) { ranges.addElement(iv); // add initial range @@ -633,7 +656,9 @@ public class MapList i++; } if (i == fe) + { iv[1] = endpos; + } ranges.addElement(iv); // add only - or final range } else @@ -750,223 +775,6 @@ public class MapList } /** - * test routine. not incremental. - * - * @param ml - * @param fromS - * @param fromE - */ - public static void testMap(MapList ml, int fromS, int fromE) - { - for (int from = 1; from <= 25; from++) - { - int[] too = ml.shiftFrom(from); - System.out.print("ShiftFrom(" + from + ")=="); - if (too == null) - { - System.out.print("NaN\n"); - } - else - { - System.out.print(too[0] + " % " + too[1] + " (" + too[2] + ")"); - System.out.print("\t+--+\t"); - int[] toofrom = ml.shiftTo(too[0]); - if (toofrom != null) - { - if (toofrom[0] != from) - { - System.err.println("Mapping not reflexive:" + from + " " - + too[0] + "->" + toofrom[0]); - } - System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0] - + " % " + toofrom[1] + " (" + toofrom[2] + ")"); - } - else - { - System.out.println("ShiftTo(" + too[0] + ")==" - + "NaN! - not Bijective Mapping!"); - } - } - } - int mmap[][] = ml.makeFromMap(); - System.out.println("FromMap : (" + mmap[0][0] + " " + mmap[0][1] + " " - + mmap[0][2] + " " + mmap[0][3] + " "); - for (int i = 1; i <= mmap[1].length; i++) - { - if (mmap[1][i - 1] == -1) - { - System.out.print(i + "=XXX"); - - } - else - { - System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1])); - } - if (i % 20 == 0) - { - System.out.print("\n"); - } - else - { - System.out.print(","); - } - } - // test range function - System.out.print("\nTest locateInFrom\n"); - { - int f = mmap[0][2], t = mmap[0][3]; - while (f <= t) - { - System.out.println("Range " + f + " to " + t); - int rng[] = ml.locateInFrom(f, t); - if (rng != null) - { - for (int i = 0; i < rng.length; i++) - { - System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";")); - } - } - else - { - System.out.println("No range!"); - } - System.out.print("\nReversed\n"); - rng = ml.locateInFrom(t, f); - if (rng != null) - { - for (int i = 0; i < rng.length; i++) - { - System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";")); - } - } - else - { - System.out.println("No range!"); - } - System.out.print("\n"); - f++; - t--; - } - } - System.out.print("\n"); - mmap = ml.makeToMap(); - System.out.println("ToMap : (" + mmap[0][0] + " " + mmap[0][1] + " " - + mmap[0][2] + " " + mmap[0][3] + " "); - for (int i = 1; i <= mmap[1].length; i++) - { - if (mmap[1][i - 1] == -1) - { - System.out.print(i + "=XXX"); - - } - else - { - System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1])); - } - if (i % 20 == 0) - { - System.out.print("\n"); - } - else - { - System.out.print(","); - } - } - System.out.print("\n"); - // test range function - System.out.print("\nTest locateInTo\n"); - { - int f = mmap[0][2], t = mmap[0][3]; - while (f <= t) - { - System.out.println("Range " + f + " to " + t); - int rng[] = ml.locateInTo(f, t); - if (rng != null) - { - for (int i = 0; i < rng.length; i++) - { - System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";")); - } - } - else - { - System.out.println("No range!"); - } - System.out.print("\nReversed\n"); - rng = ml.locateInTo(t, f); - if (rng != null) - { - for (int i = 0; i < rng.length; i++) - { - System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";")); - } - } - else - { - System.out.println("No range!"); - } - f++; - t--; - System.out.print("\n"); - } - } - - } - - public static void main(String argv[]) - { - MapList ml = new MapList(new int[] - { 1, 5, 10, 15, 25, 20 }, new int[] - { 51, 1 }, 1, 3); - MapList ml1 = new MapList(new int[] - { 1, 3, 17, 4 }, new int[] - { 51, 1 }, 1, 3); - MapList ml2 = new MapList(new int[] - { 1, 60 }, new int[] - { 1, 20 }, 3, 1); - // test internal consistency - int to[] = new int[51]; - MapList.testMap(ml, 1, 60); - MapList mldna = new MapList(new int[] - { 2, 2, 6, 8, 12, 16 }, new int[] - { 1, 3 }, 3, 1); - int[] frm = mldna.locateInFrom(1, 1); - testLocateFrom(mldna, 1, 1, new int[] - { 2, 2, 6, 7 }); - MapList.testMap(mldna, 1, 3); - /* - * for (int from=1; from<=51; from++) { int[] too=ml.shiftTo(from); int[] - * toofrom=ml.shiftFrom(too[0]); - * System.out.println("ShiftFrom("+from+")=="+too[0]+" % - * "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]); } - */ - System.out.print("Success?\n"); // if we get here - something must be - // working! - } - - private static void testLocateFrom(MapList mldna, int i, int j, int[] ks) - { - int[] frm = mldna.locateInFrom(i, j); - if (frm == ks || java.util.Arrays.equals(frm, ks)) - { - System.out.println("Success test locate from " + i + " to " + j); - } - else - { - System.err.println("Failed test locate from " + i + " to " + j); - for (int c = 0; c < frm.length; c++) - { - System.err.print(frm[c] + ((c % 2 == 0) ? "," : ";")); - } - System.err.println("Expected"); - for (int c = 0; c < ks.length; c++) - { - System.err.print(ks[c] + ((c % 2 == 0) ? "," : ";")); - } - } - } - - /** * * @return a MapList whose From range is this maplist's To Range, and vice * versa diff --git a/test/jalview/util/MapListTest.java b/test/jalview/util/MapListTest.java new file mode 100644 index 0000000..2c8e207 --- /dev/null +++ b/test/jalview/util/MapListTest.java @@ -0,0 +1,213 @@ +package jalview.util; + +import java.util.Arrays; + +import org.junit.Assert; +import org.junit.Test; + +public class MapListTest +{ + + @Test + public void testSomething() + { + MapList ml = new MapList(new int[] + { 1, 5, 10, 15, 25, 20 }, new int[] + { 51, 1 }, 1, 3); + MapList ml1 = new MapList(new int[] + { 1, 3, 17, 4 }, new int[] + { 51, 1 }, 1, 3); + MapList ml2 = new MapList(new int[] + { 1, 60 }, new int[] + { 1, 20 }, 3, 1); + // test internal consistency + int to[] = new int[51]; + testMap(ml, 1, 60); + MapList mldna = new MapList(new int[] + { 2, 2, 6, 8, 12, 16 }, new int[] + { 1, 3 }, 3, 1); + int[] frm = mldna.locateInFrom(1, 1); + testLocateFrom(mldna, 1, 1, new int[] + { 2, 2, 6, 7 }); + testMap(mldna, 1, 3); + /* + * for (int from=1; from<=51; from++) { int[] too=ml.shiftTo(from); int[] + * toofrom=ml.shiftFrom(too[0]); + * System.out.println("ShiftFrom("+from+")=="+too[0]+" % + * "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]); } + */ + } + + private static void testLocateFrom(MapList mldna, int i, int j, int[] ks) + { + int[] frm = mldna.locateInFrom(i, j); + Assert.assertEquals("Failed test locate from " + i + " to " + j, + Arrays.toString(frm), Arrays.toString(ks)); + } + + /** + * test routine. not incremental. + * + * @param ml + * @param fromS + * @param fromE + */ + private void testMap(MapList ml, int fromS, int fromE) + { + // todo convert to JUnit style tests + for (int from = 1; from <= 25; from++) + { + int[] too = ml.shiftFrom(from); + System.out.print("ShiftFrom(" + from + ")=="); + if (too == null) + { + System.out.print("NaN\n"); + } + else + { + System.out.print(too[0] + " % " + too[1] + " (" + too[2] + ")"); + System.out.print("\t+--+\t"); + int[] toofrom = ml.shiftTo(too[0]); + if (toofrom != null) + { + if (toofrom[0] != from) + { + System.err.println("Mapping not reflexive:" + from + " " + + too[0] + "->" + toofrom[0]); + } + System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0] + + " % " + toofrom[1] + " (" + toofrom[2] + ")"); + } + else + { + System.out.println("ShiftTo(" + too[0] + ")==" + + "NaN! - not Bijective Mapping!"); + } + } + } + int mmap[][] = ml.makeFromMap(); + System.out.println("FromMap : (" + mmap[0][0] + " " + mmap[0][1] + " " + + mmap[0][2] + " " + mmap[0][3] + " "); + for (int i = 1; i <= mmap[1].length; i++) + { + if (mmap[1][i - 1] == -1) + { + System.out.print(i + "=XXX"); + + } + else + { + System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1])); + } + if (i % 20 == 0) + { + System.out.print("\n"); + } + else + { + System.out.print(","); + } + } + // test range function + System.out.print("\nTest locateInFrom\n"); + { + int f = mmap[0][2], t = mmap[0][3]; + while (f <= t) + { + System.out.println("Range " + f + " to " + t); + int rng[] = ml.locateInFrom(f, t); + if (rng != null) + { + for (int i = 0; i < rng.length; i++) + { + System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";")); + } + } + else + { + System.out.println("No range!"); + } + System.out.print("\nReversed\n"); + rng = ml.locateInFrom(t, f); + if (rng != null) + { + for (int i = 0; i < rng.length; i++) + { + System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";")); + } + } + else + { + System.out.println("No range!"); + } + System.out.print("\n"); + f++; + t--; + } + } + System.out.print("\n"); + mmap = ml.makeToMap(); + System.out.println("ToMap : (" + mmap[0][0] + " " + mmap[0][1] + " " + + mmap[0][2] + " " + mmap[0][3] + " "); + for (int i = 1; i <= mmap[1].length; i++) + { + if (mmap[1][i - 1] == -1) + { + System.out.print(i + "=XXX"); + + } + else + { + System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1])); + } + if (i % 20 == 0) + { + System.out.print("\n"); + } + else + { + System.out.print(","); + } + } + System.out.print("\n"); + // test range function + System.out.print("\nTest locateInTo\n"); + { + int f = mmap[0][2], t = mmap[0][3]; + while (f <= t) + { + System.out.println("Range " + f + " to " + t); + int rng[] = ml.locateInTo(f, t); + if (rng != null) + { + for (int i = 0; i < rng.length; i++) + { + System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";")); + } + } + else + { + System.out.println("No range!"); + } + System.out.print("\nReversed\n"); + rng = ml.locateInTo(t, f); + if (rng != null) + { + for (int i = 0; i < rng.length; i++) + { + System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";")); + } + } + else + { + System.out.println("No range!"); + } + f++; + t--; + System.out.print("\n"); + } + } + + } + +}