mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 18:37:50 +00:00
Add _.memoize tests for max cache size.
This commit is contained in:
36
test/test.js
36
test/test.js
@@ -12,6 +12,9 @@
|
|||||||
/** Used as the `TypeError` message for "Functions" methods. */
|
/** Used as the `TypeError` message for "Functions" methods. */
|
||||||
var FUNC_ERROR_TEXT = 'Expected a function';
|
var FUNC_ERROR_TEXT = 'Expected a function';
|
||||||
|
|
||||||
|
/** Used as the maximum memoize cache size. */
|
||||||
|
var MAX_MEMOIZE_SIZE = 500;
|
||||||
|
|
||||||
/** Used as references for various `Number` constants. */
|
/** Used as references for various `Number` constants. */
|
||||||
var MAX_SAFE_INTEGER = 9007199254740991,
|
var MAX_SAFE_INTEGER = 9007199254740991,
|
||||||
MAX_INTEGER = 1.7976931348623157e+308;
|
MAX_INTEGER = 1.7976931348623157e+308;
|
||||||
@@ -14662,6 +14665,39 @@
|
|||||||
|
|
||||||
_.memoize.Cache = oldCache;
|
_.memoize.Cache = oldCache;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
QUnit.test('should enforce a max cache size of `MAX_MEMOIZE_SIZE`', function(assert) {
|
||||||
|
assert.expect(2);
|
||||||
|
|
||||||
|
var memoized = _.memoize(identity),
|
||||||
|
cache = memoized.cache;
|
||||||
|
|
||||||
|
lodashStable.times(MAX_MEMOIZE_SIZE, memoized);
|
||||||
|
assert.strictEqual(cache.size, MAX_MEMOIZE_SIZE);
|
||||||
|
|
||||||
|
memoized(MAX_MEMOIZE_SIZE);
|
||||||
|
assert.strictEqual(cache.size, 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
QUnit.test('should not error when the max cache size is exceeded with a weak map', function(assert) {
|
||||||
|
assert.expect(1);
|
||||||
|
|
||||||
|
if (WeakMap) {
|
||||||
|
var memoized = _.memoize(identity),
|
||||||
|
pass = true;
|
||||||
|
|
||||||
|
try {
|
||||||
|
memoized.cache = new WeakMap;
|
||||||
|
lodashStable.times(MAX_MEMOIZE_SIZE + 1, function() { memoized({}); });
|
||||||
|
} catch (e) {
|
||||||
|
pass = false;
|
||||||
|
}
|
||||||
|
assert.ok(pass);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
skipAssert(assert);
|
||||||
|
}
|
||||||
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|||||||
Reference in New Issue
Block a user