@@ -20,37 +20,41 @@ import next._
20
20
class TestLazyListExtensions {
21
21
22
22
// 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.)
23
26
def assertConstantMemory [T ](xs : LazyList [T ]): Unit =
24
27
// `force` does cycle detection, so if this terminates, the collection is
25
28
// either finite or a cycle
26
29
xs.force
27
30
28
31
@ Test
29
- def testEmpty1 (): Unit = {
32
+ def cycleEmpty1 (): Unit = {
30
33
val xs = LazyList .empty // realized
31
34
val cyc = xs.cycle
32
35
assertTrue(cyc.isEmpty)
33
36
assertTrue(cyc.size == 0 )
34
37
assertEquals(Nil , cyc.toList)
35
38
}
36
39
@ 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
39
43
val cyc = xs.cycle
40
44
assertTrue(cyc.isEmpty)
41
45
assertTrue(cyc.size == 0 )
42
46
assertEquals(Nil , cyc.toList)
43
47
}
44
48
@ Test
45
- def testNonEmpty (): Unit = {
49
+ def cycleNonEmpty (): Unit = {
46
50
val xs = LazyList (1 , 2 , 3 )
47
51
val cyc = xs.cycle
48
52
assertFalse(cyc.isEmpty)
49
53
assertConstantMemory(cyc)
50
54
assertEquals(LazyList (1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 ), cyc.take(8 ))
51
55
}
52
56
@ Test
53
- def testToString (): Unit = {
57
+ def cycleToString (): Unit = {
54
58
assertEquals(" LazyList()" ,
55
59
LazyList .empty.cycle.toString)
56
60
assertEquals(" LazyList(<not computed>)" ,
@@ -60,22 +64,22 @@ class TestLazyListExtensions {
60
64
LazyList (1 , 2 , 3 ).cycle.force.toString)
61
65
}
62
66
@ Test
63
- def testRepeats (): Unit = {
67
+ def cycleRepeats (): Unit = {
64
68
val xs = LazyList (1 , 2 , 3 )
65
69
val cyc = xs.cycle
66
70
assertFalse(cyc.isEmpty)
67
71
assertEquals(LazyList (1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 ), cyc.take(8 ))
68
72
}
69
73
@ Test
70
- def testConstantMemory1 (): Unit = {
74
+ def cycleConstantMemory1 (): Unit = {
71
75
val xs = LazyList (1 , 2 , 3 )
72
76
val cyc = xs.cycle
73
77
assertTrue(cyc.tail eq cyc.tail.tail.tail.tail)
74
78
assertTrue(cyc.tail.tail eq cyc.drop(4 ).tail)
75
79
assertTrue(cyc.tail eq cyc.drop(3 ).tail)
76
80
}
77
81
@ Test
78
- def testConstantMemory2 (): Unit = {
82
+ def cycleConstantMemory2 (): Unit = {
79
83
var counter = 0
80
84
def count (): Int = { counter += 1 ; counter }
81
85
val xs = count() #:: count() #:: count() #:: LazyList .empty
@@ -85,7 +89,7 @@ class TestLazyListExtensions {
85
89
assertEquals(3 , counter)
86
90
}
87
91
@ Test
88
- def testConstantMemory3 (): Unit = {
92
+ def cycleConstantMemory3 (): Unit = {
89
93
val xs = LazyList (1 , 2 , 3 )
90
94
val cyc = xs.cycle
91
95
assertConstantMemory(cyc)
@@ -97,19 +101,20 @@ class TestLazyListExtensions {
97
101
assertConstantMemory(cyc.drop(10 ))
98
102
}
99
103
@ Test
100
- def testUnbounded (): Unit = {
104
+ def cycleUnbounded (): Unit = {
101
105
val xs = LazyList .from(1 )
102
106
val cyc = xs.cycle
103
107
assertEquals(LazyList (1 , 2 , 3 ), cyc.take(3 ))
104
108
}
105
109
@ Test
106
- def testSecondCallIsSafeButNotIdempotent (): Unit = {
110
+ def cycleSecondCallIsSafeButNotIdempotent (): Unit = {
107
111
val xs = LazyList (1 , 2 , 3 )
108
112
// this is safe to do
109
113
val twice = xs.cycle.cycle
110
114
// and the contents are as expected
111
115
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.
113
118
assertFalse(twice.tail eq twice.tail.tail.tail.tail)
114
119
}
115
120
}
0 commit comments