mfbt/tests/TestCeilingFloor.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mfbt/tests/TestCeilingFloor.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,85 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "mozilla/MathAlgorithms.h"
    1.10 +
    1.11 +using mozilla::CeilingLog2;
    1.12 +using mozilla::FloorLog2;
    1.13 +using mozilla::RoundUpPow2;
    1.14 +
    1.15 +static void
    1.16 +TestCeiling()
    1.17 +{
    1.18 +  for (uint32_t i = 0; i <= 1; i++)
    1.19 +    MOZ_RELEASE_ASSERT(CeilingLog2(i) == 0);
    1.20 +
    1.21 +  for (uint32_t i = 2; i <= 2; i++)
    1.22 +    MOZ_RELEASE_ASSERT(CeilingLog2(i) == 1);
    1.23 +
    1.24 +  for (uint32_t i = 3; i <= 4; i++)
    1.25 +    MOZ_RELEASE_ASSERT(CeilingLog2(i) == 2);
    1.26 +
    1.27 +  for (uint32_t i = 5; i <= 8; i++)
    1.28 +    MOZ_RELEASE_ASSERT(CeilingLog2(i) == 3);
    1.29 +
    1.30 +  for (uint32_t i = 9; i <= 16; i++)
    1.31 +    MOZ_RELEASE_ASSERT(CeilingLog2(i) == 4);
    1.32 +}
    1.33 +
    1.34 +static void
    1.35 +TestFloor()
    1.36 +{
    1.37 +  for (uint32_t i = 0; i <= 1; i++)
    1.38 +    MOZ_RELEASE_ASSERT(FloorLog2(i) == 0);
    1.39 +
    1.40 +  for (uint32_t i = 2; i <= 3; i++)
    1.41 +    MOZ_RELEASE_ASSERT(FloorLog2(i) == 1);
    1.42 +
    1.43 +  for (uint32_t i = 4; i <= 7; i++)
    1.44 +    MOZ_RELEASE_ASSERT(FloorLog2(i) == 2);
    1.45 +
    1.46 +  for (uint32_t i = 8; i <= 15; i++)
    1.47 +    MOZ_RELEASE_ASSERT(FloorLog2(i) == 3);
    1.48 +
    1.49 +  for (uint32_t i = 16; i <= 31; i++)
    1.50 +    MOZ_RELEASE_ASSERT(FloorLog2(i) == 4);
    1.51 +}
    1.52 +
    1.53 +static void
    1.54 +TestRoundUpPow2()
    1.55 +{
    1.56 +  MOZ_RELEASE_ASSERT(RoundUpPow2(0) == 1);
    1.57 +  MOZ_RELEASE_ASSERT(RoundUpPow2(1) == 1);
    1.58 +  MOZ_RELEASE_ASSERT(RoundUpPow2(2) == 2);
    1.59 +  MOZ_RELEASE_ASSERT(RoundUpPow2(3) == 4);
    1.60 +  MOZ_RELEASE_ASSERT(RoundUpPow2(4) == 4);
    1.61 +  MOZ_RELEASE_ASSERT(RoundUpPow2(5) == 8);
    1.62 +  MOZ_RELEASE_ASSERT(RoundUpPow2(6) == 8);
    1.63 +  MOZ_RELEASE_ASSERT(RoundUpPow2(7) == 8);
    1.64 +  MOZ_RELEASE_ASSERT(RoundUpPow2(8) == 8);
    1.65 +  MOZ_RELEASE_ASSERT(RoundUpPow2(9) == 16);
    1.66 +
    1.67 +  MOZ_RELEASE_ASSERT(RoundUpPow2(15) == 16);
    1.68 +  MOZ_RELEASE_ASSERT(RoundUpPow2(16) == 16);
    1.69 +  MOZ_RELEASE_ASSERT(RoundUpPow2(17) == 32);
    1.70 +
    1.71 +  MOZ_RELEASE_ASSERT(RoundUpPow2(31) == 32);
    1.72 +  MOZ_RELEASE_ASSERT(RoundUpPow2(32) == 32);
    1.73 +  MOZ_RELEASE_ASSERT(RoundUpPow2(33) == 64);
    1.74 +
    1.75 +  size_t MaxPow2 = size_t(1) << (sizeof(size_t) * CHAR_BIT - 1);
    1.76 +  MOZ_RELEASE_ASSERT(RoundUpPow2(MaxPow2 - 1) == MaxPow2);
    1.77 +  MOZ_RELEASE_ASSERT(RoundUpPow2(MaxPow2) == MaxPow2);
    1.78 +  // not valid to round up when past the max power of two
    1.79 +}
    1.80 +
    1.81 +int main()
    1.82 +{
    1.83 +  TestCeiling();
    1.84 +  TestFloor();
    1.85 +
    1.86 +  TestRoundUpPow2();
    1.87 +  return 0;
    1.88 +}

mercurial