1a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Copyright 2008 the V8 project authors. All rights reserved. 2a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Redistribution and use in source and binary forms, with or without 3a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// modification, are permitted provided that the following conditions are 4a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// met: 5a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 6a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// * Redistributions of source code must retain the above copyright 7a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// notice, this list of conditions and the following disclaimer. 8a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// * Redistributions in binary form must reproduce the above 9a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// copyright notice, this list of conditions and the following 10a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// disclaimer in the documentation and/or other materials provided 11a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// with the distribution. 12a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// * Neither the name of Google Inc. nor the names of its 13a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// contributors may be used to endorse or promote products derived 14a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// from this software without specific prior written permission. 15a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 16a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 28a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar s = "test test test"; 29a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 30a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar MAX_DOUBLE = 1.7976931348623157e+308; 31a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar MIN_DOUBLE = -MAX_DOUBLE; 32a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar MAX_SMI = Math.pow(2,30)-1; 33a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar MIN_SMI = -Math.pow(2,30); 34a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 35a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(10, s.lastIndexOf("test", Infinity), "tinf"); 36a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(10, s.lastIndexOf("test", MAX_DOUBLE), "tmaxdouble"); 37a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(10, s.lastIndexOf("test", MAX_SMI), "tmaxsmi"); 38a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(10, s.lastIndexOf("test", s.length * 2), "t2length"); 39a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(10, s.lastIndexOf("test", 15), "t15"); 40a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(10, s.lastIndexOf("test", 14), "t14"); 41a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(10, s.lastIndexOf("test", 10), "t10"); 42a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(5, s.lastIndexOf("test", 9), "t9"); 43a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(5, s.lastIndexOf("test", 6), "t6"); 44a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(5, s.lastIndexOf("test", 5), "t5"); 45a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, s.lastIndexOf("test", 4), "t4"); 46a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, s.lastIndexOf("test", 0), "t0"); 47a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, s.lastIndexOf("test", -1), "t-1"); 48a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, s.lastIndexOf("test", -s.length), "t-len"); 49a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, s.lastIndexOf("test", MIN_SMI), "tminsmi"); 50a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, s.lastIndexOf("test", MIN_DOUBLE), "tmindouble"); 51a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, s.lastIndexOf("test", -Infinity), "tneginf"); 52a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(10, s.lastIndexOf("test"), "t"); 53a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(-1, s.lastIndexOf("notpresent"), "n"); 54a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(-1, s.lastIndexOf(), "none"); 55a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(10, s.lastIndexOf("test", "not a number"), "nan"); 56a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 57a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar longNonMatch = "overlong string that doesn't match"; 58a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar longAlmostMatch = "test test test!"; 59a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar longAlmostMatch2 = "!test test test"; 60a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 61a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 62a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(-1, s.lastIndexOf(longNonMatch), "long"); 63a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(-1, s.lastIndexOf(longNonMatch, 10), "longpos"); 64a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(-1, s.lastIndexOf(longNonMatch, NaN), "longnan"); 65a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(-1, s.lastIndexOf(longAlmostMatch), "tlong"); 66a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(-1, s.lastIndexOf(longAlmostMatch, 10), "tlongpos"); 67a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(-1, s.lastIndexOf(longAlmostMatch), "tlongnan"); 68a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 69a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar nonInitialMatch = "est"; 70a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 71a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(-1, s.lastIndexOf(nonInitialMatch, 0), "noninit"); 72a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(-1, s.lastIndexOf(nonInitialMatch, -1), "noninitneg"); 73a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(-1, s.lastIndexOf(nonInitialMatch, MIN_SMI), "noninitminsmi"); 74a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(-1, s.lastIndexOf(nonInitialMatch, MIN_DOUBLE), "noninitmindbl"); 75a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(-1, s.lastIndexOf(nonInitialMatch, -Infinity), "noninitneginf"); 76a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 77a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfor (var i = s.length + 10; i >= 0; i--) { 78a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block var expected = i < s.length ? i : s.length; 79a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block assertEquals(expected, s.lastIndexOf("", i), "empty" + i); 80a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block} 81a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 82a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 83a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar reString = "asdf[a-z]+(asdf)?"; 84a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 85a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(4, reString.lastIndexOf("[a-z]+"), "r4"); 86a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(10, reString.lastIndexOf("(asdf)?"), "r10"); 87a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 88a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(1, String.prototype.lastIndexOf.length, "length"); 89