Michael Wright | d614ee4 | 2013-05-21 14:11:34 -0700 | [diff] [blame] | 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 | |
| 24 | namespace android { |
| 25 | |
| 26 | class BitSetTest : public testing::Test { |
| 27 | protected: |
| 28 | BitSet32 b1; |
| 29 | BitSet32 b2; |
| 30 | virtual void TearDown() { |
| 31 | b1.clear(); |
| 32 | b2.clear(); |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | |
| 37 | TEST_F(BitSetTest, 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 | } |
| 52 | TEST_F(BitSetTest, 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 | |
| 68 | TEST_F(BitSetTest, 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 | } // namespace android |