2018-11-08 12:26:20 -05:00
|
|
|
// Based on https://github.com/golang/go/blob/891682/src/bufio/bufio_test.go
|
2018-11-07 14:17:36 -05:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2019-02-26 00:35:50 -05:00
|
|
|
const { Buffer } = Deno;
|
2019-03-07 19:25:16 -05:00
|
|
|
type Reader = Deno.Reader;
|
2019-05-23 22:04:06 -04:00
|
|
|
import { test, runIfMain } from "../testing/mod.ts";
|
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
|
|
|
assertNotEquals,
|
|
|
|
fail
|
|
|
|
} from "../testing/asserts.ts";
|
|
|
|
import {
|
|
|
|
BufReader,
|
|
|
|
BufWriter,
|
|
|
|
BufferFullError,
|
|
|
|
UnexpectedEOFError
|
|
|
|
} from "./bufio.ts";
|
2018-11-07 20:28:01 -05:00
|
|
|
import * as iotest from "./iotest.ts";
|
2018-12-17 22:40:42 -05:00
|
|
|
import { charCode, copyBytes, stringsReader } from "./util.ts";
|
2018-11-08 02:05:06 -05:00
|
|
|
|
|
|
|
const encoder = new TextEncoder();
|
2018-11-07 13:16:07 -05:00
|
|
|
|
2019-07-07 15:20:41 -04:00
|
|
|
function assertNotEOF<T extends {}>(val: T | Deno.EOF): T {
|
|
|
|
assertNotEquals(val, Deno.EOF);
|
2019-05-23 22:04:06 -04:00
|
|
|
return val as T;
|
|
|
|
}
|
|
|
|
|
2018-11-07 21:01:32 -05:00
|
|
|
async function readBytes(buf: BufReader): Promise<string> {
|
2018-11-07 13:16:07 -05:00
|
|
|
const b = new Uint8Array(1000);
|
|
|
|
let nb = 0;
|
|
|
|
while (true) {
|
|
|
|
let c = await buf.readByte();
|
2019-07-07 15:20:41 -04:00
|
|
|
if (c === Deno.EOF) {
|
2018-11-07 13:16:07 -05:00
|
|
|
break; // EOF
|
|
|
|
}
|
|
|
|
b[nb] = c;
|
|
|
|
nb++;
|
|
|
|
}
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
return decoder.decode(b.subarray(0, nb));
|
|
|
|
}
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function bufioReaderSimple(): Promise<void> {
|
2018-11-07 13:16:07 -05:00
|
|
|
const data = "hello world";
|
2018-11-07 21:01:32 -05:00
|
|
|
const b = new BufReader(stringsReader(data));
|
2018-11-07 13:16:07 -05:00
|
|
|
const s = await readBytes(b);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(s, data);
|
2018-11-07 13:16:07 -05:00
|
|
|
});
|
2018-11-07 14:17:36 -05:00
|
|
|
|
2019-03-04 19:53:35 -05:00
|
|
|
interface ReadMaker {
|
|
|
|
name: string;
|
|
|
|
fn: (r: Reader) => Reader;
|
|
|
|
}
|
2018-11-07 14:17:36 -05:00
|
|
|
|
|
|
|
const readMakers: ReadMaker[] = [
|
2019-04-24 07:41:23 -04:00
|
|
|
{ name: "full", fn: (r): Reader => r },
|
|
|
|
{
|
|
|
|
name: "byte",
|
|
|
|
fn: (r): iotest.OneByteReader => new iotest.OneByteReader(r)
|
|
|
|
},
|
|
|
|
{ name: "half", fn: (r): iotest.HalfReader => new iotest.HalfReader(r) }
|
2018-11-07 20:28:01 -05:00
|
|
|
// TODO { name: "data+err", r => new iotest.DataErrReader(r) },
|
|
|
|
// { name: "timeout", fn: r => new iotest.TimeoutReader(r) },
|
2018-11-07 14:17:36 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
// Call read to accumulate the text of a file
|
2018-11-07 21:01:32 -05:00
|
|
|
async function reads(buf: BufReader, m: number): Promise<string> {
|
2018-11-07 14:17:36 -05:00
|
|
|
const b = new Uint8Array(1000);
|
|
|
|
let nb = 0;
|
|
|
|
while (true) {
|
2019-07-07 15:20:41 -04:00
|
|
|
const result = await buf.read(b.subarray(nb, nb + m));
|
|
|
|
if (result === Deno.EOF) {
|
2018-11-07 14:17:36 -05:00
|
|
|
break;
|
|
|
|
}
|
2019-07-07 15:20:41 -04:00
|
|
|
nb += result;
|
2018-11-07 14:17:36 -05:00
|
|
|
}
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
return decoder.decode(b.subarray(0, nb));
|
|
|
|
}
|
|
|
|
|
2019-03-04 19:53:35 -05:00
|
|
|
interface NamedBufReader {
|
|
|
|
name: string;
|
|
|
|
fn: (r: BufReader) => Promise<string>;
|
|
|
|
}
|
2018-11-07 14:17:36 -05:00
|
|
|
|
2018-11-07 21:01:32 -05:00
|
|
|
const bufreaders: NamedBufReader[] = [
|
2019-04-24 07:41:23 -04:00
|
|
|
{ name: "1", fn: (b: BufReader): Promise<string> => reads(b, 1) },
|
|
|
|
{ name: "2", fn: (b: BufReader): Promise<string> => reads(b, 2) },
|
|
|
|
{ name: "3", fn: (b: BufReader): Promise<string> => reads(b, 3) },
|
|
|
|
{ name: "4", fn: (b: BufReader): Promise<string> => reads(b, 4) },
|
|
|
|
{ name: "5", fn: (b: BufReader): Promise<string> => reads(b, 5) },
|
|
|
|
{ name: "7", fn: (b: BufReader): Promise<string> => reads(b, 7) },
|
2018-11-07 23:19:08 -05:00
|
|
|
{ name: "bytes", fn: readBytes }
|
2018-11-07 20:28:01 -05:00
|
|
|
// { name: "lines", fn: readLines },
|
2018-11-07 14:17:36 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
const MIN_READ_BUFFER_SIZE = 16;
|
|
|
|
const bufsizes: number[] = [
|
|
|
|
0,
|
|
|
|
MIN_READ_BUFFER_SIZE,
|
|
|
|
23,
|
|
|
|
32,
|
|
|
|
46,
|
|
|
|
64,
|
|
|
|
93,
|
|
|
|
128,
|
|
|
|
1024,
|
|
|
|
4096
|
|
|
|
];
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function bufioBufReader(): Promise<void> {
|
2018-11-07 14:17:36 -05:00
|
|
|
const texts = new Array<string>(31);
|
|
|
|
let str = "";
|
|
|
|
let all = "";
|
|
|
|
for (let i = 0; i < texts.length - 1; i++) {
|
|
|
|
texts[i] = str + "\n";
|
|
|
|
all += texts[i];
|
2018-12-18 21:29:39 -05:00
|
|
|
str += String.fromCharCode((i % 26) + 97);
|
2018-11-07 14:17:36 -05:00
|
|
|
}
|
|
|
|
texts[texts.length - 1] = all;
|
|
|
|
|
|
|
|
for (let text of texts) {
|
|
|
|
for (let readmaker of readMakers) {
|
|
|
|
for (let bufreader of bufreaders) {
|
|
|
|
for (let bufsize of bufsizes) {
|
|
|
|
const read = readmaker.fn(stringsReader(text));
|
2018-11-07 21:01:32 -05:00
|
|
|
const buf = new BufReader(read, bufsize);
|
2018-11-07 14:17:36 -05:00
|
|
|
const s = await bufreader.fn(buf);
|
|
|
|
const debugStr =
|
|
|
|
`reader=${readmaker.name} ` +
|
|
|
|
`fn=${bufreader.name} bufsize=${bufsize} want=${text} got=${s}`;
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(s, text, debugStr);
|
2018-11-07 14:17:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-11-07 23:19:08 -05:00
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function bufioBufferFull(): Promise<void> {
|
2018-11-07 23:19:08 -05:00
|
|
|
const longString =
|
2019-06-19 00:22:01 -04:00
|
|
|
"And now, hello, world! It is the time for all good men to come to the" +
|
|
|
|
" aid of their party";
|
2018-11-07 23:19:08 -05:00
|
|
|
const buf = new BufReader(stringsReader(longString), MIN_READ_BUFFER_SIZE);
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
try {
|
|
|
|
await buf.readSlice(charCode("!"));
|
|
|
|
fail("readSlice should throw");
|
|
|
|
} catch (err) {
|
|
|
|
assert(err instanceof BufferFullError);
|
|
|
|
assert(err.partial instanceof Uint8Array);
|
|
|
|
assertEquals(decoder.decode(err.partial), "And now, hello, ");
|
|
|
|
}
|
|
|
|
|
|
|
|
const line = assertNotEOF(await buf.readSlice(charCode("!")));
|
|
|
|
const actual = decoder.decode(line);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(actual, "world!");
|
2018-11-08 02:05:06 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
const testInput = encoder.encode(
|
|
|
|
"012\n345\n678\n9ab\ncde\nfgh\nijk\nlmn\nopq\nrst\nuvw\nxy"
|
|
|
|
);
|
|
|
|
const testInputrn = encoder.encode(
|
2019-06-19 00:22:01 -04:00
|
|
|
"012\r\n345\r\n678\r\n9ab\r\ncde\r\nfgh\r\nijk\r\nlmn\r\nopq\r\nrst\r\n" +
|
|
|
|
"uvw\r\nxy\r\n\n\r\n"
|
2018-11-08 02:05:06 -05:00
|
|
|
);
|
|
|
|
const testOutput = encoder.encode("0123456789abcdefghijklmnopqrstuvwxy");
|
|
|
|
|
|
|
|
// TestReader wraps a Uint8Array and returns reads of a specific length.
|
|
|
|
class TestReader implements Reader {
|
|
|
|
constructor(private data: Uint8Array, private stride: number) {}
|
|
|
|
|
2019-07-07 15:20:41 -04:00
|
|
|
async read(buf: Uint8Array): Promise<number | Deno.EOF> {
|
2018-11-08 02:05:06 -05:00
|
|
|
let nread = this.stride;
|
|
|
|
if (nread > this.data.byteLength) {
|
|
|
|
nread = this.data.byteLength;
|
|
|
|
}
|
|
|
|
if (nread > buf.byteLength) {
|
|
|
|
nread = buf.byteLength;
|
|
|
|
}
|
2019-07-07 15:20:41 -04:00
|
|
|
if (nread === 0) {
|
|
|
|
return Deno.EOF;
|
|
|
|
}
|
2018-11-08 02:05:06 -05:00
|
|
|
copyBytes(buf as Uint8Array, this.data);
|
|
|
|
this.data = this.data.subarray(nread);
|
2019-07-07 15:20:41 -04:00
|
|
|
return nread;
|
2018-11-08 02:05:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function testReadLine(input: Uint8Array): Promise<void> {
|
|
|
|
for (let stride = 1; stride < 2; stride++) {
|
|
|
|
let done = 0;
|
|
|
|
let reader = new TestReader(input, stride);
|
|
|
|
let l = new BufReader(reader, input.byteLength + 1);
|
|
|
|
while (true) {
|
2019-05-23 22:04:06 -04:00
|
|
|
const r = await l.readLine();
|
2019-07-07 15:20:41 -04:00
|
|
|
if (r === Deno.EOF) {
|
2018-11-08 02:05:06 -05:00
|
|
|
break;
|
|
|
|
}
|
2019-05-23 22:04:06 -04:00
|
|
|
const { line, more } = r;
|
|
|
|
assertEquals(more, false);
|
2019-03-04 19:53:35 -05:00
|
|
|
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
|
2018-11-08 02:05:06 -05:00
|
|
|
let want = testOutput.subarray(done, done + line.byteLength);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(
|
2018-11-08 02:05:06 -05:00
|
|
|
line,
|
|
|
|
want,
|
|
|
|
`Bad line at stride ${stride}: want: ${want} got: ${line}`
|
|
|
|
);
|
|
|
|
done += line.byteLength;
|
|
|
|
}
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(
|
2018-11-08 02:05:06 -05:00
|
|
|
done,
|
|
|
|
testOutput.byteLength,
|
|
|
|
`readLine didn't return everything: got: ${done}, ` +
|
|
|
|
`want: ${testOutput} (stride: ${stride})`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function bufioReadLine(): Promise<void> {
|
2018-11-08 02:05:06 -05:00
|
|
|
await testReadLine(testInput);
|
|
|
|
await testReadLine(testInputrn);
|
2018-11-07 23:19:08 -05:00
|
|
|
});
|
2018-11-08 04:01:20 -05:00
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function bufioPeek(): Promise<void> {
|
2018-11-08 04:01:20 -05:00
|
|
|
const decoder = new TextDecoder();
|
|
|
|
let p = new Uint8Array(10);
|
|
|
|
// string is 16 (minReadBufferSize) long.
|
|
|
|
let buf = new BufReader(
|
|
|
|
stringsReader("abcdefghijklmnop"),
|
|
|
|
MIN_READ_BUFFER_SIZE
|
|
|
|
);
|
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
let actual = assertNotEOF(await buf.peek(1));
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(decoder.decode(actual), "a");
|
2018-11-08 04:01:20 -05:00
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
actual = assertNotEOF(await buf.peek(4));
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(decoder.decode(actual), "abcd");
|
2018-11-08 04:01:20 -05:00
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
try {
|
|
|
|
await buf.peek(32);
|
|
|
|
fail("peek() should throw");
|
|
|
|
} catch (err) {
|
|
|
|
assert(err instanceof BufferFullError);
|
|
|
|
assert(err.partial instanceof Uint8Array);
|
|
|
|
assertEquals(decoder.decode(err.partial), "abcdefghijklmnop");
|
|
|
|
}
|
2018-11-08 04:01:20 -05:00
|
|
|
|
|
|
|
await buf.read(p.subarray(0, 3));
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(decoder.decode(p.subarray(0, 3)), "abc");
|
2018-11-08 04:01:20 -05:00
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
actual = assertNotEOF(await buf.peek(1));
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(decoder.decode(actual), "d");
|
2018-11-08 04:01:20 -05:00
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
actual = assertNotEOF(await buf.peek(1));
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(decoder.decode(actual), "d");
|
2018-11-08 04:01:20 -05:00
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
actual = assertNotEOF(await buf.peek(1));
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(decoder.decode(actual), "d");
|
2018-11-08 04:01:20 -05:00
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
actual = assertNotEOF(await buf.peek(2));
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(decoder.decode(actual), "de");
|
2018-11-08 04:01:20 -05:00
|
|
|
|
2019-07-07 15:20:41 -04:00
|
|
|
let res = await buf.read(p.subarray(0, 3));
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(decoder.decode(p.subarray(0, 3)), "def");
|
2019-07-07 15:20:41 -04:00
|
|
|
assert(res !== Deno.EOF);
|
2018-11-08 04:01:20 -05:00
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
actual = assertNotEOF(await buf.peek(4));
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(decoder.decode(actual), "ghij");
|
2018-11-08 04:01:20 -05:00
|
|
|
|
|
|
|
await buf.read(p);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(decoder.decode(p), "ghijklmnop");
|
2018-11-08 04:01:20 -05:00
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
actual = assertNotEOF(await buf.peek(0));
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(decoder.decode(actual), "");
|
2018-11-08 04:01:20 -05:00
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
const r = await buf.peek(1);
|
2019-07-07 15:20:41 -04:00
|
|
|
assert(r === Deno.EOF);
|
2018-11-08 04:01:20 -05:00
|
|
|
/* TODO
|
2019-06-19 00:22:01 -04:00
|
|
|
Test for issue 3022, not exposing a reader's error on a successful Peek.
|
2018-11-08 04:01:20 -05:00
|
|
|
buf = NewReaderSize(dataAndEOFReader("abcd"), 32)
|
|
|
|
if s, err := buf.Peek(2); string(s) != "ab" || err != nil {
|
|
|
|
t.Errorf(`Peek(2) on "abcd", EOF = %q, %v; want "ab", nil`, string(s), err)
|
|
|
|
}
|
|
|
|
if s, err := buf.Peek(4); string(s) != "abcd" || err != nil {
|
2019-06-19 00:22:01 -04:00
|
|
|
t.Errorf(
|
|
|
|
`Peek(4) on "abcd", EOF = %q, %v; want "abcd", nil`,
|
|
|
|
string(s),
|
|
|
|
err
|
|
|
|
)
|
2018-11-08 04:01:20 -05:00
|
|
|
}
|
|
|
|
if n, err := buf.Read(p[0:5]); string(p[0:n]) != "abcd" || err != nil {
|
|
|
|
t.Fatalf("Read after peek = %q, %v; want abcd, EOF", p[0:n], err)
|
|
|
|
}
|
|
|
|
if n, err := buf.Read(p[0:1]); string(p[0:n]) != "" || err != io.EOF {
|
|
|
|
t.Fatalf(`second Read after peek = %q, %v; want "", EOF`, p[0:n], err)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
});
|
2018-11-09 13:25:30 -05:00
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function bufioWriter(): Promise<void> {
|
2018-11-09 13:25:30 -05:00
|
|
|
const data = new Uint8Array(8192);
|
|
|
|
|
|
|
|
for (let i = 0; i < data.byteLength; i++) {
|
2019-03-04 19:53:35 -05:00
|
|
|
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
|
2018-12-18 21:29:39 -05:00
|
|
|
data[i] = charCode(" ") + (i % (charCode("~") - charCode(" ")));
|
2018-11-09 13:25:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const w = new Buffer();
|
|
|
|
for (let nwrite of bufsizes) {
|
|
|
|
for (let bs of bufsizes) {
|
|
|
|
// Write nwrite bytes using buffer size bs.
|
|
|
|
// Check that the right amount makes it out
|
|
|
|
// and that the data is correct.
|
|
|
|
|
|
|
|
w.reset();
|
|
|
|
const buf = new BufWriter(w, bs);
|
|
|
|
|
|
|
|
const context = `nwrite=${nwrite} bufsize=${bs}`;
|
|
|
|
const n = await buf.write(data.subarray(0, nwrite));
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(n, nwrite, context);
|
2018-11-09 13:25:30 -05:00
|
|
|
|
|
|
|
await buf.flush();
|
|
|
|
|
|
|
|
const written = w.bytes();
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(written.byteLength, nwrite);
|
2018-11-09 13:25:30 -05:00
|
|
|
|
|
|
|
for (let l = 0; l < written.byteLength; l++) {
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(written[l], data[l]);
|
2018-11-09 13:25:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-12-17 22:57:45 -05:00
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function bufReaderReadFull(): Promise<void> {
|
2018-12-17 22:57:45 -05:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const dec = new TextDecoder();
|
|
|
|
const text = "Hello World";
|
|
|
|
const data = new Buffer(enc.encode(text));
|
|
|
|
const bufr = new BufReader(data, 3);
|
|
|
|
{
|
|
|
|
const buf = new Uint8Array(6);
|
2019-05-23 22:04:06 -04:00
|
|
|
const r = assertNotEOF(await bufr.readFull(buf));
|
|
|
|
assertEquals(r, buf);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(dec.decode(buf), "Hello ");
|
2018-12-17 22:57:45 -05:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const buf = new Uint8Array(6);
|
2019-05-23 22:04:06 -04:00
|
|
|
try {
|
|
|
|
await bufr.readFull(buf);
|
|
|
|
fail("readFull() should throw");
|
|
|
|
} catch (err) {
|
|
|
|
assert(err instanceof UnexpectedEOFError);
|
|
|
|
assert(err.partial instanceof Uint8Array);
|
|
|
|
assertEquals(err.partial.length, 5);
|
|
|
|
assertEquals(dec.decode(buf.subarray(0, 5)), "World");
|
|
|
|
}
|
2018-12-17 22:57:45 -05:00
|
|
|
}
|
|
|
|
});
|
2019-05-23 22:04:06 -04:00
|
|
|
|
|
|
|
runIfMain(import.meta);
|