tjson-js
Version:
Tagged JSON (TJSON): a JSON-based microformat with rich type annotations
88 lines (69 loc) • 2.71 kB
text/typescript
import { suite, test } from "mocha-typescript";
import { expect } from "chai";
import { Binary16Type, Binary32Type, Binary64Type } from "../../src/datatype/binary";
class Binary16Test {
"decodes hexadecimal"() {
let expected = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]);
expect((new Binary16Type).decode("48656c6c6f2c20776f726c6421")).to.eql(expected);
}
"encodes hexadecimal"() {
let input = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]);
expect((new Binary16Type).encode(input)).to.eql("48656c6c6f2c20776f726c6421");
}
"round trips 0-256 byte byte arrays"() {
for (let i = 0; i <= 256; i++) {
let input = new Uint8Array(i);
for (let j = 0; j < i; j++) {
input[j] = j;
}
let type = new Binary16Type;
let encoded = type.encode(input);
let output = type.decode(encoded);
expect(input).to.eql(output);
}
}
}
class Binary32Test {
"decodes RFC 4648 Base32 (lower-case, no padding)"() {
let expected = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]);
expect((new Binary32Type).decode("jbswy3dpfqqho33snrscc")).to.eql(expected);
}
"encodes RFC 4648 Base32 (lower-case, no padding)"() {
let input = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]);
expect((new Binary32Type).encode(input)).to.eql("jbswy3dpfqqho33snrscc");
}
"round trips 0-256 byte byte arrays"() {
for (let i = 0; i <= 256; i++) {
let input = new Uint8Array(i);
for (let j = 0; j < i; j++) {
input[j] = j;
}
let type = new Binary32Type;
let encoded = type.encode(input);
let output = type.decode(encoded);
expect(input).to.eql(output);
}
}
}
class Binary64Test {
"decodes RFC 4648 Base64url (no padding)"() {
let expected = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]);
expect((new Binary64Type).decode("SGVsbG8sIHdvcmxkIQ")).to.eql(expected);
}
"encodes RFC 4648 Base64url (no padding)"() {
let input = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]);
expect((new Binary64Type).encode(input)).to.eql("SGVsbG8sIHdvcmxkIQ");
}
"round trips 0-256 byte byte arrays"() {
for (let i = 0; i <= 256; i++) {
let input = new Uint8Array(i);
for (let j = 0; j < i; j++) {
input[j] = j;
}
let type = new Binary64Type;
let encoded = type.encode(input);
let output = type.decode(encoded);
expect(input).to.eql(output);
}
}
}