add array test

This commit is contained in:
ivan 2018-12-13 14:27:09 +08:00
parent 57307f3089
commit c192f44bd7
6 changed files with 133 additions and 6 deletions

View File

@ -2,5 +2,6 @@ lazy val root = (project in file("."))
.settings(
name := "algo-scala",
version := "1.0",
scalaVersion := "2.12.8"
scalaVersion := "2.12.8",
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % "test"
)

View File

@ -0,0 +1,53 @@
package ch05_array
class ArrayDemo(capacity: Int) {
var data: Array[Char] = new Array[Char](capacity)
var length: Int = 0
def find(index: Int): Char = {
if (index < 0 || index > length) {
return 0.toChar
}
data(index)
}
def insert(index: Int, value: Char): Boolean = {
if (length == capacity) {
return false
}
if (index < 0 || index >= capacity) {
return false
}
for (i <- length until index by -1) {
data(i) = data(i - 1)
}
data(index) = value
length += 1
true
}
def delete(index: Int): Char = {
if (length == 0) {
throw new IllegalStateException("array is empty")
}
if (index >= length) {
throw new IllegalStateException("index out of range, current data length is " + length)
}
val result = data(index)
for (i <- index until length-1) {
data(i) = data(i + 1)
}
length -= 1
result
}
def print: String = {
data.subSequence(0, length).toString
}
}

View File

@ -1,4 +1,6 @@
import scala.util.control.Breaks._
package ch11_sorts
import scala.util.control.Breaks.{break, breakable}
/**
* 冒泡排序插入排序选择排序

View File

@ -1,3 +1,5 @@
package ch15_bsearch
object BSearch {
def search(nums: Array[Int], target: Int): Int = {
var low = 0

View File

@ -1,3 +1,5 @@
package ch15_bsearch
object BSearchRecursive {
def search(nums: Array[Int], target: Int): Int = {
return searchInternal(nums, target, 0, nums.length - 1)

View File

@ -0,0 +1,67 @@
package ch05_array
import org.scalatest.{FlatSpec, Matchers}
class ArrayDemoSpec extends FlatSpec with Matchers {
behavior of "ArrayDemoTest"
it should "find value after insert" in {
val demo = new ArrayDemo(10)
assert(demo.insert(0, 'a'))
assert(demo.insert(1, 'b'))
assert(demo.insert(2, 'c'))
assert(demo.insert(3, 'd'))
assert(demo.insert(4, 'e'))
demo.print should equal("abcde")
assert(demo.insert(2, 'x'))
demo.print should equal("abxcde")
}
it should "not insert value if capacity is full " in {
val demo = new ArrayDemo(10)
for (i <- Range(0, 10)) {
demo.insert(i, (i + 97).toChar)
}
assert(!demo.insert(1, 'a'))
}
it should "not insert if index is negative" in {
val demo = new ArrayDemo(10)
assert(!demo.insert(-1, 'a'))
}
it should "not insert if index is exceed capacity" in {
val demo = new ArrayDemo(10)
assert(!demo.insert(10, 'a'))
assert(!demo.insert(11, 'a'))
}
it should "not find after delete" in {
val demo = new ArrayDemo(10)
assert(demo.insert(0, 'a'))
assert(demo.insert(1, 'b'))
assert(demo.insert(2, 'c'))
assert(demo.insert(3, 'd'))
assert(demo.insert(4, 'e'))
demo.print should equal("abcde")
assert(demo.insert(2, 'x'))
demo.print should equal("abxcde")
demo.delete(2) should equal('x')
demo.find(2) should not equal ('x')
demo.print should equal("abcde")
}
it should "not delete for empty array" in {
val demo = new ArrayDemo(10)
assertThrows[IllegalStateException] {
demo.delete(2) should equal('x')
}
}
}