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
28var x;
29
30// Converts a number to string respecting -0.
31function stringify(n) {
32  if ((1 / n) === -Infinity) return "-0";
33  return String(n);
34}
35
36function f(expected, y) {
37  function testEval(string, x, y) {
38    var mulFunction = Function("x, y", "return " + string);
39    return mulFunction(x, y);
40  }
41  function mulTest(expected, x, y) {
42    assertEquals(expected, x * y);
43    assertEquals(expected, testEval(stringify(x) + " * y", x, y));
44    assertEquals(expected, testEval("x * " + stringify(y), x, y));
45    assertEquals(expected, testEval(stringify(x) + " * " + stringify(y), x, y));
46  }
47  mulTest(expected, x, y);
48  mulTest(-expected, -x, y);
49  mulTest(-expected, x, -y);
50  mulTest(expected, -x, -y);
51  if (x === y) return;  // Symmetric cases not necessary.
52  mulTest(expected, y, x);
53  mulTest(-expected, -y, x);
54  mulTest(-expected, y, -x);
55  mulTest(expected, -y, -x);
56}
57
58x = 0;
59f(0, 0);
60x = 1;
61f(0, 0);
62f(1, 1);
63x = 2;
64f(0, 0);
65f(2, 1);
66f(4, 2);
67x = 3;
68f(0, 0);
69f(3, 1);
70f(6, 2);
71f(9, 3);
72x = 4;
73f(0, 0);
74f(4, 1);
75f(8, 2);
76f(12, 3);
77f(16, 4);
78x = 5;
79f(0, 0);
80f(5, 1);
81f(10, 2);
82f(15, 3);
83f(20, 4);
84f(25, 5);
85x = 7;
86f(0, 0);
87f(7, 1);
88f(14, 2);
89f(21, 3);
90f(28, 4);
91f(35, 5);
92f(49, 7);
93x = 8;
94f(0, 0);
95f(8, 1);
96f(16, 2);
97f(24, 3);
98f(32, 4);
99f(40, 5);
100f(56, 7);
101f(64, 8);
102x = 9;
103f(0, 0);
104f(9, 1);
105f(18, 2);
106f(27, 3);
107f(36, 4);
108f(45, 5);
109f(63, 7);
110f(72, 8);
111f(81, 9);
112x = 15;
113f(0, 0);
114f(15, 1);
115f(30, 2);
116f(45, 3);
117f(60, 4);
118f(75, 5);
119f(105, 7);
120f(120, 8);
121f(135, 9);
122f(225, 15);
123x = 16;
124f(0, 0);
125f(16, 1);
126f(32, 2);
127f(48, 3);
128f(64, 4);
129f(80, 5);
130f(112, 7);
131f(128, 8);
132f(144, 9);
133f(240, 15);
134f(256, 16);
135x = 17;
136f(0, 0);
137f(17, 1);
138f(34, 2);
139f(51, 3);
140f(68, 4);
141f(85, 5);
142f(119, 7);
143f(136, 8);
144f(153, 9);
145f(255, 15);
146f(272, 16);
147f(289, 17);
148x = 31;
149f(0, 0);
150f(31, 1);
151f(62, 2);
152f(93, 3);
153f(124, 4);
154f(155, 5);
155f(217, 7);
156f(248, 8);
157f(279, 9);
158f(465, 15);
159f(496, 16);
160f(527, 17);
161f(961, 31);
162x = 32;
163f(0, 0);
164f(32, 1);
165f(64, 2);
166f(96, 3);
167f(128, 4);
168f(160, 5);
169f(224, 7);
170f(256, 8);
171f(288, 9);
172f(480, 15);
173f(512, 16);
174f(544, 17);
175f(992, 31);
176f(1024, 32);
177x = 33;
178f(0, 0);
179f(33, 1);
180f(66, 2);
181f(99, 3);
182f(132, 4);
183f(165, 5);
184f(231, 7);
185f(264, 8);
186f(297, 9);
187f(495, 15);
188f(528, 16);
189f(561, 17);
190f(1023, 31);
191f(1056, 32);
192f(1089, 33);
193x = 63;
194f(0, 0);
195f(63, 1);
196f(126, 2);
197f(189, 3);
198f(252, 4);
199f(315, 5);
200f(441, 7);
201f(504, 8);
202f(567, 9);
203f(945, 15);
204f(1008, 16);
205f(1071, 17);
206f(1953, 31);
207f(2016, 32);
208f(2079, 33);
209f(3969, 63);
210x = 64;
211f(0, 0);
212f(64, 1);
213f(128, 2);
214f(192, 3);
215f(256, 4);
216f(320, 5);
217f(448, 7);
218f(512, 8);
219f(576, 9);
220f(960, 15);
221f(1024, 16);
222f(1088, 17);
223f(1984, 31);
224f(2048, 32);
225f(2112, 33);
226f(4032, 63);
227f(4096, 64);
228x = 65;
229f(0, 0);
230f(65, 1);
231f(130, 2);
232f(195, 3);
233f(260, 4);
234f(325, 5);
235f(455, 7);
236f(520, 8);
237f(585, 9);
238f(975, 15);
239f(1040, 16);
240f(1105, 17);
241f(2015, 31);
242f(2080, 32);
243f(2145, 33);
244f(4095, 63);
245f(4160, 64);
246f(4225, 65);
247x = 127;
248f(0, 0);
249f(127, 1);
250f(254, 2);
251f(381, 3);
252f(508, 4);
253f(635, 5);
254f(889, 7);
255f(1016, 8);
256f(1143, 9);
257f(1905, 15);
258f(2032, 16);
259f(2159, 17);
260f(3937, 31);
261f(4064, 32);
262f(4191, 33);
263f(8001, 63);
264f(8128, 64);
265f(8255, 65);
266f(16129, 127);
267x = 128;
268f(0, 0);
269f(128, 1);
270f(256, 2);
271f(384, 3);
272f(512, 4);
273f(640, 5);
274f(896, 7);
275f(1024, 8);
276f(1152, 9);
277f(1920, 15);
278f(2048, 16);
279f(2176, 17);
280f(3968, 31);
281f(4096, 32);
282f(4224, 33);
283f(8064, 63);
284f(8192, 64);
285f(8320, 65);
286f(16256, 127);
287f(16384, 128);
288x = 129;
289f(0, 0);
290f(129, 1);
291f(258, 2);
292f(387, 3);
293f(516, 4);
294f(645, 5);
295f(903, 7);
296f(1032, 8);
297f(1161, 9);
298f(1935, 15);
299f(2064, 16);
300f(2193, 17);
301f(3999, 31);
302f(4128, 32);
303f(4257, 33);
304f(8127, 63);
305f(8256, 64);
306f(8385, 65);
307f(16383, 127);
308f(16512, 128);
309f(16641, 129);
310x = 255;
311f(0, 0);
312f(255, 1);
313f(510, 2);
314f(765, 3);
315f(1020, 4);
316f(1275, 5);
317f(1785, 7);
318f(2040, 8);
319f(2295, 9);
320f(3825, 15);
321f(4080, 16);
322f(4335, 17);
323f(7905, 31);
324f(8160, 32);
325f(8415, 33);
326f(16065, 63);
327f(16320, 64);
328f(16575, 65);
329f(32385, 127);
330f(32640, 128);
331f(32895, 129);
332f(65025, 255);
333x = 256;
334f(0, 0);
335f(256, 1);
336f(512, 2);
337f(768, 3);
338f(1024, 4);
339f(1280, 5);
340f(1792, 7);
341f(2048, 8);
342f(2304, 9);
343f(3840, 15);
344f(4096, 16);
345f(4352, 17);
346f(7936, 31);
347f(8192, 32);
348f(8448, 33);
349f(16128, 63);
350f(16384, 64);
351f(16640, 65);
352f(32512, 127);
353f(32768, 128);
354f(33024, 129);
355f(65280, 255);
356f(65536, 256);
357x = 257;
358f(0, 0);
359f(257, 1);
360f(514, 2);
361f(771, 3);
362f(1028, 4);
363f(1285, 5);
364f(1799, 7);
365f(2056, 8);
366f(2313, 9);
367f(3855, 15);
368f(4112, 16);
369f(4369, 17);
370f(7967, 31);
371f(8224, 32);
372f(8481, 33);
373f(16191, 63);
374f(16448, 64);
375f(16705, 65);
376f(32639, 127);
377f(32896, 128);
378f(33153, 129);
379f(65535, 255);
380f(65792, 256);
381f(66049, 257);
382x = 511;
383f(0, 0);
384f(511, 1);
385f(1022, 2);
386f(1533, 3);
387f(2044, 4);
388f(2555, 5);
389f(3577, 7);
390f(4088, 8);
391f(4599, 9);
392f(7665, 15);
393f(8176, 16);
394f(8687, 17);
395f(15841, 31);
396f(16352, 32);
397f(16863, 33);
398f(32193, 63);
399f(32704, 64);
400f(33215, 65);
401f(64897, 127);
402f(65408, 128);
403f(65919, 129);
404f(130305, 255);
405f(130816, 256);
406f(131327, 257);
407f(261121, 511);
408x = 512;
409f(0, 0);
410f(512, 1);
411f(1024, 2);
412f(1536, 3);
413f(2048, 4);
414f(2560, 5);
415f(3584, 7);
416f(4096, 8);
417f(4608, 9);
418f(7680, 15);
419f(8192, 16);
420f(8704, 17);
421f(15872, 31);
422f(16384, 32);
423f(16896, 33);
424f(32256, 63);
425f(32768, 64);
426f(33280, 65);
427f(65024, 127);
428f(65536, 128);
429f(66048, 129);
430f(130560, 255);
431f(131072, 256);
432f(131584, 257);
433f(261632, 511);
434f(262144, 512);
435x = 513;
436f(0, 0);
437f(513, 1);
438f(1026, 2);
439f(1539, 3);
440f(2052, 4);
441f(2565, 5);
442f(3591, 7);
443f(4104, 8);
444f(4617, 9);
445f(7695, 15);
446f(8208, 16);
447f(8721, 17);
448f(15903, 31);
449f(16416, 32);
450f(16929, 33);
451f(32319, 63);
452f(32832, 64);
453f(33345, 65);
454f(65151, 127);
455f(65664, 128);
456f(66177, 129);
457f(130815, 255);
458f(131328, 256);
459f(131841, 257);
460f(262143, 511);
461f(262656, 512);
462f(263169, 513);
463x = 1023;
464f(0, 0);
465f(1023, 1);
466f(2046, 2);
467f(3069, 3);
468f(4092, 4);
469f(5115, 5);
470f(7161, 7);
471f(8184, 8);
472f(9207, 9);
473f(15345, 15);
474f(16368, 16);
475f(17391, 17);
476f(31713, 31);
477f(32736, 32);
478f(33759, 33);
479f(64449, 63);
480f(65472, 64);
481f(66495, 65);
482f(129921, 127);
483f(130944, 128);
484f(131967, 129);
485f(260865, 255);
486f(261888, 256);
487f(262911, 257);
488f(522753, 511);
489f(523776, 512);
490f(524799, 513);
491f(1046529, 1023);
492