test.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import RopeSequence from "./index.js"
  2. import assert from "assert"
  3. function describe(rope) {
  4. if (rope.left) return "(" + describe(rope.left) + ", " + describe(rope.right) + ")"
  5. else return rope.length
  6. }
  7. function appendBuild(n) {
  8. var rope = RopeSequence.empty
  9. for (var i = 0; i < n; i++)
  10. rope = rope.append([i])
  11. return rope
  12. }
  13. function dequeBuild(n) {
  14. var mid = n >> 1, rope = RopeSequence.empty
  15. for (var from = mid - 1, to = mid; to < n; from--, to++) {
  16. rope = rope.append([to])
  17. if (from >= 0) rope = RopeSequence.from([from]).append(rope)
  18. }
  19. return rope
  20. }
  21. function flatBuild(n) {
  22. var arr = []
  23. for (var i = 0; i < n; i++) arr.push(i)
  24. return RopeSequence.from(arr)
  25. }
  26. var SIZE = 10000
  27. function checkForEach(rope, name, start, end, offset) {
  28. var cur = start
  29. rope.forEach(function(elt, i) {
  30. assert.equal(elt, cur + offset, "Proper element at " + cur + " in " + name)
  31. assert.equal(cur, i, "Accurate index passed")
  32. cur++
  33. }, start, end)
  34. assert.equal(cur, end, "Enough elements iterated in " + name)
  35. rope.forEach(function(elt, i) {
  36. cur--
  37. assert.equal(elt, cur + offset, "Proper element during reverse iter at " + cur + " in " + name)
  38. assert.equal(cur, i, "Accurate index passed by reverse iter")
  39. }, end, start)
  40. assert.equal(cur, start, "Enough elements reverse-iterated in " + name + " -- " + cur + " " + start)
  41. }
  42. function check(rope, size, name, offset) {
  43. if (!offset) offset = 0
  44. assert.equal(rope.length, size, "Size of " + name)
  45. for (var i = 0; i < rope.length; i++)
  46. assert.equal(rope.get(i), offset + i, "Field at " + i + " in " + name)
  47. checkForEach(rope, name, 0, rope.length, offset)
  48. for (var i = 0, e = Math.min(10, Math.floor(size / 100)); i < e; i++) {
  49. var start = Math.floor(Math.random() * size), end = start + Math.ceil(Math.random() * (size - start))
  50. checkForEach(rope, name + "-" + start + "-" + end, start, end, offset)
  51. check(rope.slice(start, end), end - start, name + "-sliced-" + start + "-" + end, offset + start)
  52. }
  53. }
  54. check(appendBuild(SIZE), SIZE, "appended")
  55. check(dequeBuild(SIZE), SIZE, "dequed")
  56. check(flatBuild(SIZE), SIZE, "flat")
  57. var small = RopeSequence.from([1, 2, 4]), empty = RopeSequence.empty
  58. assert.equal(small.append(empty), small, "ID append")
  59. assert.equal(small.prepend(empty), small, "ID prepend")
  60. assert.equal(empty.append(empty), empty, "empty append")
  61. assert.equal(small.slice(0, 0), empty, "empty slice")
  62. var sum = 0
  63. small.forEach(function(v) { if (v == 2) return false; sum += v })
  64. assert.equal(sum, 1, "abort iteration")
  65. assert.deepEqual(small.map(function(x) { return x + 1 }), [2, 3, 5], "mapping")
  66. console.log("All passed")