a zip code crypto-currency system good for red ONLY

test-buffer-indexof.js 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. 'use strict';
  2. var Buffer = require('../../').Buffer;
  3. var assert = require('assert');
  4. var Buffer = require('../../').Buffer;
  5. var b = Buffer.from('abcdef');
  6. var buf_a = Buffer.from('a');
  7. var buf_bc = Buffer.from('bc');
  8. var buf_f = Buffer.from('f');
  9. var buf_z = Buffer.from('z');
  10. var buf_empty = Buffer.from('');
  11. assert.equal(b.indexOf('a'), 0);
  12. assert.equal(b.indexOf('a', 1), -1);
  13. assert.equal(b.indexOf('a', -1), -1);
  14. assert.equal(b.indexOf('a', -4), -1);
  15. assert.equal(b.indexOf('a', -b.length), 0);
  16. assert.equal(b.indexOf('a', NaN), 0);
  17. assert.equal(b.indexOf('a', -Infinity), 0);
  18. assert.equal(b.indexOf('a', Infinity), -1);
  19. assert.equal(b.indexOf('bc'), 1);
  20. assert.equal(b.indexOf('bc', 2), -1);
  21. assert.equal(b.indexOf('bc', -1), -1);
  22. assert.equal(b.indexOf('bc', -3), -1);
  23. assert.equal(b.indexOf('bc', -5), 1);
  24. assert.equal(b.indexOf('bc', NaN), 1);
  25. assert.equal(b.indexOf('bc', -Infinity), 1);
  26. assert.equal(b.indexOf('bc', Infinity), -1);
  27. assert.equal(b.indexOf('f'), b.length - 1);
  28. assert.equal(b.indexOf('z'), -1);
  29. assert.equal(b.indexOf(''), -1);
  30. assert.equal(b.indexOf('', 1), -1);
  31. assert.equal(b.indexOf('', b.length + 1), -1);
  32. assert.equal(b.indexOf('', Infinity), -1);
  33. assert.equal(b.indexOf(buf_a), 0);
  34. assert.equal(b.indexOf(buf_a, 1), -1);
  35. assert.equal(b.indexOf(buf_a, -1), -1);
  36. assert.equal(b.indexOf(buf_a, -4), -1);
  37. assert.equal(b.indexOf(buf_a, -b.length), 0);
  38. assert.equal(b.indexOf(buf_a, NaN), 0);
  39. assert.equal(b.indexOf(buf_a, -Infinity), 0);
  40. assert.equal(b.indexOf(buf_a, Infinity), -1);
  41. assert.equal(b.indexOf(buf_bc), 1);
  42. assert.equal(b.indexOf(buf_bc, 2), -1);
  43. assert.equal(b.indexOf(buf_bc, -1), -1);
  44. assert.equal(b.indexOf(buf_bc, -3), -1);
  45. assert.equal(b.indexOf(buf_bc, -5), 1);
  46. assert.equal(b.indexOf(buf_bc, NaN), 1);
  47. assert.equal(b.indexOf(buf_bc, -Infinity), 1);
  48. assert.equal(b.indexOf(buf_bc, Infinity), -1);
  49. assert.equal(b.indexOf(buf_f), b.length - 1);
  50. assert.equal(b.indexOf(buf_z), -1);
  51. assert.equal(b.indexOf(buf_empty), -1);
  52. assert.equal(b.indexOf(buf_empty, 1), -1);
  53. assert.equal(b.indexOf(buf_empty, b.length + 1), -1);
  54. assert.equal(b.indexOf(buf_empty, Infinity), -1);
  55. assert.equal(b.indexOf(0x61), 0);
  56. assert.equal(b.indexOf(0x61, 1), -1);
  57. assert.equal(b.indexOf(0x61, -1), -1);
  58. assert.equal(b.indexOf(0x61, -4), -1);
  59. assert.equal(b.indexOf(0x61, -b.length), 0);
  60. assert.equal(b.indexOf(0x61, NaN), 0);
  61. assert.equal(b.indexOf(0x61, -Infinity), 0);
  62. assert.equal(b.indexOf(0x61, Infinity), -1);
  63. assert.equal(b.indexOf(0x0), -1);
  64. // test offsets
  65. assert.equal(b.indexOf('d', 2), 3);
  66. assert.equal(b.indexOf('f', 5), 5);
  67. assert.equal(b.indexOf('f', -1), 5);
  68. assert.equal(b.indexOf('f', 6), -1);
  69. assert.equal(b.indexOf(Buffer.from('d'), 2), 3);
  70. assert.equal(b.indexOf(Buffer.from('f'), 5), 5);
  71. assert.equal(b.indexOf(Buffer.from('f'), -1), 5);
  72. assert.equal(b.indexOf(Buffer.from('f'), 6), -1);
  73. assert.equal(Buffer.from('ff').indexOf(Buffer.from('f'), 1, 'ucs2'), -1);
  74. // test hex encoding
  75. assert.strictEqual(
  76. Buffer.from(b.toString('hex'), 'hex')
  77. .indexOf('64', 0, 'hex'),
  78. 3
  79. );
  80. assert.strictEqual(
  81. Buffer.from(b.toString('hex'), 'hex')
  82. .indexOf(Buffer.from('64', 'hex'), 0, 'hex'),
  83. 3
  84. );
  85. // test base64 encoding
  86. assert.strictEqual(
  87. Buffer.from(b.toString('base64'), 'base64')
  88. .indexOf('ZA==', 0, 'base64'),
  89. 3
  90. );
  91. assert.strictEqual(
  92. Buffer.from(b.toString('base64'), 'base64')
  93. .indexOf(Buffer.from('ZA==', 'base64'), 0, 'base64'),
  94. 3
  95. );
  96. // test ascii encoding
  97. assert.strictEqual(
  98. Buffer.from(b.toString('ascii'), 'ascii')
  99. .indexOf('d', 0, 'ascii'),
  100. 3
  101. );
  102. assert.strictEqual(
  103. Buffer.from(b.toString('ascii'), 'ascii')
  104. .indexOf(Buffer.from('d', 'ascii'), 0, 'ascii'),
  105. 3
  106. );
  107. // test latin1 encoding
  108. assert.strictEqual(
  109. Buffer.from(b.toString('latin1'), 'latin1')
  110. .indexOf('d', 0, 'latin1'),
  111. 3
  112. );
  113. assert.strictEqual(
  114. Buffer.from(b.toString('latin1'), 'latin1')
  115. .indexOf(Buffer.from('d', 'latin1'), 0, 'latin1'),
  116. 3
  117. );
  118. assert.strictEqual(
  119. Buffer.from('aa\u00e8aa', 'latin1')
  120. .indexOf('\u00e8', 'latin1'),
  121. 2
  122. );
  123. assert.strictEqual(
  124. Buffer.from('\u00e8', 'latin1')
  125. .indexOf('\u00e8', 'latin1'),
  126. 0
  127. );
  128. assert.strictEqual(
  129. Buffer.from('\u00e8', 'latin1')
  130. .indexOf(Buffer.from('\u00e8', 'latin1'), 'latin1'),
  131. 0
  132. );
  133. // test binary encoding
  134. assert.strictEqual(
  135. Buffer.from(b.toString('binary'), 'binary')
  136. .indexOf('d', 0, 'binary'),
  137. 3
  138. );
  139. assert.strictEqual(
  140. Buffer.from(b.toString('binary'), 'binary')
  141. .indexOf(Buffer.from('d', 'binary'), 0, 'binary'),
  142. 3
  143. );
  144. assert.strictEqual(
  145. Buffer.from('aa\u00e8aa', 'binary')
  146. .indexOf('\u00e8', 'binary'),
  147. 2
  148. );
  149. assert.strictEqual(
  150. Buffer.from('\u00e8', 'binary')
  151. .indexOf('\u00e8', 'binary'),
  152. 0
  153. );
  154. assert.strictEqual(
  155. Buffer.from('\u00e8', 'binary')
  156. .indexOf(Buffer.from('\u00e8', 'binary'), 'binary'),
  157. 0
  158. );
  159. // test optional offset with passed encoding
  160. assert.equal(Buffer.from('aaaa0').indexOf('30', 'hex'), 4);
  161. assert.equal(Buffer.from('aaaa00a').indexOf('3030', 'hex'), 4);
  162. {
  163. // test usc2 encoding
  164. var twoByteString = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
  165. assert.equal(8, twoByteString.indexOf('\u0395', 4, 'ucs2'));
  166. assert.equal(6, twoByteString.indexOf('\u03a3', -4, 'ucs2'));
  167. assert.equal(4, twoByteString.indexOf('\u03a3', -6, 'ucs2'));
  168. assert.equal(4, twoByteString.indexOf(
  169. Buffer.from('\u03a3', 'ucs2'), -6, 'ucs2'));
  170. assert.equal(-1, twoByteString.indexOf('\u03a3', -2, 'ucs2'));
  171. }
  172. var mixedByteStringUcs2 =
  173. Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395', 'ucs2');
  174. assert.equal(6, mixedByteStringUcs2.indexOf('bc', 0, 'ucs2'));
  175. assert.equal(10, mixedByteStringUcs2.indexOf('\u03a3', 0, 'ucs2'));
  176. assert.equal(-1, mixedByteStringUcs2.indexOf('\u0396', 0, 'ucs2'));
  177. assert.equal(
  178. 6, mixedByteStringUcs2.indexOf(Buffer.from('bc', 'ucs2'), 0, 'ucs2'));
  179. assert.equal(
  180. 10, mixedByteStringUcs2.indexOf(Buffer.from('\u03a3', 'ucs2'), 0, 'ucs2'));
  181. assert.equal(
  182. -1, mixedByteStringUcs2.indexOf(Buffer.from('\u0396', 'ucs2'), 0, 'ucs2'));
  183. {
  184. var twoByteString = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
  185. // Test single char pattern
  186. assert.equal(0, twoByteString.indexOf('\u039a', 0, 'ucs2'));
  187. assert.equal(2, twoByteString.indexOf('\u0391', 0, 'ucs2'), 'Alpha');
  188. assert.equal(4, twoByteString.indexOf('\u03a3', 0, 'ucs2'), 'First Sigma');
  189. assert.equal(6, twoByteString.indexOf('\u03a3', 6, 'ucs2'), 'Second Sigma');
  190. assert.equal(8, twoByteString.indexOf('\u0395', 0, 'ucs2'), 'Epsilon');
  191. assert.equal(-1, twoByteString.indexOf('\u0392', 0, 'ucs2'), 'Not beta');
  192. // Test multi-char pattern
  193. assert.equal(
  194. 0, twoByteString.indexOf('\u039a\u0391', 0, 'ucs2'), 'Lambda Alpha');
  195. assert.equal(
  196. 2, twoByteString.indexOf('\u0391\u03a3', 0, 'ucs2'), 'Alpha Sigma');
  197. assert.equal(
  198. 4, twoByteString.indexOf('\u03a3\u03a3', 0, 'ucs2'), 'Sigma Sigma');
  199. assert.equal(
  200. 6, twoByteString.indexOf('\u03a3\u0395', 0, 'ucs2'), 'Sigma Epsilon');
  201. }
  202. var mixedByteStringUtf8 = Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395');
  203. assert.equal(5, mixedByteStringUtf8.indexOf('bc'));
  204. assert.equal(5, mixedByteStringUtf8.indexOf('bc', 5));
  205. assert.equal(5, mixedByteStringUtf8.indexOf('bc', -8));
  206. assert.equal(7, mixedByteStringUtf8.indexOf('\u03a3'));
  207. assert.equal(-1, mixedByteStringUtf8.indexOf('\u0396'));
  208. // Test complex string indexOf algorithms. Only trigger for long strings.
  209. // Long string that isn't a simple repeat of a shorter string.
  210. var longString = 'A';
  211. for (var i = 66; i < 76; i++) { // from 'B' to 'K'
  212. longString = longString + String.fromCharCode(i) + longString;
  213. }
  214. var longBufferString = Buffer.from(longString);
  215. // pattern of 15 chars, repeated every 16 chars in long
  216. var pattern = 'ABACABADABACABA';
  217. for (var i = 0; i < longBufferString.length - pattern.length; i += 7) {
  218. var index = longBufferString.indexOf(pattern, i);
  219. assert.equal((i + 15) & ~0xf, index, 'Long ABACABA...-string at index ' + i);
  220. }
  221. assert.equal(510, longBufferString.indexOf('AJABACA'), 'Long AJABACA, First J');
  222. assert.equal(
  223. 1534, longBufferString.indexOf('AJABACA', 511), 'Long AJABACA, Second J');
  224. pattern = 'JABACABADABACABA';
  225. assert.equal(
  226. 511, longBufferString.indexOf(pattern), 'Long JABACABA..., First J');
  227. assert.equal(
  228. 1535, longBufferString.indexOf(pattern, 512), 'Long JABACABA..., Second J');
  229. // Search for a non-ASCII string in a pure ASCII string.
  230. var asciiString = Buffer.from(
  231. 'arglebargleglopglyfarglebargleglopglyfarglebargleglopglyf');
  232. assert.equal(-1, asciiString.indexOf('\x2061'));
  233. assert.equal(3, asciiString.indexOf('leb', 0));
  234. // Search in string containing many non-ASCII chars.
  235. var allCodePoints = [];
  236. for (var i = 0; i < 65536; i++) allCodePoints[i] = i;
  237. var allCharsString = String.fromCharCode.apply(String, allCodePoints);
  238. var allCharsBufferUtf8 = Buffer.from(allCharsString);
  239. var allCharsBufferUcs2 = Buffer.from(allCharsString, 'ucs2');
  240. // Search for string long enough to trigger complex search with ASCII pattern
  241. // and UC16 subject.
  242. assert.equal(-1, allCharsBufferUtf8.indexOf('notfound'));
  243. assert.equal(-1, allCharsBufferUcs2.indexOf('notfound'));
  244. // Needle is longer than haystack, but only because it's encoded as UTF-16
  245. assert.strictEqual(Buffer.from('aaaa').indexOf('a'.repeat(4), 'ucs2'), -1);
  246. assert.strictEqual(Buffer.from('aaaa').indexOf('a'.repeat(4), 'utf8'), 0);
  247. assert.strictEqual(Buffer.from('aaaa').indexOf('你好', 'ucs2'), -1);
  248. // Haystack has odd length, but the needle is UCS2.
  249. // assert.strictEqual(Buffer.from('aaaaa').indexOf('b', 'ucs2'), -1);
  250. {
  251. // Find substrings in Utf8.
  252. var lengths = [1, 3, 15]; // Single char, simple and complex.
  253. var indices = [0x5, 0x60, 0x400, 0x680, 0x7ee, 0xFF02, 0x16610, 0x2f77b];
  254. for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
  255. for (var i = 0; i < indices.length; i++) {
  256. var index = indices[i];
  257. var length = lengths[lengthIndex];
  258. if (index + length > 0x7F) {
  259. length = 2 * length;
  260. }
  261. if (index + length > 0x7FF) {
  262. length = 3 * length;
  263. }
  264. if (index + length > 0xFFFF) {
  265. length = 4 * length;
  266. }
  267. var patternBufferUtf8 = allCharsBufferUtf8.slice(index, index + length);
  268. assert.equal(index, allCharsBufferUtf8.indexOf(patternBufferUtf8));
  269. var patternStringUtf8 = patternBufferUtf8.toString();
  270. assert.equal(index, allCharsBufferUtf8.indexOf(patternStringUtf8));
  271. }
  272. }
  273. }
  274. {
  275. // Find substrings in Usc2.
  276. var lengths = [2, 4, 16]; // Single char, simple and complex.
  277. var indices = [0x5, 0x65, 0x105, 0x205, 0x285, 0x2005, 0x2085, 0xfff0];
  278. for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
  279. for (var i = 0; i < indices.length; i++) {
  280. var index = indices[i] * 2;
  281. var length = lengths[lengthIndex];
  282. var patternBufferUcs2 =
  283. allCharsBufferUcs2.slice(index, index + length);
  284. assert.equal(
  285. index, allCharsBufferUcs2.indexOf(patternBufferUcs2, 0, 'ucs2'));
  286. var patternStringUcs2 = patternBufferUcs2.toString('ucs2');
  287. assert.equal(
  288. index, allCharsBufferUcs2.indexOf(patternStringUcs2, 0, 'ucs2'));
  289. }
  290. }
  291. }
  292. assert.throws(function() {
  293. b.indexOf(function() { });
  294. });
  295. assert.throws(function() {
  296. b.indexOf({});
  297. });
  298. assert.throws(function() {
  299. b.indexOf([]);
  300. });
  301. // All code for handling encodings is shared between Buffer.indexOf and
  302. // Buffer.lastIndexOf, so only testing the separate lastIndexOf semantics.
  303. // Test lastIndexOf basic functionality; Buffer b contains 'abcdef'.
  304. // lastIndexOf string:
  305. assert.equal(b.lastIndexOf('a'), 0);
  306. assert.equal(b.lastIndexOf('a', 1), 0);
  307. assert.equal(b.lastIndexOf('b', 1), 1);
  308. assert.equal(b.lastIndexOf('c', 1), -1);
  309. assert.equal(b.lastIndexOf('a', -1), 0);
  310. assert.equal(b.lastIndexOf('a', -4), 0);
  311. assert.equal(b.lastIndexOf('a', -b.length), 0);
  312. assert.equal(b.lastIndexOf('a', -b.length - 1), -1);
  313. assert.equal(b.lastIndexOf('a', NaN), 0);
  314. assert.equal(b.lastIndexOf('a', -Infinity), -1);
  315. assert.equal(b.lastIndexOf('a', Infinity), 0);
  316. // lastIndexOf Buffer:
  317. assert.equal(b.lastIndexOf(buf_a), 0);
  318. assert.equal(b.lastIndexOf(buf_a, 1), 0);
  319. assert.equal(b.lastIndexOf(buf_a, -1), 0);
  320. assert.equal(b.lastIndexOf(buf_a, -4), 0);
  321. assert.equal(b.lastIndexOf(buf_a, -b.length), 0);
  322. assert.equal(b.lastIndexOf(buf_a, -b.length - 1), -1);
  323. assert.equal(b.lastIndexOf(buf_a, NaN), 0);
  324. assert.equal(b.lastIndexOf(buf_a, -Infinity), -1);
  325. assert.equal(b.lastIndexOf(buf_a, Infinity), 0);
  326. assert.equal(b.lastIndexOf(buf_bc), 1);
  327. assert.equal(b.lastIndexOf(buf_bc, 2), 1);
  328. assert.equal(b.lastIndexOf(buf_bc, -1), 1);
  329. assert.equal(b.lastIndexOf(buf_bc, -3), 1);
  330. assert.equal(b.lastIndexOf(buf_bc, -5), 1);
  331. assert.equal(b.lastIndexOf(buf_bc, -6), -1);
  332. assert.equal(b.lastIndexOf(buf_bc, NaN), 1);
  333. assert.equal(b.lastIndexOf(buf_bc, -Infinity), -1);
  334. assert.equal(b.lastIndexOf(buf_bc, Infinity), 1);
  335. assert.equal(b.lastIndexOf(buf_f), b.length - 1);
  336. assert.equal(b.lastIndexOf(buf_z), -1);
  337. assert.equal(b.lastIndexOf(buf_empty), -1);
  338. assert.equal(b.lastIndexOf(buf_empty, 1), -1);
  339. assert.equal(b.lastIndexOf(buf_empty, b.length + 1), -1);
  340. assert.equal(b.lastIndexOf(buf_empty, Infinity), -1);
  341. // lastIndexOf number:
  342. assert.equal(b.lastIndexOf(0x61), 0);
  343. assert.equal(b.lastIndexOf(0x61, 1), 0);
  344. assert.equal(b.lastIndexOf(0x61, -1), 0);
  345. assert.equal(b.lastIndexOf(0x61, -4), 0);
  346. assert.equal(b.lastIndexOf(0x61, -b.length), 0);
  347. assert.equal(b.lastIndexOf(0x61, -b.length - 1), -1);
  348. assert.equal(b.lastIndexOf(0x61, NaN), 0);
  349. assert.equal(b.lastIndexOf(0x61, -Infinity), -1);
  350. assert.equal(b.lastIndexOf(0x61, Infinity), 0);
  351. assert.equal(b.lastIndexOf(0x0), -1);
  352. // Test weird offset arguments.
  353. // Behaviour should match String.lastIndexOf:
  354. assert.equal(b.lastIndexOf('b', 0), -1);
  355. assert.equal(b.lastIndexOf('b', undefined), 1);
  356. assert.equal(b.lastIndexOf('b', null), -1);
  357. assert.equal(b.lastIndexOf('b', {}), 1);
  358. assert.equal(b.lastIndexOf('b', []), -1);
  359. assert.equal(b.lastIndexOf('b', [2]), 1);
  360. // Test needles longer than the haystack.
  361. assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'ucs2'), -1);
  362. assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'utf8'), -1);
  363. assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'latin1'), -1);
  364. assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'binary'), -1);
  365. assert.strictEqual(b.lastIndexOf(Buffer.from('aaaaaaaaaaaaaaa')), -1);
  366. assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 2, 'ucs2'), -1);
  367. assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 3, 'utf8'), -1);
  368. assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 5, 'latin1'), -1);
  369. assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 5, 'binary'), -1);
  370. assert.strictEqual(b.lastIndexOf(Buffer.from('aaaaaaaaaaaaaaa'), 7), -1);
  371. // 你好 expands to a total of 6 bytes using UTF-8 and 4 bytes using UTF-16
  372. assert.strictEqual(buf_bc.lastIndexOf('你好', 'ucs2'), -1);
  373. assert.strictEqual(buf_bc.lastIndexOf('你好', 'utf8'), -1);
  374. assert.strictEqual(buf_bc.lastIndexOf('你好', 'latin1'), -1);
  375. assert.strictEqual(buf_bc.lastIndexOf('你好', 'binary'), -1);
  376. assert.strictEqual(buf_bc.lastIndexOf(Buffer.from('你好')), -1);
  377. assert.strictEqual(buf_bc.lastIndexOf('你好', 2, 'ucs2'), -1);
  378. assert.strictEqual(buf_bc.lastIndexOf('你好', 3, 'utf8'), -1);
  379. assert.strictEqual(buf_bc.lastIndexOf('你好', 5, 'latin1'), -1);
  380. assert.strictEqual(buf_bc.lastIndexOf('你好', 5, 'binary'), -1);
  381. assert.strictEqual(buf_bc.lastIndexOf(Buffer.from('你好'), 7), -1);
  382. // Test lastIndexOf on a longer buffer:
  383. var bufferString = new Buffer('a man a plan a canal panama');
  384. assert.equal(15, bufferString.lastIndexOf('canal'));
  385. assert.equal(21, bufferString.lastIndexOf('panama'));
  386. assert.equal(0, bufferString.lastIndexOf('a man a plan a canal panama'));
  387. assert.equal(-1, bufferString.lastIndexOf('a man a plan a canal mexico'));
  388. assert.equal(-1, bufferString.lastIndexOf('a man a plan a canal mexico city'));
  389. assert.equal(-1, bufferString.lastIndexOf(Buffer.from('a'.repeat(1000))));
  390. assert.equal(0, bufferString.lastIndexOf('a man a plan', 4));
  391. assert.equal(13, bufferString.lastIndexOf('a '));
  392. assert.equal(13, bufferString.lastIndexOf('a ', 13));
  393. assert.equal(6, bufferString.lastIndexOf('a ', 12));
  394. assert.equal(0, bufferString.lastIndexOf('a ', 5));
  395. assert.equal(13, bufferString.lastIndexOf('a ', -1));
  396. assert.equal(0, bufferString.lastIndexOf('a ', -27));
  397. assert.equal(-1, bufferString.lastIndexOf('a ', -28));
  398. // Test lastIndexOf for the case that the first character can be found,
  399. // but in a part of the buffer that does not make search to search
  400. // due do length constraints.
  401. var abInUCS2 = Buffer.from('ab', 'ucs2');
  402. assert.strictEqual(-1, Buffer.from('µaaaa¶bbbb', 'latin1').lastIndexOf('µ'));
  403. assert.strictEqual(-1, Buffer.from('µaaaa¶bbbb', 'binary').lastIndexOf('µ'));
  404. assert.strictEqual(-1, Buffer.from('bc').lastIndexOf('ab'));
  405. assert.strictEqual(-1, Buffer.from('abc').lastIndexOf('qa'));
  406. assert.strictEqual(-1, Buffer.from('abcdef').lastIndexOf('qabc'));
  407. assert.strictEqual(-1, Buffer.from('bc').lastIndexOf(Buffer.from('ab')));
  408. assert.strictEqual(-1, Buffer.from('bc', 'ucs2').lastIndexOf('ab', 'ucs2'));
  409. assert.strictEqual(-1, Buffer.from('bc', 'ucs2').lastIndexOf(abInUCS2));
  410. assert.strictEqual(0, Buffer.from('abc').lastIndexOf('ab'));
  411. assert.strictEqual(0, Buffer.from('abc').lastIndexOf('ab', 1));
  412. assert.strictEqual(0, Buffer.from('abc').lastIndexOf('ab', 2));
  413. assert.strictEqual(0, Buffer.from('abc').lastIndexOf('ab', 3));
  414. // The above tests test the LINEAR and SINGLE-CHAR strategies.
  415. // Now, we test the BOYER-MOORE-HORSPOOL strategy.
  416. // Test lastIndexOf on a long buffer w multiple matches:
  417. pattern = 'JABACABADABACABA';
  418. assert.equal(1535, longBufferString.lastIndexOf(pattern));
  419. assert.equal(1535, longBufferString.lastIndexOf(pattern, 1535));
  420. assert.equal(511, longBufferString.lastIndexOf(pattern, 1534));
  421. // Finally, give it a really long input to trigger fallback from BMH to
  422. // regular BOYER-MOORE (which has better worst-case complexity).
  423. // Generate a really long Thue-Morse sequence of 'yolo' and 'swag',
  424. // "yolo swag swag yolo swag yolo yolo swag" ..., goes on for about 5MB.
  425. // This is hard to search because it all looks similar, but never repeats.
  426. // countBits returns the number of bits in the binary reprsentation of n.
  427. function countBits(n) {
  428. for (var count = 0; n > 0; count++) {
  429. n = n & (n - 1); // remove top bit
  430. }
  431. return count;
  432. }
  433. var parts = [];
  434. for (var i = 0; i < 1000000; i++) {
  435. parts.push((countBits(i) % 2 === 0) ? 'yolo' : 'swag');
  436. }
  437. var reallyLong = new Buffer(parts.join(' '));
  438. assert.equal('yolo swag swag yolo', reallyLong.slice(0, 19).toString());
  439. // Expensive reverse searches. Stress test lastIndexOf:
  440. pattern = reallyLong.slice(0, 100000); // First 1/50th of the pattern.
  441. assert.equal(4751360, reallyLong.lastIndexOf(pattern));
  442. assert.equal(3932160, reallyLong.lastIndexOf(pattern, 4000000));
  443. assert.equal(2949120, reallyLong.lastIndexOf(pattern, 3000000));
  444. pattern = reallyLong.slice(100000, 200000); // Second 1/50th.
  445. assert.equal(4728480, reallyLong.lastIndexOf(pattern));
  446. pattern = reallyLong.slice(0, 1000000); // First 1/5th.
  447. assert.equal(3932160, reallyLong.lastIndexOf(pattern));
  448. pattern = reallyLong.slice(0, 2000000); // first 2/5ths.
  449. assert.equal(0, reallyLong.lastIndexOf(pattern));
  450. // test truncation of Number arguments to uint8
  451. {
  452. var buf = Buffer.from('this is a test');
  453. assert.strictEqual(buf.indexOf(0x6973), 3);
  454. assert.strictEqual(buf.indexOf(0x697320), 4);
  455. assert.strictEqual(buf.indexOf(0x69732069), 2);
  456. assert.strictEqual(buf.indexOf(0x697374657374), 0);
  457. assert.strictEqual(buf.indexOf(0x69737374), 0);
  458. assert.strictEqual(buf.indexOf(0x69737465), 11);
  459. assert.strictEqual(buf.indexOf(0x69737465), 11);
  460. assert.strictEqual(buf.indexOf(-140), 0);
  461. assert.strictEqual(buf.indexOf(-152), 1);
  462. assert.strictEqual(buf.indexOf(0xff), -1);
  463. assert.strictEqual(buf.indexOf(0xffff), -1);
  464. }