Skip to content

Commit

Permalink
add array test
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan committed Dec 13, 2018
1 parent 57307f3 commit c192f44
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 6 deletions.
3 changes: 2 additions & 1 deletion scala/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
53 changes: 53 additions & 0 deletions scala/src/main/scala/ch05_array/ArrayDemo.scala
Original file line number Diff line number Diff line change
@@ -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
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import scala.util.control.Breaks._
package ch11_sorts

import scala.util.control.Breaks.{break, breakable}

/**
* 冒泡排序、插入排序、选择排序
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package ch15_bsearch

object BSearch {
def search(nums: Array[Int], target: Int): Int = {
var low = 0
Expand All @@ -12,7 +14,7 @@ object BSearch {
return mid
}
}

return -1
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package ch15_bsearch

object BSearchRecursive {
def search(nums: Array[Int], target: Int): Int = {
return searchInternal(nums, target, 0, nums.length - 1)
}

def searchInternal(nums:Array[Int], target: Int, low: Int, high: Int): Int = {
if(low <= high){
val mid = low + ((high - low) >> 2)
if(nums(mid) > target){
searchInternal(nums, target, low, mid - 1)
} else if (nums(mid) < target){
} else if (nums(mid) < target){
searchInternal(nums, target, mid + 1, high)
} else {
return mid
}
}else{
return -1
return -1
}
}
}
67 changes: 67 additions & 0 deletions scala/src/test/scala/ch05_array/ArrayDemoSpec.scala
Original file line number Diff line number Diff line change
@@ -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')
}
}

}

0 comments on commit c192f44

Please sign in to comment.