shields/test/lru-cache.spec.js
2017-03-30 00:18:30 +02:00

141 lines
4.5 KiB
JavaScript

var assert = require('assert');
var LRU = require('../lib/lru-cache.js');
describe('The LRU cache', function () {
it("should support being called without new", function() {
var cache = LRU(1);
assert(cache instanceof LRU);
});
it("should support a zero capacity", function() {
var cache = new LRU(0);
cache.set('key', 'value');
assert.equal(cache.cache.size, 0);
});
it("should support a one capacity", function() {
var cache = new LRU(1);
cache.set('key1', 'value1');
assert.equal(cache.cache.size, 1);
assert.equal(cache.newest, cache.cache.get('key1'));
assert.equal(cache.oldest, cache.cache.get('key1'));
cache.set('key2', 'value2');
assert.equal(cache.cache.size, 1);
assert.equal(cache.newest, cache.cache.get('key2'));
assert.equal(cache.oldest, cache.cache.get('key2'));
assert.equal(cache.get('key1'), undefined);
assert.equal(cache.get('key2'), 'value2');
});
it("should remove the oldest element when reaching capacity", function() {
var cache = new LRU(2);
cache.set('key1', 'value1');
cache.set('key2', 'value2');
cache.set('key3', 'value3');
var slot1 = cache.cache.get('key1');
var slot2 = cache.cache.get('key2');
var slot3 = cache.cache.get('key3');
assert.equal(cache.cache.size, 2);
assert.equal(cache.oldest, slot2);
assert.equal(cache.newest, slot3);
assert.equal(slot2.older, null);
assert.equal(slot2.newer, slot3);
assert.equal(slot3.older, slot2);
assert.equal(slot3.newer, null);
assert.equal(cache.cache.get('key1'), undefined);
assert.equal(cache.get('key1'), undefined);
assert.equal(cache.get('key2'), 'value2');
assert.equal(cache.get('key3'), 'value3');
});
it("should make sure that resetting a key in cache makes it newest", function() {
var cache = new LRU(2);
cache.set('key', 'value');
cache.set('key2', 'value2');
assert.equal(cache.oldest, cache.cache.get('key'));
assert.equal(cache.newest, cache.cache.get('key2'));
cache.set('key', 'value');
assert.equal(cache.oldest, cache.cache.get('key2'));
assert.equal(cache.newest, cache.cache.get('key'));
});
it("should make sure that getting a key in cache makes it newest", function() {
var slot1, slot2, slot3;
// When the key is oldest.
var cache = new LRU(2);
cache.set('key1', 'value1');
cache.set('key2', 'value2');
slot1 = cache.cache.get('key1');
slot2 = cache.cache.get('key2');
assert.equal(cache.oldest, slot1);
assert.equal(cache.newest, slot2);
assert.equal(slot1.older, null);
assert.equal(slot1.newer, slot2);
assert.equal(slot2.older, slot1);
assert.equal(slot2.newer, null);
assert.equal(cache.get('key1'), 'value1');
slot1 = cache.cache.get('key1');
slot2 = cache.cache.get('key2');
assert.equal(cache.oldest, slot2);
assert.equal(cache.newest, slot1);
assert.equal(slot2.older, null);
assert.equal(slot2.newer, slot1);
assert.equal(slot1.older, slot2);
assert.equal(slot1.newer, null);
// When the key is newest.
cache = new LRU(2);
cache.set('key1', 'value1');
cache.set('key2', 'value2');
assert.equal(cache.get('key2'), 'value2');
slot1 = cache.cache.get('key1');
slot2 = cache.cache.get('key2');
assert.equal(cache.oldest, slot1);
assert.equal(cache.newest, slot2);
assert.equal(slot1.older, null);
assert.equal(slot1.newer, slot2);
assert.equal(slot2.older, slot1);
assert.equal(slot2.newer, null);
// When the key is in the middle.
cache = new LRU(3);
cache.set('key1', 'value1');
cache.set('key2', 'value2');
cache.set('key3', 'value3');
slot1 = cache.cache.get('key1');
slot2 = cache.cache.get('key2');
slot3 = cache.cache.get('key3');
assert.equal(cache.oldest, slot1);
assert.equal(cache.newest, slot3);
assert.equal(slot1.older, null);
assert.equal(slot1.newer, slot2);
assert.equal(slot2.older, slot1);
assert.equal(slot2.newer, slot3);
assert.equal(slot3.older, slot2);
assert.equal(slot3.newer, null);
assert.equal(cache.get('key2'), 'value2');
slot1 = cache.cache.get('key1');
slot2 = cache.cache.get('key2');
slot3 = cache.cache.get('key3');
assert.equal(cache.oldest, slot1);
assert.equal(cache.newest, slot2);
assert.equal(slot1.older, null);
assert.equal(slot1.newer, slot3);
assert.equal(slot3.older, slot1);
assert.equal(slot3.newer, slot2);
assert.equal(slot2.older, slot3);
assert.equal(slot2.newer, null);
});
});