diff --git a/src/tilecache.js b/src/tilecache.js
index a643ae49..45344a05 100644
--- a/src/tilecache.js
+++ b/src/tilecache.js
@@ -155,7 +155,7 @@ $.TileCache.prototype = /** @lends OpenSeadragon.TileCache.prototype */{
// Note that just because we're unloading a tile doesn't necessarily mean
// we're unloading an image. With repeated calls it should sort itself out, though.
- if ( this._imagesLoadedCount >= this._maxImageCacheCount ) {
+ if ( this._imagesLoadedCount > this._maxImageCacheCount ) {
var worstTile = null;
var worstTileIndex = -1;
var prevTile, worstTime, worstLevel, prevTime, prevLevel, prevTileRecord;
diff --git a/test/coverage.html b/test/coverage.html
index 07ab4d7f..97e1a0c9 100644
--- a/test/coverage.html
+++ b/test/coverage.html
@@ -69,6 +69,7 @@
+
diff --git a/test/events.js b/test/modules/events.js
similarity index 100%
rename from test/events.js
rename to test/modules/events.js
diff --git a/test/formats.js b/test/modules/formats.js
similarity index 100%
rename from test/formats.js
rename to test/modules/formats.js
diff --git a/test/modules/tilecache.js b/test/modules/tilecache.js
new file mode 100644
index 00000000..afb0ca52
--- /dev/null
+++ b/test/modules/tilecache.js
@@ -0,0 +1,111 @@
+/* global module, asyncTest, $, ok, equal, notEqual, start, test, Util, testLog */
+
+(function() {
+
+ // ----------
+ module('TileCache', {
+ setup: function () {
+ testLog.reset();
+ },
+ teardown: function () {
+ }
+ });
+
+ // ----------
+ asyncTest('basics', function() {
+ var fakeTiledImage0 = {};
+ var fakeTiledImage1 = {};
+
+ var fakeTile0 = {
+ url: 'foo.jpg',
+ image: {},
+ unload: function() {}
+ };
+
+ var fakeTile1 = {
+ url: 'foo.jpg',
+ image: {},
+ unload: function() {}
+ };
+
+ var cache = new OpenSeadragon.TileCache();
+ equal(cache.numTilesLoaded(), 0, 'no tiles to begin with');
+
+ cache.cacheTile({
+ tile: fakeTile0,
+ tiledImage: fakeTiledImage0
+ });
+
+ equal(cache.numTilesLoaded(), 1, 'tile count after cache');
+
+ cache.cacheTile({
+ tile: fakeTile1,
+ tiledImage: fakeTiledImage1
+ });
+
+ equal(cache.numTilesLoaded(), 2, 'tile count after second cache');
+
+ cache.clearTilesFor(fakeTiledImage0);
+
+ equal(cache.numTilesLoaded(), 1, 'tile count after first clear');
+
+ cache.clearTilesFor(fakeTiledImage1);
+
+ equal(cache.numTilesLoaded(), 0, 'tile count after second clear');
+
+ start();
+ });
+
+ // ----------
+ asyncTest('maxImageCacheCount', function() {
+ var fakeTiledImage0 = {};
+
+ var fakeTile0 = {
+ url: 'different.jpg',
+ image: {},
+ unload: function() {}
+ };
+
+ var fakeTile1 = {
+ url: 'same.jpg',
+ image: {},
+ unload: function() {}
+ };
+
+ var fakeTile2 = {
+ url: 'same.jpg',
+ image: {},
+ unload: function() {}
+ };
+
+ var cache = new OpenSeadragon.TileCache({
+ maxImageCacheCount: 1
+ });
+
+ equal(cache.numTilesLoaded(), 0, 'no tiles to begin with');
+
+ cache.cacheTile({
+ tile: fakeTile0,
+ tiledImage: fakeTiledImage0
+ });
+
+ equal(cache.numTilesLoaded(), 1, 'tile count after add');
+
+ cache.cacheTile({
+ tile: fakeTile1,
+ tiledImage: fakeTiledImage0
+ });
+
+ equal(cache.numTilesLoaded(), 1, 'tile count after add of second image');
+
+ cache.cacheTile({
+ tile: fakeTile2,
+ tiledImage: fakeTiledImage0
+ });
+
+ equal(cache.numTilesLoaded(), 2, 'tile count after additional same image');
+
+ start();
+ });
+
+})();
diff --git a/test/test.html b/test/test.html
index 48773e1a..c74148ba 100644
--- a/test/test.html
+++ b/test/test.html
@@ -34,6 +34,7 @@
+