Will Drewry | de2cad7 | 2017-03-10 15:53:34 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 <stdint.h> |
| 18 | |
| 19 | #include <ese/bit_spec.h> |
| 20 | #include <gtest/gtest.h> |
| 21 | |
| 22 | using ::testing::Test; |
| 23 | |
| 24 | struct TestSpec { |
| 25 | struct { |
| 26 | struct bit_spec bit[8]; |
| 27 | } single; |
| 28 | struct { |
| 29 | struct bit_spec high; |
| 30 | struct bit_spec mid; |
| 31 | struct bit_spec low; |
| 32 | } adjacent; |
| 33 | struct { |
| 34 | struct bit_spec all8; |
| 35 | struct bit_spec upper6; |
| 36 | struct bit_spec lower6; |
| 37 | } overlap; |
| 38 | }; |
| 39 | |
| 40 | const struct TestSpec kTestSpec = { |
| 41 | .single = { |
| 42 | .bit = { |
| 43 | { .value = 1, .shift = 0 }, |
| 44 | { .value = 1, .shift = 1 }, |
| 45 | { .value = 1, .shift = 2 }, |
| 46 | { .value = 1, .shift = 3 }, |
| 47 | { .value = 1, .shift = 4 }, |
| 48 | { .value = 1, .shift = 5 }, |
| 49 | { .value = 1, .shift = 6 }, |
| 50 | { .value = 1, .shift = 7 }, |
| 51 | }, |
| 52 | }, |
| 53 | .adjacent = { |
| 54 | .high = { .value = 0x3, .shift = 6 }, |
| 55 | .mid = { .value = 0xf, .shift = 2 }, |
| 56 | .low = { .value = 0x3, .shift = 0 }, |
| 57 | }, |
| 58 | .overlap = { |
| 59 | .all8 = { .value = 0xff, .shift = 0 }, |
| 60 | .upper6 = { .value = 0x3f, .shift = 2 }, |
| 61 | .lower6 = { .value = 0x3f, .shift = 0 }, |
| 62 | }, |
| 63 | }; |
| 64 | |
| 65 | class BitSpecTest : public virtual Test { |
| 66 | public: |
| 67 | BitSpecTest() { |
| 68 | } |
| 69 | virtual ~BitSpecTest() { } |
| 70 | virtual void SetUp() { } |
| 71 | virtual void TearDown() { } |
| 72 | }; |
| 73 | |
| 74 | TEST_F(BitSpecTest, single_bits_assign_accrue) { |
| 75 | uint8_t byte = 0; |
| 76 | uint8_t expected_byte = 0; |
| 77 | // Accrue bits. |
| 78 | for (int bit = 0; bit < 8; ++bit) { |
| 79 | expected_byte |= (1 << bit); |
| 80 | bs_assign(&byte, kTestSpec.single.bit[bit], 1); |
| 81 | EXPECT_EQ(expected_byte, byte); |
| 82 | EXPECT_EQ(1, bs_get(kTestSpec.single.bit[bit], expected_byte)); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | TEST_F(BitSpecTest, single_bits_assign1_individual) { |
| 87 | // One bit at a time. |
| 88 | for (int bit = 0; bit < 8; ++bit) { |
| 89 | uint8_t expected_byte = (1 << bit); |
| 90 | uint8_t byte = bs_set(0, kTestSpec.single.bit[bit], 1); |
| 91 | EXPECT_EQ(expected_byte, byte); |
| 92 | EXPECT_EQ(1, bs_get(kTestSpec.single.bit[bit], expected_byte)); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | TEST_F(BitSpecTest, single_bits_assign0_individual) { |
| 97 | // One bit at a time. |
| 98 | for (int bit = 0; bit < 8; ++bit) { |
| 99 | uint8_t expected_byte = 0xff ^ (1 << bit); |
| 100 | uint8_t byte = bs_set(0xff, kTestSpec.single.bit[bit], 0); |
| 101 | EXPECT_EQ(expected_byte, byte); |
| 102 | EXPECT_EQ(0, bs_get(kTestSpec.single.bit[bit], expected_byte)); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | TEST_F(BitSpecTest, single_bits_clear_individual) { |
| 107 | // One bit at a time. |
| 108 | for (int bit = 0; bit < 8; ++bit) { |
| 109 | uint8_t byte = 0xff; |
| 110 | uint8_t expected_byte = 0xff ^ (1 << bit); |
| 111 | byte &= bs_clear(kTestSpec.single.bit[bit]); |
| 112 | EXPECT_EQ(expected_byte, byte); |
| 113 | EXPECT_EQ(0, bs_get(kTestSpec.single.bit[bit], expected_byte)); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | TEST_F(BitSpecTest, adjacent_bit_assign) { |
| 118 | uint8_t byte = 0; |
| 119 | EXPECT_EQ(0, bs_get(kTestSpec.adjacent.high, byte)); |
| 120 | EXPECT_EQ(0, bs_get(kTestSpec.adjacent.mid, byte)); |
| 121 | EXPECT_EQ(0, bs_get(kTestSpec.adjacent.low, byte)); |
| 122 | byte = 0xff; |
| 123 | EXPECT_EQ(0x3, bs_get(kTestSpec.adjacent.high, byte)); |
| 124 | EXPECT_EQ(0xf, bs_get(kTestSpec.adjacent.mid, byte)); |
| 125 | EXPECT_EQ(0x3, bs_get(kTestSpec.adjacent.low, byte)); |
| 126 | for (int i = 0; i < 0xf; ++i) { |
| 127 | bs_assign(&byte, kTestSpec.adjacent.mid, i); |
| 128 | EXPECT_EQ(0x3, bs_get(kTestSpec.adjacent.high, byte)); |
| 129 | EXPECT_EQ(i, bs_get(kTestSpec.adjacent.mid, byte)); |
| 130 | EXPECT_EQ(0x3, bs_get(kTestSpec.adjacent.low, byte)); |
| 131 | } |
| 132 | byte = 0xff; |
| 133 | for (int i = 0; i < 0x3; ++i) { |
| 134 | bs_assign(&byte, kTestSpec.adjacent.low, i); |
| 135 | bs_assign(&byte, kTestSpec.adjacent.high, i); |
| 136 | EXPECT_EQ(i, bs_get(kTestSpec.adjacent.high, byte)); |
| 137 | EXPECT_EQ(0xf, bs_get(kTestSpec.adjacent.mid, byte)); |
| 138 | EXPECT_EQ(i, bs_get(kTestSpec.adjacent.low, byte)); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | TEST_F(BitSpecTest, overlap_bit_assign) { |
| 143 | uint8_t byte = 0; |
| 144 | EXPECT_EQ(0, bs_get(kTestSpec.overlap.upper6, byte)); |
| 145 | EXPECT_EQ(0, bs_get(kTestSpec.overlap.lower6, byte)); |
| 146 | EXPECT_EQ(0, bs_get(kTestSpec.overlap.all8, byte)); |
| 147 | byte = 0xff; |
| 148 | EXPECT_EQ(0x3f, bs_get(kTestSpec.overlap.upper6, byte)); |
| 149 | EXPECT_EQ(0x3f, bs_get(kTestSpec.overlap.lower6, byte)); |
| 150 | EXPECT_EQ(0xff, bs_get(kTestSpec.overlap.all8, byte)); |
| 151 | |
| 152 | byte = 0; |
| 153 | for (int i = 0; i < 0x3f; ++i) { |
| 154 | bs_assign(&byte, kTestSpec.overlap.lower6, i); |
| 155 | EXPECT_EQ((i & (0x3f << 2)) >> 2, bs_get(kTestSpec.overlap.upper6, byte)); |
| 156 | EXPECT_EQ(i, bs_get(kTestSpec.overlap.lower6, byte)); |
| 157 | EXPECT_EQ(i, bs_get(kTestSpec.overlap.all8, byte)); |
| 158 | } |
| 159 | byte = 0; |
| 160 | for (int i = 0; i < 0x3f; ++i) { |
| 161 | bs_assign(&byte, kTestSpec.overlap.upper6, i); |
| 162 | EXPECT_EQ(i, bs_get(kTestSpec.overlap.upper6, byte)); |
| 163 | EXPECT_EQ((i << 2) & 0x3f, bs_get(kTestSpec.overlap.lower6, byte)); |
| 164 | EXPECT_EQ(i << 2, bs_get(kTestSpec.overlap.all8, byte)); |
| 165 | } |
| 166 | byte = 0; |
| 167 | for (int i = 0; i < 0xff; ++i) { |
| 168 | bs_assign(&byte, kTestSpec.overlap.all8, i); |
| 169 | EXPECT_EQ(i >> 2, bs_get(kTestSpec.overlap.upper6, byte)); |
| 170 | EXPECT_EQ(i & 0x3f, bs_get(kTestSpec.overlap.lower6, byte)); |
| 171 | EXPECT_EQ(i, bs_get(kTestSpec.overlap.all8, byte)); |
| 172 | } |
| 173 | } |