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

Unified 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/cctest/test-conversions.cc
diff --git a/test/cctest/test-conversions.cc b/test/cctest/test-conversions.cc
index d13badd20273ad31a236f4673b8758b190e4da9f..65cd96e0dffe42419ca3d0e65eb5f791a121ae76 100644
--- a/test/cctest/test-conversions.cc
+++ b/test/cctest/test-conversions.cc
@@ -456,3 +456,49 @@ TEST(TryNumberToSizeWithMaxSizePlusOne) {
CHECK(!TryNumberToSize(*heap_number, &result));
}
}
+
+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);
+}
« 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