deno.land / std@0.166.0 / bytes / 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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { concat, copy, endsWith, equals, includesNeedle, indexOfNeedle, lastIndexOfNeedle, repeat, startsWith,} from "./mod.ts";import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";
Deno.test("[bytes] indexOfNeedle1", () => { const i = indexOfNeedle( new Uint8Array([1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 3]), new Uint8Array([0, 1, 2]), ); assertEquals(i, 2);});
Deno.test("[bytes] indexOfNeedle2", () => { const i = indexOfNeedle(new Uint8Array([0, 0, 1]), new Uint8Array([0, 1])); assertEquals(i, 1);});
Deno.test("[bytes] indexOfNeedle3", () => { const encoder = new TextEncoder(); const i = indexOfNeedle(encoder.encode("Deno"), encoder.encode("D")); assertEquals(i, 0);});
Deno.test("[bytes] indexOfNeedle4", () => { const i = indexOfNeedle(new Uint8Array(), new Uint8Array([0, 1])); assertEquals(i, -1);});
Deno.test("[bytes] indexOfNeedle with start index", () => { const i = indexOfNeedle( new Uint8Array([0, 1, 2, 0, 1, 2]), new Uint8Array([0, 1]), 1, ); assertEquals(i, 3);});
Deno.test("[bytes] indexOfNeedle with start index 2", () => { const i = indexOfNeedle( new Uint8Array([0, 1, 2, 0, 1, 2]), new Uint8Array([0, 1]), 7, ); assertEquals(i, -1);});
Deno.test("[bytes] indexOfNeedle with start index < 0", () => { assertEquals( indexOfNeedle( new Uint8Array([0, 1, 2, 0, 1, 2]), new Uint8Array([0, 1]), 3, ), 3, ); assertEquals( indexOfNeedle( new Uint8Array([0, 1, 2, 1, 1, 2]), new Uint8Array([0, 1]), 3, ), -1, );});
Deno.test("[bytes] lastIndexOfNeedle1", () => { const i = lastIndexOfNeedle( new Uint8Array([0, 1, 2, 0, 1, 2, 0, 1, 3]), new Uint8Array([0, 1, 2]), ); assertEquals(i, 3);});
Deno.test("[bytes] lastIndexOfNeedle2", () => { const i = lastIndexOfNeedle( new Uint8Array([0, 1, 1]), new Uint8Array([0, 1]), ); assertEquals(i, 0);});
Deno.test("[bytes] lastIndexOfNeedle3", () => { const i = lastIndexOfNeedle(new Uint8Array(), new Uint8Array([0, 1])); assertEquals(i, -1);});
Deno.test("[bytes] lastIndexOfNeedle with start index", () => { const i = lastIndexOfNeedle( new Uint8Array([0, 1, 2, 0, 1, 2]), new Uint8Array([0, 1]), 2, ); assertEquals(i, 0);});
Deno.test("[bytes] lastIndexOfNeedle with start index 2", () => { const i = lastIndexOfNeedle( new Uint8Array([0, 1, 2, 0, 1, 2]), new Uint8Array([0, 1]), -1, ); assertEquals(i, -1);});
Deno.test("[bytes] equals", () => { const v = equals(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 3])); const v2 = equals(new Uint8Array([0, 1, 2, 2]), new Uint8Array([0, 1, 2, 3])); const v3 = equals(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2])); assert(v); assert(!v2); assert(!v3);});
Deno.test("[bytes] equals randomized testing", () => { // run tests before and after cutoff for (let len = 995; len <= 1005; len++) { const arr1 = crypto.getRandomValues(new Uint8Array(len)); const arr2 = crypto.getRandomValues(new Uint8Array(len)); const arr3 = arr1.slice(0); // the chance of arr1 equaling arr2 is basically 0 // but introduce an inequality at the end just in case arr2[arr2.length - 1] = arr1[arr1.length - 1] ^ 1; // arr3 is arr1 but with an inequality in the very last element // this is to test the equality check when length isn't a multiple of 4 arr3[arr3.length - 1] ^= 1; // arrays with same underlying ArrayBuffer should be equal assert(equals(arr1, arr1)); // equal arrays with different underlying ArrayBuffers should be equal assert(equals(arr1, arr1.slice(0))); // inequal arrays should be inequal assert(!equals(arr1, arr2)); assert(!equals(arr1, arr3)); }});
Deno.test("[bytes] startsWith", () => { const v = startsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); const v2 = startsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 2])); const v3 = startsWith( new Uint8Array([0, 1, 2]), new Uint8Array([0, 2, 3, 4]), ); assert(v); assert(!v2); assert(!v3);});
Deno.test("[bytes] endsWith", () => { const v = endsWith(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2])); const v2 = endsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); const v3 = endsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1, 2, 3])); assert(v); assert(!v2); assert(!v3);});
Deno.test("[bytes] repeat", () => { // input / output / count / error message const repeatTestCase = [ ["", "", 0], ["", "", 1], ["", "", 1.1, "bytes: repeat count must be an integer"], ["", "", 2], ["", "", 0], ["-", "", 0], ["-", "-", -1, "bytes: negative repeat count"], ["-", "----------", 10], ["abc ", "abc abc abc ", 3], ]; for (const [input, output, count, errMsg] of repeatTestCase) { if (errMsg) { assertThrows( () => { repeat(new TextEncoder().encode(input as string), count as number); }, Error, errMsg as string, ); } else { const newBytes = repeat( new TextEncoder().encode(input as string), count as number, );
assertEquals(new TextDecoder().decode(newBytes), output); } }});
Deno.test("[bytes] concat", () => { const encoder = new TextEncoder(); const u1 = encoder.encode("Hello "); const u2 = encoder.encode("World"); const joined = concat(u1, u2); assertEquals(new TextDecoder().decode(joined), "Hello World"); assert(u1 !== joined); assert(u2 !== joined);});
Deno.test("[bytes] concat empty arrays", () => { const u1 = new Uint8Array(); const u2 = new Uint8Array(); const joined = concat(u1, u2); assertEquals(joined.byteLength, 0); assert(u1 !== joined); assert(u2 !== joined);});
Deno.test("[bytes] concat multiple arrays", () => { const encoder = new TextEncoder(); const u1 = encoder.encode("Hello "); const u2 = encoder.encode("W"); const u3 = encoder.encode("o"); const u4 = encoder.encode("r"); const u5 = encoder.encode("l"); const u6 = encoder.encode("d"); const joined = concat(u1, u2, u3, u4, u5, u6); assertEquals(new TextDecoder().decode(joined), "Hello World"); assert(u1 !== joined); assert(u2 !== joined);});
Deno.test("[bytes] includesNeedle", () => { const encoder = new TextEncoder(); const source = encoder.encode("deno.land"); const pattern = encoder.encode("deno");
assert(includesNeedle(source, pattern)); assert(includesNeedle(new Uint8Array([0, 1, 2, 3]), new Uint8Array([2, 3])));
assert(includesNeedle(source, pattern, -10)); assert(!includesNeedle(source, pattern, -1));});
Deno.test("[bytes] copy", function () { const dst = new Uint8Array(4);
dst.fill(0); let src = Uint8Array.of(1, 2); let len = copy(src, dst, 0); assert(len === 2); assertEquals(dst, Uint8Array.of(1, 2, 0, 0));
dst.fill(0); src = Uint8Array.of(1, 2); len = copy(src, dst, 1); assert(len === 2); assertEquals(dst, Uint8Array.of(0, 1, 2, 0));
dst.fill(0); src = Uint8Array.of(1, 2, 3, 4, 5); len = copy(src, dst); assert(len === 4); assertEquals(dst, Uint8Array.of(1, 2, 3, 4));
dst.fill(0); src = Uint8Array.of(1, 2); len = copy(src, dst, 100); assert(len === 0); assertEquals(dst, Uint8Array.of(0, 0, 0, 0));
dst.fill(0); src = Uint8Array.of(3, 4); len = copy(src, dst, -2); assert(len === 2); assertEquals(dst, Uint8Array.of(3, 4, 0, 0));});
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2458

Version Info

Tagged at
2 months ago