UNPKG

reign

Version:

A persistent, typed-objects implementation.

475 lines (413 loc) 13.6 kB
"use strict"; var _2 = require("../.."); var _3 = require("./"); var _symbols = require("../../symbols"); describeRealm('ArrayType', function (options) { var realm = void 0; var ArrayType = void 0; var StructType = void 0; var instance = void 0; var T = void 0; before(function () { realm = options.realm; T = realm.T; ArrayType = realm.ArrayType; StructType = realm.StructType; }); it('ArrayType should be an instance of realm.TypeClass', function () { ArrayType.should.be.an.instanceOf(realm.TypeClass); }); PRIMITIVE_NAMES.forEach(function (typeName) { describe("Array<" + typeName + ">", function () { var Type = void 0; var XArray = void 0; var array = void 0; var input = void 0; before(function () { Type = T[typeName]; XArray = Type.Array; input = Array.from({ length: 16 }, function () { return Type.randomValue(); }); }); it("Array<" + typeName + "> should be an instance of ArrayType", function () { XArray.should.be.an.instanceOf(ArrayType); }); it('should create an instance of the array', function () { array = new XArray(16); }); it("array should be an instanceOf Array<" + typeName + ">", function () { array.should.be.an.instanceOf(XArray); }); it('should create an instance of the array from an array', function () { array = new XArray(input); }); it('should have the right length', function () { array.length.should.equal(input.length); }); it('should have the right contents', function () { input.forEach(function (expected, index) { array[index].should.equal(expected); }); }); it('should iterate the items in the array', function () { var i = 0; var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = array[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var item = _step.value; item.should.equal(input[i]); i++; } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } }); it('should iterate the array with .forEach()', function () { array.length.should.equal(16); array.forEach(function (item, index, arr) { item.should.equal(input[index]); arr.should.equal(array); }); }); it('should map over the array with .map()', function () { array.length.should.equal(16); var out = array.map(function (item, index, arr) { item.should.equal(input[index]); arr.should.equal(array); return item + 1; }); out.should.eql(input.map(function (item) { return item + 1; })); }); it('should .filter() the array', function () { array.length.should.equal(16); var alts = array.filter(function (item, index, arr) { item.should.equal(input[index]); arr.should.equal(array); return index % 2; }); alts.length.should.equal(array.length / 2); alts.forEach(function (item, index) { item.should.equal(array[index * 2 + 1]); }); }); it('should reduce the array to a final value', function () { array.length.should.equal(16); var result = array.reduce(function (acc, item) { acc.push(item); return acc; }, []); result.should.eql(input); }); it('should reduce without an initial value', function () { array.length.should.equal(16); var result = array.reduce(function (acc, item) { return item; }); result.should.equal(input[15]); }); it('should clear the array', function () { XArray.clear(array[_symbols.$Backing], array[_symbols.$Address]); }); it('should have empty values for contents', function () { array.length.should.equal(16); array.forEach(function (item) { item.should.eql(Type.emptyValue()); }); }); it('should destroy the array', function () { XArray.destructor(array[_symbols.$Backing], array[_symbols.$Address]); }); it('should now have a length of zero', function () { array.length.should.equal(0); }); it('should create a random array', function () { var random = XArray.randomValue(); }); describe('Multidimensional', function () { var Grid = void 0; var grid = void 0; before(function () { Grid = new ArrayType(XArray); }); it('should create an instance', function () { grid = new Grid(Array.from({ length: 16 }, function () { return input; })); }); it('should have the right length', function () { grid.length.should.equal(16); }); it('should allow nested lookups', function () { grid[1][1].should.equal(input[1]); }); }); describe('Array within a struct', function () { var Simple = void 0; var simple = void 0; before(function () { Simple = new StructType({ value: XArray }); }); it('should create an instance', function () { simple = new Simple(); }); it('should load the property', function () { simple.value.should.be.an.instanceOf(XArray); simple.value.length.should.equal(0); }); it('should set a new value', function () { simple.value = input; }); it('should have cast the value to the right type', function () { simple.value.should.be.an.instanceOf(XArray); }); it('should have the right length', function () { simple.value.length.should.equal(input.length); }); it('should overwrite the value', function () { simple.value = [Type.randomValue()]; }); it('should have the right length', function () { simple.value.length.should.equal(1); }); it('should create an instance with some values', function () { simple = new Simple({ value: input }); }); it('should have stored the value', function () { simple.value.length.should.equal(input.length); simple.value.should.be.an.instanceOf(XArray); simple.value.map(function (item) { return item; }).should.eql(input); }); }); describe('Struct within an array', function () { var ListNode = void 0; var ListArray = void 0; var array = void 0; before(function () { ListNode = new StructType(); ListNode.finalize({ value: Type, prev: ListNode.ref, next: ListNode.ref }); }); it('should create an array of list items', function () { ListArray = new ArrayType(ListNode); }); it('should create a new array instance', function () { array = new ListArray(16); }); it('should have set the default values', function () { array.length.should.equal(16); array.forEach(function (item) { item.value.should.equal(Type.emptyValue()); }); }); it('should create a struct array from input', function () { array = new ListArray(input.map(function (value) { return { value: value }; })); }); it('should have the right length', function () { array.length.should.equal(input.length); }); it('should have set the right values', function () { input.forEach(function (value, index) { array[index].value.should.eql(value); }); }); }); }); }); it('should create a new base array type', function () { new ArrayType(T.Int32).should.equal(T.Int32.Array); }); it('T.Int32.Array should be an instance of ArrayType', function () { T.Int32.Array.should.be.an.instanceOf(ArrayType); }); it('should create an instance of the array', function () { var instance = new T.Int32.Array(20); }); it('should create an instance of the array from an array', function () { var input = Array.from({ length: 20 }, function (_, index) { return index + 1; }); var instance = new T.Int32.Array(input); }); describe('Benchmarks', function () { var _Custom = void 0, _Native = void 0; var custom = void 0, native = void 0; var input = void 0; it('should set up the types', function () { _Custom = T.Int32.Array; _Native = Int32Array; input = Array.from({ length: 200 }, function (_, index) { return index + 1; }); custom = new _Custom(input); native = new _Native(input); native.length.should.equal(custom.length); }); benchmark('Read single elements', 100000, { Custom: function Custom() { return custom[2]; }, Native: function Native() { return native[2]; } }); benchmark('Read multiple elements', 100000, { Custom: function Custom() { return custom[2] + custom[6] + custom[9]; }, Native: function Native() { return native[2] + native[6] + native[9]; } }); benchmark('Sum elements', 5000, { Custom: function Custom() { var total = 0; var _iteratorNormalCompletion2 = true; var _didIteratorError2 = false; var _iteratorError2 = undefined; try { for (var _iterator2 = custom[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { var item = _step2.value; total += item; } } catch (err) { _didIteratorError2 = true; _iteratorError2 = err; } finally { try { if (!_iteratorNormalCompletion2 && _iterator2.return) { _iterator2.return(); } } finally { if (_didIteratorError2) { throw _iteratorError2; } } } return total; }, Native: function Native() { var total = 0; var _iteratorNormalCompletion3 = true; var _didIteratorError3 = false; var _iteratorError3 = undefined; try { for (var _iterator3 = native[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { var item = _step3.value; total += item; } } catch (err) { _didIteratorError3 = true; _iteratorError3 = err; } finally { try { if (!_iteratorNormalCompletion3 && _iterator3.return) { _iterator3.return(); } } finally { if (_didIteratorError3) { throw _iteratorError3; } } } return total; } }); benchmark('.forEach()', 10000, { Custom: function Custom() { var total = 0; custom.forEach(function (item) { return total += item; }); return total; }, Native: function Native() { var total = 0; native.forEach(function (item) { return total += item; }); return total; } }); benchmark('.map()', 10000, { Custom: function Custom() { return custom.map(function (item) { return item + 1; }); }, Native: function Native() { return native.map(function (item) { return item + 1; }); } }); benchmark('.filter()', 10000, { Custom: function Custom() { return custom.filter(function (item, index) { return index % 2; }); }, Native: function Native() { return native.filter(function (item, index) { return index % 2; }); } }); benchmark('.reduce()', 10000, { Custom: function Custom() { return custom.reduce(function (acc, item) { return acc + item; }, 0); }, Native: function Native() { return native.reduce(function (acc, item) { return acc + item; }, 0); } }); benchmark('.reduce() without an initial value', 10000, { Custom: function Custom() { return custom.reduce(function (acc, item) { return item; }); }, Native: function Native() { return native.reduce(function (acc, item) { return item; }); } }); benchmark('Create instances', 2000, { Custom: function Custom() { return new _Custom(input); }, Native: function Native() { return new _Native(input); } }); }); });