/
SkippableLociIteratorSuite.scala
119 lines (108 loc) · 2.38 KB
/
SkippableLociIteratorSuite.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package org.hammerlab.genomics.loci.iterator
import hammerlab.test.Suite
import org.hammerlab.genomics.reference.test.IntervalsUtil
import org.hammerlab.genomics.reference.test.LociConversions._
import org.hammerlab.genomics.reference.{ Interval, Locus }
class SkippableLociIteratorSuite
extends Suite
with IntervalsUtil {
def strs =
TestSkippableLociIterator(
10 → "a",
11 → "b",
20 → "c",
21 → "d",
30 → "e",
31 → "f",
33 → "g",
34 → "h",
40 → "i",
50 → "j"
)
test("no skips") {
==(
strs.toSeq,
List(
10 → "a",
11 → "b",
20 → "c",
21 → "d",
30 → "e",
31 → "f",
33 → "g",
34 → "h",
40 → "i",
50 → "j"
)
)
}
test("skip all") {
val it = strs
it.skipTo(51)
it.toList should be(Nil)
}
test("misc skips") {
val it = strs
it.skipTo(15)
==(it.next(), 20 → "c")
it.skipTo(30)
==(it.next(), 30 → "e")
intercept[IllegalArgumentException] {
it.skipTo(30)
}
==(it.next(), 31 → "f")
it.skipTo(32)
==(it.next(), 33 → "g")
it.skipTo(34)
==(it.next(), 34 → "h")
it.skipTo(41)
==(it.next(), 50 → "j")
==(it.hasNext, false)
}
test("intersect") {
==(
strs.intersect(
new LociIterator(
Iterator(
Interval( 8, 11),
Interval(14, 16),
Interval(30, 35),
Interval(38, 42),
Interval(50, 51)
).buffered
)
).toSeq,
Seq(
10 → "a",
30 → "e",
31 → "f",
33 → "g",
34 → "h",
40 → "i",
50 → "j"
)
)
}
}
case class TestSkippableLociIterator(elems: (Int, String)*)
extends SkippableLocusKeyedIterator[String] {
val it = elems.iterator.map(t => (t._1: Locus) → t._2).buffered
override def _advance: Option[(Locus, String)] = {
if (!it.hasNext)
None
else {
val (nextLocus, str) = it.next
if (nextLocus < locus)
_advance
else {
locus = nextLocus
Some(nextLocus → str)
}
}
}
override def skipTo(newLocus: Locus): TestSkippableLociIterator.this.type = {
super.skipTo(newLocus)
while (it.hasNext && locusFn(it.head) < locus) it.next()
this
}
}