Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
takayahilton committed Jul 5, 2020
1 parent 90fde63 commit 0a5996a
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tests/src/test/scala/cats/tests/ReducibleSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import cats.syntax.option._
import cats.syntax.reducible._
import org.scalacheck.Arbitrary

import scala.collection.mutable

class ReducibleSuiteAdditional extends CatsSuite {

test("Reducible[NonEmptyList].reduceLeftM stack safety") {
Expand All @@ -22,6 +24,12 @@ class ReducibleSuiteAdditional extends CatsSuite {
actual should ===(Some(expected))
}

test("Reducible[NonEmptyList].reduceRightTo stack safety") {
val n = 100000L
val actual = (1L to n).toList.toNel.get.reduceRightTo(identity) { case (a, r) => r.map(_ + a) }.value
actual should ===((1L to n).sum)
}

// exists method written in terms of reduceRightTo
def contains[F[_]: Reducible, A: Eq](as: F[A], goal: A): Eval[Boolean] =
as.reduceRightTo(_ === goal) { (a, lb) =>
Expand Down Expand Up @@ -76,6 +84,15 @@ class ReducibleSuiteAdditional extends CatsSuite {
assert(contains(large, 10000).value)
}

test("Reducible[NonEmptyList].reduceMapA can breakout") {
val notAllEven = NonEmptyList.of(2, 4, 6, 9, 10, 12, 14)
val out = mutable.ListBuffer[Int]()

notAllEven.reduceMapA { a => out += a; if (a % 2 == 0) Some(a) else None }

out.toList should ===(List(2, 4, 6, 9))
}

// A simple non-empty stream with lazy `foldRight` and `reduceRightTo` implementations.
case class NES[A](h: A, t: Stream[A]) {
def toStream: Stream[A] = h #:: t
Expand Down

0 comments on commit 0a5996a

Please sign in to comment.