1 /*
2 * Copyright (C) 2015 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 "BitUtils.h"
18
19 #include <gtest/gtest.h>
20
21 namespace android {
22 namespace tests {
23
TEST(BitInRange,testInvalidRange)24 TEST(BitInRange, testInvalidRange) {
25 uint8_t arr[2] = { 0xff, 0xff };
26 EXPECT_FALSE(testBitInRange(arr, 0, 0));
27 EXPECT_FALSE(testBitInRange(arr, 1, 0));
28 }
29
TEST(BitInRange,testNoBits)30 TEST(BitInRange, testNoBits) {
31 uint8_t arr[1];
32 arr[0] = 0;
33 EXPECT_FALSE(testBitInRange(arr, 0, 8));
34 }
35
TEST(BitInRange,testOneBit)36 TEST(BitInRange, testOneBit) {
37 uint8_t arr[1];
38 for (int i = 0; i < 8; ++i) {
39 arr[0] = 1 << i;
40 EXPECT_TRUE(testBitInRange(arr, 0, 8));
41 }
42 }
43
TEST(BitInRange,testZeroStart)44 TEST(BitInRange, testZeroStart) {
45 uint8_t arr[1] = { 0x10 };
46 for (int i = 0; i < 5; ++i) {
47 EXPECT_FALSE(testBitInRange(arr, 0, i));
48 }
49 for (int i = 5; i <= 8; ++i) {
50 EXPECT_TRUE(testBitInRange(arr, 0, i));
51 }
52 }
53
TEST(BitInRange,testByteBoundaryEnd)54 TEST(BitInRange, testByteBoundaryEnd) {
55 uint8_t arr[1] = { 0x10 };
56 for (int i = 0; i < 5; ++i) {
57 EXPECT_TRUE(testBitInRange(arr, i, 8));
58 }
59 for (int i = 5; i <= 8; ++i) {
60 EXPECT_FALSE(testBitInRange(arr, i, 8));
61 }
62 }
63
TEST(BitInRange,testMultiByteArray)64 TEST(BitInRange, testMultiByteArray) {
65 // bits set: 11 and 16
66 uint8_t arr[3] = { 0x00, 0x08, 0x01 };
67 for (int start = 0; start < 24; ++start) {
68 for (int end = start + 1; end <= 24; ++end) {
69 if (start > 16 || end <= 11 || (start > 11 && end <= 16)) {
70 EXPECT_FALSE(testBitInRange(arr, start, end))
71 << "range = (" << start << ", " << end << ")";
72 } else {
73 EXPECT_TRUE(testBitInRange(arr, start, end))
74 << "range = (" << start << ", " << end << ")";
75 }
76 }
77 }
78 }
79
80 } // namespace tests
81 } // namespace android
82