/tests/src/test/scala/scalaz/MonoidTest.scala

http://github.com/scalaz/scalaz · Scala · 61 lines · 48 code · 13 blank · 0 comment · 6 complexity · 803dc24320e0fd586bd57b29fcf9b38f MD5 · raw file

  1. package scalaz
  2. import std.AllInstances._
  3. import org.scalacheck.Prop.forAll
  4. object MonoidTest extends SpecLite {
  5. "multiply" ! forAll{ (a: Int, b: Int) =>
  6. if(b <= 0) {
  7. Monoid[Int].multiply(a, b) must_=== 0
  8. } else {
  9. Monoid[Int].multiply(a, b) must_=== (a * b)
  10. }
  11. }
  12. "endo multiply" in {
  13. import syntax.monoid._
  14. def inc(i: Int) = i + 1
  15. val incTimesThree: Endo[Int] = Endo(inc).multiply(3)
  16. incTimesThree(0) must_===(3)
  17. }
  18. "endo kleisli multiply" in {
  19. import syntax.monoid._
  20. val k = Kleisli { i: Int => if (i % 2 == 0) Some(i * 2) else None }
  21. val kTimes3 = k.endo.multiply(3)
  22. kTimes3.run(2) must_=== (Some(16))
  23. }
  24. "unfold" in {
  25. val ss = std.stream.unfold(1) {
  26. case x if x < 10 => Some((x.toString, x * 2))
  27. case _ => None
  28. }
  29. ss.toList must_===(List("1", "2", "4", "8"))
  30. }
  31. "intercalate empty" in (
  32. Foldable[List].intercalate(List[String](), "oops")
  33. must_===("")
  34. )
  35. "intercalate" in {
  36. val xs = List(Vector(Cord("this"), Cord("has")), Vector(),
  37. Vector(Cord("elements")), Vector(Cord("beneath")), Vector())
  38. ((Foldable[List] compose Foldable[Vector]).intercalate(xs, Cord("!!")).toString
  39. must_===(Cord("this!!has!!elements!!beneath").toString))
  40. }
  41. "invariant functor" in {
  42. import InvariantFunctorTest._
  43. import syntax.invariantFunctor._
  44. val sg: Monoid[Num] = Monoid[Int].xmap[Num](Num.apply _, _.x)
  45. sg.append(Num(1), Num(2)) must_===(Num(3))
  46. sg.zero must_===(Num(0))
  47. }
  48. }