michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: michael@0: #include "jsapi-tests/tests.h" michael@0: #include "vm/NumericConversions.h" michael@0: michael@0: using js::detail::ToIntWidth; michael@0: using js::detail::ToUintWidth; michael@0: michael@0: BEGIN_TEST(testToUint8TwiceUint8Range) michael@0: { michael@0: double d = -256; michael@0: uint8_t expected = 0; michael@0: do { michael@0: CHECK(ToUintWidth(d) == expected); michael@0: michael@0: d++; michael@0: expected++; michael@0: } while (d <= 256); michael@0: return true; michael@0: } michael@0: END_TEST(testToUint8TwiceUint8Range) michael@0: michael@0: BEGIN_TEST(testToInt8) michael@0: { michael@0: double d = -128; michael@0: int8_t expected = -128; michael@0: do { michael@0: CHECK(ToIntWidth(d) == expected); michael@0: michael@0: d++; michael@0: expected++; michael@0: } while (expected < 127); michael@0: return true; michael@0: } michael@0: END_TEST(testToInt8) michael@0: michael@0: BEGIN_TEST(testToUint32Large) michael@0: { michael@0: CHECK(ToUintWidth(pow(2.0, 83)) == 0); michael@0: CHECK(ToUintWidth(pow(2.0, 83) + pow(2.0, 31)) == (1U << 31)); michael@0: CHECK(ToUintWidth(pow(2.0, 83) + 2 * pow(2.0, 31)) == 0); michael@0: CHECK(ToUintWidth(pow(2.0, 83) + 3 * pow(2.0, 31)) == (1U << 31)); michael@0: CHECK(ToUintWidth(pow(2.0, 84)) == 0); michael@0: CHECK(ToUintWidth(pow(2.0, 84) + pow(2.0, 31)) == 0); michael@0: CHECK(ToUintWidth(pow(2.0, 84) + pow(2.0, 32)) == 0); michael@0: return true; michael@0: } michael@0: END_TEST(testToUint32Large) michael@0: michael@0: BEGIN_TEST(testToUint64Large) michael@0: { michael@0: CHECK(ToUintWidth(pow(2.0, 115)) == 0); michael@0: CHECK(ToUintWidth(pow(2.0, 115) + pow(2.0, 63)) == (1ULL << 63)); michael@0: CHECK(ToUintWidth(pow(2.0, 115) + 2 * pow(2.0, 63)) == 0); michael@0: CHECK(ToUintWidth(pow(2.0, 115) + 3 * pow(2.0, 63)) == (1ULL << 63)); michael@0: CHECK(ToUintWidth(pow(2.0, 116)) == 0); michael@0: CHECK(ToUintWidth(pow(2.0, 116) + pow(2.0, 63)) == 0); michael@0: CHECK(ToUintWidth(pow(2.0, 116) + pow(2.0, 64)) == 0); michael@0: return true; michael@0: } michael@0: END_TEST(testToUint64Large)