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