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 /**
     29  * @fileoverview Test indexing on strings with [].
     30  */
     31 
     32 var foo = "Foo";
     33 assertEquals("Foo", foo);
     34 assertEquals("F", foo[0]);
     35 assertEquals("o", foo[1]);
     36 assertEquals("o", foo[2]);
     37 
     38 assertEquals("F", foo["0" + ""], "string index");
     39 assertEquals("o", foo["1"], "string index");
     40 assertEquals("o", foo["2"], "string index");
     41 
     42 assertEquals("undefined", typeof(foo[3]), "out of range");
     43 // SpiderMonkey 1.5 fails this next one.  So does FF 2.0.6.
     44 assertEquals("undefined", typeof(foo[-1]), "known failure in SpiderMonkey 1.5");
     45 assertEquals("undefined", typeof(foo[-2]), "negative index");
     46 
     47 foo[0] = "f";
     48 assertEquals("Foo", foo);
     49 
     50 foo[3] = "t";
     51 assertEquals("Foo", foo);
     52 assertEquals("undefined", typeof(foo[3]), "out of range");
     53 assertEquals("undefined", typeof(foo[-2]), "negative index");
     54 
     55 var S = new String("foo");
     56 assertEquals("foo", S);
     57 assertEquals("f", S[0], "string object");
     58 assertEquals("f", S["0"], "string object");
     59 S[0] = 'bente';
     60 assertEquals("f", S[0], "string object");
     61 assertEquals("f", S["0"], "string object");
     62 S[-2] = 'spider';
     63 assertEquals('spider', S[-2]);
     64 S[3] = 'monkey';
     65 assertEquals('monkey', S[3]);
     66 S['foo'] = 'Fu';
     67 assertEquals("Fu", S.foo);
     68 
     69 // In FF this is ignored I think.  In V8 it puts a property on the String object
     70 // but you won't ever see it because it is hidden by the 0th character in the
     71 // string.  The net effect is pretty much the same.
     72 S["0"] = 'bente';
     73 assertEquals("f", S[0], "string object");
     74 assertEquals("f", S["0"], "string object");
     75 
     76 assertEquals(true, 0 in S, "0 in");
     77 assertEquals(false, -1 in S, "-1 in");
     78 assertEquals(true, 2 in S, "2 in");
     79 assertEquals(true, 3 in S, "3 in");
     80 assertEquals(false, 4 in S, "3 in");
     81 
     82 assertEquals(true, "0" in S, '"0" in');
     83 assertEquals(false, "-1" in S, '"-1" in');
     84 assertEquals(true, "2" in S, '"2" in');
     85 assertEquals(true, "3" in S, '"3" in');
     86 assertEquals(false, "4" in S, '"3" in');
     87 
     88 assertEquals(true, S.hasOwnProperty(0), "0 hasOwnProperty");
     89 assertEquals(false, S.hasOwnProperty(-1), "-1 hasOwnProperty");
     90 assertEquals(true, S.hasOwnProperty(2), "2 hasOwnProperty");
     91 assertEquals(true, S.hasOwnProperty(3), "3 hasOwnProperty");
     92 assertEquals(false, S.hasOwnProperty(4), "3 hasOwnProperty");
     93 
     94 assertEquals(true, S.hasOwnProperty("0"), '"0" hasOwnProperty');
     95 assertEquals(false, S.hasOwnProperty("-1"), '"-1" hasOwnProperty');
     96 assertEquals(true, S.hasOwnProperty("2"), '"2" hasOwnProperty');
     97 assertEquals(true, S.hasOwnProperty("3"), '"3" hasOwnProperty');
     98 assertEquals(false, S.hasOwnProperty("4"), '"3" hasOwnProperty');
     99 
    100 assertEquals(true, "foo".hasOwnProperty(0), "foo 0 hasOwnProperty");
    101 assertEquals(false, "foo".hasOwnProperty(-1), "foo -1 hasOwnProperty");
    102 assertEquals(true, "foo".hasOwnProperty(2), "foo 2 hasOwnProperty");
    103 assertEquals(false, "foo".hasOwnProperty(4), "foo 3 hasOwnProperty");
    104 
    105 assertEquals(true, "foo".hasOwnProperty("0"), 'foo "0" hasOwnProperty');
    106 assertEquals(false, "foo".hasOwnProperty("-1"), 'foo "-1" hasOwnProperty');
    107 assertEquals(true, "foo".hasOwnProperty("2"), 'foo "2" hasOwnProperty');
    108 assertEquals(false, "foo".hasOwnProperty("4"), 'foo "3" hasOwnProperty');
    109 
    110 //assertEquals(true, 0 in "foo", "0 in");
    111 //assertEquals(false, -1 in "foo", "-1 in");
    112 //assertEquals(true, 2 in "foo", "2 in");
    113 //assertEquals(false, 3 in "foo", "3 in");
    114 //
    115 //assertEquals(true, "0" in "foo", '"0" in');
    116 //assertEquals(false, "-1" in "foo", '"-1" in');
    117 //assertEquals(true, "2" in "foo", '"2" in');
    118 //assertEquals(false, "3" in "foo", '"3" in');
    119 
    120 delete S[3];
    121 assertEquals("undefined", typeof(S[3]));
    122 assertEquals(false, 3 in S);
    123 assertEquals(false, "3" in S);
    124 
    125 var N = new Number(43);
    126 assertEquals(43, N);
    127 N[-2] = "Alpha";
    128 assertEquals("Alpha", N[-2]);
    129 N[0] = "Zappa";
    130 assertEquals("Zappa", N[0]);
    131 assertEquals("Zappa", N["0"]);
    132 
    133 var A = ["V", "e", "t", "t", "e", "r"];
    134 var A2 = (A[0] = "v");
    135 assertEquals('v', A[0]);
    136 assertEquals('v', A2);
    137 
    138 var S = new String("Onkel");
    139 var S2 = (S[0] = 'o');
    140 assertEquals('O', S[0]);
    141 assertEquals('o', S2);
    142 
    143 var s = "Tante";
    144 var s2 = (s[0] = 't');
    145 assertEquals('T', s[0]);
    146 assertEquals('t', s2);
    147 
    148 var S2 = (S[-2] = 'o');
    149 assertEquals('o', S[-2]);
    150 assertEquals('o', S2);
    151 
    152 var s2 = (s[-2] = 't');
    153 assertEquals('undefined', typeof(s[-2]));
    154 assertEquals('t', s2);
    155