-
-
Notifications
You must be signed in to change notification settings - Fork 399
/
Copy pathBinarySearchSpec.scala
47 lines (41 loc) · 1.78 KB
/
BinarySearchSpec.scala
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
package Search
import org.scalatest.flatspec.AnyFlatSpec
class BinarySearchSpec extends AnyFlatSpec {
"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)
}
"A Binary Search 1" 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)
}
"A lower Bound" should "return the index of first appearance element" in {
val m = List(1, 3, 5, 5, 5, 9, 9, 13, 13, 20)
assert(BinarySearch.lowerBound(m, 5) === 2)
assert(BinarySearch.lowerBound(m, 9) === 5)
}
"A lower bound" should "return the index of number larger than an element and which is smallest" in {
val m = List(1, 3, 5, 5, 5, 9, 9, 13, 13, 20)
assert(BinarySearch.lowerBound(m, 4) === 2)
assert(BinarySearch.lowerBound(m, 8) === 5)
assert(BinarySearch.lowerBound(m, 10) === 7)
}
}