tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

testToSignedOrUnsignedInteger.cpp (2121B)


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