1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <gtest/gtest.h>
18 #include <sys/param.h>
19 
TEST(sys_param_test,powerof2_positives)20 TEST(sys_param_test, powerof2_positives) {
21   ASSERT_TRUE(powerof2(1));
22   ASSERT_TRUE(powerof2(2));
23   ASSERT_TRUE(powerof2(4));
24   ASSERT_TRUE(powerof2(8));
25   ASSERT_FALSE(powerof2(3));
26   ASSERT_FALSE(powerof2(5));
27   ASSERT_FALSE(powerof2(7));
28   ASSERT_FALSE(powerof2(9));
29   ASSERT_FALSE(powerof2(10));
30 }
31 
TEST(sys_param_test,powerof2_zero)32 TEST(sys_param_test, powerof2_zero) {
33   // 0 isn't a power of 2, but for compatibility, we assume it is.
34   ASSERT_TRUE(powerof2(0));
35   uint32_t zero = 0;
36   ASSERT_TRUE(powerof2(zero));
37 }
38 
TEST(sys_param_test,powerof2_negatives)39 TEST(sys_param_test, powerof2_negatives) {
40   // negative numbers can never be a power of 2, but for compatibility,
41   // we assume they can be.
42   int32_t min32 = INT32_MIN;
43   int64_t min64 = INT64_MIN;
44   ASSERT_TRUE(powerof2(min32));
45   ASSERT_FALSE(powerof2(min32 + 1));
46   ASSERT_TRUE(powerof2(min64));
47   ASSERT_FALSE(powerof2(min64 + 1));
48 }
49