2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertNull;
26 import static org.testng.AssertJUnit.assertSame;
27 import static org.testng.AssertJUnit.assertTrue;
28 import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.List;
34 import org.testng.annotations.Test;
36 public class MapListTest
39 @Test(groups = { "Functional" })
40 public void testSomething()
42 MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 }, new int[] {
44 MapList ml1 = new MapList(new int[] { 1, 3, 17, 4 },
45 new int[] { 51, 1 }, 1, 3);
46 MapList ml2 = new MapList(new int[] { 1, 60 }, new int[] { 1, 20 }, 3,
48 // test internal consistency
49 int to[] = new int[51];
51 MapList mldna = new MapList(new int[] { 2, 2, 6, 8, 12, 16 }, new int[]
53 int[] frm = mldna.locateInFrom(1, 1);
54 testLocateFrom(mldna, 1, 1, new int[] { 2, 2, 6, 7 });
57 * for (int from=1; from<=51; from++) { int[] too=ml.shiftTo(from); int[]
58 * toofrom=ml.shiftFrom(too[0]);
59 * System.out.println("ShiftFrom("+from+")=="+too[0]+" %
60 * "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]); }
64 private static void testLocateFrom(MapList mldna, int i, int j, int[] ks)
66 int[] frm = mldna.locateInFrom(i, j);
67 assertEquals("Failed test locate from " + i + " to " + j,
68 Arrays.toString(frm), Arrays.toString(ks));
72 * test routine. not incremental.
78 private void testMap(MapList ml, int fromS, int fromE)
80 // todo convert to JUnit style tests
81 for (int from = 1; from <= 25; from++)
83 int[] too = ml.shiftFrom(from);
84 System.out.print("ShiftFrom(" + from + ")==");
87 System.out.print("NaN\n");
91 System.out.print(too[0] + " % " + too[1] + " (" + too[2] + ")");
92 System.out.print("\t+--+\t");
93 int[] toofrom = ml.shiftTo(too[0]);
96 if (toofrom[0] != from)
98 System.err.println("Mapping not reflexive:" + from + " "
99 + too[0] + "->" + toofrom[0]);
101 System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0]
102 + " % " + toofrom[1] + " (" + toofrom[2] + ")");
106 System.out.println("ShiftTo(" + too[0] + ")=="
107 + "NaN! - not Bijective Mapping!");
111 int mmap[][] = ml.makeFromMap();
112 System.out.println("FromMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
113 + mmap[0][2] + " " + mmap[0][3] + " ");
114 for (int i = 1; i <= mmap[1].length; i++)
116 if (mmap[1][i - 1] == -1)
118 System.out.print(i + "=XXX");
123 System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
127 System.out.print("\n");
131 System.out.print(",");
134 // test range function
135 System.out.print("\nTest locateInFrom\n");
137 int f = mmap[0][2], t = mmap[0][3];
140 System.out.println("Range " + f + " to " + t);
141 int rng[] = ml.locateInFrom(f, t);
144 for (int i = 0; i < rng.length; i++)
146 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
151 System.out.println("No range!");
153 System.out.print("\nReversed\n");
154 rng = ml.locateInFrom(t, f);
157 for (int i = 0; i < rng.length; i++)
159 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
164 System.out.println("No range!");
166 System.out.print("\n");
171 System.out.print("\n");
172 mmap = ml.makeToMap();
173 System.out.println("ToMap : (" + mmap[0][0] + " " + mmap[0][1] + " "
174 + mmap[0][2] + " " + mmap[0][3] + " ");
175 for (int i = 1; i <= mmap[1].length; i++)
177 if (mmap[1][i - 1] == -1)
179 System.out.print(i + "=XXX");
184 System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));
188 System.out.print("\n");
192 System.out.print(",");
195 System.out.print("\n");
196 // test range function
197 System.out.print("\nTest locateInTo\n");
199 int f = mmap[0][2], t = mmap[0][3];
202 System.out.println("Range " + f + " to " + t);
203 int rng[] = ml.locateInTo(f, t);
206 for (int i = 0; i < rng.length; i++)
208 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
213 System.out.println("No range!");
215 System.out.print("\nReversed\n");
216 rng = ml.locateInTo(t, f);
219 for (int i = 0; i < rng.length; i++)
221 System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));
226 System.out.println("No range!");
230 System.out.print("\n");
236 * Tests for method that locates ranges in the 'from' map for given range in
239 @Test(groups = { "Functional" })
240 public void testLocateInFrom_noIntrons()
243 * Simple mapping with no introns
245 int[] codons = new int[] { 1, 12 };
246 int[] protein = new int[] { 1, 4 };
247 MapList ml = new MapList(codons, protein, 3, 1);
248 assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(1, 1)));
249 assertEquals("[4, 6]", Arrays.toString(ml.locateInFrom(2, 2)));
250 assertEquals("[7, 9]", Arrays.toString(ml.locateInFrom(3, 3)));
251 assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
252 assertEquals("[1, 6]", Arrays.toString(ml.locateInFrom(1, 2)));
253 assertEquals("[1, 9]", Arrays.toString(ml.locateInFrom(1, 3)));
254 assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 4)));
255 assertEquals("[4, 9]", Arrays.toString(ml.locateInFrom(2, 3)));
256 assertEquals("[4, 12]", Arrays.toString(ml.locateInFrom(2, 4)));
257 assertEquals("[7, 12]", Arrays.toString(ml.locateInFrom(3, 4)));
258 assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
260 assertNull(ml.locateInFrom(0, 0));
261 assertNull(ml.locateInFrom(1, 5));
262 assertNull(ml.locateInFrom(-1, 1));
266 * Tests for method that locates ranges in the 'from' map for given range in
269 @Test(groups = { "Functional" })
270 public void testLocateInFrom_withIntrons()
273 * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
274 * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
276 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
277 int[] protein = { 1, 4 };
278 MapList ml = new MapList(codons, protein, 3, 1);
279 assertEquals("[2, 3, 5, 5]", Arrays.toString(ml.locateInFrom(1, 1)));
280 assertEquals("[6, 7, 9, 9]", Arrays.toString(ml.locateInFrom(2, 2)));
281 assertEquals("[10, 10, 12, 12, 14, 14]",
282 Arrays.toString(ml.locateInFrom(3, 3)));
283 assertEquals("[16, 18]", Arrays.toString(ml.locateInFrom(4, 4)));
287 * Tests for method that locates ranges in the 'to' map for given range in the
290 @Test(groups = { "Functional" })
291 public void testLocateInTo_noIntrons()
294 * Simple mapping with no introns
296 int[] codons = new int[] { 1, 12 };
297 int[] protein = new int[] { 1, 4 };
298 MapList ml = new MapList(codons, protein, 3, 1);
299 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 3)));
300 assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
301 assertEquals("[3, 3]", Arrays.toString(ml.locateInTo(7, 9)));
302 assertEquals("[4, 4]", Arrays.toString(ml.locateInTo(10, 12)));
303 assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 6)));
304 assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(1, 9)));
305 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(1, 12)));
306 assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
307 assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(4, 12)));
310 * A part codon is treated as if a whole one.
312 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 1)));
313 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 2)));
314 assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 4)));
315 assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(2, 8)));
316 assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(3, 11)));
317 assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(5, 11)));
319 assertNull(ml.locateInTo(0, 0));
320 assertNull(ml.locateInTo(1, 13));
321 assertNull(ml.locateInTo(-1, 1));
325 * Tests for method that locates ranges in the 'to' map for given range in the
328 @Test(groups = { "Functional" })
329 public void testLocateInTo_withIntrons()
332 * Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
333 * 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
335 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
337 * Mapped proteins at positions 1, 3, 4, 6 in the sequence
339 int[] protein = { 1, 1, 3, 4, 6, 6 };
340 MapList ml = new MapList(codons, protein, 3, 1);
343 * Can't map from an unmapped position
345 assertNull(ml.locateInTo(1, 2));
346 assertNull(ml.locateInTo(2, 4));
347 assertNull(ml.locateInTo(4, 4));
350 * Valid range or subrange of codon1 maps to protein1.
352 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 2)));
353 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 3)));
354 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 5)));
355 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 3)));
356 assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 5)));
358 // codon position 6 starts the next protein:
359 assertEquals("[1, 1, 3, 3]", Arrays.toString(ml.locateInTo(3, 6)));
361 // codon positions 7 to 17 (part) cover proteins 2/3/4 at positions 3/4/6
362 assertEquals("[3, 4, 6, 6]", Arrays.toString(ml.locateInTo(7, 17)));
367 * Test equals method.
369 @Test(groups = { "Functional" })
370 public void testEquals()
372 int[] codons = new int[] { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
373 int[] protein = new int[] { 1, 4 };
374 MapList ml = new MapList(codons, protein, 3, 1);
375 MapList ml1 = new MapList(codons, protein, 3, 1); // same values
376 MapList ml2 = new MapList(codons, protein, 2, 1); // fromRatio differs
377 MapList ml3 = new MapList(codons, protein, 3, 2); // toRatio differs
379 MapList ml6 = new MapList(codons, protein, 3, 1); // fromShifts differ
381 MapList ml7 = new MapList(codons, protein, 3, 1); // toShifts differ
383 assertTrue(ml.equals(ml));
384 assertTrue(ml.equals(ml1));
385 assertTrue(ml1.equals(ml));
387 assertFalse(ml.equals(null));
388 assertFalse(ml.equals("hello"));
389 assertFalse(ml.equals(ml2));
390 assertFalse(ml.equals(ml3));
391 assertFalse(ml.equals(ml6));
392 assertFalse(ml.equals(ml7));
393 assertFalse(ml6.equals(ml7));
397 MapList ml4 = new MapList(codons, null, 3, 1); // toShifts null
398 assertFalse(ml.equals(ml4));
399 } catch (NullPointerException e)
401 // actually thrown by constructor before equals can be called
405 MapList ml5 = new MapList(null, protein, 3, 1); // fromShifts null
406 assertFalse(ml.equals(ml5));
407 } catch (NullPointerException e)
409 // actually thrown by constructor before equals can be called
414 * Test for the method that flattens a list of ranges into a single array.
416 @Test(groups = { "Functional" })
417 public void testGetRanges()
419 List<int[]> ranges = new ArrayList<int[]>();
420 ranges.add(new int[] { 2, 3 });
421 ranges.add(new int[] { 5, 6 });
422 assertEquals("[2, 3, 5, 6]", Arrays.toString(MapList.getRanges(ranges)));
426 * Check state after construction
428 @Test(groups = { "Functional" })
429 public void testConstructor()
431 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
432 int[] protein = { 1, 1, 3, 4, 6, 6 };
433 MapList ml = new MapList(codons, protein, 3, 1);
434 assertEquals(3, ml.getFromRatio());
435 assertEquals(2, ml.getFromLowest());
436 assertEquals(18, ml.getFromHighest());
437 assertEquals(1, ml.getToLowest());
438 assertEquals(6, ml.getToHighest());
439 assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
440 prettyPrint(ml.getFromRanges()));
441 assertEquals("{[1, 1], [3, 4], [6, 6]}", prettyPrint(ml.getToRanges()));
444 * Also copy constructor
446 MapList ml2 = new MapList(ml);
447 assertEquals(3, ml2.getFromRatio());
448 assertEquals(2, ml2.getFromLowest());
449 assertEquals(18, ml2.getFromHighest());
450 assertEquals(1, ml2.getToLowest());
451 assertEquals(6, ml2.getToHighest());
452 assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
453 prettyPrint(ml2.getFromRanges()));
454 assertEquals("{[1, 1], [3, 4], [6, 6]}", prettyPrint(ml2.getToRanges()));
459 codons = new int[] { 9, 6 };
460 protein = new int[] { 100, 91, 80, 79 };
461 ml = new MapList(codons, protein, 3, 1);
462 assertEquals(6, ml.getFromLowest());
463 assertEquals(9, ml.getFromHighest());
464 assertEquals(79, ml.getToLowest());
465 assertEquals(100, ml.getToHighest());
469 * Test constructor can merge consecutive ranges
471 @Test(groups = { "Functional" })
472 public void testConstructor_mergeRanges()
474 int[] codons = { 2, 3, 3, 7, 9, 10, 12, 12, 14, 14, 16, 17 };
475 int[] protein = { 1, 1, 1, 3, 6, 6 };
476 MapList ml = new MapList(codons, protein, 3, 1);
477 assertEquals(3, ml.getFromRatio());
478 assertEquals(2, ml.getFromLowest());
479 assertEquals(17, ml.getFromHighest());
480 assertEquals(1, ml.getToLowest());
481 assertEquals(6, ml.getToHighest());
482 assertEquals("{[2, 7], [9, 10], [12, 12], [14, 14], [16, 17]}",
483 prettyPrint(ml.getFromRanges()));
484 assertEquals("{[1, 3], [6, 6]}", prettyPrint(ml.getToRanges()));
488 * Convert a List of {[i, j], [k, l], ...} to "[[i, j], [k, l], ...]"
493 private String prettyPrint(List<int[]> ranges)
495 StringBuilder sb = new StringBuilder(ranges.size() * 5);
496 boolean first = true;
498 for (int[] range : ranges)
504 sb.append(Arrays.toString(range));
508 return sb.toString();
512 * Test the method that creates an inverse mapping
514 @Test(groups = { "Functional" })
515 public void testGetInverse()
517 int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
518 int[] protein = { 1, 1, 3, 4, 6, 6 };
520 MapList ml = new MapList(codons, protein, 3, 1);
521 MapList ml2 = ml.getInverse();
522 assertEquals(ml.getFromRatio(), ml2.getToRatio());
523 assertEquals(ml.getFromRatio(), ml2.getToRatio());
524 assertEquals(ml.getToHighest(), ml2.getFromHighest());
525 assertEquals(ml.getFromHighest(), ml2.getToHighest());
526 assertEquals(prettyPrint(ml.getFromRanges()),
527 prettyPrint(ml2.getToRanges()));
528 assertEquals(prettyPrint(ml.getToRanges()),
529 prettyPrint(ml2.getFromRanges()));
532 @Test(groups = { "Functional" })
533 public void testToString()
535 MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 }, new int[] {
537 String s = ml.toString();
538 assertEquals("[ [1, 5] [10, 15] [25, 20] ] To [ [51, 1] ]",
542 @Test(groups = { "Functional" })
543 public void testAddMapList()
545 MapList ml = new MapList(new int[] { 11, 15, 20, 25, 35, 30 },
546 new int[] { 72, 22 }, 1, 3);
547 assertEquals(11, ml.getFromLowest());
548 assertEquals(35, ml.getFromHighest());
549 assertEquals(22, ml.getToLowest());
550 assertEquals(72, ml.getToHighest());
552 MapList ml2 = new MapList(new int[] { 2, 4, 37, 40 }, new int[] { 12,
553 17, 78, 83, 88, 96 }, 1, 3);
555 assertEquals(2, ml.getFromLowest());
556 assertEquals(40, ml.getFromHighest());
557 assertEquals(12, ml.getToLowest());
558 assertEquals(96, ml.getToHighest());
560 String s = ml.toString();
562 "[ [11, 15] [20, 25] [35, 30] [2, 4] [37, 40] ] To [ [72, 22] [12, 17] [78, 83] [88, 96] ]",
566 @Test(groups = { "Functional" })
567 public void testAddMapList_contiguous()
569 MapList ml = new MapList(new int[] { 11, 15 }, new int[] { 72, 58 }, 1,
572 MapList ml2 = new MapList(new int[] { 15, 16 }, new int[] { 58, 53 },
575 assertEquals("[ [11, 16] ] To [ [72, 53] ]", ml.toString());
578 @Test(groups = "Functional")
579 public void testAddRange()
581 int[] range = { 1, 5 };
582 List<int[]> ranges = new ArrayList<int[]>();
584 // add to empty list:
585 MapList.addRange(range, ranges);
586 assertEquals(1, ranges.size());
587 assertSame(range, ranges.get(0));
589 // extend contiguous (same position):
590 MapList.addRange(new int[] { 5, 10 }, ranges);
591 assertEquals(1, ranges.size());
592 assertEquals(1, ranges.get(0)[0]);
593 assertEquals(10, ranges.get(0)[1]);
595 // extend contiguous (next position):
596 MapList.addRange(new int[] { 11, 15 }, ranges);
597 assertEquals(1, ranges.size());
598 assertEquals(1, ranges.get(0)[0]);
599 assertEquals(15, ranges.get(0)[1]);
601 // change direction: range is not merged:
602 MapList.addRange(new int[] { 16, 10 }, ranges);
603 assertEquals(2, ranges.size());
604 assertEquals(16, ranges.get(1)[0]);
605 assertEquals(10, ranges.get(1)[1]);
607 // extend reverse contiguous (same position):
608 MapList.addRange(new int[] { 10, 8 }, ranges);
609 assertEquals(2, ranges.size());
610 assertEquals(16, ranges.get(1)[0]);
611 assertEquals(8, ranges.get(1)[1]);
613 // extend reverse contiguous (next position):
614 MapList.addRange(new int[] { 7, 6 }, ranges);
615 assertEquals(2, ranges.size());
616 assertEquals(16, ranges.get(1)[0]);
617 assertEquals(6, ranges.get(1)[1]);
619 // change direction: range is not merged:
620 MapList.addRange(new int[] { 6, 9 }, ranges);
621 assertEquals(3, ranges.size());
622 assertEquals(6, ranges.get(2)[0]);
623 assertEquals(9, ranges.get(2)[1]);
625 // not contiguous: not merged
626 MapList.addRange(new int[] { 11, 12 }, ranges);
627 assertEquals(4, ranges.size());
628 assertEquals(11, ranges.get(3)[0]);
629 assertEquals(12, ranges.get(3)[1]);
633 * Check state after construction
635 @Test(groups = { "Functional" })
636 public void testConstructor_withLists()
641 int[][] codons = new int[][] { { 9, 6 } };
642 int[][] protein = new int[][] { { 100, 91 }, { 80, 79 } };
643 MapList ml = new MapList(Arrays.asList(codons), Arrays.asList(protein),
645 assertEquals(6, ml.getFromLowest());
646 assertEquals(9, ml.getFromHighest());
647 assertEquals(79, ml.getToLowest());
648 assertEquals(100, ml.getToHighest());
652 * Test that method that inspects for the (first) forward or reverse from
653 * range. Single position ranges are ignored.
655 @Test(groups = { "Functional" })
656 public void testIsFromForwardStrand()
658 // [3-9] declares forward strand
659 MapList ml = new MapList(new int[] { 2, 2, 3, 9, 12, 11 },
660 new int[] { 20, 11 }, 1, 1);
661 assertTrue(ml.isFromForwardStrand());
663 // [11-5] declares reverse strand ([13-14] is ignored)
664 ml = new MapList(new int[] { 2, 2, 11, 5, 13, 14 },
665 new int[] { 20, 11 }, 1, 1);
666 assertFalse(ml.isFromForwardStrand());
668 // all single position ranges - defaults to forward strand
669 ml = new MapList(new int[] { 2, 2, 4, 4, 6, 6 }, new int[] { 3, 1 }, 1,
671 assertTrue(ml.isFromForwardStrand());
675 * Test the method that merges a list of ranges where possible
677 @Test(groups = { "Functional" })
678 public void testCoalesceRanges()
680 assertNull(MapList.coalesceRanges(null));
681 List<int[]> ranges = new ArrayList<int[]>();
682 assertSame(ranges, MapList.coalesceRanges(ranges));
683 ranges.add(new int[] { 1, 3 });
684 assertSame(ranges, MapList.coalesceRanges(ranges));
686 // add non-contiguous range:
687 ranges.add(new int[] { 5, 6 });
688 assertSame(ranges, MapList.coalesceRanges(ranges));
690 // 'contiguous' range in opposite direction is not merged:
691 ranges.add(new int[] { 7, 6 });
692 assertSame(ranges, MapList.coalesceRanges(ranges));
694 // merging in forward direction:
696 ranges.add(new int[] { 1, 3 });
697 ranges.add(new int[] { 4, 5 });
698 ranges.add(new int[] { 5, 5 });
699 ranges.add(new int[] { 5, 7 });
700 List<int[]> merged = MapList.coalesceRanges(ranges);
701 assertEquals(1, merged.size());
702 assertArrayEquals(new int[] { 1, 7 }, merged.get(0));
703 // verify input list is unchanged
704 assertEquals(4, ranges.size());
705 assertArrayEquals(new int[] { 1, 3 }, ranges.get(0));
706 assertArrayEquals(new int[] { 4, 5 }, ranges.get(1));
707 assertArrayEquals(new int[] { 5, 5 }, ranges.get(2));
708 assertArrayEquals(new int[] { 5, 7 }, ranges.get(3));
710 // merging in reverse direction:
712 ranges.add(new int[] { 7, 5 });
713 ranges.add(new int[] { 5, 4 });
714 ranges.add(new int[] { 4, 4 });
715 ranges.add(new int[] { 3, 1 });
716 merged = MapList.coalesceRanges(ranges);
717 assertEquals(1, merged.size());
718 assertArrayEquals(new int[] { 7, 1 }, merged.get(0));
720 // merging with switches of direction:
722 ranges.add(new int[] { 1, 3 });
723 ranges.add(new int[] { 4, 5 });
724 ranges.add(new int[] { 5, 5 });
725 ranges.add(new int[] { 6, 6 });
726 ranges.add(new int[] { 12, 10 });
727 ranges.add(new int[] { 9, 8 });
728 ranges.add(new int[] { 8, 8 });
729 ranges.add(new int[] { 7, 7 });
730 merged = MapList.coalesceRanges(ranges);
731 assertEquals(2, merged.size());
732 assertArrayEquals(new int[] { 1, 6 }, merged.get(0));
733 assertArrayEquals(new int[] { 12, 7 }, merged.get(1));
737 * Test the method that merges a list of ranges where possible
739 @Test(groups = { "Functional" })
740 public void testCoalesceRanges_withOverlap()
742 List<int[]> ranges = new ArrayList<int[]>();
743 ranges.add(new int[] { 1, 3 });
744 ranges.add(new int[] { 2, 5 });
747 * [2, 5] should extend [1, 3]
749 List<int[]> merged = MapList.coalesceRanges(ranges);
750 assertEquals(1, merged.size());
751 assertArrayEquals(new int[] { 1, 5 }, merged.get(0));
754 * a subsumed interval should be dropped
757 ranges.add(new int[] { 1, 6 });
758 ranges.add(new int[] { 2, 4 });
759 merged = MapList.coalesceRanges(ranges);
760 assertEquals(1, merged.size());
761 assertArrayEquals(new int[] { 1, 6 }, merged.get(0));
764 ranges.add(new int[] { 1, 5 });
765 ranges.add(new int[] { 1, 6 });
766 merged = MapList.coalesceRanges(ranges);
767 assertEquals(1, merged.size());
768 assertArrayEquals(new int[] { 1, 6 }, merged.get(0));
771 * merge duplicate ranges
774 ranges.add(new int[] { 1, 3 });
775 ranges.add(new int[] { 1, 3 });
776 merged = MapList.coalesceRanges(ranges);
777 assertEquals(1, merged.size());
778 assertArrayEquals(new int[] { 1, 3 }, merged.get(0));
784 ranges.add(new int[] { 9, 5 });
785 ranges.add(new int[] { 9, 4 });
786 ranges.add(new int[] { 8, 3 });
787 ranges.add(new int[] { 3, 2 });
788 ranges.add(new int[] { 1, 0 });
789 merged = MapList.coalesceRanges(ranges);
790 assertEquals(1, merged.size());
791 assertArrayEquals(new int[] { 9, 0 }, merged.get(0));