deno.land / std@0.166.0 / io / buffer_test.ts

buffer_test.ts
نووسراو ببینە
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
// Copyright 2018-2022 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 { copy } from "../bytes/mod.ts";import { assert, assertEquals, assertRejects, assertThrows, fail,} from "../testing/asserts.ts";import { Buffer, BufferFullError, BufReader, BufWriter, BufWriterSync, PartialReadError, readLines, readStringDelim,} from "./buffer.ts";import * as iotest from "./_iotest.ts";import { StringReader } from "./readers.ts";import { writeAllSync } from "../streams/conversion.ts";import { StringWriter } from "./writers.ts";
const MAX_SIZE = 2 ** 32 - 2;// N controls how many iterations of certain checks are performed.const N = 100;let testBytes: Uint8Array | null;let testString: string | null;
const ignoreMaxSizeTests = true;
function init() { if (testBytes == null) { testBytes = new Uint8Array(N); for (let i = 0; i < N; i++) { testBytes[i] = "a".charCodeAt(0) + (i % 26); } const decoder = new TextDecoder(); testString = decoder.decode(testBytes); }}
function check(buf: Buffer, s: string) { const bytes = buf.bytes(); assertEquals(buf.length, bytes.byteLength); const decoder = new TextDecoder(); const bytesStr = decoder.decode(bytes); assertEquals(bytesStr, s); assertEquals(buf.length, s.length);}
// Fill buf through n writes of byte slice fub.// The initial contents of buf corresponds to the string s;// the result is the final contents of buf returned as a string.async function fillBytes( buf: Buffer, s: string, n: number, fub: Uint8Array,): Promise<string> { check(buf, s); for (; n > 0; n--) { const m = await buf.write(fub); assertEquals(m, fub.byteLength); const decoder = new TextDecoder(); s += decoder.decode(fub); check(buf, s); } return s;}
// Empty buf through repeated reads into fub.// The initial contents of buf corresponds to the string s.async function empty( buf: Buffer, s: string, fub: Uint8Array,) { check(buf, s); while (true) { const r = await buf.read(fub); if (r === null) { break; } s = s.slice(r); check(buf, s); } check(buf, "");}
function repeat(c: string, bytes: number): Uint8Array { assertEquals(c.length, 1); const ui8 = new Uint8Array(bytes); ui8.fill(c.charCodeAt(0)); return ui8;}
Deno.test("bufferNewBuffer", () => { init(); assert(testBytes); assert(testString); const buf = new Buffer(testBytes.buffer); check(buf, testString);});
Deno.test("bufferBasicOperations", async () => { assert(testBytes); assert(testString); const buf = new Buffer(); for (let i = 0; i < 5; i++) { check(buf, "");
buf.reset(); check(buf, "");
buf.truncate(0); check(buf, "");
let n = await buf.write(testBytes.subarray(0, 1)); assertEquals(n, 1); check(buf, "a");
n = await buf.write(testBytes.subarray(1, 2)); assertEquals(n, 1); check(buf, "ab");
n = await buf.write(testBytes.subarray(2, 26)); assertEquals(n, 24); check(buf, testString.slice(0, 26));
buf.truncate(26); check(buf, testString.slice(0, 26));
buf.truncate(20); check(buf, testString.slice(0, 20));
await empty(buf, testString.slice(0, 20), new Uint8Array(5)); await empty(buf, "", new Uint8Array(100));
// TODO(bartlomieju): buf.writeByte() // TODO(bartlomieju): buf.readByte() }});
Deno.test("bufferReadEmptyAtEOF", async () => { // check that EOF of 'buf' is not reached (even though it's empty) if // results are written to buffer that has 0 length (ie. it can't store any data) const buf = new Buffer(); const zeroLengthTmp = new Uint8Array(0); const result = await buf.read(zeroLengthTmp); assertEquals(result, 0);});
Deno.test("bufferLargeByteWrites", async () => { init(); const buf = new Buffer(); const limit = 9; for (let i = 3; i < limit; i += 3) { const s = await fillBytes(buf, "", 5, testBytes!); await empty(buf, s, new Uint8Array(Math.floor(testString!.length / i))); } check(buf, "");});
Deno.test("bufferTooLargeByteWrites", async () => { init(); const tmp = new Uint8Array(72); const growLen = Number.MAX_VALUE; const xBytes = repeat("x", 0); const buf = new Buffer(xBytes.buffer); await buf.read(tmp);
assertThrows( () => { buf.grow(growLen); }, Error, "grown beyond the maximum size", );});
Deno.test({ name: "bufferGrowWriteMaxBuffer", ignore: ignoreMaxSizeTests, fn() { const bufSize = 16 * 1024; const capacities = [MAX_SIZE, MAX_SIZE - 1]; for (const capacity of capacities) { let written = 0; const buf = new Buffer(); const writes = Math.floor(capacity / bufSize); for (let i = 0; i < writes; i++) { written += buf.writeSync(repeat("x", bufSize)); }
if (written < capacity) { written += buf.writeSync(repeat("x", capacity - written)); }
assertEquals(written, capacity); } },});
Deno.test({ name: "bufferGrowReadCloseMaxBufferPlus1", ignore: ignoreMaxSizeTests, async fn() { const reader = new Buffer(new ArrayBuffer(MAX_SIZE + 1)); const buf = new Buffer();
await assertRejects( async () => { await buf.readFrom(reader); }, Error, "grown beyond the maximum size", ); },});
Deno.test({ name: "bufferGrowReadSyncCloseMaxBufferPlus1", ignore: ignoreMaxSizeTests, fn() { const reader = new Buffer(new ArrayBuffer(MAX_SIZE + 1)); const buf = new Buffer();
assertThrows( () => { buf.readFromSync(reader); }, Error, "grown beyond the maximum size", ); },});
Deno.test({ name: "bufferGrowReadSyncCloseToMaxBuffer", ignore: ignoreMaxSizeTests, fn() { const capacities = [MAX_SIZE, MAX_SIZE - 1]; for (const capacity of capacities) { const reader = new Buffer(new ArrayBuffer(capacity)); const buf = new Buffer(); buf.readFromSync(reader);
assertEquals(buf.length, capacity); } },});
Deno.test({ name: "bufferGrowReadCloseToMaxBuffer", ignore: ignoreMaxSizeTests, async fn() { const capacities = [MAX_SIZE, MAX_SIZE - 1]; for (const capacity of capacities) { const reader = new Buffer(new ArrayBuffer(capacity)); const buf = new Buffer(); await buf.readFrom(reader); assertEquals(buf.length, capacity); } },});
Deno.test({ name: "bufferReadCloseToMaxBufferWithInitialGrow", ignore: ignoreMaxSizeTests, async fn() { const capacities = [MAX_SIZE, MAX_SIZE - 1, MAX_SIZE - 512]; for (const capacity of capacities) { const reader = new Buffer(new ArrayBuffer(capacity)); const buf = new Buffer(); buf.grow(MAX_SIZE); await buf.readFrom(reader); assertEquals(buf.length, capacity); } },});
Deno.test("bufferLargeByteReads", async () => { init(); assert(testBytes); assert(testString); const buf = new Buffer(); for (let i = 3; i < 30; i += 3) { const n = Math.floor(testBytes.byteLength / i); const s = await fillBytes(buf, "", 5, testBytes.subarray(0, n)); await empty(buf, s, new Uint8Array(testString.length)); } check(buf, "");});
Deno.test("bufferCapWithPreallocatedSlice", () => { const buf = new Buffer(new ArrayBuffer(10)); assertEquals(buf.capacity, 10);});
Deno.test("bufferReadFrom", async () => { init(); assert(testBytes); assert(testString); const buf = new Buffer(); for (let i = 3; i < 30; i += 3) { const s = await fillBytes( buf, "", 5, testBytes.subarray(0, Math.floor(testBytes.byteLength / i)), ); const b = new Buffer(); await b.readFrom(buf); const fub = new Uint8Array(testString.length); await empty(b, s, fub); } await assertRejects(async function () { await new Buffer().readFrom(null!); });});
Deno.test("bufferReadFromSync", async () => { init(); assert(testBytes); assert(testString); const buf = new Buffer(); for (let i = 3; i < 30; i += 3) { const s = await fillBytes( buf, "", 5, testBytes.subarray(0, Math.floor(testBytes.byteLength / i)), ); const b = new Buffer(); b.readFromSync(buf); const fub = new Uint8Array(testString.length); await empty(b, s, fub); } assertThrows(function () { new Buffer().readFromSync(null!); });});
Deno.test("bufferTestGrow", async () => { const tmp = new Uint8Array(72); for (const startLen of [0, 100, 1000, 10000]) { const xBytes = repeat("x", startLen); for (const growLen of [0, 100, 1000, 10000]) { const buf = new Buffer(xBytes.buffer); // If we read, this affects buf.off, which is good to test. const nread = (await buf.read(tmp)) ?? 0; buf.grow(growLen); const yBytes = repeat("y", growLen); await buf.write(yBytes); // Check that buffer has correct data. assertEquals( buf.bytes().subarray(0, startLen - nread), xBytes.subarray(nread), ); assertEquals( buf.bytes().subarray(startLen - nread, startLen - nread + growLen), yBytes, ); } }});
Deno.test("testBufferBytesArrayBufferLength", () => { // defaults to copy const args = [undefined, { copy: true }] as const; for (const arg of args) { const bufSize = 64 * 1024; const bytes = new TextEncoder().encode("a".repeat(bufSize)); const reader = new Buffer(); writeAllSync(reader, bytes);
const writer = new Buffer(); writer.readFromSync(reader); const actualBytes = writer.bytes(arg);
assertEquals(actualBytes.byteLength, bufSize); assert(actualBytes.buffer !== writer.bytes(arg).buffer); assertEquals(actualBytes.byteLength, actualBytes.buffer.byteLength); }});
Deno.test("testBufferBytesCopyFalse", () => { const bufSize = 64 * 1024; const bytes = new TextEncoder().encode("a".repeat(bufSize)); const reader = new Buffer(); writeAllSync(reader, bytes);
const writer = new Buffer(); writer.readFromSync(reader); const actualBytes = writer.bytes({ copy: false });
assertEquals(actualBytes.byteLength, bufSize); assertEquals(actualBytes.buffer, writer.bytes({ copy: false }).buffer); assert(actualBytes.buffer.byteLength > actualBytes.byteLength);});
Deno.test("testBufferBytesCopyFalseGrowExactBytes", () => { const bufSize = 64 * 1024; const bytes = new TextEncoder().encode("a".repeat(bufSize)); const reader = new Buffer(); writeAllSync(reader, bytes);
const writer = new Buffer(); writer.grow(bufSize); writer.readFromSync(reader); const actualBytes = writer.bytes({ copy: false });
assertEquals(actualBytes.byteLength, bufSize); assertEquals(actualBytes.buffer.byteLength, actualBytes.byteLength);});
async function readBytes(buf: BufReader): Promise<string> { const b = new Uint8Array(1000); let nb = 0; while (true) { const c = await buf.readByte(); if (c === null) { break; // EOF } b[nb] = c; nb++; } const decoder = new TextDecoder(); return decoder.decode(b.subarray(0, nb));}
Deno.test("bufioReaderSimple", async function () { const data = "hello world"; const b = new BufReader(new StringReader(data)); const s = await readBytes(b); assertEquals(s, data);});
interface ReadMaker { name: string; fn: (r: Deno.Reader) => Deno.Reader;}
const readMakers: ReadMaker[] = [ { name: "full", fn: (r): Deno.Reader => r }, { name: "byte", fn: (r): iotest.OneByteReader => new iotest.OneByteReader(r), }, { name: "half", fn: (r): iotest.HalfReader => new iotest.HalfReader(r) }, // TODO(bartlomieju): { name: "data+err", r => new iotest.DataErrReader(r) }, // { name: "timeout", fn: r => new iotest.TimeoutReader(r) },];
// Call read to accumulate the text of a fileasync function reads(buf: BufReader, m: number): Promise<string> { const b = new Uint8Array(1000); let nb = 0; while (true) { const result = await buf.read(b.subarray(nb, nb + m)); if (result === null) { break; } nb += result; } const decoder = new TextDecoder(); return decoder.decode(b.subarray(0, nb));}
interface NamedBufReader { name: string; fn: (r: BufReader) => Promise<string>;}
const bufreaders: NamedBufReader[] = [ { 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) }, { name: "bytes", fn: readBytes }, // { name: "lines", fn: readLines },];
const MIN_READ_BUFFER_SIZE = 16;const bufsizes: number[] = [ 0, MIN_READ_BUFFER_SIZE, 23, 32, 46, 64, 93, 128, 1024, 4096,];
Deno.test("bufioBufReader", async function () { 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]; str += String.fromCharCode((i % 26) + 97); } texts[texts.length - 1] = all;
for (const text of texts) { for (const readmaker of readMakers) { for (const bufreader of bufreaders) { for (const bufsize of bufsizes) { const read = readmaker.fn(new StringReader(text)); const buf = new BufReader(read, bufsize); const s = await bufreader.fn(buf); const debugStr = `reader=${readmaker.name} ` + `fn=${bufreader.name} bufsize=${bufsize} want=${text} got=${s}`; assertEquals(s, text, debugStr); } } } }});
Deno.test("bufioBufferFull", async function () { const longString = "And now, hello, world! It is the time for all good men to come to the" + " aid of their party"; const buf = new BufReader(new StringReader(longString), MIN_READ_BUFFER_SIZE); const decoder = new TextDecoder();
try { await buf.readSlice("!".charCodeAt(0)); 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 = await buf.readSlice("!".charCodeAt(0)); assert(line !== null); const actual = decoder.decode(line); assertEquals(actual, "world!");});
Deno.test("bufioReadString", async function () { const string = "And now, hello world!"; const buf = new BufReader(new StringReader(string), MIN_READ_BUFFER_SIZE);
const line = await buf.readString(","); assert(line !== null); assertEquals(line, "And now,"); assertEquals(line.length, 8);
const line2 = await buf.readString(","); assert(line2 !== null); assertEquals(line2, " hello world!");
assertEquals(await buf.readString(","), null);
try { await buf.readString("deno");
fail("should throw"); } catch (err) { assert(err instanceof Error); assert(err.message, "Delimiter should be a single character"); }});
const encoder = new TextEncoder();
const testInput = encoder.encode( "012\n345\n678\n9ab\ncde\nfgh\nijk\nlmn\nopq\nrst\nuvw\nxy",);const testInputrn = encoder.encode( "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",);const testOutput = encoder.encode("0123456789abcdefghijklmnopqrstuvwxy");
// TestReader wraps a Uint8Array and returns reads of a specific length.class TestReader implements Deno.Reader { constructor(private data: Uint8Array, private stride: number) {}
read(buf: Uint8Array): Promise<number | null> { let nread = this.stride; if (nread > this.data.byteLength) { nread = this.data.byteLength; } if (nread > buf.byteLength) { nread = buf.byteLength; } if (nread === 0) { return Promise.resolve(null); } copy(this.data, buf as Uint8Array); this.data = this.data.subarray(nread); return Promise.resolve(nread); }}
async function testReadLine(input: Uint8Array) { for (let stride = 1; stride < 2; stride++) { let done = 0; const reader = new TestReader(input, stride); const l = new BufReader(reader, input.byteLength + 1); while (true) { const r = await l.readLine(); if (r === null) { break; } const { line, more } = r; assertEquals(more, false); const want = testOutput.subarray(done, done + line.byteLength); assertEquals( line, want, `Bad line at stride ${stride}: want: ${want} got: ${line}`, ); done += line.byteLength; } assertEquals( done, testOutput.byteLength, `readLine didn't return everything: got: ${done}, ` + `want: ${testOutput} (stride: ${stride})`, ); }}
Deno.test("bufioReadLine", async function () { await testReadLine(testInput); await testReadLine(testInputrn);});
Deno.test("bufioReadLineBadResource", async () => { const file = await Deno.open("README.md"); const bufReader = new BufReader(file); file.close(); await assertRejects(async () => { await bufReader.readLine(); }, Deno.errors.BadResource);});
Deno.test("bufioReadLineBufferFullError", async () => { const input = "@".repeat(5000) + "\n"; const bufReader = new BufReader(new StringReader(input)); const r = await bufReader.readLine();
assert(r !== null);
const { line, more } = r; assertEquals(more, true); assertEquals(line, encoder.encode("@".repeat(4096)));});
Deno.test("[io] readStringDelim basic", async () => { const delim = "!#$%&()=~"; const exp = [ "", "a", "bc", "def", "", "!", "!#", "!#$%&()=", "#$%&()=~", "", "", ]; const str = exp.join(delim); const arr: string[] = []; for await (const v of readStringDelim(new StringReader(str), delim)) { arr.push(v); } assertEquals(arr, exp);});
Deno.test("[io] readStringDelim bigger delim than buf size", async () => { // 0123456789... const delim = Array.from({ length: 1025 }).map((_, i) => i % 10).join(""); const exp = ["", "a", "bc", "def", "01", "012345678", "123456789", "", ""]; const str = exp.join(delim); const arr: string[] = []; for await (const v of readStringDelim(new StringReader(str), delim)) { arr.push(v); } assertEquals(arr, exp);});
Deno.test("[io] readStringDelim delim=1213", async () => { const delim = "1213"; const exp = ["", "a", "bc", "def", "01", "012345678", "123456789", "", ""]; const str = exp.join(delim); const arr: string[] = []; for await (const v of readStringDelim(new StringReader(str), "1213")) { arr.push(v); } assertEquals(arr, exp);});
Deno.test("bufioPeek", async function () { const decoder = new TextDecoder(); const p = new Uint8Array(10); // string is 16 (minReadBufferSize) long. const buf = new BufReader( new StringReader("abcdefghijklmnop"), MIN_READ_BUFFER_SIZE, );
let actual = await buf.peek(1); assert(actual !== null); assertEquals(decoder.decode(actual), "a");
actual = await buf.peek(4); assert(actual !== null); assertEquals(decoder.decode(actual), "abcd");
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"); }
await buf.read(p.subarray(0, 3)); assertEquals(decoder.decode(p.subarray(0, 3)), "abc");
actual = await buf.peek(1); assert(actual !== null); assertEquals(decoder.decode(actual), "d");
actual = await buf.peek(1); assert(actual !== null); assertEquals(decoder.decode(actual), "d");
actual = await buf.peek(1); assert(actual !== null); assertEquals(decoder.decode(actual), "d");
actual = await buf.peek(2); assert(actual !== null); assertEquals(decoder.decode(actual), "de");
const res = await buf.read(p.subarray(0, 3)); assertEquals(decoder.decode(p.subarray(0, 3)), "def"); assert(res !== null);
actual = await buf.peek(4); assert(actual !== null); assertEquals(decoder.decode(actual), "ghij");
await buf.read(p); assertEquals(decoder.decode(p), "ghijklmnop");
actual = await buf.peek(0); assert(actual !== null); assertEquals(decoder.decode(actual), "");
const r = await buf.peek(1); assert(r === null); /* TODO Test for issue 3022, not exposing a reader's error on a successful Peek. 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 { t.Errorf( `Peek(4) on "abcd", EOF = %q, %v; want "abcd", nil`, string(s), err ) } 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) } */});
Deno.test("bufioWriter", 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("bufioWriterSync", 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("bufReaderReadFull", async function () { 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); const r = await bufr.readFull(buf); assert(r !== null); assertEquals(r, buf); assertEquals(dec.decode(buf), "Hello "); } { const buf = new Uint8Array(6); try { await bufr.readFull(buf); fail("readFull() should throw PartialReadError"); } catch (err) { assert(err instanceof PartialReadError); assert(err.partial instanceof Uint8Array); assertEquals(err.partial.length, 5); assertEquals(dec.decode(buf.subarray(0, 5)), "World"); } }});
Deno.test("readStringDelimAndLines", async function () { const enc = new TextEncoder(); const data = new Buffer( enc.encode("Hello World\tHello World 2\tHello World 3"), ); const chunks_ = [];
for await (const c of readStringDelim(data, "\t")) { chunks_.push(c); }
assertEquals(chunks_.length, 3); assertEquals(chunks_, ["Hello World", "Hello World 2", "Hello World 3"]);
const linesData = new Buffer(enc.encode("0\n1\n2\n3\n4\n5\n6\n7\n8\n9")); const linesDataWithTrailingNewLine = new Buffer(enc.encode("1\n2\n3\n")); // consider data with windows newlines too const linesDataWindows = new Buffer( enc.encode("0\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9"), ); const lines_ = [];
for await (const l of readLines(linesData)) { lines_.push(l); }
assertEquals(lines_.length, 10); assertEquals(lines_, ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]);
lines_.length = 0; for await (const l of readLines(linesDataWithTrailingNewLine)) { lines_.push(l); }
assertEquals(lines_.length, 3); assertEquals(lines_, ["1", "2", "3"]); // No empty line at the end
// Now test for "windows" lines lines_.length = 0; for await (const l of readLines(linesDataWindows)) { lines_.push(l); } assertEquals(lines_.length, 10); assertEquals(lines_, ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]);});
Deno.test("readLinesWithEncodingISO-8859-15", async function () { const lines_ = []; const file_ = await Deno.open("./io/testdata/iso-8859-15.txt");
for await (const l of readLines(file_, { encoding: "iso-8859-15" })) { lines_.push(l); }
file_.close();
assertEquals(lines_.length, 12); assertEquals(lines_, [ "\u0020!\"#$%&'()*+,-./", "0123456789:;<=>?", "@ABCDEFGHIJKLMNO", "PQRSTUVWXYZ[\\]^_", "`abcdefghijklmno", "pqrstuvwxyz{|}~", "\u00a0¡¢£€¥Š§š©ª«¬\u00ad®¯", "°±²³Žµ¶·ž¹º»ŒœŸ¿", "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ", "ÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞß", "àáâãäåæçèéêëìíîï", "ðñòóôõö÷øùúûüýþÿ", ]);});
/* TODO(kt3k): Enable this testDeno.test( "bufReaderShouldNotShareArrayBufferAcrossReads", async function () { const decoder = new TextDecoder(); const data = "abcdefghijklmnopqrstuvwxyz"; const bufSize = 25; const b = new BufReader(new StringReader(data), bufSize);
const r1 = (await b.readLine()) as ReadLineResult; assert(r1 !== null); assertEquals(decoder.decode(r1.line), "abcdefghijklmnopqrstuvwxy");
const r2 = (await b.readLine()) as ReadLineResult; assert(r2 !== null); assertEquals(decoder.decode(r2.line), "z"); assert( r1.line.buffer !== r2.line.buffer, "array buffer should not be shared across reads", ); },);*/
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: Deno.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 (let i = 0; i < cache.length; i++) buf.set(cache[i], 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: Deno.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 (let i = 0; i < cache.length; i++) buf.set(cache[i], i);
assertEquals(data, buf); },});
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2448

Version Info

Tagged at
2 months ago