heap.test.coffee 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. Heap = require '..'
  2. {random} = Math
  3. describe 'Heap#push, Heap#pop', ->
  4. it 'should sort an array using push and pop', ->
  5. heap = new Heap
  6. heap.push(random()) for i in [1..10]
  7. sorted = (heap.pop() until heap.empty())
  8. sorted.slice().sort().should.eql(sorted)
  9. it 'should work with custom comparison function', ->
  10. cmp = (a, b) ->
  11. return -1 if a > b
  12. return 1 if a < b
  13. 0
  14. heap = new Heap(cmp)
  15. heap.push(random()) for i in [1..10]
  16. sorted = (heap.pop() until heap.empty())
  17. sorted.slice().sort().reverse().should.eql(sorted)
  18. describe 'Heap#replace', ->
  19. it 'should behave like pop() followed by push()', ->
  20. heap = new Heap
  21. heap.push(v) for v in [1..5]
  22. heap.replace(3).should.eql(1)
  23. heap.toArray().sort().should.eql([2,3,3,4,5])
  24. describe 'Heap#pushpop', ->
  25. it 'should behave like push() followed by pop()', ->
  26. heap = new Heap
  27. heap.push(v) for v in [1..5]
  28. heap.pushpop(6).should.eql(1)
  29. heap.toArray().sort().should.eql([2..6])
  30. describe 'Heap#contains', ->
  31. it 'should return whether it contains the value', ->
  32. heap = new Heap
  33. heap.push(v) for v in [1..5]
  34. heap.contains(v).should.be.true for v in [1..5]
  35. heap.contains(0).should.be.false
  36. heap.contains(6).should.be.false
  37. describe 'Heap#peek', ->
  38. it 'should return the top value', ->
  39. heap = new Heap
  40. heap.push(1)
  41. heap.peek().should.eql(1)
  42. heap.push(2)
  43. heap.peek().should.eql(1)
  44. heap.pop()
  45. heap.peek().should.eql(2)
  46. describe 'Heap#clone', ->
  47. it 'should return a cloned heap', ->
  48. a = new Heap
  49. a.push(v) for v in [1..5]
  50. b = a.clone()
  51. a.toArray().should.eql(b.toArray())
  52. describe 'Heap.nsmallest', ->
  53. it 'should return exactly n elements when size() >= n', ->
  54. Heap.nsmallest([1..10], 3).should.eql([1..3])
  55. array = [1,3,2,1,3,4,4,2,3,4,5,1,2,3,4,5,2,1,3,4,5,6,7,2]
  56. Heap.nsmallest(array, 2).should.eql([1, 1])
  57. it 'should return size() elements when size() <= n', ->
  58. Heap.nsmallest([3..1], 10).should.eql([1..3])
  59. describe 'Heap.nlargest', ->
  60. it 'should return exactly n elements when size() >= n', ->
  61. Heap.nlargest([1..10], 3).should.eql([10..8])
  62. it 'should return size() elements when size() <= n', ->
  63. Heap.nlargest([3..1], 10).should.eql([3..1])
  64. describe 'Heap#updateItem', ->
  65. it 'should return correct order', ->
  66. a = x: 1
  67. b = x: 2
  68. c = x: 3
  69. h = new Heap (m, n) -> m.x - n.x
  70. h.push(a)
  71. h.push(b)
  72. h.push(c)
  73. c.x = 0
  74. h.updateItem(c)
  75. h.pop().should.eql(c)
  76. it 'should return correct order when used statically', ->
  77. a = x: 1
  78. b = x: 2
  79. c = x: 3
  80. h = []
  81. cmp = (m, n) -> m.x - n.x
  82. Heap.push(h, a, cmp)
  83. Heap.push(h, b, cmp)
  84. Heap.push(h, c, cmp)
  85. c.x = 0
  86. Heap.updateItem(h, c, cmp)
  87. Heap.pop(h, cmp).should.eql(c)