Skip to content

Commit

Permalink
Add basic tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
carmandrew committed Nov 29, 2011
1 parent 7f46366 commit 91a3a38
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 0 deletions.
1 change: 1 addition & 0 deletions sample.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<head>
<title>JavaScript LRU Cache: Sample</title>
<script src="cache.js"></script>
<script src="test.js"></script>
<script>
var cachetest;

Expand Down
160 changes: 160 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
var TIMEOUT = 50

function assertEqual(a, b) {
if (a !== b) {
throw "AssertEqual Failed: " + a + " !== " + b;
}
}
function assertNotEqual(a, b) {
if (a === b) {
throw "AssertEqual Failed: " + a + " !== " + b;
}
}

function testBasicCaching() {
var cache = new Cache();
cache.setItem("foo", "bar");
assertEqual(cache.getItem("foo"), "bar");
assertEqual(cache.getItem("missing"), null);
var stats = cache.getStats();
assertEqual(stats.hits, 1);
assertEqual(stats.misses, 1);
assertEqual(cache.toHtmlString(), "1 item(s) in cache<br /><ul><li>foo = bar</li></ul>");
assertEqual(cache.count_, 1);

cache.clear()
assertEqual(cache.getItem("foo"), null);
assertEqual(cache.count_, 0);
}

function testAbsoluteExpiration() {
var cache = new Cache();
cache.setItem("foo", "bar", {
expirationAbsolute: new Date(new Date().getTime() + TIMEOUT*2)
});
assertEqual(cache.getItem("foo"), "bar");

setTimeout(function() {
assertEqual(cache.getItem("foo"), "bar");
}, TIMEOUT);
setTimeout(function() {
assertEqual(cache.getItem("foo"), null);
}, TIMEOUT*3);
}

function testSlidingExpiration() {
var cache = new Cache();
cache.setItem("foo", "bar", {
expirationSliding: TIMEOUT * 2 / 1000
});
assertEqual(cache.getItem("foo"), "bar");

setTimeout(function() {
assertEqual(cache.getItem("foo"), "bar");
setTimeout(function() {
assertEqual(cache.getItem("foo"), "bar");
setTimeout(function() {
assertEqual(cache.getItem("foo"), "bar");
setTimeout(function() {
assertEqual(cache.getItem("foo"), null);
}, TIMEOUT*3);
}, TIMEOUT);
}, TIMEOUT);
}, TIMEOUT);
}

function testLRUExpiration() {
var cache = new Cache(2);
cache.setItem("foo1", "bar1");
cache.setItem("foo2", "bar2");
setTimeout(function() {
// Access an item so foo1 will be the LRU
assertEqual(cache.getItem("foo2"), "bar2");

cache.setItem("foo3", "bar3");
assertEqual(cache.count_, 3);

// Allow time for cache to be purged
setTimeout(function() {
assertEqual(cache.getItem("foo1"), null);
assertEqual(cache.getItem("foo2"), "bar2");
assertEqual(cache.getItem("foo3"), "bar3");
assertEqual(cache.count_, 2);
}, TIMEOUT)
}, TIMEOUT)
}

function testPriorityExpiration() {
var cache = new Cache(2);
cache.setItem("foo1", "bar1", {
priority: CachePriority.HIGH
});
cache.setItem("foo2", "bar2");
setTimeout(function() {
// Access an item so foo1 will be the LRU
assertEqual(cache.getItem("foo2"), "bar2");

setTimeout(function() {
cache.setItem("foo3", "bar3");
assertEqual(cache.count_, 3);

// Allow time for cache to be purged
setTimeout(function() {
assertEqual(cache.getItem("foo1"), "bar1");
assertEqual(cache.getItem("foo2"), null);
assertEqual(cache.getItem("foo3"), "bar3");
assertEqual(cache.count_, 2);
}, TIMEOUT)
}, TIMEOUT)
}, TIMEOUT)
}

function testResize() {
var cache = new Cache();
cache.setItem("foo1", "bar1");
setTimeout(function() {
cache.setItem("foo2", "bar2");
setTimeout(function() {
cache.setItem("foo3", "bar3");
cache.resize(2);
assertEqual(cache.getItem("foo1"), null);
assertEqual(cache.getItem("foo2"), "bar2");
setTimeout(function() {
assertEqual(cache.getItem("foo3"), "bar3");
cache.resize(1);
assertEqual(cache.getItem("foo1"), null);
assertEqual(cache.getItem("foo2"), null);
assertEqual(cache.getItem("foo3"), "bar3");
}, TIMEOUT)
}, TIMEOUT)
}, TIMEOUT)
}

function testFillFactor() {
var cache = new Cache(100);
var counter = 0;
for (var i = 1; i <= 100; i++) {
cache.setItem("foo" + i, "bar" + i);
}
assertEqual(cache.count_, 100);
setTimeout(function() {
assertEqual(cache.count_, 100);
cache.setItem("purge", "do it");
setTimeout(function() {
assertEqual(cache.count_, 75);
}, TIMEOUT)
}, TIMEOUT)
}


console.log("Running tests...")
testBasicCaching();
testAbsoluteExpiration();
testSlidingExpiration();
testLRUExpiration();
testPriorityExpiration();
testResize();
testFillFactor();
setTimeout(function() {
console.log("All tests passed!")
}, TIMEOUT * 5)

0 comments on commit 91a3a38

Please sign in to comment.