BitSet_test.cpp revision bab6ea0bb70dd6d093a0765befc7d6893e2312bf
1/* 2 * Copyright (C) 2013 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#define LOG_TAG "BitSet_test" 18 19#include <utils/BitSet.h> 20#include <cutils/log.h> 21#include <gtest/gtest.h> 22#include <unistd.h> 23 24namespace android { 25 26class BitSet32Test : public testing::Test { 27protected: 28 BitSet32 b1; 29 BitSet32 b2; 30 virtual void TearDown() { 31 b1.clear(); 32 b2.clear(); 33 } 34}; 35 36 37TEST_F(BitSet32Test, BitWiseOr) { 38 b1.markBit(2); 39 b2.markBit(4); 40 41 BitSet32 tmp = b1 | b2; 42 EXPECT_EQ(tmp.count(), 2u); 43 EXPECT_TRUE(tmp.hasBit(2) && tmp.hasBit(4)); 44 // Check that the operator is symmetric 45 EXPECT_TRUE((b2 | b1) == (b1 | b2)); 46 47 b1 |= b2; 48 EXPECT_EQ(b1.count(), 2u); 49 EXPECT_TRUE(b1.hasBit(2) && b1.hasBit(4)); 50 EXPECT_TRUE(b2.hasBit(4) && b2.count() == 1u); 51} 52TEST_F(BitSet32Test, BitWiseAnd_Disjoint) { 53 b1.markBit(2); 54 b1.markBit(4); 55 b1.markBit(6); 56 57 BitSet32 tmp = b1 & b2; 58 EXPECT_TRUE(tmp.isEmpty()); 59 // Check that the operator is symmetric 60 EXPECT_TRUE((b2 & b1) == (b1 & b2)); 61 62 b2 &= b1; 63 EXPECT_TRUE(b2.isEmpty()); 64 EXPECT_EQ(b1.count(), 3u); 65 EXPECT_TRUE(b1.hasBit(2) && b1.hasBit(4) && b1.hasBit(6)); 66} 67 68TEST_F(BitSet32Test, BitWiseAnd_NonDisjoint) { 69 b1.markBit(2); 70 b1.markBit(4); 71 b1.markBit(6); 72 b2.markBit(3); 73 b2.markBit(6); 74 b2.markBit(9); 75 76 BitSet32 tmp = b1 & b2; 77 EXPECT_EQ(tmp.count(), 1u); 78 EXPECT_TRUE(tmp.hasBit(6)); 79 // Check that the operator is symmetric 80 EXPECT_TRUE((b2 & b1) == (b1 & b2)); 81 82 b1 &= b2; 83 EXPECT_EQ(b1.count(), 1u); 84 EXPECT_EQ(b2.count(), 3u); 85 EXPECT_TRUE(b2.hasBit(3) && b2.hasBit(6) && b2.hasBit(9)); 86} 87 88class BitSet64Test : public testing::Test { 89protected: 90 BitSet64 b1; 91 BitSet64 b2; 92 virtual void TearDown() { 93 b1.clear(); 94 b2.clear(); 95 } 96}; 97 98 99TEST_F(BitSet64Test, BitWiseOr) { 100 b1.markBit(20); 101 b2.markBit(40); 102 103 BitSet64 tmp = b1 | b2; 104 EXPECT_EQ(tmp.count(), 2u); 105 EXPECT_TRUE(tmp.hasBit(20) && tmp.hasBit(40)); 106 // Check that the operator is symmetric 107 EXPECT_TRUE((b2 | b1) == (b1 | b2)); 108 109 b1 |= b2; 110 EXPECT_EQ(b1.count(), 2u); 111 EXPECT_TRUE(b1.hasBit(20) && b1.hasBit(40)); 112 EXPECT_TRUE(b2.hasBit(40) && b2.count() == 1u); 113} 114TEST_F(BitSet64Test, BitWiseAnd_Disjoint) { 115 b1.markBit(20); 116 b1.markBit(40); 117 b1.markBit(60); 118 119 BitSet64 tmp = b1 & b2; 120 EXPECT_TRUE(tmp.isEmpty()); 121 // Check that the operator is symmetric 122 EXPECT_TRUE((b2 & b1) == (b1 & b2)); 123 124 b2 &= b1; 125 EXPECT_TRUE(b2.isEmpty()); 126 EXPECT_EQ(b1.count(), 3u); 127 EXPECT_TRUE(b1.hasBit(20) && b1.hasBit(40) && b1.hasBit(60)); 128} 129 130TEST_F(BitSet64Test, BitWiseAnd_NonDisjoint) { 131 b1.markBit(20); 132 b1.markBit(40); 133 b1.markBit(60); 134 b2.markBit(30); 135 b2.markBit(60); 136 b2.markBit(63); 137 138 BitSet64 tmp = b1 & b2; 139 EXPECT_EQ(tmp.count(), 1u); 140 EXPECT_TRUE(tmp.hasBit(60)); 141 // Check that the operator is symmetric 142 EXPECT_TRUE((b2 & b1) == (b1 & b2)); 143 144 b1 &= b2; 145 EXPECT_EQ(b1.count(), 1u); 146 EXPECT_EQ(b2.count(), 3u); 147 EXPECT_TRUE(b2.hasBit(30) && b2.hasBit(60) && b2.hasBit(63)); 148} 149} // namespace android 150