Skip to content

Commit

Permalink
test(ses): Consolidate makeLRUCacheMap testing
Browse files Browse the repository at this point in the history
  • Loading branch information
gibson042 committed Jan 18, 2024
1 parent 3212a0d commit 94e478b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 46 deletions.
38 changes: 0 additions & 38 deletions packages/ses/test/error/test-note-log-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import test from 'ava';

import { makeNoteLogArgsArrayKit } from '../../src/error/note-log-args.js';
import { makeLRUCacheMap } from '../../src/make-lru-cachemap.js';

test('note log args array kit basic', t => {
const { addLogArgs, takeLogArgsArray } = makeNoteLogArgsArrayKit(3, 2);
Expand All @@ -24,40 +23,3 @@ test('note log args array kit basic', t => {
t.deepEqual(takeLogArgsArray(e3), undefined);
t.deepEqual(takeLogArgsArray(e4), [['d']]);
});

test('weak LRUCacheMap', t => {
/** @type {WeakMap<{}, number>} */
const lru = makeLRUCacheMap(3);
const o1 = {};
const o2 = {};
const o3 = {};
const o4 = {};

// Overflow drops the oldest.
lru.set(o1, 1);
t.is(lru.get(o1), 1);
lru.set(o3, 2);
lru.set(o2, 3);
lru.set(o4, 4); // drops o1
t.falsy(lru.has(o1));
t.is(lru.get(o4), 4);
lru.set(o4, 5);
t.is(lru.get(o4), 5);
t.true(lru.has(o4));
lru.set(o1, 6); // drops o3
t.is(lru.get(o1), 6);
t.false(lru.has(o3));

// Explicit delete keeps all other elements.
lru.delete(o1); // explicit delete o1
t.is(lru.get(o1), undefined);
t.false(lru.has(o1));
t.true(lru.has(o2));
t.true(lru.has(o4));
t.false(lru.has(o3));
lru.set(o3, 7);
lru.set(o1, 8); // drops o2
t.false(lru.has(o2));
t.is(lru.get(o1), 8);
t.is(lru.get(o3), 7);
});
72 changes: 64 additions & 8 deletions packages/ses/test/test-lru-cache-map.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,78 @@
// @ts-check
import test from 'ava';

import { makeLRUCacheMap } from '../src/make-lru-cachemap.js';

test('lru cache map basic', t => {
test('makeLRUCacheMap', t => {
/** @type {WeakMap<{}, number>} */
const lruMap = makeLRUCacheMap(2);
const assertNoEntry = key => {
t.is(lruMap.has(key), false);
t.is(lruMap.get(key), undefined);
};
const assertEntry = (key, expectedValue) => {
t.is(lruMap.has(key), true);
t.is(lruMap.get(key), expectedValue);
};
const key1 = {};
const key2 = {};
const key3 = {};
t.is(lruMap.has(key1), false);
assertNoEntry(key1);

// Populate up to capacity.
lruMap.set(key1, 'x');
lruMap.set(key2, 'y');
t.is(lruMap.has(key2), true);
t.is(lruMap.has(key1), true);
t.is(lruMap.has(key3), false);
assertEntry(key2, 'y');
assertEntry(key1, 'x');
assertNoEntry(key3);

// Evict key2.
lruMap.set(key3, 'z');
t.is(lruMap.has(key1), true);
t.is(lruMap.has(key2), false);
t.is(lruMap.has(key3), true);
assertEntry(key1, 'x');
assertNoEntry(key2);
assertEntry(key3, 'z');

// Overwrite key3.
lruMap.set(key3, 'zz');
assertEntry(key1, 'x');
assertNoEntry(key2);
assertEntry(key3, 'zz');

// Evict key1.
lruMap.set(key2, 'y');
assertNoEntry(key1);
assertEntry(key2, 'y');
assertEntry(key3, 'zz');

// Delete key3, preserving key2.
lruMap.delete(key3);
assertNoEntry(key1);
assertEntry(key2, 'y');
assertNoEntry(key3);

// Add key1, preserving key2.
lruMap.set(key1, 'x');
assertEntry(key2, 'y');
assertEntry(key1, 'x');
assertNoEntry(key3);

// Delete key2, preserving key1.
lruMap.delete(key2);
assertEntry(key1, 'x');
assertNoEntry(key2);
assertNoEntry(key3);

// Delete key1.
lruMap.delete(key1);
assertNoEntry(key1);
assertNoEntry(key2);
assertNoEntry(key3);

// Repopulate with eviction.
lruMap.set(key1, 'xx');
lruMap.set(key2, 'yy');
lruMap.set(key3, 'zz');
assertNoEntry(key1);
assertEntry(key2, 'yy');
assertEntry(key3, 'zz');
});

0 comments on commit 94e478b

Please sign in to comment.