2018-04-24 00:59:00 +02:00
|
|
|
set_global('blueslip', global.make_zblueslip());
|
2019-12-29 13:49:29 +01:00
|
|
|
const Dict = zrequire('dict').Dict;
|
2013-09-10 23:33:46 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('basic', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const d = new Dict();
|
2013-08-07 22:28:45 +02:00
|
|
|
|
2020-02-03 08:04:28 +01:00
|
|
|
assert.equal(d.size, 0);
|
2017-04-21 18:05:50 +02:00
|
|
|
|
2020-02-04 23:46:56 +01:00
|
|
|
assert.deepEqual(Array.from(d.keys()), []);
|
2013-08-07 22:28:45 +02:00
|
|
|
|
|
|
|
d.set('foo', 'bar');
|
|
|
|
assert.equal(d.get('foo'), 'bar');
|
2020-02-03 08:04:28 +01:00
|
|
|
assert.notEqual(d.size, 0);
|
2013-08-07 22:28:45 +02:00
|
|
|
|
|
|
|
d.set('foo', 'baz');
|
|
|
|
assert.equal(d.get('foo'), 'baz');
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 1);
|
2013-08-07 22:28:45 +02:00
|
|
|
|
|
|
|
d.set('bar', 'qux');
|
|
|
|
assert.equal(d.get('foo'), 'baz');
|
|
|
|
assert.equal(d.get('bar'), 'qux');
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 2);
|
2013-08-07 22:28:45 +02:00
|
|
|
|
|
|
|
assert.equal(d.has('bar'), true);
|
|
|
|
assert.equal(d.has('baz'), false);
|
|
|
|
|
2020-02-04 23:46:56 +01:00
|
|
|
assert.deepEqual(Array.from(d.keys()), ['foo', 'bar']);
|
|
|
|
assert.deepEqual(Array.from(d.values()), ['baz', 'qux']);
|
|
|
|
assert.deepEqual(Array.from(d), [['foo', 'baz'], ['bar', 'qux']]);
|
2013-08-07 22:28:45 +02:00
|
|
|
|
2020-02-03 07:41:38 +01:00
|
|
|
d.delete('bar');
|
2013-08-07 22:28:45 +02:00
|
|
|
assert.equal(d.has('bar'), false);
|
|
|
|
assert.strictEqual(d.get('bar'), undefined);
|
|
|
|
|
2020-02-04 23:46:56 +01:00
|
|
|
assert.deepEqual(Array.from(d.keys()), ['foo']);
|
2013-08-20 22:34:36 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const val = ['foo'];
|
|
|
|
const res = d.set('abc', val);
|
2020-02-03 07:18:13 +01:00
|
|
|
assert.strictEqual(res, d);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-08-07 22:28:45 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('undefined_keys', () => {
|
2020-02-01 05:10:50 +01:00
|
|
|
blueslip.clear_test_data();
|
2018-04-24 00:59:00 +02:00
|
|
|
blueslip.set_test_data('error', 'Tried to call a Dict method with an undefined key.');
|
2013-09-10 23:33:46 +02:00
|
|
|
|
2019-12-26 15:34:17 +01:00
|
|
|
const d = new Dict();
|
2013-09-10 23:06:56 +02:00
|
|
|
|
|
|
|
assert.equal(d.has(undefined), false);
|
|
|
|
assert.strictEqual(d.get(undefined), undefined);
|
2019-12-26 15:34:17 +01:00
|
|
|
assert.equal(blueslip.get_test_logs('error').length, 2);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-09-10 23:00:54 +02:00
|
|
|
|
2020-02-01 05:10:50 +01:00
|
|
|
run_test('non-strings', () => {
|
|
|
|
blueslip.clear_test_data();
|
|
|
|
blueslip.set_test_data('error', 'Tried to call a Dict method with a non-string.');
|
|
|
|
|
|
|
|
const d = new Dict();
|
|
|
|
|
|
|
|
d.set('17', 'value');
|
|
|
|
assert.equal(d.get(17), 'value');
|
|
|
|
assert.equal(blueslip.get_test_logs('error').length, 1);
|
|
|
|
});
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('restricted_keys', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const d = new Dict();
|
2013-08-07 22:28:45 +02:00
|
|
|
|
|
|
|
assert.equal(d.has('__proto__'), false);
|
|
|
|
assert.equal(d.has('hasOwnProperty'), false);
|
|
|
|
assert.equal(d.has('toString'), false);
|
|
|
|
|
|
|
|
assert.strictEqual(d.get('__proto__'), undefined);
|
|
|
|
assert.strictEqual(d.get('hasOwnProperty'), undefined);
|
|
|
|
assert.strictEqual(d.get('toString'), undefined);
|
|
|
|
|
|
|
|
d.set('hasOwnProperty', function () {return true;});
|
|
|
|
assert.equal(d.has('blah'), false);
|
|
|
|
|
|
|
|
d.set('__proto__', 'foo');
|
|
|
|
d.set('foo', 'bar');
|
|
|
|
assert.equal(d.get('foo'), 'bar');
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-08-07 22:28:45 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('construction', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const d1 = new Dict();
|
2013-08-07 22:28:45 +02:00
|
|
|
|
2020-02-04 23:46:56 +01:00
|
|
|
assert.deepEqual(Array.from(d1), []);
|
2013-08-07 22:28:45 +02:00
|
|
|
|
2020-02-03 06:46:13 +01:00
|
|
|
const d2 = new Dict();
|
|
|
|
d2.set('foo', 'bar');
|
|
|
|
d2.set('baz', 'qux');
|
2020-02-04 23:46:56 +01:00
|
|
|
assert.deepEqual(Array.from(d2), [['foo', 'bar'], ['baz', 'qux']]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-08-14 19:08:03 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('each', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const d = new Dict();
|
2013-08-14 19:08:03 +02:00
|
|
|
d.set('apple', 40);
|
|
|
|
d.set('banana', 50);
|
|
|
|
d.set('carrot', 60);
|
|
|
|
|
2020-02-04 23:46:56 +01:00
|
|
|
let unseen_keys = Array.from(d.keys());
|
2013-08-14 19:08:03 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let cnt = 0;
|
2020-02-03 09:26:53 +01:00
|
|
|
for (const [k, v] of d) {
|
2013-08-14 19:08:03 +02:00
|
|
|
assert.equal(v, d.get(k));
|
|
|
|
unseen_keys = _.without(unseen_keys, k);
|
|
|
|
cnt += 1;
|
2020-02-03 09:26:53 +01:00
|
|
|
}
|
2013-08-14 19:08:03 +02:00
|
|
|
|
2020-02-03 08:42:48 +01:00
|
|
|
assert.equal(cnt, d.size);
|
2013-08-14 19:08:03 +02:00
|
|
|
assert.equal(unseen_keys.length, 0);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-08-20 22:48:07 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('num_items', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const d = new Dict();
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 0);
|
2017-04-21 18:05:50 +02:00
|
|
|
|
2013-08-23 02:54:47 +02:00
|
|
|
d.set('foo', 1);
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 1);
|
2017-04-21 18:05:50 +02:00
|
|
|
|
2013-08-23 02:54:47 +02:00
|
|
|
d.set('foo', 2);
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 1);
|
2017-04-21 18:05:50 +02:00
|
|
|
|
2013-08-23 02:54:47 +02:00
|
|
|
d.set('bar', 1);
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 2);
|
2020-02-03 07:41:38 +01:00
|
|
|
d.delete('foo');
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 1);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-04-07 00:37:32 +02:00
|
|
|
|
2019-12-29 14:00:09 +01:00
|
|
|
/*
|
|
|
|
run_test('benchmark', () => {
|
|
|
|
const d = new Dict();
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('clear', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const d = new Dict();
|
2017-04-07 00:37:32 +02:00
|
|
|
|
|
|
|
function populate() {
|
|
|
|
d.set('foo', 1);
|
|
|
|
assert.equal(d.get('foo'), 1);
|
|
|
|
d.set('bar', 2);
|
|
|
|
assert.equal(d.get('bar'), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
populate();
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 2);
|
2017-04-07 00:37:32 +02:00
|
|
|
|
|
|
|
d.clear();
|
|
|
|
assert.equal(d.get('foo'), undefined);
|
|
|
|
assert.equal(d.get('bar'), undefined);
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 0);
|
2017-04-07 00:37:32 +02:00
|
|
|
|
|
|
|
// make sure it still works after clearing
|
|
|
|
populate();
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 2);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|