| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- import LinkedList, {NodeStructure} from 'handsontable/utils/dataStructures/linkedList';
- /**
- * Refactored implementation of LinkedList tests by Github user Jakehp
- * (part of javascript-algorithms project - all project contributors at repository website)
- *
- * Link to repository: https://github.com/mgechev/javascript-algorithms
- */
- describe('Node', () => {
- it('should be a constructor function', () => {
- expect(typeof NodeStructure).toBe('function');
- });
- it('should construct properly', () => {
- var node = new NodeStructure('data');
- expect(node.data).toBe('data');
- expect(node.next).toBe(null);
- expect(node.prev).toBe(null);
- });
- });
- describe('Linked List', () => {
- it('should be a constructor function', () => {
- expect(typeof LinkedList).toBe('function');
- });
- it('should push properly', () => {
- var linkedList = new LinkedList();
- linkedList.push(1);
- linkedList.push(2);
- linkedList.push(3);
- linkedList.push(4);
- linkedList.push(5);
- expect(linkedList.first.data).toBe(1);
- expect(linkedList.first.next.data).toBe(2);
- expect(linkedList.first.next.next.data).toBe(3);
- expect(linkedList.first.next.next.next.data).toBe(4);
- expect(linkedList.first.next.next.next.next.data).toBe(5);
- expect(linkedList.last.data).toBe(5);
- });
- it('should pop properly', () => {
- var linkedList = new LinkedList();
- linkedList.push(1);
- linkedList.push(2);
- linkedList.push(3);
- linkedList.push(4);
- linkedList.push(5);
- expect(linkedList.pop().data).toBe(5);
- expect(linkedList.pop().data).toBe(4);
- expect(linkedList.pop().data).toBe(3);
- expect(linkedList.pop().data).toBe(2);
- expect(linkedList.pop().data).toBe(1);
- });
- it('should shift properly', () => {
- var linkedList = new LinkedList();
- linkedList.push(1);
- linkedList.push(2);
- linkedList.push(3);
- linkedList.push(4);
- linkedList.push(5);
- expect(linkedList.shift().data).toBe(1);
- expect(linkedList.shift().data).toBe(2);
- expect(linkedList.shift().data).toBe(3);
- expect(linkedList.shift().data).toBe(4);
- expect(linkedList.shift().data).toBe(5);
- });
- it('should reverse properly', () => {
- var linkedList = new LinkedList();
- linkedList.push(1);
- linkedList.push(2);
- linkedList.push(3);
- linkedList.push(4);
- linkedList.push(5);
- linkedList.reverse();
- expect(linkedList.shift().data).toBe(5);
- expect(linkedList.shift().data).toBe(4);
- expect(linkedList.shift().data).toBe(3);
- expect(linkedList.shift().data).toBe(2);
- expect(linkedList.shift().data).toBe(1);
- });
- it('should recursive reverse properly', () => {
- var linkedList = new LinkedList();
- linkedList.push(1);
- linkedList.push(2);
- linkedList.push(3);
- linkedList.push(4);
- linkedList.push(5);
- linkedList.recursiveReverse();
- expect(linkedList.shift().data).toBe(5);
- expect(linkedList.shift().data).toBe(4);
- expect(linkedList.shift().data).toBe(3);
- expect(linkedList.shift().data).toBe(2);
- expect(linkedList.shift().data).toBe(1);
- });
- it('should unshift properly', () => {
- var linkedList = new LinkedList();
- linkedList.push(1);
- linkedList.push(2);
- linkedList.push(3);
- linkedList.push(4);
- linkedList.push(5);
- linkedList.unshift(3);
- expect(linkedList.shift().data).toBe(3);
- expect(linkedList.shift().data).toBe(1);
- expect(linkedList.shift().data).toBe(2);
- expect(linkedList.shift().data).toBe(3);
- expect(linkedList.shift().data).toBe(4);
- expect(linkedList.shift().data).toBe(5);
- });
- it('should properly check for existing cycle', () => {
- var linkedList = new LinkedList();
- var last = new NodeStructure(2);
- var first = new NodeStructure(1);
- last.next = first;
- last.prev = first;
- first.next = last;
- first.prev = last;
- linkedList.first = first;
- linkedList.last = last;
- expect(linkedList.hasCycle()).toBe(true);
- });
- it('should properly check for non existing cycle', () => {
- var linkedList = new LinkedList();
- linkedList.push(1);
- linkedList.push(2);
- linkedList.push(3);
- linkedList.push(4);
- linkedList.push(5);
- expect(linkedList.hasCycle()).toBe(false);
- });
- it('should inorder properly', () => {
- var linkedList = new LinkedList();
- linkedList.push(1);
- linkedList.push(2);
- linkedList.push(3);
- linkedList.push(4);
- linkedList.push(5);
- var pushedValue = 1;
- function callback(node) {
- expect(node.data).toBe(pushedValue++);
- }
- linkedList.inorder(callback);
- });
- });
|