js/src/jsapi-tests/testToIntWidth.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: set ts=8 sts=4 et sw=4 tw=99:
michael@0 3 */
michael@0 4 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 5 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 7
michael@0 8 #include <math.h>
michael@0 9
michael@0 10 #include "jsapi-tests/tests.h"
michael@0 11 #include "vm/NumericConversions.h"
michael@0 12
michael@0 13 using js::detail::ToIntWidth;
michael@0 14 using js::detail::ToUintWidth;
michael@0 15
michael@0 16 BEGIN_TEST(testToUint8TwiceUint8Range)
michael@0 17 {
michael@0 18 double d = -256;
michael@0 19 uint8_t expected = 0;
michael@0 20 do {
michael@0 21 CHECK(ToUintWidth<uint8_t>(d) == expected);
michael@0 22
michael@0 23 d++;
michael@0 24 expected++;
michael@0 25 } while (d <= 256);
michael@0 26 return true;
michael@0 27 }
michael@0 28 END_TEST(testToUint8TwiceUint8Range)
michael@0 29
michael@0 30 BEGIN_TEST(testToInt8)
michael@0 31 {
michael@0 32 double d = -128;
michael@0 33 int8_t expected = -128;
michael@0 34 do {
michael@0 35 CHECK(ToIntWidth<int8_t>(d) == expected);
michael@0 36
michael@0 37 d++;
michael@0 38 expected++;
michael@0 39 } while (expected < 127);
michael@0 40 return true;
michael@0 41 }
michael@0 42 END_TEST(testToInt8)
michael@0 43
michael@0 44 BEGIN_TEST(testToUint32Large)
michael@0 45 {
michael@0 46 CHECK(ToUintWidth<uint32_t>(pow(2.0, 83)) == 0);
michael@0 47 CHECK(ToUintWidth<uint32_t>(pow(2.0, 83) + pow(2.0, 31)) == (1U << 31));
michael@0 48 CHECK(ToUintWidth<uint32_t>(pow(2.0, 83) + 2 * pow(2.0, 31)) == 0);
michael@0 49 CHECK(ToUintWidth<uint32_t>(pow(2.0, 83) + 3 * pow(2.0, 31)) == (1U << 31));
michael@0 50 CHECK(ToUintWidth<uint32_t>(pow(2.0, 84)) == 0);
michael@0 51 CHECK(ToUintWidth<uint32_t>(pow(2.0, 84) + pow(2.0, 31)) == 0);
michael@0 52 CHECK(ToUintWidth<uint32_t>(pow(2.0, 84) + pow(2.0, 32)) == 0);
michael@0 53 return true;
michael@0 54 }
michael@0 55 END_TEST(testToUint32Large)
michael@0 56
michael@0 57 BEGIN_TEST(testToUint64Large)
michael@0 58 {
michael@0 59 CHECK(ToUintWidth<uint64_t>(pow(2.0, 115)) == 0);
michael@0 60 CHECK(ToUintWidth<uint64_t>(pow(2.0, 115) + pow(2.0, 63)) == (1ULL << 63));
michael@0 61 CHECK(ToUintWidth<uint64_t>(pow(2.0, 115) + 2 * pow(2.0, 63)) == 0);
michael@0 62 CHECK(ToUintWidth<uint64_t>(pow(2.0, 115) + 3 * pow(2.0, 63)) == (1ULL << 63));
michael@0 63 CHECK(ToUintWidth<uint64_t>(pow(2.0, 116)) == 0);
michael@0 64 CHECK(ToUintWidth<uint64_t>(pow(2.0, 116) + pow(2.0, 63)) == 0);
michael@0 65 CHECK(ToUintWidth<uint64_t>(pow(2.0, 116) + pow(2.0, 64)) == 0);
michael@0 66 return true;
michael@0 67 }
michael@0 68 END_TEST(testToUint64Large)

mercurial