2019-12-29 14:40:02 +01:00
|
|
|
set_global('blueslip', global.make_zblueslip());
|
|
|
|
const IntDict = zrequire('int_dict').IntDict;
|
|
|
|
|
|
|
|
run_test('basic', () => {
|
|
|
|
const d = new IntDict();
|
|
|
|
|
2020-02-03 08:04:28 +01:00
|
|
|
assert.equal(d.size, 0);
|
2019-12-29 14:40:02 +01:00
|
|
|
|
2020-02-04 23:46:56 +01:00
|
|
|
assert.deepEqual(Array.from(d.keys()), []);
|
2019-12-29 14:40:02 +01:00
|
|
|
|
|
|
|
d.set(101, 'bar');
|
|
|
|
assert.equal(d.get(101), 'bar');
|
2020-02-03 08:04:28 +01:00
|
|
|
assert.notEqual(d.size, 0);
|
2019-12-29 14:40:02 +01:00
|
|
|
|
|
|
|
d.set(101, 'baz');
|
|
|
|
assert.equal(d.get(101), 'baz');
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 1);
|
2019-12-29 14:40:02 +01:00
|
|
|
|
|
|
|
d.set(102, 'qux');
|
|
|
|
assert.equal(d.get(101), 'baz');
|
|
|
|
assert.equal(d.get(102), 'qux');
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 2);
|
2019-12-29 14:40:02 +01:00
|
|
|
|
|
|
|
assert.equal(d.has(102), true);
|
|
|
|
assert.equal(d.has(999), false);
|
|
|
|
|
2020-02-04 23:46:56 +01:00
|
|
|
assert.deepEqual(Array.from(d.keys()), [101, 102]);
|
|
|
|
assert.deepEqual(Array.from(d.values()), ['baz', 'qux']);
|
2019-12-29 14:40:02 +01:00
|
|
|
|
2020-02-03 07:41:38 +01:00
|
|
|
d.delete(102);
|
2019-12-29 14:40:02 +01:00
|
|
|
assert.equal(d.has(102), false);
|
|
|
|
assert.strictEqual(d.get(102), undefined);
|
|
|
|
|
2020-02-04 23:46:56 +01:00
|
|
|
assert.deepEqual(Array.from(d.keys()), [101]);
|
2019-12-29 14:40:02 +01:00
|
|
|
|
|
|
|
const val = ['fred'];
|
|
|
|
const res = d.set(103, val);
|
2020-02-03 07:18:13 +01:00
|
|
|
assert.strictEqual(res, d);
|
2019-12-29 14:40:02 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
run_test('each', () => {
|
|
|
|
const d = new IntDict();
|
|
|
|
d.set(4, 40);
|
|
|
|
d.set(5, 50);
|
|
|
|
d.set(6, 60);
|
|
|
|
|
2020-02-04 23:46:56 +01:00
|
|
|
let unseen_keys = Array.from(d.keys());
|
2019-12-29 14:40:02 +01:00
|
|
|
|
|
|
|
let cnt = 0;
|
2020-02-03 09:26:53 +01:00
|
|
|
for (const [k, v] of d) {
|
2019-12-29 14:40:02 +01:00
|
|
|
assert.equal(v, d.get(k));
|
|
|
|
unseen_keys = _.without(unseen_keys, k);
|
|
|
|
cnt += 1;
|
2020-02-03 09:26:53 +01:00
|
|
|
}
|
2019-12-29 14:40:02 +01:00
|
|
|
|
2020-02-03 08:42:48 +01:00
|
|
|
assert.equal(cnt, d.size);
|
2019-12-29 14:40:02 +01:00
|
|
|
assert.equal(unseen_keys.length, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
run_test('benchmark', () => {
|
|
|
|
const d = new IntDict();
|
|
|
|
const n = 5000;
|
|
|
|
const t1 = new Date().getTime();
|
|
|
|
|
|
|
|
_.each(_.range(n), (i) => {
|
|
|
|
d.set(i, i);
|
|
|
|
});
|
|
|
|
|
|
|
|
_.each(_.range(n), (i) => {
|
|
|
|
d.get(i, i);
|
|
|
|
});
|
|
|
|
|
|
|
|
const t2 = new Date().getTime();
|
|
|
|
const elapsed = t2 - t1;
|
|
|
|
console.log('elapsed (milli)', elapsed);
|
|
|
|
console.log('per (micro)', 1000 * elapsed / n);
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
|
2020-01-05 22:16:56 +01:00
|
|
|
run_test('filter_values', () => {
|
|
|
|
const d = new IntDict();
|
|
|
|
|
|
|
|
d.set(1, "fred");
|
|
|
|
d.set(2, "foo");
|
|
|
|
d.set(3, "bar");
|
|
|
|
d.set(4, "baz");
|
|
|
|
d.set(4, "fay");
|
|
|
|
|
|
|
|
const pred = (v) => {
|
|
|
|
return v.startsWith('f');
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.deepEqual(d.filter_values(pred).sort(), ['fay', 'foo', 'fred']);
|
|
|
|
});
|
|
|
|
|
2019-12-29 14:40:02 +01:00
|
|
|
run_test('undefined_keys', () => {
|
|
|
|
blueslip.clear_test_data();
|
|
|
|
blueslip.set_test_data('error', 'Tried to call a IntDict method with an undefined key.');
|
|
|
|
|
|
|
|
const d = new IntDict();
|
|
|
|
|
|
|
|
assert.equal(d.has(undefined), false);
|
|
|
|
assert.strictEqual(d.get(undefined), undefined);
|
|
|
|
assert.equal(blueslip.get_test_logs('error').length, 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
run_test('non integers', () => {
|
|
|
|
blueslip.clear_test_data();
|
|
|
|
blueslip.set_test_data('error', 'Tried to call a IntDict method with a non-integer.');
|
|
|
|
|
|
|
|
const d = new IntDict();
|
|
|
|
|
|
|
|
assert.equal(d.has('some-string'), false);
|
|
|
|
assert.equal(blueslip.get_test_logs('error').length, 1);
|
|
|
|
|
|
|
|
// verify stringified ints still work
|
|
|
|
blueslip.clear_test_data();
|
|
|
|
blueslip.set_test_data('error', 'Tried to call a IntDict method with a non-integer.');
|
|
|
|
|
|
|
|
d.set('5', 'five');
|
|
|
|
assert.equal(d.has(5), true);
|
|
|
|
assert.equal(d.has('5'), true);
|
|
|
|
|
|
|
|
assert.equal(d.get(5), 'five');
|
|
|
|
assert.equal(d.get('5'), 'five');
|
|
|
|
assert.equal(blueslip.get_test_logs('error').length, 3);
|
|
|
|
});
|
|
|
|
|
|
|
|
run_test('num_items', () => {
|
|
|
|
const d = new IntDict();
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 0);
|
2019-12-29 14:40:02 +01:00
|
|
|
|
|
|
|
d.set(101, 1);
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 1);
|
2019-12-29 14:40:02 +01:00
|
|
|
|
|
|
|
d.set(101, 2);
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 1);
|
2019-12-29 14:40:02 +01:00
|
|
|
|
|
|
|
d.set(102, 1);
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 2);
|
2020-02-03 07:41:38 +01:00
|
|
|
d.delete(101);
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 1);
|
2019-12-29 14:40:02 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
run_test('clear', () => {
|
|
|
|
const d = new IntDict();
|
|
|
|
|
|
|
|
function populate() {
|
|
|
|
d.set(101, 1);
|
|
|
|
assert.equal(d.get(101), 1);
|
|
|
|
d.set(102, 2);
|
|
|
|
assert.equal(d.get(102), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
populate();
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 2);
|
2019-12-29 14:40:02 +01:00
|
|
|
|
|
|
|
d.clear();
|
|
|
|
assert.equal(d.get(101), undefined);
|
|
|
|
assert.equal(d.get(102), undefined);
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 0);
|
2019-12-29 14:40:02 +01:00
|
|
|
|
|
|
|
// make sure it still works after clearing
|
|
|
|
populate();
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 2);
|
2019-12-29 14:40:02 +01:00
|
|
|
});
|