Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/library/scala/collection/immutable/LazyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,13 @@ final class LazyList[+A] private(private[this] var lazyState: () => LazyList.Sta
*/
@SerialVersionUID(3L)
object LazyList extends SeqFactory[LazyList] {
// Override SeqFactory methods.
override def range[A: Integral](start: A, end: A): LazyList[A] =
range(start, end, step = Integral[A].one)

override def range[A](start: A, end: A, step: A)(implicit ev: Integral[A]): LazyList[A] =
LazyList.iterate(start)(ev.plus(_, step)).takeWhile(ev.lt(_, end))
Comment thread
BalmungSan marked this conversation as resolved.
Outdated
Comment thread
BalmungSan marked this conversation as resolved.
Outdated

// Eagerly evaluate cached empty instance
private[this] val _empty = newLL(State.Empty).force

Expand Down
14 changes: 13 additions & 1 deletion test/junit/scala/collection/immutable/LazyListTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,20 @@ class LazyListTest {
LazyList(1).lazyAppendedAll({ count += 1; Seq(2)}).toList
assertEquals(1, count)
}

@Test
def lazyRangeAllowsMoreThanIntMaxValue(): Unit = {
val totalElements: Long = Int.MaxValue.toLong + 2L
val count: Long =
LazyList
.range(start = 0L, end = totalElements)
.foldLeft(0L) { case (acc, _) =>
acc + 1L
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even with inlining turned on, it's a lot to ask of a unit test. Maybe it's sufficient to test construction and head.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was wondering the same.
However, not sure if just asking for the head ensures there are more than Int.MaxValue elements.

So not sure if there is a smart way to test this without it being a CPU trap.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a reverse range we don't need to compute a lot of elements.
Let me know what you think of the new test.
C.C. @NthPortal

assertEquals(totalElements, count)
}
}

object LazyListTest {
var serializationForceCount = 0
}
}