From 0c3d5eca5cdc07a7deadde908b3f5b84ccb6c94b Mon Sep 17 00:00:00 2001 From: GaborFarkas Date: Sun, 9 Oct 2016 12:46:58 +0200 Subject: [PATCH] Add tests for the linked list struct --- test/spec/ol/structs/linkedlist.test.js | 267 ++++++++++++++++++++++++ 1 file changed, 267 insertions(+) create mode 100644 test/spec/ol/structs/linkedlist.test.js diff --git a/test/spec/ol/structs/linkedlist.test.js b/test/spec/ol/structs/linkedlist.test.js new file mode 100644 index 0000000000..661fdd66cf --- /dev/null +++ b/test/spec/ol/structs/linkedlist.test.js @@ -0,0 +1,267 @@ +goog.provide('ol.test.structs.LinkedList'); + +goog.require('ol.structs.LinkedList'); + +describe('ol.structs.LinkedList', function() { + var ll; + var item = {}; + var item2 = {}; + beforeEach(function() { + ll = new ol.structs.LinkedList(); + }); + + it('defaults to circular', function() { + expect(ll.circular_).to.be(true); + }); + + it('creates an empty list', function() { + expect(ll.length_).to.be(0); + expect(ll.first_).to.be(undefined); + expect(ll.last_).to.be(undefined); + expect(ll.head_).to.be(undefined); + }); + + describe('#insertItem', function() { + beforeEach(function() { + ll.insertItem(item); + }); + + it('inserts an item into the list', function() { + expect(ll.length_).to.be(1); + }); + + it('sets the cursor to the inserted item', function() { + expect(ll.head_.data).to.be(item); + }); + + it('links the previous item to the new one', function() { + ll.insertItem(item2); + expect(ll.head_.prev.data).to.be(item); + expect(ll.head_.prev.next.data).to.be(item2); + }); + }); + + describe('#removeItem', function() { + var item3 = {}; + beforeEach(function() { + ll.insertItem(item); + ll.insertItem(item2); + ll.insertItem(item3); + }); + + it('removes the current item', function() { + ll.removeItem(); + expect(ll.length_).to.be(2); + expect(ll.head_.data).not.to.be(item3); + }); + + it('sets the cursor to the next item if possible', function() { + ll.removeItem(); + expect(ll.head_.data).to.be(item); + }); + + it('otherwise sets the cursor to the prevous item', function() { + ll = new ol.structs.LinkedList(false); + ll.insertItem(item); + ll.insertItem(item2); + ll.insertItem(item3); + ll.removeItem(); + expect(ll.head_.data).to.be(item2); + }); + + it('empties a list with only one item', function() { + ll = new ol.structs.LinkedList(); + ll.insertItem(item); + ll.removeItem(); + expect(ll.length_).to.be(0); + expect(ll.head_).to.be(undefined); + expect(ll.first_).to.be(undefined); + expect(ll.last_).to.be(undefined); + }); + }); + + describe('#firstItem', function() { + it('sets the cursor to the first item and returns its data', function() { + ll.insertItem(item); + ll.insertItem(item2); + var i = ll.firstItem(); + expect(i).to.be(item); + expect(ll.head_.data).to.be(item); + }); + + it('returns undefined on empty list', function() { + var i = ll.firstItem(); + expect(i).to.be(undefined); + }); + }); + + describe('#lastItem', function() { + it('sets the cursor to the last item and returns its data', function() { + ll.insertItem(item); + ll.insertItem(item2); + ll.firstItem(); + var i = ll.lastItem(); + expect(i).to.be(item2); + expect(ll.head_.data).to.be(item2); + }); + + it('returns undefined on empty list', function() { + var i = ll.lastItem(); + expect(i).to.be(undefined); + }); + }); + + describe('#nextItem', function() { + it('sets the cursor to the next item and returns its data', function() { + ll.insertItem(item); + ll.insertItem(item2); + ll.firstItem(); + var i = ll.nextItem(); + expect(i).to.be(item2); + expect(ll.head_.data).to.be(item2); + }); + + it('returns undefined on empty list', function() { + var i = ll.nextItem(); + expect(i).to.be(undefined); + }); + }); + + describe('#prevItem', function() { + it('sets the cursor to the previous item and returns its data', function() { + ll.insertItem(item); + ll.insertItem(item2); + var i = ll.prevItem(); + expect(i).to.be(item); + expect(ll.head_.data).to.be(item); + }); + + it('returns undefined on empty list', function() { + var i = ll.prevItem(); + expect(i).to.be(undefined); + }); + }); + + describe('#getNextItem', function() { + it('returns the data of the next item without stepping the cursor', function() { + ll.insertItem(item); + ll.insertItem(item2); + ll.firstItem(); + var i = ll.getNextItem(); + expect(i).to.be(item2); + expect(ll.head_.data).to.be(item); + }); + + it('returns undefined on empty list', function() { + var i = ll.getNextItem(); + expect(i).to.be(undefined); + }); + }); + + describe('#getPrevItem', function() { + it('returns the data of the previous item without stepping the cursor', function() { + ll.insertItem(item); + ll.insertItem(item2); + var i = ll.getPrevItem(); + expect(i).to.be(item); + expect(ll.head_.data).to.be(item2); + }); + + it('returns undefined on empty list', function() { + var i = ll.getPrevItem(); + expect(i).to.be(undefined); + }); + }); + + describe('#getCurrItem', function() { + it('returns the data of the current item', function() { + var item3 = {}; + ll.insertItem(item); + ll.insertItem(item2); + ll.insertItem(item3); + ll.prevItem(); + var i = ll.getCurrItem(); + expect(i).to.be(item2); + expect(ll.head_.data).to.be(item2); + }); + + it('returns undefined on empty list', function() { + var i = ll.getCurrItem(); + expect(i).to.be(undefined); + }); + }); + + describe('#getLength', function() { + it('returns the length of the list', function() { + ll.insertItem(item); + ll.insertItem(item2); + var l = ll.getLength(); + expect(l).to.be(2); + }); + }); + + describe('#concat', function() { + var ll2, item3; + beforeEach(function() { + item3 = {}; + ll2 = new ol.structs.LinkedList(); + ll2.insertItem(item); + ll2.insertItem(item2); + ll2.insertItem(item3); + }); + + it('concatenates a second list with the current one', function() { + var item4 = {}; + var item5 = {}; + var item6 = {}; + ll.insertItem(item4); + ll.insertItem(item5); + ll.insertItem(item6); + ll.prevItem(); + ll.concat(ll2); + expect(ll.length_).to.be(6); + expect(ll.head_.data).to.be(item5); + expect(ll.head_.next.data).to.be(item); + expect(ll.head_.next.next.next.next.data).to.be(item6); + }); + + it('receives the second list if the current one is empty', function() { + ll.concat(ll2); + expect(ll.length_).to.be(3); + expect(ll.first_.data).to.be(item); + expect(ll.last_.data).to.be(item3); + expect(ll.head_.data).to.be(item3); + }); + + it('destroys the second list', function() { + ll.concat(ll2); + expect(ll2.length_).to.be(0); + expect(ll2.first_).to.be(undefined); + expect(ll2.last_).to.be(undefined); + expect(ll2.head_).to.be(undefined); + }); + }); + + describe('when circular', function() { + beforeEach(function() { + ll = new ol.structs.LinkedList(); + ll.insertItem(item); + }); + + describe('#insertItem', function() { + it('initializes the list in a circular way', function() { + expect(ll.head_.prev.data).to.be(item); + expect(ll.head_.next.data).to.be(item); + }); + }); + + describe('#setFirstItem', function() { + it('resets the first item to the current one', function() { + ll.insertItem(item2); + ll.setFirstItem(); + expect(ll.first_.data).to.be(item2); + expect(ll.last_.data).to.be(item); + }); + }); + }); +});