-
-
Notifications
You must be signed in to change notification settings - Fork 400
Expand file tree
/
Copy pathBinarySearchSpec.scala
More file actions
48 lines (41 loc) · 1.68 KB
/
BinarySearchSpec.scala
File metadata and controls
48 lines (41 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package Search
import org.scalatest.FlatSpec
class BinarySearchSpec extends FlatSpec {
"A Binary Search" should "return the index of an element in an array" in {
val l = List.range(1,10)
assert(BinarySearch.binarySearch(l,2) === 1)
assert(BinarySearch.binarySearch(l,5) === 4)
}
it should "return -1 if the element is not present in the list" in {
val l = List.range(1,5)
val m = List(1,3,5,7)
assert(BinarySearch.binarySearch(l,7) === -1)
assert(BinarySearch.binarySearch(l,0) === -1)
assert(BinarySearch.binarySearch(m,2) === -1)
assert(BinarySearch.binarySearch(m,6) === -1)
assert(BinarySearch.binarySearch(m,0) === -1)
}
"A Binary Search With Range" should "return the index of an element in an array" in {
val l = List.range(1,10)
assert(BinarySearch.binarySearch(l,2,0,5) === 1)
assert(BinarySearch.binarySearch(l,5,3,6) === 4)
assert(BinarySearch.binarySearch(l,2,6,9) === -1)
assert(BinarySearch.binarySearch(l,5,1,3) === -1)
}
it should "return -1 if the element is not present in the list" in {
val l = List.range(1,5)
assert(BinarySearch.binarySearch(l,7,0,4) === -1)
assert(BinarySearch.binarySearch(l,7,1,3) === -1)
}
"An Unbounded Binary Search With Range" should "return the index of an element in an array" in {
val l = Stream.range(1,100)
assert(BinarySearch.unboundedBinarySearch(l,2) === 1)
assert(BinarySearch.unboundedBinarySearch(l,5) === 4)
}
"An Unbounded Binary Search" should "fail if you are finding corner element and array is not infinite" in {
val l = Stream.range(1,100)
intercept[IndexOutOfBoundsException](
BinarySearch.unboundedBinarySearch(l, 99)
)
}
}