no bug - Import translations from android-l10n r=release a=l10n CLOSED TREE
[gecko.git] / mfbt / tests / TestCeilingFloor.cpp
blob7bdd6ea27ccf7adf5edda02be0bff7aeabe2188e
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 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/MathAlgorithms.h"
9 using mozilla::CeilingLog2;
10 using mozilla::FloorLog2;
11 using mozilla::RoundUpPow2;
13 static void TestCeiling() {
14 for (uint32_t i = 0; i <= 1; i++) {
15 MOZ_RELEASE_ASSERT(CeilingLog2(i) == 0);
17 for (uint32_t i = 2; i <= 2; i++) {
18 MOZ_RELEASE_ASSERT(CeilingLog2(i) == 1);
20 for (uint32_t i = 3; i <= 4; i++) {
21 MOZ_RELEASE_ASSERT(CeilingLog2(i) == 2);
23 for (uint32_t i = 5; i <= 8; i++) {
24 MOZ_RELEASE_ASSERT(CeilingLog2(i) == 3);
26 for (uint32_t i = 9; i <= 16; i++) {
27 MOZ_RELEASE_ASSERT(CeilingLog2(i) == 4);
31 static void TestFloor() {
32 for (uint32_t i = 0; i <= 1; i++) {
33 MOZ_RELEASE_ASSERT(FloorLog2(i) == 0);
35 for (uint32_t i = 2; i <= 3; i++) {
36 MOZ_RELEASE_ASSERT(FloorLog2(i) == 1);
38 for (uint32_t i = 4; i <= 7; i++) {
39 MOZ_RELEASE_ASSERT(FloorLog2(i) == 2);
41 for (uint32_t i = 8; i <= 15; i++) {
42 MOZ_RELEASE_ASSERT(FloorLog2(i) == 3);
44 for (uint32_t i = 16; i <= 31; i++) {
45 MOZ_RELEASE_ASSERT(FloorLog2(i) == 4);
49 static void TestRoundUpPow2() {
50 MOZ_RELEASE_ASSERT(RoundUpPow2(0) == 1);
51 MOZ_RELEASE_ASSERT(RoundUpPow2(1) == 1);
52 MOZ_RELEASE_ASSERT(RoundUpPow2(2) == 2);
53 MOZ_RELEASE_ASSERT(RoundUpPow2(3) == 4);
54 MOZ_RELEASE_ASSERT(RoundUpPow2(4) == 4);
55 MOZ_RELEASE_ASSERT(RoundUpPow2(5) == 8);
56 MOZ_RELEASE_ASSERT(RoundUpPow2(6) == 8);
57 MOZ_RELEASE_ASSERT(RoundUpPow2(7) == 8);
58 MOZ_RELEASE_ASSERT(RoundUpPow2(8) == 8);
59 MOZ_RELEASE_ASSERT(RoundUpPow2(9) == 16);
61 MOZ_RELEASE_ASSERT(RoundUpPow2(15) == 16);
62 MOZ_RELEASE_ASSERT(RoundUpPow2(16) == 16);
63 MOZ_RELEASE_ASSERT(RoundUpPow2(17) == 32);
65 MOZ_RELEASE_ASSERT(RoundUpPow2(31) == 32);
66 MOZ_RELEASE_ASSERT(RoundUpPow2(32) == 32);
67 MOZ_RELEASE_ASSERT(RoundUpPow2(33) == 64);
69 size_t MaxPow2 = size_t(1) << (sizeof(size_t) * CHAR_BIT - 1);
70 MOZ_RELEASE_ASSERT(RoundUpPow2(MaxPow2 - 1) == MaxPow2);
71 MOZ_RELEASE_ASSERT(RoundUpPow2(MaxPow2) == MaxPow2);
72 // not valid to round up when past the max power of two
75 int main() {
76 TestCeiling();
77 TestFloor();
79 TestRoundUpPow2();
80 return 0;