2019-01-05 20:54:13 -08:00
|
|
|
import "allocator/arena";
|
|
|
|
|
|
|
|
import { BSONDecoder, BSONHandler } from "../../assembly/decoder";
|
|
|
|
|
|
|
|
declare function logStr(str: string): void;
|
|
|
|
declare function logF64(val: f64): void;
|
|
|
|
|
|
|
|
class BSONTestHandler extends BSONHandler {
|
2019-01-05 22:39:33 -08:00
|
|
|
isFirstKey: boolean = true
|
|
|
|
inObject: Array<boolean> = [false]
|
|
|
|
result: string = ""
|
2019-01-05 20:54:13 -08:00
|
|
|
|
|
|
|
setString(name: string, value: string): void {
|
2019-01-05 22:39:33 -08:00
|
|
|
this.writeKey(name);
|
|
|
|
this.writeString(value);
|
2019-01-05 20:54:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
setBoolean(name: string, value: bool): void {
|
2019-01-05 22:39:33 -08:00
|
|
|
this.writeKey(name);
|
|
|
|
this.writeBoolean(value);
|
2019-01-05 20:54:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
setNull(name: string): void {
|
2019-01-05 22:39:33 -08:00
|
|
|
this.writeKey(name);
|
|
|
|
this.write("null");
|
2019-01-05 20:54:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
setInteger(name: string, value: i32): void {
|
2019-01-05 22:39:33 -08:00
|
|
|
this.writeKey(name);
|
|
|
|
this.writeInteger(value);
|
2019-01-05 20:54:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
setUint8Array(name: string, value: Uint8Array): void {
|
2019-01-05 22:39:33 -08:00
|
|
|
assert(false, "Not implemented");
|
2019-01-05 20:54:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pushArray(name: string): bool {
|
2019-01-05 22:39:33 -08:00
|
|
|
this.writeKey(name);
|
|
|
|
this.write("[");
|
|
|
|
this.isFirstKey = true
|
|
|
|
this.inObject.push(false);
|
2019-01-05 20:54:13 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
popArray(): void {
|
2019-01-05 22:39:33 -08:00
|
|
|
this.write("]");
|
2019-01-05 20:54:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pushObject(name: string): bool {
|
2019-01-05 22:39:33 -08:00
|
|
|
this.writeKey(name);
|
|
|
|
this.write("{");
|
|
|
|
this.isFirstKey = true
|
|
|
|
this.inObject.push(true);
|
2019-01-05 20:54:13 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
popObject(): void {
|
2019-01-05 22:39:33 -08:00
|
|
|
this.write("}");
|
|
|
|
}
|
|
|
|
|
|
|
|
private writeKey(str: string): void {
|
|
|
|
if (!this.isFirstKey ) {
|
|
|
|
this.write(",");
|
|
|
|
} else {
|
|
|
|
this.isFirstKey = false;
|
|
|
|
}
|
|
|
|
if (str != null) {
|
|
|
|
this.writeString(str);
|
|
|
|
this.write(":");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private writeString(str: string): void {
|
|
|
|
// TODO: Implement encoding
|
|
|
|
this.write(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
private writeBoolean(value: bool): void {
|
|
|
|
this.write(value ? "true" : "false");
|
|
|
|
}
|
|
|
|
|
|
|
|
private writeInteger(value: i32): void {
|
|
|
|
// TODO: More efficient encoding
|
|
|
|
let arr: Array<i32> = [value];
|
|
|
|
this.write(arr.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
private write(str: string): void {
|
|
|
|
this.result += str;
|
2019-01-05 20:54:13 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export class StringConversionTests {
|
2019-01-05 22:39:33 -08:00
|
|
|
private static handler : BSONTestHandler = new BSONTestHandler();
|
2019-01-05 20:54:13 -08:00
|
|
|
|
|
|
|
static setUp(): void {
|
2019-01-05 22:39:33 -08:00
|
|
|
this.handler = new BSONTestHandler();
|
2019-01-05 20:54:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static createDecoder(): BSONDecoder<BSONTestHandler> {
|
2019-01-05 22:39:33 -08:00
|
|
|
return new BSONDecoder(this.handler);
|
2019-01-05 20:54:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static shouldHandleEmptyObject(): bool {
|
2019-01-05 22:39:33 -08:00
|
|
|
this.handler.pushObject(null);
|
2019-01-05 20:54:13 -08:00
|
|
|
this.createDecoder().deserialize(hex2bin("0500000000"));
|
2019-01-05 22:39:33 -08:00
|
|
|
this.handler.popObject();
|
|
|
|
return this.handler.result == "{}";
|
2019-01-05 20:54:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static shouldHandleInt32(): bool {
|
2019-01-05 22:39:33 -08:00
|
|
|
this.handler.pushObject(null);
|
2019-01-05 20:54:13 -08:00
|
|
|
this.createDecoder().deserialize(hex2bin("0e00000010696e74003412000000"));
|
2019-01-05 22:39:33 -08:00
|
|
|
this.handler.popObject();
|
|
|
|
return this.handler.result == "{int:4660}"
|
2019-01-05 20:54:13 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function bytes2array(typedArr: Uint8Array): Array<u8> {
|
|
|
|
let arr = new Array<u8>();
|
|
|
|
for (let i = 0; i < typedArr.length; i++) {
|
|
|
|
arr.push(typedArr[i]);
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
function hex2bin(hex: string): Uint8Array {
|
|
|
|
let bin = new Uint8Array(hex.length >>> 1);
|
|
|
|
for (let i = 0, len = hex.length >>> 1; i < len; i++) {
|
|
|
|
bin[i] = u32(parseInt(hex.substr(i << 1, 2), 16));
|
|
|
|
}
|
|
|
|
return bin;
|
|
|
|
}
|