test-conversions.cc 19.8 KB
Newer Older
1
// Copyright 2011 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 28 29

#include <stdlib.h>

30
#include "src/base/platform/platform.h"
31
#include "src/execution/isolate.h"
32
#include "src/heap/factory-inl.h"
33
#include "src/init/v8.h"
34
#include "src/numbers/conversions.h"
35
#include "src/objects/heap-number-inl.h"
36
#include "src/objects/objects.h"
37
#include "src/objects/smi.h"
38
#include "test/cctest/cctest.h"
39

40 41
namespace v8 {
namespace internal {
42 43

TEST(Hex) {
44 45 46 47 48 49 50 51 52 53 54 55 56
  CHECK_EQ(0.0, StringToDouble("0x0", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(0.0, StringToDouble("0X0", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(1.0, StringToDouble("0x1", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(16.0, StringToDouble("0x10", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(255.0, StringToDouble("0xFF", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(175.0, StringToDouble("0xAF", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));

  CHECK_EQ(0.0, StringToDouble("0x0", ALLOW_HEX));
  CHECK_EQ(0.0, StringToDouble("0X0", ALLOW_HEX));
  CHECK_EQ(1.0, StringToDouble("0x1", ALLOW_HEX));
  CHECK_EQ(16.0, StringToDouble("0x10", ALLOW_HEX));
  CHECK_EQ(255.0, StringToDouble("0xFF", ALLOW_HEX));
  CHECK_EQ(175.0, StringToDouble("0xAF", ALLOW_HEX));
57 58 59 60
}


TEST(Octal) {
61 62 63 64 65 66 67 68 69 70 71 72 73
  CHECK_EQ(0.0, StringToDouble("0o0", ALLOW_OCTAL | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(0.0, StringToDouble("0O0", ALLOW_OCTAL | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(1.0, StringToDouble("0o1", ALLOW_OCTAL | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(7.0, StringToDouble("0o7", ALLOW_OCTAL | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(8.0, StringToDouble("0o10", ALLOW_OCTAL | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(63.0, StringToDouble("0o77", ALLOW_OCTAL | ALLOW_IMPLICIT_OCTAL));

  CHECK_EQ(0.0, StringToDouble("0o0", ALLOW_OCTAL));
  CHECK_EQ(0.0, StringToDouble("0O0", ALLOW_OCTAL));
  CHECK_EQ(1.0, StringToDouble("0o1", ALLOW_OCTAL));
  CHECK_EQ(7.0, StringToDouble("0o7", ALLOW_OCTAL));
  CHECK_EQ(8.0, StringToDouble("0o10", ALLOW_OCTAL));
  CHECK_EQ(63.0, StringToDouble("0o77", ALLOW_OCTAL));
74 75 76 77
}


TEST(ImplicitOctal) {
78 79 80 81 82 83 84 85 86 87 88 89 90
  CHECK_EQ(0.0, StringToDouble("0", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(0.0, StringToDouble("00", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(1.0, StringToDouble("01", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(7.0, StringToDouble("07", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(8.0, StringToDouble("010", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(63.0, StringToDouble("077", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));

  CHECK_EQ(0.0, StringToDouble("0", ALLOW_HEX));
  CHECK_EQ(0.0, StringToDouble("00", ALLOW_HEX));
  CHECK_EQ(1.0, StringToDouble("01", ALLOW_HEX));
  CHECK_EQ(7.0, StringToDouble("07", ALLOW_HEX));
  CHECK_EQ(10.0, StringToDouble("010", ALLOW_HEX));
  CHECK_EQ(77.0, StringToDouble("077", ALLOW_HEX));
91 92

  const double x = 010000000000;  // Power of 2, no rounding errors.
93 94 95 96 97 98 99
  CHECK_EQ(x * x * x * x * x, StringToDouble("01"
                                             "0000000000"
                                             "0000000000"
                                             "0000000000"
                                             "0000000000"
                                             "0000000000",
                                             ALLOW_IMPLICIT_OCTAL));
100 101 102
}


103
TEST(Binary) {
104 105 106 107 108 109 110 111 112 113 114
  CHECK_EQ(0.0, StringToDouble("0b0", ALLOW_BINARY | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(0.0, StringToDouble("0B0", ALLOW_BINARY | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(1.0, StringToDouble("0b1", ALLOW_BINARY | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(2.0, StringToDouble("0b10", ALLOW_BINARY | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(3.0, StringToDouble("0b11", ALLOW_BINARY | ALLOW_IMPLICIT_OCTAL));

  CHECK_EQ(0.0, StringToDouble("0b0", ALLOW_BINARY));
  CHECK_EQ(0.0, StringToDouble("0B0", ALLOW_BINARY));
  CHECK_EQ(1.0, StringToDouble("0b1", ALLOW_BINARY));
  CHECK_EQ(2.0, StringToDouble("0b10", ALLOW_BINARY));
  CHECK_EQ(3.0, StringToDouble("0b11", ALLOW_BINARY));
115
}
116 117


118
TEST(MalformedOctal) {
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
  CHECK_EQ(8.0, StringToDouble("08", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(81.0, StringToDouble("081", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(78.0, StringToDouble("078", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));

  CHECK(std::isnan(StringToDouble("07.7", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL)));
  CHECK(std::isnan(StringToDouble("07.8", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL)));
  CHECK(std::isnan(StringToDouble("07e8", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL)));
  CHECK(std::isnan(StringToDouble("07e7", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL)));

  CHECK_EQ(8.7, StringToDouble("08.7", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(8e7, StringToDouble("08e7", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));

  CHECK_EQ(0.001, StringToDouble("0.001", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(0.713, StringToDouble("0.713", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));

  CHECK_EQ(8.0, StringToDouble("08", ALLOW_HEX));
  CHECK_EQ(81.0, StringToDouble("081", ALLOW_HEX));
  CHECK_EQ(78.0, StringToDouble("078", ALLOW_HEX));

  CHECK_EQ(7.7, StringToDouble("07.7", ALLOW_HEX));
  CHECK_EQ(7.8, StringToDouble("07.8", ALLOW_HEX));
  CHECK_EQ(7e8, StringToDouble("07e8", ALLOW_HEX));
  CHECK_EQ(7e7, StringToDouble("07e7", ALLOW_HEX));

  CHECK_EQ(8.7, StringToDouble("08.7", ALLOW_HEX));
  CHECK_EQ(8e7, StringToDouble("08e7", ALLOW_HEX));

  CHECK_EQ(0.001, StringToDouble("0.001", ALLOW_HEX));
  CHECK_EQ(0.713, StringToDouble("0.713", ALLOW_HEX));
148 149 150 151
}


TEST(TrailingJunk) {
152 153
  CHECK_EQ(8.0, StringToDouble("8q", ALLOW_TRAILING_JUNK));
  CHECK_EQ(63.0, StringToDouble("077qqq",
154
                                ALLOW_IMPLICIT_OCTAL | ALLOW_TRAILING_JUNK));
155 156 157 158
  CHECK_EQ(10.0,
           StringToDouble("10e", ALLOW_IMPLICIT_OCTAL | ALLOW_TRAILING_JUNK));
  CHECK_EQ(10.0,
           StringToDouble("10e-", ALLOW_IMPLICIT_OCTAL | ALLOW_TRAILING_JUNK));
159 160 161 162
}


TEST(NonStrDecimalLiteral) {
163 164 165 166 167 168 169 170
  CHECK(std::isnan(
      StringToDouble(" ", NO_FLAGS, std::numeric_limits<double>::quiet_NaN())));
  CHECK(std::isnan(
      StringToDouble("", NO_FLAGS, std::numeric_limits<double>::quiet_NaN())));
  CHECK(std::isnan(
      StringToDouble(" ", NO_FLAGS, std::numeric_limits<double>::quiet_NaN())));
  CHECK_EQ(0.0, StringToDouble("", NO_FLAGS));
  CHECK_EQ(0.0, StringToDouble(" ", NO_FLAGS));
171 172
}

173

174
TEST(IntegerStrLiteral) {
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
  CHECK_EQ(0.0, StringToDouble("0.0", NO_FLAGS));
  CHECK_EQ(0.0, StringToDouble("0", NO_FLAGS));
  CHECK_EQ(0.0, StringToDouble("00", NO_FLAGS));
  CHECK_EQ(0.0, StringToDouble("000", NO_FLAGS));
  CHECK_EQ(1.0, StringToDouble("1", NO_FLAGS));
  CHECK_EQ(-1.0, StringToDouble("-1", NO_FLAGS));
  CHECK_EQ(-1.0, StringToDouble("  -1  ", NO_FLAGS));
  CHECK_EQ(1.0, StringToDouble("  +1  ", NO_FLAGS));
  CHECK(std::isnan(StringToDouble("  -  1  ", NO_FLAGS)));
  CHECK(std::isnan(StringToDouble("  +  1  ", NO_FLAGS)));

  CHECK_EQ(0.0, StringToDouble("0e0", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(0.0, StringToDouble("0e1", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(0.0, StringToDouble("0e-1", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(0.0, StringToDouble("0e-100000", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(0.0, StringToDouble("0e+100000", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));
  CHECK_EQ(0.0, StringToDouble("0.", ALLOW_HEX | ALLOW_IMPLICIT_OCTAL));
192 193
}

194

195
TEST(LongNumberStr) {
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
  CHECK_EQ(1e10, StringToDouble("1"
                                "0000000000",
                                NO_FLAGS));
  CHECK_EQ(1e20, StringToDouble("1"
                                "0000000000"
                                "0000000000",
                                NO_FLAGS));

  CHECK_EQ(1e60, StringToDouble("1"
                                "0000000000"
                                "0000000000"
                                "0000000000"
                                "0000000000"
                                "0000000000"
                                "0000000000",
                                NO_FLAGS));

  CHECK_EQ(1e-2, StringToDouble("."
                                "0"
                                "1",
                                NO_FLAGS));
  CHECK_EQ(1e-11, StringToDouble("."
                                 "0000000000"
                                 "1",
                                 NO_FLAGS));
  CHECK_EQ(1e-21, StringToDouble("."
                                 "0000000000"
                                 "0000000000"
                                 "1",
225
                                 NO_FLAGS));
226

227 228 229 230 231 232 233 234 235
  CHECK_EQ(1e-61, StringToDouble("."
                                 "0000000000"
                                 "0000000000"
                                 "0000000000"
                                 "0000000000"
                                 "0000000000"
                                 "0000000000"
                                 "1",
                                 NO_FLAGS));
236 237 238 239

  // x = 24414062505131248.0 and y = 24414062505131252.0 are representable in
  // double. Check chat z = (x + y) / 2 is rounded to x...
  CHECK_EQ(24414062505131248.0,
240
           StringToDouble("24414062505131250.0", NO_FLAGS));
241 242 243

  // ... and z = (x + y) / 2 + delta is rounded to y.
  CHECK_EQ(24414062505131252.0,
244
           StringToDouble("24414062505131250.000000001", NO_FLAGS));
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
}


TEST(MaximumSignificantDigits) {
  char num[] =
      "4.4501477170144020250819966727949918635852426585926051135169509"
      "122872622312493126406953054127118942431783801370080830523154578"
      "251545303238277269592368457430440993619708911874715081505094180"
      "604803751173783204118519353387964161152051487413083163272520124"
      "606023105869053620631175265621765214646643181420505164043632222"
      "668006474326056011713528291579642227455489682133472873831754840"
      "341397809846934151055619529382191981473003234105366170879223151"
      "087335413188049110555339027884856781219017754500629806224571029"
      "581637117459456877330110324211689177656713705497387108207822477"
      "584250967061891687062782163335299376138075114200886249979505279"
      "101870966346394401564490729731565935244123171539810221213221201"
      "847003580761626016356864581135848683152156368691976240370422601"
      "6998291015625000000000000000000000000000000000e-308";

264
  CHECK_EQ(4.4501477170144017780491e-308, StringToDouble(num, NO_FLAGS));
265 266 267 268

  // Changes the result of strtod (at least in glibc implementation).
  num[sizeof(num) - 8] = '1';

269
  CHECK_EQ(4.4501477170144022721148e-308, StringToDouble(num, NO_FLAGS));
270 271
}

272

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
TEST(MinimumExponent) {
  // Same test but with different point-position.
  char num[] =
  "445014771701440202508199667279499186358524265859260511351695091"
  "228726223124931264069530541271189424317838013700808305231545782"
  "515453032382772695923684574304409936197089118747150815050941806"
  "048037511737832041185193533879641611520514874130831632725201246"
  "060231058690536206311752656217652146466431814205051640436322226"
  "680064743260560117135282915796422274554896821334728738317548403"
  "413978098469341510556195293821919814730032341053661708792231510"
  "873354131880491105553390278848567812190177545006298062245710295"
  "816371174594568773301103242116891776567137054973871082078224775"
  "842509670618916870627821633352993761380751142008862499795052791"
  "018709663463944015644907297315659352441231715398102212132212018"
  "470035807616260163568645811358486831521563686919762403704226016"
  "998291015625000000000000000000000000000000000e-1108";

290
  CHECK_EQ(4.4501477170144017780491e-308, StringToDouble(num, NO_FLAGS));
291 292 293 294

  // Changes the result of strtod (at least in glibc implementation).
  num[sizeof(num) - 8] = '1';

295
  CHECK_EQ(4.4501477170144022721148e-308, StringToDouble(num, NO_FLAGS));
296 297 298 299 300 301
}


TEST(MaximumExponent) {
  char num[] = "0.16e309";

302
  CHECK_EQ(1.59999999999999997765e+308, StringToDouble(num, NO_FLAGS));
303 304
}

305 306

TEST(ExponentNumberStr) {
307 308 309 310 311 312
  CHECK_EQ(1e1, StringToDouble("1e1", NO_FLAGS));
  CHECK_EQ(1e1, StringToDouble("1e+1", NO_FLAGS));
  CHECK_EQ(1e-1, StringToDouble("1e-1", NO_FLAGS));
  CHECK_EQ(1e100, StringToDouble("1e+100", NO_FLAGS));
  CHECK_EQ(1e-100, StringToDouble("1e-100", NO_FLAGS));
  CHECK_EQ(1e-106, StringToDouble(".000001e-100", NO_FLAGS));
313 314
}

315 316 317 318
using OneBit1 = base::BitField<uint32_t, 0, 1>;
using OneBit2 = base::BitField<uint32_t, 7, 1>;
using EightBit1 = base::BitField<uint32_t, 0, 8>;
using EightBit2 = base::BitField<uint32_t, 13, 8>;
319

320 321 322 323 324 325
TEST(BitField) {
  uint32_t x;

  // One bit bit field can hold values 0 and 1.
  CHECK(!OneBit1::is_valid(static_cast<uint32_t>(-1)));
  CHECK(!OneBit2::is_valid(static_cast<uint32_t>(-1)));
326
  for (unsigned i = 0; i < 2; i++) {
327 328 329 330 331 332 333 334 335 336 337 338 339 340
    CHECK(OneBit1::is_valid(i));
    x = OneBit1::encode(i);
    CHECK_EQ(i, OneBit1::decode(x));

    CHECK(OneBit2::is_valid(i));
    x = OneBit2::encode(i);
    CHECK_EQ(i, OneBit2::decode(x));
  }
  CHECK(!OneBit1::is_valid(2));
  CHECK(!OneBit2::is_valid(2));

  // Eight bit bit field can hold values from 0 tp 255.
  CHECK(!EightBit1::is_valid(static_cast<uint32_t>(-1)));
  CHECK(!EightBit2::is_valid(static_cast<uint32_t>(-1)));
341
  for (unsigned i = 0; i < 256; i++) {
342 343 344 345 346 347 348 349 350 351
    CHECK(EightBit1::is_valid(i));
    x = EightBit1::encode(i);
    CHECK_EQ(i, EightBit1::decode(x));
    CHECK(EightBit2::is_valid(i));
    x = EightBit2::encode(i);
    CHECK_EQ(i, EightBit2::decode(x));
  }
  CHECK(!EightBit1::is_valid(256));
  CHECK(!EightBit2::is_valid(256));
}
352

353 354
using UpperBits = base::BitField64<int, 61, 3>;
using MiddleBits = base::BitField64<int, 31, 2>;
355

356 357 358 359
TEST(BitField64) {
  uint64_t x;

  // Test most significant bits.
360
  x = 0xE000'0000'0000'0000;
361 362 363 364
  CHECK(x == UpperBits::encode(7));
  CHECK_EQ(7, UpperBits::decode(x));

  // Test the 32/64-bit boundary bits.
365
  x = 0x0000'0001'8000'0000;
366 367 368
  CHECK(x == MiddleBits::encode(3));
  CHECK_EQ(3, MiddleBits::decode(x));
}
369 370 371 372 373


static void CheckNonArrayIndex(bool expected, const char* chars) {
  auto isolate = CcTest::i_isolate();
  auto string = isolate->factory()->NewStringFromAsciiChecked(chars);
374
  CHECK_EQ(expected, IsSpecialIndex(*string));
375 376 377
}


dcarney's avatar
dcarney committed
378
TEST(SpecialIndexParsing) {
379 380
  auto isolate = CcTest::i_isolate();
  HandleScope scope(isolate);
381
  CheckNonArrayIndex(false, "");
382
  CheckNonArrayIndex(false, "-");
dcarney's avatar
dcarney committed
383 384
  CheckNonArrayIndex(true, "0");
  CheckNonArrayIndex(true, "-0");
385 386
  CheckNonArrayIndex(false, "01");
  CheckNonArrayIndex(false, "-01");
dcarney's avatar
dcarney committed
387 388 389
  CheckNonArrayIndex(true, "0.5");
  CheckNonArrayIndex(true, "-0.5");
  CheckNonArrayIndex(true, "1");
390
  CheckNonArrayIndex(true, "-1");
dcarney's avatar
dcarney committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404
  CheckNonArrayIndex(true, "10");
  CheckNonArrayIndex(true, "-10");
  CheckNonArrayIndex(true, "NaN");
  CheckNonArrayIndex(true, "Infinity");
  CheckNonArrayIndex(true, "-Infinity");
  CheckNonArrayIndex(true, "4294967295");
  CheckNonArrayIndex(true, "429496.7295");
  CheckNonArrayIndex(true, "1.3333333333333333");
  CheckNonArrayIndex(false, "1.3333333333333339");
  CheckNonArrayIndex(true, "1.333333333333331e+222");
  CheckNonArrayIndex(true, "-1.3333333333333211e+222");
  CheckNonArrayIndex(false, "-1.3333333333333311e+222");
  CheckNonArrayIndex(true, "429496.7295");
  CheckNonArrayIndex(false, "43s3");
405 406
  CheckNonArrayIndex(true, "4294967296");
  CheckNonArrayIndex(true, "-4294967296");
dcarney's avatar
dcarney committed
407 408 409 410 411
  CheckNonArrayIndex(true, "999999999999999");
  CheckNonArrayIndex(false, "9999999999999999");
  CheckNonArrayIndex(true, "-999999999999999");
  CheckNonArrayIndex(false, "-9999999999999999");
  CheckNonArrayIndex(false, "42949672964294967296429496729694966");
412
}
413 414 415 416 417 418

TEST(NoHandlesForTryNumberToSize) {
  i::Isolate* isolate = CcTest::i_isolate();
  size_t result = 0;
  {
    SealHandleScope no_handles(isolate);
419
    Smi smi = Smi::FromInt(1);
420
    CHECK(TryNumberToSize(smi, &result));
421
    CHECK_EQ(result, 1u);
422 423 424 425 426 427 428
  }
  result = 0;
  {
    HandleScope scope(isolate);
    Handle<HeapNumber> heap_number1 = isolate->factory()->NewHeapNumber(2.0);
    {
      SealHandleScope no_handles(isolate);
429
      CHECK(TryNumberToSize(*heap_number1, &result));
430
      CHECK_EQ(result, 2u);
431 432 433 434 435
    }
    Handle<HeapNumber> heap_number2 = isolate->factory()->NewHeapNumber(
        static_cast<double>(std::numeric_limits<size_t>::max()) + 10000.0);
    {
      SealHandleScope no_handles(isolate);
436
      CHECK(!TryNumberToSize(*heap_number2, &result));
437 438 439
    }
  }
}
440 441 442 443 444 445 446 447 448 449 450 451

TEST(TryNumberToSizeWithMaxSizePlusOne) {
  i::Isolate* isolate = CcTest::i_isolate();
  {
    HandleScope scope(isolate);
    // 1 << 64, larger than the limit of size_t.
    double value = 18446744073709551616.0;
    size_t result = 0;
    Handle<HeapNumber> heap_number = isolate->factory()->NewHeapNumber(value);
    CHECK(!TryNumberToSize(*heap_number, &result));
  }
}
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497

TEST(PositiveNumberToUint32) {
  i::Isolate* isolate = CcTest::i_isolate();
  i::Factory* factory = isolate->factory();
  uint32_t max = std::numeric_limits<uint32_t>::max();
  HandleScope scope(isolate);
  // Test Smi conversions.
  Handle<Object> number = handle(Smi::FromInt(0), isolate);
  CHECK_EQ(PositiveNumberToUint32(*number), 0u);
  number = handle(Smi::FromInt(-1), isolate);
  CHECK_EQ(PositiveNumberToUint32(*number), 0u);
  number = handle(Smi::FromInt(-1), isolate);
  CHECK_EQ(PositiveNumberToUint32(*number), 0u);
  number = handle(Smi::FromInt(Smi::kMinValue), isolate);
  CHECK_EQ(PositiveNumberToUint32(*number), 0u);
  number = handle(Smi::FromInt(Smi::kMaxValue), isolate);
  CHECK_EQ(PositiveNumberToUint32(*number),
           static_cast<uint32_t>(Smi::kMaxValue));
  // Test Double conversions.
  number = factory->NewHeapNumber(0.0);
  CHECK_EQ(PositiveNumberToUint32(*number), 0u);
  number = factory->NewHeapNumber(0.999);
  CHECK_EQ(PositiveNumberToUint32(*number), 0u);
  number = factory->NewHeapNumber(1.999);
  CHECK_EQ(PositiveNumberToUint32(*number), 1u);
  number = factory->NewHeapNumber(-12.0);
  CHECK_EQ(PositiveNumberToUint32(*number), 0u);
  number = factory->NewHeapNumber(12000.0);
  CHECK_EQ(PositiveNumberToUint32(*number), 12000u);
  number = factory->NewHeapNumber(static_cast<double>(Smi::kMaxValue) + 1);
  CHECK_EQ(PositiveNumberToUint32(*number),
           static_cast<uint32_t>(Smi::kMaxValue) + 1);
  number = factory->NewHeapNumber(max);
  CHECK_EQ(PositiveNumberToUint32(*number), max);
  number = factory->NewHeapNumber(static_cast<double>(max) * 1000);
  CHECK_EQ(PositiveNumberToUint32(*number), max);
  number = factory->NewHeapNumber(std::numeric_limits<double>::max());
  CHECK_EQ(PositiveNumberToUint32(*number), max);
  number = factory->NewHeapNumber(std::numeric_limits<double>::infinity());
  CHECK_EQ(PositiveNumberToUint32(*number), max);
  number =
      factory->NewHeapNumber(-1.0 * std::numeric_limits<double>::infinity());
  CHECK_EQ(PositiveNumberToUint32(*number), 0u);
  number = factory->NewHeapNumber(std::nan(""));
  CHECK_EQ(PositiveNumberToUint32(*number), 0u);
}
498 499 500

}  // namespace internal
}  // namespace v8