@bestinslot/nada
Version:
Compression-focused encoding for zero-heavy solidity calldata and bytecode
101 lines • 3.65 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const uvu_1 = require("uvu");
const assert = __importStar(require("uvu/assert"));
const nada_1 = require("../src/nada");
// roundtrip helper
function roundtrip(bytes) {
const original = Uint8Array.from(bytes);
const encoded = (0, nada_1.encode)(original);
const decoded = (0, nada_1.decode)(encoded);
assert.equal(decoded, original);
}
function roundtrip_with_limit(bytes, limit) {
const original = Uint8Array.from(bytes);
const encoded = (0, nada_1.encode)(original);
const decoded = (0, nada_1.decode_with_limit)(encoded, limit);
assert.equal(decoded, original);
}
(0, uvu_1.test)('empty input', () => {
roundtrip([]);
});
(0, uvu_1.test)('no zeros or FFs', () => {
roundtrip([1, 2, 3, 4, 5]);
});
(0, uvu_1.test)('single 0x00', () => {
roundtrip([0]);
});
(0, uvu_1.test)('long zero run', () => {
roundtrip(Array(100).fill(0));
});
(0, uvu_1.test)('single 0xFF', () => {
roundtrip([0xFF]);
});
(0, uvu_1.test)('double 0xFF', () => {
roundtrip([0xFF, 0xFF]);
});
(0, uvu_1.test)('mixed sequence', () => {
roundtrip([1, 0, 0, 0, 2, 0xFF, 0xFF, 0xFF, 3, 0]);
});
(0, uvu_1.test)('long zero run with FFs', () => {
roundtrip([0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0]);
});
(0, uvu_1.test)('long zero run with FFs and other data', () => {
roundtrip([0, 0, 0, 0, 1, 2, 3, 0xFF, 0xFF, 0]);
});
(0, uvu_1.test)('long zero run with FFs and other data, limit 10', () => {
roundtrip_with_limit([0, 0, 0, 0, 1, 2, 3, 0xFF, 0xFF, 0], 10);
});
(0, uvu_1.test)('reserved sequence 0xff00', () => {
// check error is thrown
assert.throws(function () {
(0, nada_1.decode)(Uint8Array.from([0, 0, 0, 0, 1, 2, 3, 0xFF, 0]));
}, /invalid nada sequence: 0xFF 0x00/);
});
(0, uvu_1.test)('last byte is ff', () => {
// check error is thrown
assert.throws(function () {
(0, nada_1.decode)(Uint8Array.from([0xFF]));
}, /unexpected end of input/);
});
(0, uvu_1.test)('reached limit', () => {
// check error is thrown
assert.throws(function () {
(0, nada_1.decode_with_limit)(Uint8Array.from([0, 0xFF, 0xFF, 0]), 9);
}, /output length exceeded limit: 9/);
});
uvu_1.test.run();
//# sourceMappingURL=nada.test.js.map