source: trip-planner-front/node_modules/buffer-indexof/test/test-buffer-indexof.js@ 1ad8e64

Last change on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 11.4 KB
RevLine 
[6a3a178]1'use strict';
2var test = require('tape')
3var bindexOf = require('../')
4
5var b = new Buffer('abcdef');
6var buf_a = new Buffer('a');
7var buf_bc = new Buffer('bc');
8var buf_f = new Buffer('f');
9var buf_z = new Buffer('z');
10var buf_empty = new Buffer('');
11
12test('node 6 buffer indexOf tests', function(t) {
13 t.equal(bindexOf(b, 'a'), 0);
14 t.equal(bindexOf(b, 'a', 1), -1);
15 t.equal(bindexOf(b, 'a', -1), -1);
16 t.equal(bindexOf(b, 'a', -4), -1);
17 t.equal(bindexOf(b, 'a', -b.length), 0);
18 t.equal(bindexOf(b, 'a', NaN), 0);
19 t.equal(bindexOf(b, 'a', -Infinity), 0);
20 t.equal(bindexOf(b, 'a', Infinity), -1);
21 t.equal(bindexOf(b, 'bc'), 1);
22 t.equal(bindexOf(b, 'bc', 2), -1);
23 t.equal(bindexOf(b, 'bc', -1), -1);
24 t.equal(bindexOf(b, 'bc', -3), -1);
25 t.equal(bindexOf(b, 'bc', -5), 1);
26 t.equal(bindexOf(b, 'bc', NaN), 1);
27 t.equal(bindexOf(b, 'bc', -Infinity), 1);
28 t.equal(bindexOf(b, 'bc', Infinity), -1);
29 t.equal(bindexOf(b, 'f'), b.length - 1);
30 t.equal(bindexOf(b, 'z'), -1);
31 t.equal(bindexOf(b, ''), -1);
32 t.equal(bindexOf(b, '', 1), -1);
33 t.equal(bindexOf(b, '', b.length + 1), -1);
34 t.equal(bindexOf(b, '', Infinity), -1);
35 t.equal(bindexOf(b, buf_a), 0);
36 t.equal(bindexOf(b, buf_a, 1), -1);
37 t.equal(bindexOf(b, buf_a, -1), -1);
38 t.equal(bindexOf(b, buf_a, -4), -1);
39 t.equal(bindexOf(b, buf_a, -b.length), 0);
40 t.equal(bindexOf(b, buf_a, NaN), 0);
41 t.equal(bindexOf(b, buf_a, -Infinity), 0);
42 t.equal(bindexOf(b, buf_a, Infinity), -1);
43 t.equal(bindexOf(b, buf_bc), 1);
44 t.equal(bindexOf(b, buf_bc, 2), -1);
45 t.equal(bindexOf(b, buf_bc, -1), -1);
46 t.equal(bindexOf(b, buf_bc, -3), -1);
47 t.equal(bindexOf(b, buf_bc, -5), 1);
48 t.equal(bindexOf(b, buf_bc, NaN), 1);
49 t.equal(bindexOf(b, buf_bc, -Infinity), 1);
50 t.equal(bindexOf(b, buf_bc, Infinity), -1);
51 t.equal(bindexOf(b, buf_f), b.length - 1);
52 t.equal(bindexOf(b, buf_z), -1);
53 t.equal(bindexOf(b, buf_empty), -1);
54 t.equal(bindexOf(b, buf_empty, 1), -1);
55 t.equal(bindexOf(b, buf_empty, b.length + 1), -1);
56 t.equal(bindexOf(b, buf_empty, Infinity), -1);
57 t.equal(bindexOf(b, 0x61), 0);
58 t.equal(bindexOf(b, 0x61, 1), -1);
59 t.equal(bindexOf(b, 0x61, -1), -1);
60 t.equal(bindexOf(b, 0x61, -4), -1);
61 t.equal(bindexOf(b, 0x61, -b.length), 0);
62 t.equal(bindexOf(b, 0x61, NaN), 0);
63 t.equal(bindexOf(b, 0x61, -Infinity), 0);
64 t.equal(bindexOf(b, 0x61, Infinity), -1);
65 t.equal(bindexOf(b, 0x0), -1);
66
67 // test offsets
68 t.equal(bindexOf(b, 'd', 2), 3);
69 t.equal(bindexOf(b, 'f', 5), 5);
70 t.equal(bindexOf(b, 'f', -1), 5);
71 t.equal(bindexOf(b, 'f', 6), -1);
72
73 t.equal(bindexOf(b, new Buffer('d'), 2), 3);
74 t.equal(bindexOf(b, new Buffer('f'), 5), 5);
75 t.equal(bindexOf(b, new Buffer('f'), -1), 5);
76 t.equal(bindexOf(b, new Buffer('f'), 6), -1);
77
78 // This one doesn't make any sense
79 // t.equal(bindexOf(new Buffer('ff'), new Buffer('f'), 1, 'ucs2'), -1);
80
81 // test hex encoding
82 t.equal(
83 bindexOf(
84 new Buffer(b.toString('hex'), 'hex'),
85 '64',
86 0,
87 'hex'
88 ),
89 3
90 );
91 t.equal(
92 bindexOf(
93 new Buffer(b.toString('hex'), 'hex'),
94 new Buffer('64', 'hex'), 0, 'hex'
95 ),
96 3
97 );
98
99 // test base64 encoding
100 t.equal(
101 bindexOf(
102 new Buffer(b.toString('base64'), 'base64'),
103 'ZA==', 0, 'base64'
104 ),
105 3
106 );
107 t.equal(
108 bindexOf(
109 new Buffer(b.toString('base64'), 'base64'),
110 new Buffer('ZA==', 'base64'), 0, 'base64'
111 ),
112 3
113 );
114
115 // test ascii encoding
116 t.equal(
117 bindexOf(
118 new Buffer(b.toString('ascii'), 'ascii'),
119 'd', 0, 'ascii'
120 ),
121 3
122 );
123 t.equal(
124 bindexOf(
125 new Buffer(b.toString('ascii'), 'ascii'),
126 new Buffer('d', 'ascii'), 0, 'ascii'
127 ),
128 3
129 );
130
131 // test latin1 encoding
132 // does not work in LTS
133 /*
134 t.equal(
135 bindexOf(
136 new Buffer(b.toString('latin1'), 'latin1'),
137 'd',
138 0,
139 'latin1'
140 ),
141 3
142 );
143 t.equal(
144 bindexOf(
145 new Buffer(b.toString('latin1'), 'latin1'),
146 new Buffer('d', 'latin1'),
147 0,
148 'latin1'
149 ),
150 3
151 );
152 t.equal(
153 bindexOf(
154 new Buffer('aa\u00e8aa', 'latin1'),
155 '\u00e8',
156 'latin1'
157 ),
158 2
159 );
160 t.equal(
161 bindexOf(
162 new Buffer('\u00e8', 'latin1'),
163 '\u00e8',
164 'latin1'
165 ),
166 0
167 );
168 t.equal(
169 bindexOf(
170 new Buffer('\u00e8', 'latin1'),
171 new Buffer('\u00e8', 'latin1'),
172 0,
173 'latin1'
174 ),
175 0
176 );
177 */
178 // test binary encoding
179 t.equal(
180 bindexOf(
181 new Buffer(b.toString('binary'), 'binary'),
182 'd',
183 0,
184 'binary'
185 ),
186 3
187 );
188 t.equal(
189 bindexOf(
190 new Buffer(b.toString('binary'), 'binary'),
191 new Buffer('d', 'binary'),
192 0,
193 'binary'
194 ),
195 3
196 );
197 t.equal(
198 bindexOf(
199 new Buffer('aa\u00e8aa', 'binary'),
200 '\u00e8',
201 0,
202 'binary'
203 ),
204 2
205 );
206 t.equal(
207 bindexOf(
208 new Buffer('\u00e8', 'binary'),
209 '\u00e8',
210 0,
211 'binary'
212 ),
213 0
214 );
215 t.equal(
216 bindexOf(
217 new Buffer('\u00e8', 'binary'),
218 new Buffer('\u00e8', 'binary'),
219 0,
220 'binary'
221 ),
222 0
223 );
224
225
226 // test optional offset with passed encoding
227 t.equal(new Buffer('aaaa0').indexOf('30', 'hex'), 4);
228 t.equal(new Buffer('aaaa00a').indexOf('3030', 'hex'), 4);
229
230 {
231 // test usc2 encoding
232 var twoByteString = new Buffer('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
233
234 t.equal(8, twoByteString.indexOf('\u0395', 4, 'ucs2'));
235 t.equal(6, twoByteString.indexOf('\u03a3', -4, 'ucs2'));
236 t.equal(4, twoByteString.indexOf('\u03a3', -6, 'ucs2'));
237 t.equal(4, twoByteString.indexOf(
238 new Buffer('\u03a3', 'ucs2'), -6, 'ucs2'));
239 t.equal(-1, twoByteString.indexOf('\u03a3', -2, 'ucs2'));
240 }
241
242 var mixedByteStringUcs2 =
243 new Buffer('\u039a\u0391abc\u03a3\u03a3\u0395', 'ucs2');
244 t.equal(6, mixedByteStringUcs2.indexOf('bc', 0, 'ucs2'));
245 t.equal(10, mixedByteStringUcs2.indexOf('\u03a3', 0, 'ucs2'));
246 t.equal(-1, mixedByteStringUcs2.indexOf('\u0396', 0, 'ucs2'));
247
248 t.equal(
249 6, mixedByteStringUcs2.indexOf(new Buffer('bc', 'ucs2'), 0, 'ucs2'));
250 t.equal(
251 10, mixedByteStringUcs2.indexOf(new Buffer('\u03a3', 'ucs2'), 0, 'ucs2'));
252 t.equal(
253 -1, mixedByteStringUcs2.indexOf(new Buffer('\u0396', 'ucs2'), 0, 'ucs2'));
254
255 {
256 var twoByteString = new Buffer('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
257
258 // Test single char pattern
259 t.equal(0, twoByteString.indexOf('\u039a', 0, 'ucs2'));
260 t.equal(2, twoByteString.indexOf('\u0391', 0, 'ucs2'), 'Alpha');
261 t.equal(4, twoByteString.indexOf('\u03a3', 0, 'ucs2'), 'First Sigma');
262 t.equal(6, twoByteString.indexOf('\u03a3', 6, 'ucs2'), 'Second Sigma');
263 t.equal(8, twoByteString.indexOf('\u0395', 0, 'ucs2'), 'Epsilon');
264 t.equal(-1, twoByteString.indexOf('\u0392', 0, 'ucs2'), 'Not beta');
265
266 // Test multi-char pattern
267 t.equal(
268 0, twoByteString.indexOf('\u039a\u0391', 0, 'ucs2'), 'Lambda Alpha');
269 t.equal(
270 2, twoByteString.indexOf('\u0391\u03a3', 0, 'ucs2'), 'Alpha Sigma');
271 t.equal(
272 4, twoByteString.indexOf('\u03a3\u03a3', 0, 'ucs2'), 'Sigma Sigma');
273 t.equal(
274 6, twoByteString.indexOf('\u03a3\u0395', 0, 'ucs2'), 'Sigma Epsilon');
275 }
276
277 var mixedByteStringUtf8 = new Buffer('\u039a\u0391abc\u03a3\u03a3\u0395');
278 t.equal(5, mixedByteStringUtf8.indexOf('bc'));
279 t.equal(5, mixedByteStringUtf8.indexOf('bc', 5));
280 t.equal(5, mixedByteStringUtf8.indexOf('bc', -8));
281 t.equal(7, mixedByteStringUtf8.indexOf('\u03a3'));
282 t.equal(-1, mixedByteStringUtf8.indexOf('\u0396'));
283
284
285 // Test complex string indexOf algorithms. Only trigger for long strings.
286 // Long string that isn't a simple repeat of a shorter string.
287 var longString = 'A';
288 for (var i = 66; i < 76; i++) { // from 'B' to 'K'
289 longString = longString + String.fromCharCode(i) + longString;
290 }
291
292 var longBufferString = new Buffer(longString);
293
294 // pattern of 15 chars, repeated every 16 chars in long
295 var pattern = 'ABACABADABACABA';
296 for (var i = 0; i < longBufferString.length - pattern.length; i += 7) {
297 var index = longBufferString.indexOf(pattern, i);
298 t.equal((i + 15) & ~0xf, index, 'Long ABACABA...-string at index ' + i);
299 }
300 t.equal(510, longBufferString.indexOf('AJABACA'), 'Long AJABACA, First J');
301 t.equal(
302 1534, longBufferString.indexOf('AJABACA', 511), 'Long AJABACA, Second J');
303
304 pattern = 'JABACABADABACABA';
305 t.equal(
306 511, longBufferString.indexOf(pattern), 'Long JABACABA..., First J');
307 t.equal(
308 1535, longBufferString.indexOf(pattern, 512), 'Long JABACABA..., Second J');
309
310 // Search for a non-ASCII string in a pure ASCII string.
311 var asciiString = new Buffer(
312 'arglebargleglopglyfarglebargleglopglyfarglebargleglopglyf');
313 t.equal(-1, asciiString.indexOf('\x2061'));
314 t.equal(3, asciiString.indexOf('leb', 0));
315
316 // Search in string containing many non-ASCII chars.
317 var allCodePoints = [];
318 for (var i = 0; i < 65536; i++) allCodePoints[i] = i;
319 var allCharsString = String.fromCharCode.apply(String, allCodePoints);
320 var allCharsBufferUtf8 = new Buffer(allCharsString);
321 var allCharsBufferUcs2 = new Buffer(allCharsString, 'ucs2');
322
323 // Search for string long enough to trigger complex search with ASCII pattern
324 // and UC16 subject.
325 t.equal(-1, allCharsBufferUtf8.indexOf('notfound'));
326 t.equal(-1, allCharsBufferUcs2.indexOf('notfound'));
327
328 // Needle is longer than haystack, but only because it's encoded as UTF-16
329 t.equal(new Buffer('aaaa').indexOf('a'.repeat(4), 'ucs2'), -1);
330
331 t.equal(new Buffer('aaaa').indexOf('a'.repeat(4), 'utf8'), 0);
332 t.equal(new Buffer('aaaa').indexOf('你好', 'ucs2'), -1);
333
334 // Haystack has odd length, but the needle is UCS2.
335 t.equal(new Buffer('aaaaa').indexOf('b', 'ucs2'), -1);
336
337 {
338 // Find substrings in Utf8.
339 var lengths = [1, 3, 15]; // Single char, simple and complex.
340 var indices = [0x5, 0x60, 0x400, 0x680, 0x7ee, 0xFF02, 0x16610, 0x2f77b];
341 for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
342 for (var i = 0; i < indices.length; i++) {
343 var index = indices[i];
344 var length = lengths[lengthIndex];
345
346 if (index + length > 0x7F) {
347 length = 2 * length;
348 }
349
350 if (index + length > 0x7FF) {
351 length = 3 * length;
352 }
353
354 if (index + length > 0xFFFF) {
355 length = 4 * length;
356 }
357
358 var patternBufferUtf8 = allCharsBufferUtf8.slice(index, index + length);
359 t.equal(index, allCharsBufferUtf8.indexOf(patternBufferUtf8));
360
361 var patternStringUtf8 = patternBufferUtf8.toString();
362 t.equal(index, allCharsBufferUtf8.indexOf(patternStringUtf8));
363 }
364 }
365 }
366
367 {
368 // Find substrings in Usc2.
369 var lengths = [2, 4, 16]; // Single char, simple and complex.
370 var indices = [0x5, 0x65, 0x105, 0x205, 0x285, 0x2005, 0x2085, 0xfff0];
371 for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
372 for (var i = 0; i < indices.length; i++) {
373 var index = indices[i] * 2;
374 var length = lengths[lengthIndex];
375
376 var patternBufferUcs2 =
377 allCharsBufferUcs2.slice(index, index + length);
378 t.equal(
379 index, allCharsBufferUcs2.indexOf(patternBufferUcs2, 0, 'ucs2'));
380
381 var patternStringUcs2 = patternBufferUcs2.toString('ucs2');
382 t.equal(
383 index, allCharsBufferUcs2.indexOf(patternStringUcs2, 0, 'ucs2'));
384 }
385 }
386 }
387
388 t.throws(function() {
389 bindexOf(b, function() { });
390 });
391 t.throws(function() {
392 bindexOf(b, {});
393 });
394 t.throws(function() {
395 bindexOf(b, []);
396 });
397
398
399 t.end();
400});
Note: See TracBrowser for help on using the repository browser.