Skip to content

Commit 2fb9fcf

Browse files
committed
tweaks in response to NthPortal review
1 parent bac0f5e commit 2fb9fcf

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

src/main/scala/scala/collection/immutable/next/package.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ package object next {
3737
// `LazyList.empty #:::` accomplishes that delay
3838
else LazyList.empty #::: {
3939
// case 2: the input is later discovered to be empty
40-
// (distinguishing this from case 3 isn't necessary for correctness, but we
41-
// might as well avoid the lazy val path if we can)
4240
if (ll.isEmpty) LazyList.empty
4341
else {
4442
// case 3: non-empty

src/test/scala/scala/collection/immutable/TestLazyListExtensions.scala

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,41 @@ import next._
2020
class TestLazyListExtensions {
2121

2222
// This method will *not* terminate for non-cyclic infinite-sized collections.
23+
// (It's kind of nasty to have tests whose failure mode is to hang, but I don't
24+
// see an obvious alternative that doesn't involve copying code from LazyList.
25+
// Perhaps this could be improved at the time this all gets merged into stdlib.)
2326
def assertConstantMemory[T](xs: LazyList[T]): Unit =
2427
// `force` does cycle detection, so if this terminates, the collection is
2528
// either finite or a cycle
2629
xs.force
2730

2831
@Test
29-
def testEmpty1(): Unit = {
32+
def cycleEmpty1(): Unit = {
3033
val xs = LazyList.empty // realized
3134
val cyc = xs.cycle
3235
assertTrue(cyc.isEmpty)
3336
assertTrue(cyc.size == 0)
3437
assertEquals(Nil, cyc.toList)
3538
}
3639
@Test
37-
def testEmpty2(): Unit = {
38-
val xs = LazyList() // not realized
40+
def cycleEmpty2(): Unit = {
41+
val xs = LazyList.empty #::: LazyList.empty // not realized
42+
assertEquals(-1, xs.knownSize) // double-check it's not realized
3943
val cyc = xs.cycle
4044
assertTrue(cyc.isEmpty)
4145
assertTrue(cyc.size == 0)
4246
assertEquals(Nil, cyc.toList)
4347
}
4448
@Test
45-
def testNonEmpty(): Unit = {
49+
def cycleNonEmpty(): Unit = {
4650
val xs = LazyList(1, 2, 3)
4751
val cyc = xs.cycle
4852
assertFalse(cyc.isEmpty)
4953
assertConstantMemory(cyc)
5054
assertEquals(LazyList(1, 2, 3, 1, 2, 3, 1, 2), cyc.take(8))
5155
}
5256
@Test
53-
def testToString(): Unit = {
57+
def cycleToString(): Unit = {
5458
assertEquals("LazyList()",
5559
LazyList.empty.cycle.toString)
5660
assertEquals("LazyList(<not computed>)",
@@ -60,22 +64,22 @@ class TestLazyListExtensions {
6064
LazyList(1, 2, 3).cycle.force.toString)
6165
}
6266
@Test
63-
def testRepeats(): Unit = {
67+
def cycleRepeats(): Unit = {
6468
val xs = LazyList(1, 2, 3)
6569
val cyc = xs.cycle
6670
assertFalse(cyc.isEmpty)
6771
assertEquals(LazyList(1, 2, 3, 1, 2, 3, 1, 2), cyc.take(8))
6872
}
6973
@Test
70-
def testConstantMemory1(): Unit = {
74+
def cycleConstantMemory1(): Unit = {
7175
val xs = LazyList(1, 2, 3)
7276
val cyc = xs.cycle
7377
assertTrue(cyc.tail eq cyc.tail.tail.tail.tail)
7478
assertTrue(cyc.tail.tail eq cyc.drop(4).tail)
7579
assertTrue(cyc.tail eq cyc.drop(3).tail)
7680
}
7781
@Test
78-
def testConstantMemory2(): Unit = {
82+
def cycleConstantMemory2(): Unit = {
7983
var counter = 0
8084
def count(): Int = { counter += 1; counter }
8185
val xs = count() #:: count() #:: count() #:: LazyList.empty
@@ -85,7 +89,7 @@ class TestLazyListExtensions {
8589
assertEquals(3, counter)
8690
}
8791
@Test
88-
def testConstantMemory3(): Unit = {
92+
def cycleConstantMemory3(): Unit = {
8993
val xs = LazyList(1, 2, 3)
9094
val cyc = xs.cycle
9195
assertConstantMemory(cyc)
@@ -97,19 +101,20 @@ class TestLazyListExtensions {
97101
assertConstantMemory(cyc.drop(10))
98102
}
99103
@Test
100-
def testUnbounded(): Unit = {
104+
def cycleUnbounded(): Unit = {
101105
val xs = LazyList.from(1)
102106
val cyc = xs.cycle
103107
assertEquals(LazyList(1, 2, 3), cyc.take(3))
104108
}
105109
@Test
106-
def testSecondCallIsSafeButNotIdempotent(): Unit = {
110+
def cycleSecondCallIsSafeButNotIdempotent(): Unit = {
107111
val xs = LazyList(1, 2, 3)
108112
// this is safe to do
109113
val twice = xs.cycle.cycle
110114
// and the contents are as expected
111115
assertEquals(LazyList(1, 2, 3, 1, 2, 3, 1, 2), twice.take(8))
112-
// but the result is not a cycle. TODO do we care?
116+
// but the result is not a cycle. it might be nice if it were, but oh well.
117+
// testing the existing behavior.
113118
assertFalse(twice.tail eq twice.tail.tail.tail.tail)
114119
}
115120
}

0 commit comments

Comments
 (0)