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/. */
6 
7 #include "mozilla/MathAlgorithms.h"
8 
9 using mozilla::Clamp;
10 using mozilla::IsPowerOfTwo;
11 
12 static void
TestClamp()13 TestClamp()
14 {
15   MOZ_RELEASE_ASSERT(Clamp(0, 0, 0) == 0);
16   MOZ_RELEASE_ASSERT(Clamp(1, 0, 0) == 0);
17   MOZ_RELEASE_ASSERT(Clamp(-1, 0, 0) == 0);
18 
19   MOZ_RELEASE_ASSERT(Clamp(0, 1, 1) == 1);
20   MOZ_RELEASE_ASSERT(Clamp(0, 1, 2) == 1);
21 
22   MOZ_RELEASE_ASSERT(Clamp(0, -1, -1) == -1);
23   MOZ_RELEASE_ASSERT(Clamp(0, -2, -1) == -1);
24 
25   MOZ_RELEASE_ASSERT(Clamp(0, 1, 3) == 1);
26   MOZ_RELEASE_ASSERT(Clamp(1, 1, 3) == 1);
27   MOZ_RELEASE_ASSERT(Clamp(2, 1, 3) == 2);
28   MOZ_RELEASE_ASSERT(Clamp(3, 1, 3) == 3);
29   MOZ_RELEASE_ASSERT(Clamp(4, 1, 3) == 3);
30   MOZ_RELEASE_ASSERT(Clamp(5, 1, 3) == 3);
31 
32   MOZ_RELEASE_ASSERT(Clamp<uint8_t>(UINT8_MAX, 0, UINT8_MAX) == UINT8_MAX);
33   MOZ_RELEASE_ASSERT(Clamp<uint8_t>(0, 0, UINT8_MAX) == 0);
34 
35   MOZ_RELEASE_ASSERT(Clamp<int8_t>(INT8_MIN, INT8_MIN, INT8_MAX) == INT8_MIN);
36   MOZ_RELEASE_ASSERT(Clamp<int8_t>(INT8_MIN, 0, INT8_MAX) == 0);
37   MOZ_RELEASE_ASSERT(Clamp<int8_t>(INT8_MAX, INT8_MIN, INT8_MAX) == INT8_MAX);
38   MOZ_RELEASE_ASSERT(Clamp<int8_t>(INT8_MAX, INT8_MIN, 0) == 0);
39 }
40 
41 static void
TestIsPowerOfTwo()42 TestIsPowerOfTwo()
43 {
44   static_assert(!IsPowerOfTwo(0u), "0 isn't a power of two");
45   static_assert(IsPowerOfTwo(1u), "1 is a power of two");
46   static_assert(IsPowerOfTwo(2u), "2 is a power of two");
47   static_assert(!IsPowerOfTwo(3u), "3 isn't a power of two");
48   static_assert(IsPowerOfTwo(4u), "4 is a power of two");
49   static_assert(!IsPowerOfTwo(5u), "5 isn't a power of two");
50   static_assert(!IsPowerOfTwo(6u), "6 isn't a power of two");
51   static_assert(!IsPowerOfTwo(7u), "7 isn't a power of two");
52   static_assert(IsPowerOfTwo(8u), "8 is a power of two");
53   static_assert(!IsPowerOfTwo(9u), "9 isn't a power of two");
54 
55   static_assert(!IsPowerOfTwo(uint8_t(UINT8_MAX/2)), "127, 0x7f isn't a power of two");
56   static_assert(IsPowerOfTwo(uint8_t(UINT8_MAX/2 + 1)), "128, 0x80 is a power of two");
57   static_assert(!IsPowerOfTwo(uint8_t(UINT8_MAX/2 + 2)), "129, 0x81 isn't a power of two");
58   static_assert(!IsPowerOfTwo(uint8_t(UINT8_MAX - 1)), "254, 0xfe isn't a power of two");
59   static_assert(!IsPowerOfTwo(uint8_t(UINT8_MAX)), "255, 0xff isn't a power of two");
60 
61   static_assert(!IsPowerOfTwo(uint16_t(UINT16_MAX/2)), "0x7fff isn't a power of two");
62   static_assert(IsPowerOfTwo(uint16_t(UINT16_MAX/2 + 1)), "0x8000 is a power of two");
63   static_assert(!IsPowerOfTwo(uint16_t(UINT16_MAX/2 + 2)), "0x8001 isn't a power of two");
64   static_assert(!IsPowerOfTwo(uint16_t(UINT16_MAX - 1)), "0xfffe isn't a power of two");
65   static_assert(!IsPowerOfTwo(uint16_t(UINT16_MAX)), "0xffff isn't a power of two");
66 
67   static_assert(!IsPowerOfTwo(uint32_t(UINT32_MAX/2)), "0x7fffffff isn't a power of two");
68   static_assert(IsPowerOfTwo(uint32_t(UINT32_MAX/2 + 1)), "0x80000000 is a power of two");
69   static_assert(!IsPowerOfTwo(uint32_t(UINT32_MAX/2 + 2)), "0x80000001 isn't a power of two");
70   static_assert(!IsPowerOfTwo(uint32_t(UINT32_MAX - 1)), "0xfffffffe isn't a power of two");
71   static_assert(!IsPowerOfTwo(uint32_t(UINT32_MAX)), "0xffffffff isn't a power of two");
72 
73   static_assert(!IsPowerOfTwo(uint64_t(UINT64_MAX/2)), "0x7fffffffffffffff isn't a power of two");
74   static_assert(IsPowerOfTwo(uint64_t(UINT64_MAX/2 + 1)), "0x8000000000000000 is a power of two");
75   static_assert(!IsPowerOfTwo(uint64_t(UINT64_MAX/2 + 2)), "0x8000000000000001 isn't a power of two");
76   static_assert(!IsPowerOfTwo(uint64_t(UINT64_MAX - 1)), "0xfffffffffffffffe isn't a power of two");
77   static_assert(!IsPowerOfTwo(uint64_t(UINT64_MAX)), "0xffffffffffffffff isn't a power of two");
78 }
79 
80 int
main()81 main()
82 {
83   TestIsPowerOfTwo();
84   TestClamp();
85 
86   return 0;
87 }
88