Add tests for the linked list struct
This commit is contained in:
267
test/spec/ol/structs/linkedlist.test.js
Normal file
267
test/spec/ol/structs/linkedlist.test.js
Normal file
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user