0eb1ac55afb9ea7d44adad01be215190ced92dfc6c1b05a6237114273ef931e8e0f3b66b6a4702e78018e2127462e5550512d777cc9e4b3199a2c6de5cca4a 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import LinkedList, {NodeStructure} from 'handsontable/utils/dataStructures/linkedList';
  2. /**
  3. * Refactored implementation of LinkedList tests by Github user Jakehp
  4. * (part of javascript-algorithms project - all project contributors at repository website)
  5. *
  6. * Link to repository: https://github.com/mgechev/javascript-algorithms
  7. */
  8. describe('Node', () => {
  9. it('should be a constructor function', () => {
  10. expect(typeof NodeStructure).toBe('function');
  11. });
  12. it('should construct properly', () => {
  13. var node = new NodeStructure('data');
  14. expect(node.data).toBe('data');
  15. expect(node.next).toBe(null);
  16. expect(node.prev).toBe(null);
  17. });
  18. });
  19. describe('Linked List', () => {
  20. it('should be a constructor function', () => {
  21. expect(typeof LinkedList).toBe('function');
  22. });
  23. it('should push properly', () => {
  24. var linkedList = new LinkedList();
  25. linkedList.push(1);
  26. linkedList.push(2);
  27. linkedList.push(3);
  28. linkedList.push(4);
  29. linkedList.push(5);
  30. expect(linkedList.first.data).toBe(1);
  31. expect(linkedList.first.next.data).toBe(2);
  32. expect(linkedList.first.next.next.data).toBe(3);
  33. expect(linkedList.first.next.next.next.data).toBe(4);
  34. expect(linkedList.first.next.next.next.next.data).toBe(5);
  35. expect(linkedList.last.data).toBe(5);
  36. });
  37. it('should pop properly', () => {
  38. var linkedList = new LinkedList();
  39. linkedList.push(1);
  40. linkedList.push(2);
  41. linkedList.push(3);
  42. linkedList.push(4);
  43. linkedList.push(5);
  44. expect(linkedList.pop().data).toBe(5);
  45. expect(linkedList.pop().data).toBe(4);
  46. expect(linkedList.pop().data).toBe(3);
  47. expect(linkedList.pop().data).toBe(2);
  48. expect(linkedList.pop().data).toBe(1);
  49. });
  50. it('should shift properly', () => {
  51. var linkedList = new LinkedList();
  52. linkedList.push(1);
  53. linkedList.push(2);
  54. linkedList.push(3);
  55. linkedList.push(4);
  56. linkedList.push(5);
  57. expect(linkedList.shift().data).toBe(1);
  58. expect(linkedList.shift().data).toBe(2);
  59. expect(linkedList.shift().data).toBe(3);
  60. expect(linkedList.shift().data).toBe(4);
  61. expect(linkedList.shift().data).toBe(5);
  62. });
  63. it('should reverse properly', () => {
  64. var linkedList = new LinkedList();
  65. linkedList.push(1);
  66. linkedList.push(2);
  67. linkedList.push(3);
  68. linkedList.push(4);
  69. linkedList.push(5);
  70. linkedList.reverse();
  71. expect(linkedList.shift().data).toBe(5);
  72. expect(linkedList.shift().data).toBe(4);
  73. expect(linkedList.shift().data).toBe(3);
  74. expect(linkedList.shift().data).toBe(2);
  75. expect(linkedList.shift().data).toBe(1);
  76. });
  77. it('should recursive reverse properly', () => {
  78. var linkedList = new LinkedList();
  79. linkedList.push(1);
  80. linkedList.push(2);
  81. linkedList.push(3);
  82. linkedList.push(4);
  83. linkedList.push(5);
  84. linkedList.recursiveReverse();
  85. expect(linkedList.shift().data).toBe(5);
  86. expect(linkedList.shift().data).toBe(4);
  87. expect(linkedList.shift().data).toBe(3);
  88. expect(linkedList.shift().data).toBe(2);
  89. expect(linkedList.shift().data).toBe(1);
  90. });
  91. it('should unshift properly', () => {
  92. var linkedList = new LinkedList();
  93. linkedList.push(1);
  94. linkedList.push(2);
  95. linkedList.push(3);
  96. linkedList.push(4);
  97. linkedList.push(5);
  98. linkedList.unshift(3);
  99. expect(linkedList.shift().data).toBe(3);
  100. expect(linkedList.shift().data).toBe(1);
  101. expect(linkedList.shift().data).toBe(2);
  102. expect(linkedList.shift().data).toBe(3);
  103. expect(linkedList.shift().data).toBe(4);
  104. expect(linkedList.shift().data).toBe(5);
  105. });
  106. it('should properly check for existing cycle', () => {
  107. var linkedList = new LinkedList();
  108. var last = new NodeStructure(2);
  109. var first = new NodeStructure(1);
  110. last.next = first;
  111. last.prev = first;
  112. first.next = last;
  113. first.prev = last;
  114. linkedList.first = first;
  115. linkedList.last = last;
  116. expect(linkedList.hasCycle()).toBe(true);
  117. });
  118. it('should properly check for non existing cycle', () => {
  119. var linkedList = new LinkedList();
  120. linkedList.push(1);
  121. linkedList.push(2);
  122. linkedList.push(3);
  123. linkedList.push(4);
  124. linkedList.push(5);
  125. expect(linkedList.hasCycle()).toBe(false);
  126. });
  127. it('should inorder properly', () => {
  128. var linkedList = new LinkedList();
  129. linkedList.push(1);
  130. linkedList.push(2);
  131. linkedList.push(3);
  132. linkedList.push(4);
  133. linkedList.push(5);
  134. var pushedValue = 1;
  135. function callback(node) {
  136. expect(node.data).toBe(pushedValue++);
  137. }
  138. linkedList.inorder(callback);
  139. });
  140. });