You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
3.1 KiB
107 lines
3.1 KiB
import org.scalatest.funsuite.AnyFunSuite
|
|
import nova.monadic_sfx.util.History
|
|
import monix.execution.atomic.Atomic
|
|
class HistoryTest extends AnyFunSuite {
|
|
val historyRef = Atomic(History(0))
|
|
|
|
test("init") {
|
|
val h = historyRef.get()
|
|
assert(h.state.values == Vector(0))
|
|
assert(h.state.sp == 0)
|
|
assert(h.current == 0)
|
|
}
|
|
|
|
test("push 1") {
|
|
val h = historyRef.transformAndGet(_.push(1))
|
|
// logger.debug(mutHistory.ints.toString)
|
|
assert(h.state.values == Vector(0, 1))
|
|
assert(h.state.values.length - 1 == h.state.sp)
|
|
assert(h.current == 1)
|
|
}
|
|
|
|
test("push 2") {
|
|
val h = historyRef.transformAndGet(_.push(2))
|
|
// logger.debug(mutHistory.ints.toString)
|
|
assert(h.state.values == Vector(0, 1, 2))
|
|
assert(h.state.values.length - 1 == h.state.sp)
|
|
assert(h.current == 2)
|
|
}
|
|
|
|
test("first forward") {
|
|
// logger.debug(mutHistory.ints.toString)
|
|
// logger.debug(mutHistory.sp.toString)
|
|
val h = historyRef.transformAndGet(_.forward)
|
|
assert(h.state.values == Vector(0, 1, 2))
|
|
assert(h.state.sp == 2)
|
|
assert(h.current == 2)
|
|
}
|
|
|
|
test("second forward") {
|
|
// logger.debug(mutHistory.ints.toString)
|
|
// logger.debug(mutHistory.sp.toString)
|
|
val h = historyRef.transformAndGet(_.forward)
|
|
assert(h.state.values == Vector(0, 1, 2))
|
|
assert(h.state.sp == 2)
|
|
assert(h.current == 2)
|
|
}
|
|
test("first backward") {
|
|
val h = historyRef.transformAndGet(_.backward)
|
|
assert(h.state.values == Vector(0, 1, 2))
|
|
assert(h.state.sp == 1)
|
|
assert(h.current == 1)
|
|
}
|
|
test("second backward") {
|
|
val h = historyRef.transformAndGet(_.backward)
|
|
assert(h.state.values == Vector(0, 1, 2))
|
|
assert(h.state.sp == 0)
|
|
assert(h.current == 0)
|
|
}
|
|
test("third backward") {
|
|
val h = historyRef.transformAndGet(_.backward)
|
|
assert(h.state.values == Vector(0, 1, 2))
|
|
assert(h.state.sp == 0)
|
|
assert(h.current == 0)
|
|
}
|
|
test("push 3") {
|
|
val h = historyRef.transformAndGet(_.push(3))
|
|
// logger.debug(mutHistory.ints.toString)
|
|
assert(h.state.values == Vector(3))
|
|
assert(h.state.sp == 0)
|
|
assert(h.current == 3)
|
|
}
|
|
test("fourth backward") {
|
|
val h = historyRef.transformAndGet(_.backward)
|
|
assert(h.state.values == Vector(3))
|
|
assert(h.state.sp == 0)
|
|
assert(h.current == 3)
|
|
}
|
|
test("lastly") {
|
|
val h1 = historyRef.transformAndGet(
|
|
_.push(4)
|
|
.push(5)
|
|
.push(6)
|
|
.push(7)
|
|
.push(8)
|
|
)
|
|
assert(h1.state.values == Vector(3, 4, 5, 6, 7, 8))
|
|
assert(h1.state.sp == 5)
|
|
assert(h1.current == 8)
|
|
val h2 = historyRef.transformAndGet(_.backward.backward)
|
|
assert(h2.current == 6)
|
|
assert(h2.state.sp == 3)
|
|
val h3 = historyRef.transformAndGet(_.push(9))
|
|
assert(h3.state.values == Vector(3, 4, 5, 9))
|
|
assert(h3.state.sp == 3)
|
|
assert(h3.current == 9)
|
|
for (i <- 1 to 4) historyRef.transform(_.backward)
|
|
// assert(h.current == None)
|
|
// assert(h.ints == Vector.empty)
|
|
val h4 = historyRef.get()
|
|
assert(h4.state.sp == 0)
|
|
assert(h4.current == 3)
|
|
val h5 = historyRef.transformAndGet(_.push(1))
|
|
assert(h5.current == 1)
|
|
assert(h5.state.sp == 0)
|
|
|
|
}
|
|
}
|