• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
28function toInt32(x) {
29  return x | 0;
30}
31
32assertEquals(0, toInt32(Infinity), "Inf");
33assertEquals(0, toInt32(-Infinity), "-Inf");
34assertEquals(0, toInt32(NaN), "NaN");
35assertEquals(0, toInt32(0.0), "zero");
36assertEquals(0, toInt32(-0.0), "-zero");
37
38assertEquals(0, toInt32(Number.MIN_VALUE));
39assertEquals(0, toInt32(-Number.MIN_VALUE));
40assertEquals(0, toInt32(0.1));
41assertEquals(0, toInt32(-0.1));
42assertEquals(1, toInt32(1), "one");
43assertEquals(1, toInt32(1.1), "onepointone");
44assertEquals(-1, toInt32(-1), "-one");
45assertEquals(0, toInt32(0.6), "truncate positive (0.6)");
46assertEquals(1, toInt32(1.6), "truncate positive (1.6)");
47assertEquals(0, toInt32(-0.6), "truncate negative (-0.6)");
48assertEquals(-1, toInt32(-1.6), "truncate negative (-1.6)");
49
50assertEquals(2147483647, toInt32(2147483647));
51assertEquals(-2147483648, toInt32(2147483648));
52assertEquals(-2147483647, toInt32(2147483649));
53
54assertEquals(-1, toInt32(4294967295));
55assertEquals(0, toInt32(4294967296));
56assertEquals(1, toInt32(4294967297));
57
58assertEquals(-2147483647, toInt32(-2147483647));
59assertEquals(-2147483648, toInt32(-2147483648));
60assertEquals(2147483647, toInt32(-2147483649));
61
62assertEquals(1, toInt32(-4294967295));
63assertEquals(0, toInt32(-4294967296));
64assertEquals(-1, toInt32(-4294967297));
65
66assertEquals(-2147483648, toInt32(2147483648.25));
67assertEquals(-2147483648, toInt32(2147483648.5));
68assertEquals(-2147483648, toInt32(2147483648.75));
69assertEquals(-1, toInt32(4294967295.25));
70assertEquals(-1, toInt32(4294967295.5));
71assertEquals(-1, toInt32(4294967295.75));
72assertEquals(-1294967296, toInt32(3000000000.25));
73assertEquals(-1294967296, toInt32(3000000000.5));
74assertEquals(-1294967296, toInt32(3000000000.75));
75
76assertEquals(-2147483648, toInt32(-2147483648.25));
77assertEquals(-2147483648, toInt32(-2147483648.5));
78assertEquals(-2147483648, toInt32(-2147483648.75));
79assertEquals(1, toInt32(-4294967295.25));
80assertEquals(1, toInt32(-4294967295.5));
81assertEquals(1, toInt32(-4294967295.75));
82assertEquals(1294967296, toInt32(-3000000000.25));
83assertEquals(1294967296, toInt32(-3000000000.5));
84assertEquals(1294967296, toInt32(-3000000000.75));
85
86var base = Math.pow(2, 64);
87assertEquals(0, toInt32(base + 0));
88assertEquals(0, toInt32(base + 1117));
89assertEquals(4096, toInt32(base + 2234));
90assertEquals(4096, toInt32(base + 3351));
91assertEquals(4096, toInt32(base + 4468));
92assertEquals(4096, toInt32(base + 5585));
93assertEquals(8192, toInt32(base + 6702));
94assertEquals(8192, toInt32(base + 7819));
95assertEquals(8192, toInt32(base + 8936));
96assertEquals(8192, toInt32(base + 10053));
97assertEquals(12288, toInt32(base + 11170));
98assertEquals(12288, toInt32(base + 12287));
99assertEquals(12288, toInt32(base + 13404));
100assertEquals(16384, toInt32(base + 14521));
101assertEquals(16384, toInt32(base + 15638));
102assertEquals(16384, toInt32(base + 16755));
103assertEquals(16384, toInt32(base + 17872));
104assertEquals(20480, toInt32(base + 18989));
105assertEquals(20480, toInt32(base + 20106));
106assertEquals(20480, toInt32(base + 21223));
107assertEquals(20480, toInt32(base + 22340));
108assertEquals(24576, toInt32(base + 23457));
109assertEquals(24576, toInt32(base + 24574));
110assertEquals(24576, toInt32(base + 25691));
111assertEquals(28672, toInt32(base + 26808));
112assertEquals(28672, toInt32(base + 27925));
113assertEquals(28672, toInt32(base + 29042));
114assertEquals(28672, toInt32(base + 30159));
115assertEquals(32768, toInt32(base + 31276));
116
117// bignum is (2^53 - 1) * 2^31 - highest number with bit 31 set.
118var bignum = Math.pow(2, 84) - Math.pow(2, 31);
119assertEquals(-Math.pow(2,31), toInt32(bignum));
120assertEquals(-Math.pow(2,31), toInt32(-bignum));
121assertEquals(0, toInt32(2 * bignum));
122assertEquals(0, toInt32(-(2 * bignum)));
123assertEquals(0, toInt32(bignum - Math.pow(2,31)));
124assertEquals(0, toInt32(-(bignum - Math.pow(2,31))));
125
126// max_fraction is largest number below 1.
127var max_fraction = (1 - Math.pow(2,-53));
128assertEquals(0, toInt32(max_fraction));
129assertEquals(0, toInt32(-max_fraction));
130