|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include <gtest/gtest.h> |
| 19 | + |
| 20 | +#include "arrow/testing/gtest_util.h" |
| 21 | +#include "arrow/util/bitmap_ops.h" |
| 22 | + |
| 23 | +namespace arrow::internal { |
| 24 | + |
| 25 | +TEST(BitmapOpsTest, PartialLeadingByteSafety) { |
| 26 | + const int64_t num_bytes = 16; |
| 27 | + const std::vector<uint8_t> left(num_bytes, 0), right(num_bytes, 0xFF); |
| 28 | + std::vector<uint8_t> out = right; |
| 29 | + int64_t num_bits = num_bytes * 8; |
| 30 | + |
| 31 | + auto left_data = left.data(); |
| 32 | + auto right_data = right.data(); |
| 33 | + auto out_data = out.data(); |
| 34 | + |
| 35 | + for (int64_t out_offset = num_bits - 1; out_offset >= 0; --out_offset) { |
| 36 | + ARROW_SCOPED_TRACE("out offset = " + std::to_string(out_offset)); |
| 37 | + for (int64_t left_offset : {out_offset, out_offset + 1}) { |
| 38 | + ARROW_SCOPED_TRACE("left offset = " + std::to_string(left_offset)); |
| 39 | + for (int64_t right_offset : {out_offset, out_offset + 2}) { |
| 40 | + ARROW_SCOPED_TRACE("right offset = " + std::to_string(right_offset)); |
| 41 | + for (int64_t length = 1; |
| 42 | + length <= num_bits - out_offset && length <= num_bits - left_offset && |
| 43 | + length <= num_bits - right_offset; |
| 44 | + ++length) { |
| 45 | + ARROW_SCOPED_TRACE("length = " + std::to_string(length)); |
| 46 | + BitmapAnd(left_data, out_offset, right_data, right_offset, length, out_offset, |
| 47 | + out_data); |
| 48 | + // Bytes before out_offset. |
| 49 | + for (int64_t i = 0; i < out_offset / 8; ++i) { |
| 50 | + EXPECT_EQ(out_data[i], 0xFF); |
| 51 | + } |
| 52 | + // The byte holding the out_offset bit. |
| 53 | + EXPECT_EQ(out_data[out_offset / 8], (1 << (out_offset % 8)) - 1); |
| 54 | + // Bytes after the last bit. |
| 55 | + for (int64_t i = (out_offset + length + 7) / 8; i < num_bytes; ++i) { |
| 56 | + EXPECT_EQ(out_data[i], 0); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +} // namespace arrow::internal |
0 commit comments