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