js/src/tests/ecma_6/Number/parseInt-01.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma_6/Number/parseInt-01.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,170 @@
     1.4 +// Any copyright is dedicated to the Public Domain.
     1.5 +// http://creativecommons.org/licenses/publicdomain/
     1.6 +
     1.7 +//-----------------------------------------------------------------------------
     1.8 +var BUGNUMBER = 886949;
     1.9 +var summary = "ES6 (draft May 2013) 15.7.3.9 Number.parseInt(string, radix)";
    1.10 +
    1.11 +print(BUGNUMBER + ": " + summary);
    1.12 +
    1.13 +/**************
    1.14 + * BEGIN TEST *
    1.15 + **************/
    1.16 +
    1.17 +var str, radix;
    1.18 +var upvar;
    1.19 +
    1.20 +/* 1. Let inputString be ToString(string). */
    1.21 +
    1.22 +assertEq(Number.parseInt({ toString: function() { return "17" } }, 10), 17);
    1.23 +
    1.24 +upvar = 0;
    1.25 +str = { get toString() { upvar++; return function() { upvar++; return "12345"; } } };
    1.26 +assertEq(Number.parseInt(str, 10), 12345);
    1.27 +assertEq(upvar, 2);
    1.28 +
    1.29 +
    1.30 +/*
    1.31 + * 2. Let S be a newly created substring of inputString consisting of the first
    1.32 + *    character that is not a StrWhiteSpaceChar and all characters following
    1.33 + *    that character. (In other words, remove leading white space.)
    1.34 + */
    1.35 +
    1.36 +var ws =
    1.37 +  ["\t", "\v", "\f", " ", "\xA0", "\uFEFF",
    1.38 +     "\u2004", "\u3000", // a few Unicode whitespaces
    1.39 +   "\r", "\n", "\u2028", "\u2029"];
    1.40 +
    1.41 +str = "8675309";
    1.42 +for (var i = 0, sz = ws.length; i < sz; i++)
    1.43 +{
    1.44 +  assertEq(Number.parseInt(ws[i] + str, 10), 8675309);
    1.45 +  for (var j = 0, sz = ws.length; j < sz; j++)
    1.46 +  {
    1.47 +    assertEq(Number.parseInt(ws[i] + ws[j] + str, 10), 8675309,
    1.48 +             ws[i].charCodeAt(0).toString(16) + ", " +
    1.49 +             ws[j].charCodeAt(0).toString(16));
    1.50 +  }
    1.51 +}
    1.52 +
    1.53 +
    1.54 +/*
    1.55 + * 3. Let sign be 1.
    1.56 + * 4. If S is not empty and the first character of S is a minus sign -, let
    1.57 + *    sign be −1.
    1.58 + */
    1.59 +str = "5552368";
    1.60 +assertEq(Number.parseInt("-" + str, 10), -Number.parseInt(str, 10));
    1.61 +assertEq(Number.parseInt(" -" + str, 10), -Number.parseInt(str, 10));
    1.62 +assertEq(Number.parseInt("-", 10), NaN);
    1.63 +assertEq(Number.parseInt("", 10), NaN);
    1.64 +assertEq(Number.parseInt("-0", 10), -0);
    1.65 +
    1.66 +
    1.67 +/*
    1.68 + * 5. If S is not empty and the first character of S is a plus sign + or a
    1.69 + *    minus sign -, then remove the first character from S.
    1.70 + */
    1.71 +assertEq(Number.parseInt("+12345", 10), 12345);
    1.72 +assertEq(Number.parseInt(" +12345", 10), 12345);
    1.73 +assertEq(Number.parseInt("-12345", 10), -12345);
    1.74 +assertEq(Number.parseInt(" -12345", 10), -12345);
    1.75 +
    1.76 +
    1.77 +/*
    1.78 + * 6.  Let R = ToInt32(radix).
    1.79 + */
    1.80 +
    1.81 +upvar = "";
    1.82 +str =
    1.83 +  { toString: function() { if (!upvar) upvar = "string"; return "42"; } };
    1.84 +radix =
    1.85 +  { toString: function() { if (!upvar) upvar = "radix"; return "10"; } };
    1.86 +
    1.87 +assertEq(Number.parseInt(str, radix), 42);
    1.88 +assertEq(upvar, "string");
    1.89 +
    1.90 +assertEq(Number.parseInt("123", null), 123);
    1.91 +assertEq(Number.parseInt("123", undefined), 123);
    1.92 +assertEq(Number.parseInt("123", NaN), 123);
    1.93 +assertEq(Number.parseInt("123", -0), 123);
    1.94 +assertEq(Number.parseInt("10", 72057594037927950), 16);
    1.95 +assertEq(Number.parseInt("10", -4294967292), 4);
    1.96 +assertEq(Number.parseInt("0x10", 1e308), 16);
    1.97 +assertEq(Number.parseInt("10", 1e308), 10);
    1.98 +assertEq(Number.parseInt("10", { valueOf: function() { return 16; } }), 16);
    1.99 +
   1.100 +
   1.101 +/*
   1.102 + * 7.  Let stripPrefix be true.
   1.103 + * 8.  If R ≠ 0, then
   1.104 + *     a. If R < 2 or R > 36, then return NaN.
   1.105 + *     b. If R ≠ 16, let stripPrefix be false.
   1.106 + * 9.  Else, R = 0
   1.107 + *     a. Let R = 10.
   1.108 + * 10. If stripPrefix is true, then
   1.109 + *     a. If the length of S is at least 2 and the first two characters of S
   1.110 + *     are either “0x” or “0X”, then remove the first two characters from S and
   1.111 + *     let R = 16.
   1.112 + */
   1.113 +var vs = ["1", "51", "917", "2343", "99963"];
   1.114 +for (var i = 0, sz = vs.length; i < sz; i++)
   1.115 +  assertEq(Number.parseInt(vs[i], 0), Number.parseInt(vs[i], 10), "bad " + vs[i]);
   1.116 +
   1.117 +assertEq(Number.parseInt("0x10"), 16);
   1.118 +assertEq(Number.parseInt("0x10", 0), 16);
   1.119 +assertEq(Number.parseInt("0x10", 16), 16);
   1.120 +assertEq(Number.parseInt("0x10", 8), 0);
   1.121 +assertEq(Number.parseInt("-0x10", 16), -16);
   1.122 +
   1.123 +assertEq(Number.parseInt("5", 1), NaN);
   1.124 +assertEq(Number.parseInt("5", 37), NaN);
   1.125 +assertEq(Number.parseInt("5", { valueOf: function() { return -1; } }), NaN);
   1.126 +
   1.127 +
   1.128 +/*
   1.129 + * 11. If S contains any character that is not a radix-R digit, then let Z be
   1.130 + *     the substring of S consisting of all characters before the first such
   1.131 + *     character; otherwise, let Z be S.
   1.132 + * 12. If Z is empty, return NaN.
   1.133 + */
   1.134 +assertEq(Number.parseInt(""), NaN);
   1.135 +assertEq(Number.parseInt("ohai"), NaN);
   1.136 +assertEq(Number.parseInt("0xohai"), NaN);
   1.137 +assertEq(Number.parseInt("-ohai"), NaN);
   1.138 +assertEq(Number.parseInt("+ohai"), NaN);
   1.139 +assertEq(Number.parseInt(" ohai"), NaN);
   1.140 +
   1.141 +assertEq(Number.parseInt("0xaohai"), 10);
   1.142 +assertEq(Number.parseInt("hohai", 18), 17);
   1.143 +
   1.144 +
   1.145 +/*
   1.146 + * 13. Let mathInt be the mathematical integer value that is represented by Z
   1.147 + *     in radix-R notation, using the letters A-Z and a-z for digits with
   1.148 + *     values 10 through 35. (However, if R is 10 and Z contains more than 20
   1.149 + *     significant digits, every significant digit after the 20th may be
   1.150 + *     replaced by a 0 digit, at the option of the implementation; and if R is
   1.151 + *     not 2, 4, 8, 10, 16, or 32, then mathInt may be an implementation-
   1.152 + *     dependent approximation to the mathematical integer value that is
   1.153 + *     represented by Z in radix-R notation.)
   1.154 + * 14. Let number be the Number value for mathInt.
   1.155 + * 15. Return sign × number.
   1.156 + */
   1.157 +assertEq(Number.parseInt("ohai", 36), 1142154);
   1.158 +assertEq(Number.parseInt("0ohai", 36), 1142154);
   1.159 +assertEq(Number.parseInt("00ohai", 36), 1142154);
   1.160 +assertEq(Number.parseInt("A", 16), 10);
   1.161 +assertEq(Number.parseInt("0A", 16), 10);
   1.162 +assertEq(Number.parseInt("00A", 16), 10);
   1.163 +assertEq(Number.parseInt("A", 17), 10);
   1.164 +assertEq(Number.parseInt("0A", 17), 10);
   1.165 +assertEq(Number.parseInt("00A", 17), 10);
   1.166 +
   1.167 +
   1.168 +/******************************************************************************/
   1.169 +
   1.170 +if (typeof reportCompare === "function")
   1.171 +  reportCompare(true, true);
   1.172 +
   1.173 +print("All tests passed!");

mercurial