deno.land / std@0.224.0 / io / buf_writer_test.ts

buf_writer_test.ts
View Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// This code has been ported almost directly from Go's src/bytes/buffer_test.go// Copyright 2009 The Go Authors. All rights reserved. BSD license.// https://github.com/golang/go/blob/master/LICENSEimport { assertEquals } from "../assert/mod.ts";import { BufWriter, BufWriterSync } from "./buf_writer.ts";import { Buffer } from "./buffer.ts";import { StringWriter } from "./string_writer.ts";import { bufsizes } from "./_test_common.ts";import type { Writer, WriterSync } from "./types.ts";
Deno.test("BufWriter.write() writes different number of bytes", async function () { const data = new Uint8Array(8192);
for (let i = 0; i < data.byteLength; i++) { data[i] = " ".charCodeAt(0) + (i % ("~".charCodeAt(0) - " ".charCodeAt(0))); }
const w = new Buffer(); for (const nwrite of bufsizes) { for (const 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)); assertEquals(n, nwrite, context);
await buf.flush();
const written = w.bytes(); assertEquals(written.byteLength, nwrite);
for (let l = 0; l < written.byteLength; l++) { assertEquals(written[l], data[l]); } } }});
Deno.test("BufWriter.writeSync()", function () { const data = new Uint8Array(8192);
for (let i = 0; i < data.byteLength; i++) { data[i] = " ".charCodeAt(0) + (i % ("~".charCodeAt(0) - " ".charCodeAt(0))); }
const w = new Buffer(); for (const nwrite of bufsizes) { for (const 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 BufWriterSync(w, bs);
const context = `nwrite=${nwrite} bufsize=${bs}`; const n = buf.writeSync(data.subarray(0, nwrite)); assertEquals(n, nwrite, context);
buf.flush();
const written = w.bytes(); assertEquals(written.byteLength, nwrite);
for (let l = 0; l < written.byteLength; l++) { assertEquals(written[l], data[l]); } } }});
Deno.test({ name: "Reset buffer after flush", async fn() { const stringWriter = new StringWriter(); const bufWriter = new BufWriter(stringWriter); const encoder = new TextEncoder(); await bufWriter.write(encoder.encode("hello\nworld\nhow\nare\nyou?\n\n")); await bufWriter.flush(); await bufWriter.write(encoder.encode("foobar\n\n")); await bufWriter.flush(); const actual = stringWriter.toString(); assertEquals(actual, "hello\nworld\nhow\nare\nyou?\n\nfoobar\n\n"); },});
Deno.test({ name: "Reset buffer after flush sync", fn() { const stringWriter = new StringWriter(); const bufWriter = new BufWriterSync(stringWriter); const encoder = new TextEncoder(); bufWriter.writeSync(encoder.encode("hello\nworld\nhow\nare\nyou?\n\n")); bufWriter.flush(); bufWriter.writeSync(encoder.encode("foobar\n\n")); bufWriter.flush(); const actual = stringWriter.toString(); assertEquals(actual, "hello\nworld\nhow\nare\nyou?\n\nfoobar\n\n"); },});
Deno.test({ name: "BufWriter.flush should write all bytes", async fn() { const bufSize = 16 * 1024; const data = new Uint8Array(bufSize); data.fill("a".charCodeAt(0));
const cache: Uint8Array[] = []; const writer: Writer = { write(p: Uint8Array): Promise<number> { cache.push(p.subarray(0, 1));
// Writer that only writes 1 byte at a time return Promise.resolve(1); }, };
const bufWriter = new BufWriter(writer); await bufWriter.write(data);
await bufWriter.flush(); const buf = new Uint8Array(cache.length); for (const [i, val] of cache.entries()) buf.set(val, i);
assertEquals(data, buf); },});
Deno.test({ name: "BufWriterSync.flush should write all bytes", fn() { const bufSize = 16 * 1024; const data = new Uint8Array(bufSize); data.fill("a".charCodeAt(0));
const cache: Uint8Array[] = []; const writer: WriterSync = { writeSync(p: Uint8Array): number { cache.push(p.subarray(0, 1)); // Writer that only writes 1 byte at a time return 1; }, };
const bufWriter = new BufWriterSync(writer); bufWriter.writeSync(data);
bufWriter.flush(); const buf = new Uint8Array(cache.length); for (const [i, val] of cache.entries()) buf.set(val, i);
assertEquals(data, buf); },});
std

Version Info

Tagged at
6 months ago