UNPKG

muschema

Version:
186 lines 6.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var test = require("tape"); var trace_1 = require("../trace"); var __1 = require("../"); var mustreams_1 = require("mustreams"); function diffPatch(schema, x, y) { var out = new mustreams_1.MuWriteStream(1); schema.diff(x, y, out); schema.diff(y, x, out); var inp = new mustreams_1.MuReadStream(out.bytes()); schema.patch(x, inp); schema.patch(y, inp); } test('array - alloc & free', function (t) { var characterSchema = new trace_1.MuSchemaTrace(new __1.MuStruct({ hp: new __1.MuUint8(100), })); var groupSchema = new __1.MuArray(characterSchema); function createCharacter() { var result = characterSchema.alloc(); result.hp = Math.random() * 100 | 0; return result; } function createGroup() { var result = groupSchema.alloc(); result.length = Math.ceil(Math.random() * 100); for (var i = 0; i < result.length; ++i) { result[i] = createCharacter(); } return result; } var logger = characterSchema.createLog('slave'); logger.begin(); var slaves = createGroup(); var gladiators = groupSchema.clone(slaves); var numCharacters = slaves.length; groupSchema.free(slaves); groupSchema.free(gladiators); logger.end(); t.equals(logger.allocCount, numCharacters * 2); t.equals(logger.freeCount, logger.allocCount); var evilGeniuses = createGroup(); var superHeroes = createGroup(); var characterAlloc = characterSchema.allocCount; diffPatch(groupSchema, evilGeniuses, superHeroes); var allocIncr = characterSchema.allocCount - characterAlloc; characterAlloc = characterSchema.allocCount; var iterations = Math.ceil(Math.random() * 100); for (var i = 0; i < iterations; ++i) { diffPatch(groupSchema, evilGeniuses, superHeroes); } t.equals(characterSchema.allocCount - characterAlloc, iterations * allocIncr); t.end(); }); test('dictionary - alloc & free', function (t) { var contactSchema = new trace_1.MuSchemaTrace(new __1.MuStruct({ email: new __1.MuString(), })); var contactsSchema = new __1.MuDictionary(contactSchema); function randomName() { var result = new Array(10); for (var i = 0; i < 10; ++i) { result[i] = String.fromCharCode((Math.random() * 26 | 0) + 97); } return result.join(''); } function randomContacts() { var result = contactsSchema.alloc(); for (var i = 0; i < (Math.random() * 100 | 0); ++i) { var name_1 = randomName(); result[name_1] = contactSchema.alloc(); result[name_1].email = name_1 + "@stardust.wizard"; } return result; } var logger = contactSchema.createLog('phone book'); logger.begin(); var alumni = randomContacts(); var alumniCopy = contactsSchema.clone(alumni); var numContacts = Object.keys(alumni).length; contactsSchema.free(alumni); contactsSchema.free(alumniCopy); logger.end(); t.equals(logger.allocCount, numContacts * 2); t.equals(logger.freeCount, logger.allocCount); var colleagues = randomContacts(); var coworkers = randomContacts(); var contactAlloc = contactSchema.allocCount; diffPatch(contactsSchema, colleagues, coworkers); var allocIncr = contactSchema.allocCount - contactAlloc; contactAlloc = contactSchema.allocCount; var iterations = Math.ceil(Math.random() * 100); for (var i = 0; i < iterations; ++i) { diffPatch(contactsSchema, colleagues, coworkers); } t.equals(contactSchema.allocCount - contactAlloc, iterations * allocIncr); t.end(); }); test('sorted array - alloc & free', function (t) { var characterSchema = new trace_1.MuSchemaTrace(new __1.MuStruct({ hp: new __1.MuUint8(100), })); function compare(a, b) { if (a.hp < b.hp) { return -1; } else if (a.hp > b.hp) { return 1; } else { return 0; } } var groupSchema = new __1.MuSortedArray(characterSchema, compare); function createCharacter() { var result = characterSchema.alloc(); result.hp = Math.random() * 100 | 0; return result; } function createGroup() { var result = groupSchema.alloc(); result.length = Math.ceil(Math.random() * 100); for (var i = 0; i < result.length; ++i) { result[i] = createCharacter(); } return result.sort(compare); } var logger = characterSchema.createLog('slave'); logger.begin(); var slaves = createGroup(); var gladiators = groupSchema.clone(slaves); var numCharacters = slaves.length; groupSchema.free(slaves); groupSchema.free(gladiators); logger.end(); t.equals(logger.allocCount, numCharacters * 2); t.equals(logger.freeCount, logger.allocCount); var horde = createGroup(); var alliance = createGroup(); var characterAlloc = characterSchema.allocCount; diffPatch(groupSchema, horde, alliance); var allocIncr = characterSchema.allocCount - characterAlloc; characterAlloc = characterSchema.allocCount; var iterations = Math.ceil(Math.random() * 100); for (var i = 0; i < iterations; ++i) { diffPatch(groupSchema, horde, alliance); } t.equals(characterSchema.allocCount - characterAlloc, iterations * allocIncr); t.end(); }); test('struct - alloc & free', function (t) { var coordSchema = new trace_1.MuSchemaTrace(new __1.MuStruct({ x: new __1.MuFloat32(), y: new __1.MuFloat32(), })); var characterSchema = new trace_1.MuSchemaTrace(new __1.MuStruct({ hp: new __1.MuUint8(100), coordinates: coordSchema, })); var coordLogger = coordSchema.createLog('coordinates'); coordLogger.begin(); var iterations = Math.ceil(Math.random() * 100); for (var i = 0; i < iterations; ++i) { var Megatron = characterSchema.alloc(); var OptimusPrime = characterSchema.clone(Megatron); characterSchema.free(Megatron); characterSchema.free(OptimusPrime); } coordLogger.end(); t.equals(coordLogger.allocCount, iterations * 2); t.equals(coordLogger.freeCount, coordLogger.allocCount); var boy = characterSchema.alloc(); var girl = characterSchema.alloc(); var coordAlloc = coordSchema.allocCount; diffPatch(characterSchema, boy, girl); var allocIncr = coordSchema.allocCount - coordAlloc; coordAlloc = coordSchema.allocCount; iterations = Math.ceil(Math.random() * 100); for (var i = 0; i < iterations; ++i) { diffPatch(characterSchema, boy, girl); } t.equals(coordSchema.allocCount - coordAlloc, iterations * allocIncr); t.end(); }); //# sourceMappingURL=memory.js.map