Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2010 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 // Test String.fromCharCode.
     29 
     30 
     31 // Test various receivers and arguments passed to String.fromCharCode.
     32 
     33 Object.prototype.fromCharCode = function(x) { return this; };
     34 
     35 var fcc = String.fromCharCode;
     36 var fcc2 = fcc;
     37 
     38 function constFun(x) { return function(y) { return x; }; }
     39 
     40 function test(num) {
     41   assertEquals(" ", String.fromCharCode(0x20));
     42   assertEquals(" ", String.fromCharCode(0x20 + 0x10000));
     43   assertEquals(" ", String.fromCharCode(0x20 - 0x10000));
     44   assertEquals(" ", String.fromCharCode(0x20 + 0.5));
     45 
     46   assertEquals("\u1234", String.fromCharCode(0x1234));
     47   assertEquals("\u1234", String.fromCharCode(0x1234 + 0x10000));
     48   assertEquals("\u1234", String.fromCharCode(0x1234 - 0x10000));
     49   assertEquals("\u1234", String.fromCharCode(0x1234 + 0.5));
     50 
     51   assertEquals("  ", String.fromCharCode(0x20, 0x20));
     52   assertEquals("  ", String.fromCharCode(0x20 + 0.5, 0x20));
     53 
     54   assertEquals(" ", fcc(0x20));
     55   assertEquals(" ", fcc(0x20 + 0x10000));
     56   assertEquals(" ", fcc(0x20 - 0x10000));
     57   assertEquals(" ", fcc(0x20 + 0.5));
     58 
     59   assertEquals("\u1234", fcc(0x1234));
     60   assertEquals("\u1234", fcc(0x1234 + 0x10000));
     61   assertEquals("\u1234", fcc(0x1234 - 0x10000));
     62   assertEquals("\u1234", fcc(0x1234 + 0.5));
     63 
     64   assertEquals("  ", fcc(0x20, 0x20));
     65   assertEquals("  ", fcc(0x20 + 0.5, 0x20));
     66 
     67   var receiver = (num < 5) ? String : (num < 9) ? "dummy" : 42;
     68   fcc2 = (num < 5) ? fcc
     69                    : (num < 9) ? constFun(Object("dummy"))
     70                                : constFun(Object(42));
     71   var expected = (num < 5) ? " " : (num < 9) ? Object("dummy") : Object(42);
     72   assertEquals(expected, receiver.fromCharCode(0x20));
     73   assertEquals(expected, receiver.fromCharCode(0x20 - 0x10000));
     74   assertEquals(expected, receiver.fromCharCode(0x20 + 0.5));
     75   assertEquals(expected, fcc2(0x20));
     76   assertEquals(expected, fcc2(0x20 - 0x10000));
     77   assertEquals(expected, fcc2(0x20 + 0.5));
     78 }
     79 
     80 // Use loop to test the custom IC.
     81 for (var i = 0; i < 10; i++) {
     82   test(i);
     83 }
     84 
     85 
     86 // Test the custom IC works correctly when the map changes.
     87 for (var i = 0; i < 10; i++) {
     88   var expected = (i < 5) ? " " : 42;
     89   if (i == 5) String.fromCharCode = function() { return 42; };
     90   assertEquals(expected, String.fromCharCode(0x20));
     91 }
     92