diff options
author | Rex Kerr <ichoran@gmail.com> | 2014-06-28 03:24:39 -0700 |
---|---|---|
committer | Rex Kerr <ichoran@gmail.com> | 2014-09-12 18:10:38 -0700 |
commit | 039f3e3b35f73886f79f00bc578739002fe034cc (patch) | |
tree | 817b21a6da4a466caa2a51823921094857b53225 /test | |
parent | 5046f7bc99b78447dfa34c891b2b413ca47bb8f9 (diff) | |
download | scala-039f3e3b35f73886f79f00bc578739002fe034cc.tar.gz scala-039f3e3b35f73886f79f00bc578739002fe034cc.tar.bz2 scala-039f3e3b35f73886f79f00bc578739002fe034cc.zip |
SI-8680 Stream.addString is too eager
Used the standard method of sending out two iterators, one twice as fast as the others, to avoid hanging on .force, .hasDefiniteSize, and .addString.
.addString appends a "..." as the last element if it detects a cycle. It knows how to print the cycle length, but there's no good way to specify what you want right now, so it's not used.
Added tests in t8680 that verify that cyclic streams give the expected results.
Added to whitelist names of methods formerly used for recursion (now looping).
Diffstat (limited to 'test')
-rw-r--r-- | test/files/run/t8680.scala | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/test/files/run/t8680.scala b/test/files/run/t8680.scala new file mode 100644 index 0000000000..2bce09c507 --- /dev/null +++ b/test/files/run/t8680.scala @@ -0,0 +1,53 @@ +object Test extends App { + def pre(n: Int) = (-n to -1).toStream + + def cyc(m: Int) = { + lazy val s: Stream[Int] = (0 until m).toStream #::: s + s + } + + def precyc(n: Int, m: Int) = pre(n) #::: cyc(m) + + def str(s: Stream[Int]) = { + val b = new StringBuilder + s.addString(b, "", "", "") + b.toString + } + + def goal(n: Int, m: Int) = (-n until m).mkString + "..." + + // Check un-forced cyclic and non-cyclic streams + assert(str(pre(2)) == pre(2).take(1).toList.mkString + "?") + assert(str(cyc(2)) == cyc(2).take(1).toList.mkString + "?") + assert(str(precyc(2,2)) == precyc(2,2).take(1).toList.mkString + "?") + assert(!pre(2).hasDefiniteSize) + assert(!cyc(2).hasDefiniteSize) + assert(!precyc(2,2).hasDefiniteSize) + + // Check forced cyclic and non-cyclic streams + assert(str(pre(2).force) == (-2 to -1).mkString) + assert(str(cyc(2).force) == (0 until 2).mkString + "...") + assert(str(precyc(2,2).force) == (-2 until 2).mkString + "...") + assert(pre(2).force.hasDefiniteSize) + assert(!cyc(2).force.hasDefiniteSize) + assert(!precyc(2,2).force.hasDefiniteSize) + + // Special cases + assert(str(cyc(1).force) == goal(0,1)) + assert(str(precyc(1,6).force) == goal(1,6)) + assert(str(precyc(6,1).force) == goal(6,1)) + + // Make sure there are no odd/even problems + for (n <- 3 to 4; m <- 3 to 4) { + assert(precyc(n,m).mkString == goal(n,m), s"mkString $n $m") + assert(!precyc(n,m).force.hasDefiniteSize, s"hasDef $n$m") + } + + // Make sure there are no cycle/prefix modulus problems + for (i <- 6 to 8) { + assert(precyc(i,3).mkString == goal(i,3), s"mkString $i 3") + assert(precyc(3,i).mkString == goal(3,i), s"mkString 3 $i") + assert(!precyc(i,3).force.hasDefiniteSize, s"hasDef $i 3") + assert(!precyc(3,i).force.hasDefiniteSize, s"hasDef 3 $i") + } +} |