blob: 68227ce64084f1447dc14b383ee015e3ddb7bf9f [file]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import Fory, { TypeInfo, Type } from "../packages/core/index";
import { describe, expect, test } from "@jest/globals";
import { fromUint8Array } from "../packages/core/lib/platformBuffer";
describe("fory", () => {
test("defaults to compatible mode unless explicitly set", () => {
expect(new Fory().config.compatible).toBe(true);
expect(new Fory({ compatible: false }).config.compatible).toBe(false);
});
test("should deserialize null work", () => {
const fory = new Fory({ compatible: false });
expect(fory.deserialize(new Uint8Array([0x41, 253]))).toBe(null);
});
test("should deserialize xlang disable work", () => {
const fory = new Fory({ compatible: false });
try {
// bits 6..7 = 01 (Fory 2.0), bit 0 = xlang flag, bit 1 = oob flag
// value 0x40 means Fory 2.0 native mode (xlang disabled)
fory.deserialize(new Uint8Array([0x40]));
throw new Error("unreachable code");
} catch (error) {
expect(error.message).toBe("support crosslanguage mode only");
}
});
test("should deserialize oob mode work", () => {
const fory = new Fory({ compatible: false });
try {
// value 0x43 = Fory 2.0 (0x40) + xlang (1) + oob (2)
fory.deserialize(new Uint8Array([0x43]));
throw new Error("unreachable code");
} catch (error) {
expect(error.message).toBe("outofband mode is not supported now");
}
});
test("can serialize and deserialize primitive types", () => {
const typeinfo = Type.int8();
testTypeInfo(typeinfo, 123);
const typeinfo2 = Type.int16();
testTypeInfo(typeinfo2, 123);
const typeinfo3 = Type.int32();
testTypeInfo(typeinfo3, 123);
const typeinfo4 = Type.bool();
testTypeInfo(typeinfo4, true);
// has precision problem
// const typeinfo5 = Type.float()
// testTypeInfo(typeinfo5, 123.456)
const typeinfo6 = Type.float64();
testTypeInfo(typeinfo6, 123.456789);
const typeinfo7 = Type.binary();
testTypeInfo(typeinfo7, new Uint8Array([1, 2, 3]), new Uint8Array([1, 2, 3]));
const typeinfo8 = Type.string();
testTypeInfo(typeinfo8, "123");
});
function testTypeInfo(typeinfo: TypeInfo, input: any, expected?: any) {
const fory = new Fory({ compatible: false });
const serialize = fory.register(typeinfo);
const result = serialize.deserialize(serialize.serialize(input));
expect(result).toEqual(expected ?? input);
}
test("should meta string state not grow across root serializations", () => {
// Meta string owners re-push themselves on every root serialize; if
// reset() does not truncate the dispose list, it grows without bound and
// every reset walks the whole history.
const fory = new Fory({ compatible: false });
const { serialize } = fory.register(
Type.struct({ namespace: "example", typeName: "Item" }, { a: Type.int32() }),
);
serialize({ a: 1 });
const metaStringWriter = (fory as any).writeContext.metaStringWriter;
const baseline = metaStringWriter.disposeMetaStringBytes.length;
for (let i = 0; i < 100; i++) {
serialize({ a: i });
}
expect(metaStringWriter.disposeMetaStringBytes.length).toBe(baseline);
});
});