1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jsapi-tests/testToIntWidth.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,68 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 + */ 1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.10 + 1.11 +#include <math.h> 1.12 + 1.13 +#include "jsapi-tests/tests.h" 1.14 +#include "vm/NumericConversions.h" 1.15 + 1.16 +using js::detail::ToIntWidth; 1.17 +using js::detail::ToUintWidth; 1.18 + 1.19 +BEGIN_TEST(testToUint8TwiceUint8Range) 1.20 +{ 1.21 + double d = -256; 1.22 + uint8_t expected = 0; 1.23 + do { 1.24 + CHECK(ToUintWidth<uint8_t>(d) == expected); 1.25 + 1.26 + d++; 1.27 + expected++; 1.28 + } while (d <= 256); 1.29 + return true; 1.30 +} 1.31 +END_TEST(testToUint8TwiceUint8Range) 1.32 + 1.33 +BEGIN_TEST(testToInt8) 1.34 +{ 1.35 + double d = -128; 1.36 + int8_t expected = -128; 1.37 + do { 1.38 + CHECK(ToIntWidth<int8_t>(d) == expected); 1.39 + 1.40 + d++; 1.41 + expected++; 1.42 + } while (expected < 127); 1.43 + return true; 1.44 +} 1.45 +END_TEST(testToInt8) 1.46 + 1.47 +BEGIN_TEST(testToUint32Large) 1.48 +{ 1.49 + CHECK(ToUintWidth<uint32_t>(pow(2.0, 83)) == 0); 1.50 + CHECK(ToUintWidth<uint32_t>(pow(2.0, 83) + pow(2.0, 31)) == (1U << 31)); 1.51 + CHECK(ToUintWidth<uint32_t>(pow(2.0, 83) + 2 * pow(2.0, 31)) == 0); 1.52 + CHECK(ToUintWidth<uint32_t>(pow(2.0, 83) + 3 * pow(2.0, 31)) == (1U << 31)); 1.53 + CHECK(ToUintWidth<uint32_t>(pow(2.0, 84)) == 0); 1.54 + CHECK(ToUintWidth<uint32_t>(pow(2.0, 84) + pow(2.0, 31)) == 0); 1.55 + CHECK(ToUintWidth<uint32_t>(pow(2.0, 84) + pow(2.0, 32)) == 0); 1.56 + return true; 1.57 +} 1.58 +END_TEST(testToUint32Large) 1.59 + 1.60 +BEGIN_TEST(testToUint64Large) 1.61 +{ 1.62 + CHECK(ToUintWidth<uint64_t>(pow(2.0, 115)) == 0); 1.63 + CHECK(ToUintWidth<uint64_t>(pow(2.0, 115) + pow(2.0, 63)) == (1ULL << 63)); 1.64 + CHECK(ToUintWidth<uint64_t>(pow(2.0, 115) + 2 * pow(2.0, 63)) == 0); 1.65 + CHECK(ToUintWidth<uint64_t>(pow(2.0, 115) + 3 * pow(2.0, 63)) == (1ULL << 63)); 1.66 + CHECK(ToUintWidth<uint64_t>(pow(2.0, 116)) == 0); 1.67 + CHECK(ToUintWidth<uint64_t>(pow(2.0, 116) + pow(2.0, 63)) == 0); 1.68 + CHECK(ToUintWidth<uint64_t>(pow(2.0, 116) + pow(2.0, 64)) == 0); 1.69 + return true; 1.70 +} 1.71 +END_TEST(testToUint64Large)