Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(358)

Side by Side Diff: test/cctest/test-conversions.cc

Issue 2577143002: [runtime] Add PositiveNumberToUint32 helper to avoid double to uint roundtrip (Closed)
Patch Set: avoid overflows Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/runtime/runtime-regexp.cc ('k') | test/mjsunit/es6/string-startswith.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 i::Isolate* isolate = CcTest::i_isolate(); 449 i::Isolate* isolate = CcTest::i_isolate();
450 { 450 {
451 HandleScope scope(isolate); 451 HandleScope scope(isolate);
452 // 1 << 64, larger than the limit of size_t. 452 // 1 << 64, larger than the limit of size_t.
453 double value = 18446744073709551616.0; 453 double value = 18446744073709551616.0;
454 size_t result = 0; 454 size_t result = 0;
455 Handle<HeapNumber> heap_number = isolate->factory()->NewHeapNumber(value); 455 Handle<HeapNumber> heap_number = isolate->factory()->NewHeapNumber(value);
456 CHECK(!TryNumberToSize(*heap_number, &result)); 456 CHECK(!TryNumberToSize(*heap_number, &result));
457 } 457 }
458 } 458 }
459
460 TEST(PositiveNumberToUint32) {
461 i::Isolate* isolate = CcTest::i_isolate();
462 i::Factory* factory = isolate->factory();
463 uint32_t max = std::numeric_limits<uint32_t>::max();
464 HandleScope scope(isolate);
465 // Test Smi conversions.
466 Handle<Object> number = handle(Smi::FromInt(0), isolate);
467 CHECK_EQ(PositiveNumberToUint32(*number), 0u);
468 number = handle(Smi::FromInt(-1), isolate);
469 CHECK_EQ(PositiveNumberToUint32(*number), 0u);
470 number = handle(Smi::FromInt(-1), isolate);
471 CHECK_EQ(PositiveNumberToUint32(*number), 0u);
472 number = handle(Smi::FromInt(Smi::kMinValue), isolate);
473 CHECK_EQ(PositiveNumberToUint32(*number), 0u);
474 number = handle(Smi::FromInt(Smi::kMaxValue), isolate);
475 CHECK_EQ(PositiveNumberToUint32(*number),
476 static_cast<uint32_t>(Smi::kMaxValue));
477 // Test Double conversions.
478 number = factory->NewHeapNumber(0.0);
479 CHECK_EQ(PositiveNumberToUint32(*number), 0u);
480 number = factory->NewHeapNumber(0.999);
481 CHECK_EQ(PositiveNumberToUint32(*number), 0u);
482 number = factory->NewHeapNumber(1.999);
483 CHECK_EQ(PositiveNumberToUint32(*number), 1u);
484 number = factory->NewHeapNumber(-12.0);
485 CHECK_EQ(PositiveNumberToUint32(*number), 0u);
486 number = factory->NewHeapNumber(12000.0);
487 CHECK_EQ(PositiveNumberToUint32(*number), 12000u);
488 number = factory->NewHeapNumber(static_cast<double>(Smi::kMaxValue) + 1);
489 CHECK_EQ(PositiveNumberToUint32(*number),
490 static_cast<uint32_t>(Smi::kMaxValue) + 1);
491 number = factory->NewHeapNumber(max);
492 CHECK_EQ(PositiveNumberToUint32(*number), max);
493 number = factory->NewHeapNumber(static_cast<double>(max) * 1000);
494 CHECK_EQ(PositiveNumberToUint32(*number), max);
495 number = factory->NewHeapNumber(std::numeric_limits<double>::max());
496 CHECK_EQ(PositiveNumberToUint32(*number), max);
497 number = factory->NewHeapNumber(std::numeric_limits<double>::infinity());
498 CHECK_EQ(PositiveNumberToUint32(*number), max);
499 number =
500 factory->NewHeapNumber(-1.0 * std::numeric_limits<double>::infinity());
501 CHECK_EQ(PositiveNumberToUint32(*number), 0u);
502 number = factory->NewHeapNumber(std::nan(""));
503 CHECK_EQ(PositiveNumberToUint32(*number), 0u);
504 }
OLDNEW
« no previous file with comments | « src/runtime/runtime-regexp.cc ('k') | test/mjsunit/es6/string-startswith.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698