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

3 years ago
  1. import org.scalatest.funsuite.AnyFunSuite
  2. import nova.monadic_sfx.util.History
  3. import monix.execution.atomic.Atomic
  4. class HistoryTest extends AnyFunSuite {
  5. val historyRef = Atomic(History(0))
  6. test("init") {
  7. val h = historyRef.get()
  8. assert(h.state.values == Vector(0))
  9. assert(h.state.sp == 0)
  10. assert(h.current == 0)
  11. }
  12. test("push 1") {
  13. val h = historyRef.transformAndGet(_.push(1))
  14. // logger.debug(mutHistory.ints.toString)
  15. assert(h.state.values == Vector(0, 1))
  16. assert(h.state.values.length - 1 == h.state.sp)
  17. assert(h.current == 1)
  18. }
  19. test("push 2") {
  20. val h = historyRef.transformAndGet(_.push(2))
  21. // logger.debug(mutHistory.ints.toString)
  22. assert(h.state.values == Vector(0, 1, 2))
  23. assert(h.state.values.length - 1 == h.state.sp)
  24. assert(h.current == 2)
  25. }
  26. test("first forward") {
  27. // logger.debug(mutHistory.ints.toString)
  28. // logger.debug(mutHistory.sp.toString)
  29. val h = historyRef.transformAndGet(_.forward)
  30. assert(h.state.values == Vector(0, 1, 2))
  31. assert(h.state.sp == 2)
  32. assert(h.current == 2)
  33. }
  34. test("second forward") {
  35. // logger.debug(mutHistory.ints.toString)
  36. // logger.debug(mutHistory.sp.toString)
  37. val h = historyRef.transformAndGet(_.forward)
  38. assert(h.state.values == Vector(0, 1, 2))
  39. assert(h.state.sp == 2)
  40. assert(h.current == 2)
  41. }
  42. test("first backward") {
  43. val h = historyRef.transformAndGet(_.backward)
  44. assert(h.state.values == Vector(0, 1, 2))
  45. assert(h.state.sp == 1)
  46. assert(h.current == 1)
  47. }
  48. test("second backward") {
  49. val h = historyRef.transformAndGet(_.backward)
  50. assert(h.state.values == Vector(0, 1, 2))
  51. assert(h.state.sp == 0)
  52. assert(h.current == 0)
  53. }
  54. test("third backward") {
  55. val h = historyRef.transformAndGet(_.backward)
  56. assert(h.state.values == Vector(0, 1, 2))
  57. assert(h.state.sp == 0)
  58. assert(h.current == 0)
  59. }
  60. test("push 3") {
  61. val h = historyRef.transformAndGet(_.push(3))
  62. // logger.debug(mutHistory.ints.toString)
  63. assert(h.state.values == Vector(3))
  64. assert(h.state.sp == 0)
  65. assert(h.current == 3)
  66. }
  67. test("fourth backward") {
  68. val h = historyRef.transformAndGet(_.backward)
  69. assert(h.state.values == Vector(3))
  70. assert(h.state.sp == 0)
  71. assert(h.current == 3)
  72. }
  73. test("lastly") {
  74. val h1 = historyRef.transformAndGet(
  75. _.push(4)
  76. .push(5)
  77. .push(6)
  78. .push(7)
  79. .push(8)
  80. )
  81. assert(h1.state.values == Vector(3, 4, 5, 6, 7, 8))
  82. assert(h1.state.sp == 5)
  83. assert(h1.current == 8)
  84. val h2 = historyRef.transformAndGet(_.backward.backward)
  85. assert(h2.current == 6)
  86. assert(h2.state.sp == 3)
  87. val h3 = historyRef.transformAndGet(_.push(9))
  88. assert(h3.state.values == Vector(3, 4, 5, 9))
  89. assert(h3.state.sp == 3)
  90. assert(h3.current == 9)
  91. for (i <- 1 to 4) historyRef.transform(_.backward)
  92. // assert(h.current == None)
  93. // assert(h.ints == Vector.empty)
  94. val h4 = historyRef.get()
  95. assert(h4.state.sp == 0)
  96. assert(h4.current == 3)
  97. val h5 = historyRef.transformAndGet(_.push(1))
  98. assert(h5.current == 1)
  99. assert(h5.state.sp == 0)
  100. }
  101. }