Home | History | Annotate | Download | only in mjsunit
      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 
     28 var s = "test test test";
     29 
     30 var MAX_DOUBLE = 1.7976931348623157e+308;
     31 var MIN_DOUBLE = -MAX_DOUBLE;
     32 var MAX_SMI = Math.pow(2,30)-1;
     33 var MIN_SMI = -Math.pow(2,30);
     34 
     35 assertEquals(10, s.lastIndexOf("test", Infinity), "tinf");
     36 assertEquals(10, s.lastIndexOf("test", MAX_DOUBLE), "tmaxdouble");
     37 assertEquals(10, s.lastIndexOf("test", MAX_SMI), "tmaxsmi");
     38 assertEquals(10, s.lastIndexOf("test", s.length * 2), "t2length");
     39 assertEquals(10, s.lastIndexOf("test", 15), "t15");
     40 assertEquals(10, s.lastIndexOf("test", 14), "t14");
     41 assertEquals(10, s.lastIndexOf("test", 10), "t10");
     42 assertEquals(5, s.lastIndexOf("test", 9), "t9");
     43 assertEquals(5, s.lastIndexOf("test", 6), "t6");
     44 assertEquals(5, s.lastIndexOf("test", 5), "t5");
     45 assertEquals(0, s.lastIndexOf("test", 4), "t4");
     46 assertEquals(0, s.lastIndexOf("test", 0), "t0");
     47 assertEquals(0, s.lastIndexOf("test", -1), "t-1");
     48 assertEquals(0, s.lastIndexOf("test", -s.length), "t-len");
     49 assertEquals(0, s.lastIndexOf("test", MIN_SMI), "tminsmi");
     50 assertEquals(0, s.lastIndexOf("test", MIN_DOUBLE), "tmindouble");
     51 assertEquals(0, s.lastIndexOf("test", -Infinity), "tneginf");
     52 assertEquals(10, s.lastIndexOf("test"), "t");
     53 assertEquals(-1, s.lastIndexOf("notpresent"), "n");
     54 assertEquals(-1, s.lastIndexOf(), "none");
     55 assertEquals(10, s.lastIndexOf("test", "not a number"), "nan");
     56 
     57 var longNonMatch = "overlong string that doesn't match";
     58 var longAlmostMatch = "test test test!";
     59 var longAlmostMatch2 = "!test test test";
     60 
     61 
     62 assertEquals(-1, s.lastIndexOf(longNonMatch), "long");
     63 assertEquals(-1, s.lastIndexOf(longNonMatch, 10), "longpos");
     64 assertEquals(-1, s.lastIndexOf(longNonMatch, NaN), "longnan");
     65 assertEquals(-1, s.lastIndexOf(longAlmostMatch), "tlong");
     66 assertEquals(-1, s.lastIndexOf(longAlmostMatch, 10), "tlongpos");
     67 assertEquals(-1, s.lastIndexOf(longAlmostMatch), "tlongnan");
     68 
     69 var nonInitialMatch = "est";
     70 
     71 assertEquals(-1, s.lastIndexOf(nonInitialMatch, 0), "noninit");
     72 assertEquals(-1, s.lastIndexOf(nonInitialMatch, -1), "noninitneg");
     73 assertEquals(-1, s.lastIndexOf(nonInitialMatch, MIN_SMI), "noninitminsmi");
     74 assertEquals(-1, s.lastIndexOf(nonInitialMatch, MIN_DOUBLE), "noninitmindbl");
     75 assertEquals(-1, s.lastIndexOf(nonInitialMatch, -Infinity), "noninitneginf");
     76 
     77 for (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 
     83 var reString = "asdf[a-z]+(asdf)?";
     84 
     85 assertEquals(4, reString.lastIndexOf("[a-z]+"), "r4");
     86 assertEquals(10, reString.lastIndexOf("(asdf)?"), "r10");
     87 
     88 assertEquals(1, String.prototype.lastIndexOf.length, "length");
     89