Test if a function is called in a given scope in scala - unit-testing

I'm following the scala course in coursera and I was asked to implement set operations in the last exercise. One of the test I failed was called
exists should be implemented in terms of forall
Both exists and forall signatures are :
type Set = Int => Boolean
def forall(s: Set, p: Int => Boolean): Boolean = {}
def exists(s: Set, p: Int => Boolean): Boolean = {
/*should eventually call forall */
}
I am not asking for the implementation, but how to perform such a unit test in scala

There are three methods that I can think of to start with:
1) Mock forall to throw a particular exception, and then call exists, and see if it throws that exception.
2) Instrument the code and call exists, and test afterwards to see if forall was called
3) Use a scala macro, which analyses the AST for exists and checks recursively to see if it calls forall.

This is fairly easy done with mock objects. I'm using Mockito in my Java projects, and it is pretty usable from Scala too, especially together with Scalatest.
Put this code to project_dir/build.sbt:
scalaVersion := "2.10.2"
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "2.0.M8",
"org.mockito" % "mockito-core" % "1.9.5"
)
Then put this code to project_dir/src/main/test/test.scala:
import org.scalatest.{FlatSpec,ShouldMatchers}
import org.scalatest.mock.MockitoSugar
package object test {
type Set = Int => Boolean
}
package test {
class Foraller {
def forall(s: Set, p: Int => Boolean): Boolean = ???
}
class Exister(foraller: Foraller) {
def exists(s: Set, p: Int => Boolean): Boolean = ??? // Fails
// def exists(s: Set, p: Int => Boolean): Boolean = foraller.forall(s, p) // Passes
}
class Test extends FlatSpec with ShouldMatchers with MockitoSugar {
"Exister" should "use Foraller in its exists method" in {
val foraller = mock[Foraller]
val exister = new Exister(foraller)
val set: Set = _ == 1
val pred: Int => Boolean = _ > 0
exister.exists(set, pred)
import org.mockito.Mockito._
verify(foraller).forall(set, pred)
}
}
}
Then invoke sbt test command in project_dir. You should see that the test has failed. Switch comments on lines in Exister class and try again.
Here we create mock object for the class which provides forall method and we use this object inside a class which provides exists method. Mockito allows verifying whether some method on some mock object is called, and this is what is working here.

Related

Function to determine membership in a list

I need to write a program which contains the function
doesExist: [A](p:A => Boolean) (xs: List[A]) Boolean.
e.g. doesExist ((x:Int) => x==2) (List(5,1,2,3)) => true
It should just return true or false depending on whether the element is found in the list.
I guess that the gist of it is to not use the built in function of lists... So here's an alternative using foldLeft:
def doesExist[A](p: A => Boolean)(xs: List[A]): Boolean = {
xs.foldLeft(false)((acc, elem) => acc || p(elem))
}
It could be a good exercise to practice pattern-matching:
def doesExist[A](p:A => Boolean)(xs: List[A]): Boolean = xs match {
case Nil => false
case head :: tail => p(head) || doesExist(p)(tail)
}
Simply:
def doesExist[A](p:A => Boolean)(xs: List[A]) = xs.exists(p)
If you cannot use exists:
def doesExist[A](p:A => Boolean)(xs: List[A]) =
xs.fold(false)((alreadyFound, a) => alreadyFound || p(a))
Here's how the Scala 2.11.8 standard library does it.
From LinearSeqOptimized.scala:
override /*IterableLike*/
def exists(p: A => Boolean): Boolean = {
var these = this // <--- Look, a var!
while (!these.isEmpty) {
if (p(these.head)) return true // <--- Look, a return!
these = these.tail
}
false
}
In other words, Scala uses a while loop, which mutates a var that points to the next object, and breaks out of the loop when it reaches the desired object.
I found out, when comparing answers to a similar question, that while with some sort of mutating iteration variable is the most efficient way to write this in Scala. Pure-functional approaches like recursion, Streams, etc. are slower—as much as 8 times slower (and still without scanning beyond the first 'true' element). I gather that the best approach is to use some kind of mutable iterator when you need to do a linear search and hide it inside a function that presents a pure-functional interface to the rest of the program. The Scala standard library does that in many places for you, so you get the best possible performance with a pure-functional interface.

Property Based Testing with Exceptions

I am writing an introduction to Haskell for my local functional programming group.
As a base I am using the Tasty-testing framework and I want to test the indexing function (!!).
MinimalExample.hs
module MinimalExample where
myIndex :: Int -> [a] -> a
myIndex _ [] = error "index too large"
myIndex 0 (x:_) = x
myIndex n (_:xs) = myIndex (n-1) xs
MinimalTests.hs
module MinimalTests where
import Test.Tasty
import Test.Tasty.SmallCheck as SC
import Test.SmallCheck.Series
import MinimalExample
main :: IO ()
main = defaultMain tests
tests :: TestTree
tests = testGroup "Tests" [scProps]
scProps :: TestTree
scProps = testGroup "(checked by SmallCheck)"
[ SC.testProperty "(!!) == myIndex" $ \lst n ->
lst !! n == myIndex (n::Int) (lst::[Int])
]
The tests should not fail on "too large indices" as the Errors/Exceptions are the same.
The tests should fail with negative input - which could be resolved by adding NonNegative as a constraint on the input, or adding the respective clause in the myIndex-function.
Can I test for exceptions in property based tests?
Or do I have to use (H)Unit-tests like in How do I test for an error in Haskell? or Haskell exceptions and unit testing , in this case how do I choose the Index in the range of 0 to length of the generated testing list.
You could use the spoon package, or write a similar function that would return the exception if you want to test that the exceptions are the same.

Filter usage in shapeless, Scala

It is easy to filter HList in shapeless by type:
val hlist = 1 :: 2 :: "3" :: true :: false :: HNil
hlist.filter[Int]
But how can I make my custom type filter? I want smth like that: for example I got list of some functions:
def function1(s: String) = s.toInt
def function2(s: String) = s.toDouble
def function3(i: Int) = i.toDouble
val hflist = function1 _ :: function3 _ :: function2 _ :: HNil
hflist customFilter[String] //> function1 _ :: function2 _ :: HNil
So after usage of this filter, list of functions from type String to some other type will be constructed.
I had an idea to use map for this, but it was not successfull.
EDITION
More information about my comment:
I tried to test this ideas in map:
So if i got some lists (lets operate with hlist & hflist):
object allFunction extends Poly1 {
implicit def default[T, M] =
at[T => M](t => {
object grabStringFunc extends skip {
implicit def stringFunc[A] = at[T => A](_ :: HNil)
}
println(hflist flatMap grabStringFunc) //> here we should see result, list of functions
})
hlist map allFunction
//> result of this should be smth like (types)
//> shapeless.::[Int => Double,shapeless.HNil]]
//> shapeless.::[Int => Double,shapeless.HNil]]
//> shapeless.::[String => Int,shapeless.::[String => Double,shapeless.HNil]]
//> shapeless.HNil
//> shapeless.HNil
Very interesting, why it compiles and works incorrect? As I think it is not works, cause object cant take type prameters in such a way...
The easiest way is to use a fold. First we need a polymorphic function that will add each item to the accumulator if it has the desired type (String => A for some A), and ignore it otherwise:
trait ignore extends Poly2 {
implicit def default[A, L <: HList] = at[A, L]((_, l) => l)
}
object keepStringFunc extends ignore {
implicit def stringFunc[A, L <: HList] = at[String => A, L](_ :: _)
}
Now the following will give the result you want in both 1.2.4 and 2.0.0-M1:
val filtered = hflist.foldRight(HNil)(keepStringFunc)
You could also write your own type class on the model of Filter, FilterAux (or Filter.Aux), etc.—and doing so would be a good exercise if you're trying to get the hang of Shapeless—but foldRight is a lot simpler.
Update: actually, for what it's worth, there's a slightly more concise way to do this with flatMap:
trait skip extends Poly1 {
implicit def default[A] = at[A](_ => HNil)
}
object grabStringFunc extends skip {
implicit def stringFunc[A] = at[String => A](_ :: HNil)
}
val filtered = hflist flatMap grabStringFunc
I personally find the foldRight version a little more obvious, but this one's also pretty elegant.
In response to your comment: you can make the solution a little more generic like this:
trait skip extends Poly1 {
implicit def default[A] = at[A](_ => HNil)
}
trait grabFuncFrom[T] extends skip {
implicit def stringFunc[A] = at[T => A](_ :: HNil)
}
object grabStringFunc extends grabFuncFrom[String]
val filtered = hflist flatMap grabStringFunc
But you're still going to need that last step where you create the higher rank function as an object (see e.g. this answer and Miles's comment there for some discussion of this issue).

Scala: Using Option for key/value pairs in a list

I'm trying to write the get method for a key, value pair implemented using a list. I want to use the Option type as I heard its good for this but I'm new to Scala and I'm not really sure how to use it in this case...
This is as far as I got, only the method header.
def get(key : String): Option[Any] = {}
My guess is you are looking for something like this:
class KeyValueStore(pairs: List[(String, Any)]) {
def get(key: String): Option[Any] = pairs.collectFirst {
case (k, v) if k == key => v
}
}
This uses the collectFirst method for sequences. If you want a more "do it yourself" approach, this should work:
def get(key: String): Option[Any] = {
def search(xs: List[(String, Any)]): Option[Any] = {
xs match {
case List() => None //end of list and key not found. We return None
case (k, v) :: rest if k == key => Some(v) // found our key. Returning some value
case _ :: rest => search(rest) // not found until nou. Carrying on with the rest of the list
}
search(pairs)
}
}
You can turn a List of Pairs into a Map:
class Store[K, V](values: List[(K, V)]) {
val map = values.toMap
def get(key: K): Option[V] = map get key
}
Although #Marius' collectFirst version is probably the most elegant (and maybe a little bit faster as it only uses one closure), I find it more intuitive to use find for your problem :
def get[A, B](key: A, pairs: List[(A, B)]): Option[B] = pairs.find(_._1 == key).map(_._2)
In case you were wondering (or need high performance), you will need either #Marius' recursive or the following imperative version which may look more familiar (although less idiomatic):
def get[A, B](key: A, pairs: List[(A, B)]): Option[B] = {
var l = pairs
var found: Option[B] = None
while (l.nonEmpty && found.isEmpty) {
val (k, v) = l.head
if (k == key) {
found = Some(v)
} else {
l = l.tail
}
}
found
}
What you must understand is that Option[B] is a class that may either be instantiated to None (which replaces and improves the null reference used in other languages) or Some(value: B). Some is a case class, which allows, among other neat features, to instantiate it without the new keyword (thanks to some compiler magic, Google Scala case class for more info). You can think of Option as a List which may contain either 0 or 1 element: most operations that can be done on sequences can also be applied to Options (such as map in the find version).

How should I remove the first occurrence of an object from a list in Scala?

What is the best way to remove the first occurrence of an object from a list in Scala?
Coming from Java, I'm accustomed to having a List.remove(Object o) method that removes the first occurrence of an element from a list. Now that I'm working in Scala, I would expect the method to return a new immutable List instead of mutating a given list. I might also expect the remove() method to take a predicate instead of an object. Taken together, I would expect to find a method like this:
/**
* Removes the first element of the given list that matches the given
* predicate, if any. To remove a specific object <code>x</code> from
* the list, use <code>(_ == x)</code> as the predicate.
*
* #param toRemove
* a predicate indicating which element to remove
* #return a new list with the selected object removed, or the same
* list if no objects satisfy the given predicate
*/
def removeFirst(toRemove: E => Boolean): List[E]
Of course, I can implement this method myself several different ways, but none of them jump out at me as being obviously the best. I would rather not convert my list to a Java list (or even to a Scala mutable list) and back again, although that would certainly work. I could use List.indexWhere(p: (A) ⇒ Boolean):
def removeFirst[E](list: List[E], toRemove: (E) => Boolean): List[E] = {
val i = list.indexWhere(toRemove)
if (i == -1)
list
else
list.slice(0, i) ++ list.slice(i+1, list.size)
}
However, using indices with linked lists is usually not the most efficient way to go.
I can write a more efficient method like this:
def removeFirst[T](list: List[T], toRemove: (T) => Boolean): List[T] = {
def search(toProcess: List[T], processed: List[T]): List[T] =
toProcess match {
case Nil => list
case head :: tail =>
if (toRemove(head))
processed.reverse ++ tail
else
search(tail, head :: processed)
}
search(list, Nil)
}
Still, that's not exactly succinct. It seems strange that there's not an existing method that would let me do this efficiently and succinctly. So, am I missing something, or is my last solution really as good as it gets?
You can clean up the code a bit with span.
scala> def removeFirst[T](list: List[T])(pred: (T) => Boolean): List[T] = {
| val (before, atAndAfter) = list span (x => !pred(x))
| before ::: atAndAfter.drop(1)
| }
removeFirst: [T](list: List[T])(pred: T => Boolean)List[T]
scala> removeFirst(List(1, 2, 3, 4, 3, 4)) { _ == 3 }
res1: List[Int] = List(1, 2, 4, 3, 4)
The Scala Collections API overview is a great place to learn about some of the lesser known methods.
This is a case where a little bit of mutability goes a long way:
def withoutFirst[A](xs: List[A])(p: A => Boolean) = {
var found = false
xs.filter(x => found || !p(x) || { found=true; false })
}
This is easily generalized to dropping the first n items matching the predicate. (i<1 || { i = i-1; false })
You can also write the filter yourself, though at this point you're almost certainly better off using span since this version will overflow the stack if the list is long:
def withoutFirst[A](xs: List[A])(p: A => Boolean): List[A] = xs match {
case x :: rest => if (p(x)) rest else x :: withoutFirst(rest)(p)
case _ => Nil
}
and anything else is more complicated than span without any clear benefits.