Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2009 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 x = "";
     29 var v = new Object();
     30 var w = new Object();
     31 var vv = function() { x += "hest"; return 1; }
     32 var ww = function() { x += "fisk"; return 2; }
     33 v.valueOf = vv;
     34 w.valueOf = ww;
     35 assertEquals(1, Math.min(v,w));
     36 assertEquals("hestfisk", x, "min");
     37 
     38 x = "";
     39 assertEquals(2, Math.max(v,w));
     40 assertEquals("hestfisk", x, "max");
     41 
     42 x = "";
     43 assertEquals(1, Math.max(v,v));
     44 assertEquals("hesthest", x, "max_identical");
     45 
     46 x = "";
     47 assertEquals(2, Math.min(w,w));
     48 assertEquals("fiskfisk", x, "max");
     49 
     50 x = "";
     51 assertEquals(Math.atan2(1, 2), Math.atan2(v, w));
     52 // JSC says fiskhest.
     53 assertEquals("hestfisk", x, "atan2");
     54 
     55 x = "";
     56 assertEquals(1, Math.pow(v, w));
     57 assertEquals("hestfisk", x, "pow");
     58 
     59 var year = { valueOf: function() { x += 1; return 2007; } };
     60 var month = { valueOf: function() { x += 2; return 2; } };
     61 var date = { valueOf: function() { x += 3; return 4; } };
     62 var hours = { valueOf: function() { x += 4; return 13; } };
     63 var minutes = { valueOf: function() { x += 5; return 50; } };
     64 var seconds = { valueOf: function() { x += 6; return 0; } };
     65 var ms = { valueOf: function() { x += 7; return 999; } };
     66 
     67 x = "";
     68 new Date(year, month, date, hours, minutes, seconds, ms);
     69 // JSC fails this one: Returns 12345671234567.
     70 assertEquals("1234567", x, "Date");
     71 
     72 x = "";
     73 Date(year, month, date, hours, minutes, seconds, ms);
     74 assertEquals("", x, "Date not constructor");
     75 
     76 x = "";
     77 Date.UTC(year, month, date, hours, minutes, seconds, ms);
     78 // JSC fails this one: Returns 12345671234567.
     79 assertEquals("1234567", x, "Date.UTC");
     80 
     81 x = "";
     82 new Date().setSeconds(seconds, ms);
     83 assertEquals("67", x, "Date.UTC");
     84 
     85 x = "";
     86 new Date().setSeconds(seconds, ms);
     87 assertEquals("67", x, "Date.setSeconds");
     88 
     89 x = "";
     90 new Date().setUTCSeconds(seconds, ms);
     91 assertEquals("67", x, "Date.setUTCSeconds");
     92 
     93 x = "";
     94 new Date().setMinutes(minutes, seconds, ms);
     95 assertEquals("567", x, "Date.setMinutes");
     96 
     97 x = "";
     98 new Date().setUTCMinutes(minutes, seconds, ms);
     99 assertEquals("567", x, "Date.setUTCMinutes");
    100 
    101 x = "";
    102 new Date().setHours(hours, minutes, seconds, ms);
    103 assertEquals("4567", x, "Date.setHours");
    104 
    105 x = "";
    106 new Date().setUTCHours(hours, minutes, seconds, ms);
    107 assertEquals("4567", x, "Date.setUTCHours");
    108 
    109 x = "";
    110 new Date().setDate(date, hours, minutes, seconds, ms);
    111 assertEquals("3", x, "Date.setDate");
    112 
    113 x = "";
    114 new Date().setUTCDate(date, hours, minutes, seconds, ms);
    115 assertEquals("3", x, "Date.setUTCDate");
    116 
    117 x = "";
    118 new Date().setMonth(month, date, hours, minutes, seconds, ms);
    119 assertEquals("23", x, "Date.setMonth");
    120 
    121 x = "";
    122 new Date().setUTCMonth(month, date, hours, minutes, seconds, ms);
    123 assertEquals("23", x, "Date.setUTCMonth");
    124 
    125 x = "";
    126 new Date().setFullYear(year, month, date, hours, minutes, seconds, ms);
    127 assertEquals("123", x, "Date.setFullYear");
    128 
    129 x = "";
    130 new Date().setUTCFullYear(year, month, date, hours, minutes, seconds, ms);
    131 assertEquals("123", x, "Date.setUTCFullYear");
    132 
    133 x = "";
    134 var a = { valueOf: function() { x += "hest"; return 97; } };
    135 var b = { valueOf: function() { x += "fisk"; return 98; } };
    136 assertEquals("ab", String.fromCharCode(a, b), "String.fromCharCode");
    137 assertEquals("hestfisk", x, "String.fromCharCode valueOf order");
    138 
    139 
    140 
    141 // Test whether valueOf is called when comparing identical objects
    142 x = "";
    143 assertTrue(a < b, "Compare objects a < b");
    144 assertEquals("hestfisk", x, "Compare objects a < b valueOf order");
    145 
    146 x = "";
    147 assertFalse(a < a, "Compare objects a < a");
    148 // assertEquals("hesthest", x, "Compare objects a < a valueOf order");
    149 
    150 x = "";
    151 assertTrue(a == a, "Compare objects a == a");
    152 assertEquals("", x, "Compare objects a == a valueOf not called");
    153 
    154 x = "";
    155 assertFalse(b > b, "Compare objects b > b");
    156 assertEquals("fiskfisk", x, "Compare objects b > b valueOf order");
    157 
    158 x = "";
    159 assertTrue(b >= b, "Compare objects b >= b");
    160 assertEquals("fiskfisk", x, "Compare objects b >= b valueOf order");
    161 
    162 x = "";
    163 assertFalse(a > b, "Compare objects a > b");
    164 assertEquals("hestfisk", x, "Compare objects a > b valueOf order");
    165 
    166 x = "";
    167 assertFalse(a > void(0), "Compare objects a > undefined");
    168 assertEquals("hest", x, "Compare objects a > undefined valueOf order");
    169 
    170 x = "";
    171 assertFalse(void(0) > b, "Compare objects undefined > b");
    172 assertEquals("fisk", x, "Compare objects undefined > b valueOf order");
    173 
    174 
    175 function identical_object_comparison() {
    176   x = "";
    177   assertTrue(a < b, "Compare objects a < b");
    178   assertEquals("hestfisk", x, "Compare objects a < b valueOf order");
    179 
    180   x = "";
    181   assertFalse(a < a, "Compare objects a < a");
    182   //  assertEquals("hesthest", x, "Compare objects a < a valueOf order");
    183 
    184   x = "";
    185   assertTrue(a == a, "Compare objects a == a");
    186   assertEquals("", x, "Compare objects a == a valueOf not called");
    187 
    188   x = "";
    189   assertFalse(b > b, "Compare objects b > b");
    190   assertEquals("fiskfisk", x, "Compare objects b > b valueOf order");
    191 
    192   x = "";
    193   assertTrue(b >= b, "Compare objects b >= b");
    194   assertEquals("fiskfisk", x, "Compare objects b >= b valueOf order");
    195 
    196   x = "";
    197   assertFalse(a > b, "Compare objects a > b");
    198   assertEquals("hestfisk", x, "Compare objects a > b valueOf order");
    199 
    200   x = "";
    201   assertFalse(a > void(0), "Compare objects a > undefined");
    202   assertEquals("hest", x, "Compare objects a > undefined valueOf order");
    203 
    204   x = "";
    205   assertFalse(void(0) > b, "Compare objects undefined > b");
    206   assertEquals("fisk", x, "Compare objects undefined > b valueOf order");
    207 }
    208 
    209 // Call inside loop to test optimization and possible caching.
    210 for (i = 0; i < 3; ++i) {
    211   identical_object_comparison();
    212 }
    213 
    214 
    215 print("ok");
    216