UNPKG

mudb

Version:

Real-time database for multiplayer games

161 lines 8.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const test = require("tape"); const index_1 = require("../index"); test('default identity', (t) => { t.equal(new index_1.MuVoid().identity, undefined); t.equal(new index_1.MuBoolean().identity, false); t.equal(new index_1.MuASCII().identity, ''); t.equal(new index_1.MuUTF8().identity, ''); t.equal(new index_1.MuFloat32().identity, 0); t.equal(new index_1.MuFloat64().identity, 0); t.equal(new index_1.MuInt8().identity, 0); t.equal(new index_1.MuInt16().identity, 0); t.equal(new index_1.MuInt32().identity, 0); t.equal(new index_1.MuUint8().identity, 0); t.equal(new index_1.MuUint16().identity, 0); t.equal(new index_1.MuUint32().identity, 0); t.equal(new index_1.MuVarint().identity, 0); t.equal(new index_1.MuRelativeVarint().identity, 0); t.deepEqual(new index_1.MuArray(new index_1.MuFloat32(), 0).identity, []); t.deepEqual(new index_1.MuSortedArray(new index_1.MuFloat32(), 0).identity, []); t.deepEqual(new index_1.MuStruct({ f: new index_1.MuFloat32() }).identity, { f: 0 }); t.deepEqual(new index_1.MuUnion({ f: new index_1.MuFloat32() }).identity, { type: '', data: undefined }); t.deepEqual(new index_1.MuBytes().identity, new Uint8Array(1)); t.deepEqual(new index_1.MuDictionary(new index_1.MuFloat32(), 0).identity, {}); t.deepEqual(new index_1.MuVector(new index_1.MuFloat32(), 3).identity, new Float32Array(3)); t.true(new index_1.MuDate().identity instanceof Date); t.equal(new index_1.MuOption(new index_1.MuFloat32()).identity, 0); t.end(); }); test('setting identity', (t) => { t.equal(new index_1.MuBoolean(true).identity, true); t.equal(new index_1.MuASCII('skr').identity, 'skr'); t.equal(new index_1.MuUTF8('Iñtërn').identity, 'Iñtërn'); t.equal(new index_1.MuFixedASCII(1).identity, ' '); t.equal(new index_1.MuFixedASCII(2).identity, ' '); t.equal(new index_1.MuFixedASCII('000').identity, '000'); t.equal(new index_1.MuFloat32(3).identity, 3); t.equal(new index_1.MuFloat64(Math.E).identity, Math.E); t.equal(new index_1.MuInt8(-0x80).identity, -0x80); t.equal(new index_1.MuInt16(-0x8000).identity, -0x8000); t.equal(new index_1.MuInt32(-0x80000000).identity, -0x80000000); t.equal(new index_1.MuUint8(0xFF).identity, 0xFF); t.equal(new index_1.MuUint16(0xFFFF).identity, 0xFFFF); t.equal(new index_1.MuUint32(0xFFFFFFFF).identity, 0xFFFFFFFF); t.equal(new index_1.MuVarint(0xFFFFFFFF).identity, 0xFFFFFFFF); t.equal(new index_1.MuRelativeVarint(0xFFFFFFFF).identity, 0xFFFFFFFF); const a = [{}, {}]; const array = new index_1.MuArray(new index_1.MuDictionary(new index_1.MuFloat32(), Infinity), Infinity, a); t.deepEqual(array.identity, a); t.isNot(array.identity, a); t.isNot(array.identity[0], a[0]); const sa = [1, 3, 2]; const sortedArray = new index_1.MuSortedArray(new index_1.MuFloat32(), Infinity, undefined, sa); t.deepEqual(sortedArray.identity, [1, 2, 3]); t.isNot(sortedArray.identity, sa); const union = new index_1.MuUnion({ f: new index_1.MuFloat32() }, 'f'); t.deepEqual(union.identity, { type: 'f', data: 0 }); t.equal(new index_1.MuOption(new index_1.MuFloat32(), 666).identity, 666); t.equal(new index_1.MuOption(new index_1.MuFloat32(), undefined).identity, 0); t.equal(new index_1.MuOption(new index_1.MuFloat32(), undefined, true).identity, undefined); const b = new Uint8Array([0, 1, 2]); const bytes = new index_1.MuBytes(b); t.deepEqual(bytes.identity, b); t.isNot(bytes.identity, b); const dict = { x: [], y: [] }; const dictionary = new index_1.MuDictionary(new index_1.MuArray(new index_1.MuFloat32(), Infinity), Infinity, dict); t.deepEqual(dictionary.identity, dict); t.isNot(dictionary.identity, dict); t.isNot(dictionary.identity.x, dict.x); const vector = new index_1.MuVector(new index_1.MuFloat32(1), 3); t.deepEqual(vector.identity, new Float32Array([1, 1, 1])); const d = new Date(); const date = new index_1.MuDate(d); t.deepEqual(date.identity, d); t.isNot(date.identity, d); const o = { n: 0, b: false, a: [] }; const json = new index_1.MuJSON(o); t.deepEqual(json.identity, o); t.isNot(json.identity, o); t.isNot(json.identity['a'], o['a']); t.end(); }); test('numeric schema identity range', (t) => { t.doesNotThrow(() => new index_1.MuFloat32(-3.4e+38)); t.doesNotThrow(() => new index_1.MuFloat32(3.4e+38)); t.doesNotThrow(() => new index_1.MuFloat64(-1.7e+308)); t.doesNotThrow(() => new index_1.MuFloat64(1.7e308)); t.doesNotThrow(() => new index_1.MuInt8(-0x80)); t.doesNotThrow(() => new index_1.MuInt8(0x7F)); t.doesNotThrow(() => new index_1.MuInt16(-0x8000)); t.doesNotThrow(() => new index_1.MuInt16(0x7FFF)); t.doesNotThrow(() => new index_1.MuInt32(-0x80000000)); t.doesNotThrow(() => new index_1.MuInt32(0x7FFFFFFF)); t.doesNotThrow(() => new index_1.MuUint8(0)); t.doesNotThrow(() => new index_1.MuUint8(0xFF)); t.doesNotThrow(() => new index_1.MuUint16(0)); t.doesNotThrow(() => new index_1.MuUint16(0xFFFF)); t.doesNotThrow(() => new index_1.MuUint32(0)); t.doesNotThrow(() => new index_1.MuUint32(0xFFFFFFFF)); t.throws(() => new index_1.MuFloat32(-3.41e+38), RangeError); t.throws(() => new index_1.MuFloat32(3.41e+38), RangeError); t.throws(() => new index_1.MuInt8(-0x81), RangeError); t.throws(() => new index_1.MuInt8(0x80), RangeError); t.throws(() => new index_1.MuInt16(-0x8001), RangeError); t.throws(() => new index_1.MuInt16(0x8000), RangeError); t.throws(() => new index_1.MuInt32(-0x80000001), RangeError); t.throws(() => new index_1.MuInt32(0x80000000), RangeError); t.throws(() => new index_1.MuUint8(-1), RangeError); t.throws(() => new index_1.MuUint8(0x100), RangeError); t.throws(() => new index_1.MuUint16(-1), RangeError); t.throws(() => new index_1.MuUint16(0x10000), RangeError); t.throws(() => new index_1.MuUint32(-1), RangeError); t.throws(() => new index_1.MuUint32(0x100000000), RangeError); t.end(); }); test('setting Infinity as identity', (t) => { t.doesNotThrow(() => new index_1.MuFloat32(Infinity)); t.doesNotThrow(() => new index_1.MuFloat64(Infinity)); t.throws(() => new index_1.MuInt8(Infinity)); t.throws(() => new index_1.MuInt16(Infinity)); t.throws(() => new index_1.MuInt32(Infinity)); t.throws(() => new index_1.MuUint8(Infinity)); t.throws(() => new index_1.MuUint16(Infinity)); t.throws(() => new index_1.MuUint32(Infinity)); t.throws(() => new index_1.MuVarint(Infinity)); t.throws(() => new index_1.MuRelativeVarint(Infinity)); t.doesNotThrow(() => new index_1.MuFloat32(-Infinity)); t.doesNotThrow(() => new index_1.MuFloat64(-Infinity)); t.throws(() => new index_1.MuInt8(-Infinity)); t.throws(() => new index_1.MuInt16(-Infinity)); t.throws(() => new index_1.MuInt32(-Infinity)); t.throws(() => new index_1.MuUint8(-Infinity)); t.throws(() => new index_1.MuUint16(-Infinity)); t.throws(() => new index_1.MuUint32(-Infinity)); t.throws(() => new index_1.MuVarint(-Infinity)); t.throws(() => new index_1.MuRelativeVarint(-Infinity)); t.equal(new index_1.MuFloat32(Infinity).identity, Infinity); t.equal(new index_1.MuFloat32(-Infinity).identity, -Infinity); t.equal(new index_1.MuFloat64(Infinity).identity, Infinity); t.equal(new index_1.MuFloat64(-Infinity).identity, -Infinity); t.end(); }); test('setting NaN as identity', (t) => { t.doesNotThrow(() => new index_1.MuFloat32(NaN)); t.doesNotThrow(() => new index_1.MuFloat64(NaN)); t.throws(() => new index_1.MuInt8(NaN)); t.throws(() => new index_1.MuInt16(NaN)); t.throws(() => new index_1.MuInt32(NaN)); t.throws(() => new index_1.MuUint8(NaN)); t.throws(() => new index_1.MuUint16(NaN)); t.throws(() => new index_1.MuUint32(NaN)); t.throws(() => new index_1.MuVarint(NaN)); t.throws(() => new index_1.MuRelativeVarint(NaN)); const f32 = new index_1.MuFloat32(NaN); t.true(f32.identity !== f32.identity); const f64 = new index_1.MuFloat64(NaN); t.true(f64.identity !== f64.identity); t.end(); }); //# sourceMappingURL=identity.js.map