| 1 | /* Copyright (c) 2013, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | #include <stddef.h> |
| 26 | #include <algorithm> |
| 27 | |
| 28 | #include "my_bitmap.h" |
| 29 | #include "my_inttypes.h" |
| 30 | #include "my_sys.h" |
| 31 | #include "sql/sql_bitmap.h" |
| 32 | |
| 33 | namespace bitmap_unittest { |
| 34 | |
| 35 | const int BITMAP_SIZE = 128; |
| 36 | |
| 37 | class BitmapTest : public ::testing::Test { |
| 38 | protected: |
| 39 | BitmapTest() = default; |
| 40 | |
| 41 | void SetUp() override { bitmap.init(); } |
| 42 | |
| 43 | Bitmap<BITMAP_SIZE> bitmap; |
| 44 | }; |
| 45 | |
| 46 | TEST_F(BitmapTest, IntersectTest) { |
| 47 | bitmap.set_prefix(4); |
| 48 | bitmap.intersect(0xBBBBULL); |
| 49 | EXPECT_TRUE(bitmap.is_set(0)); |
| 50 | EXPECT_TRUE(bitmap.is_set(1)); |
| 51 | EXPECT_FALSE(bitmap.is_set(2)); |
| 52 | EXPECT_TRUE(bitmap.is_set(3)); |
| 53 | bitmap.clear_bit(0); |
| 54 | bitmap.clear_bit(1); |
| 55 | bitmap.clear_bit(3); |
| 56 | EXPECT_TRUE(bitmap.is_clear_all()); |
| 57 | } |
| 58 | |
| 59 | TEST_F(BitmapTest, ULLTest) { |
| 60 | bitmap.set_all(); |
| 61 | bitmap.intersect(0x0123456789ABCDEFULL); |
| 62 | ulonglong ull = bitmap.to_ulonglong(); |
| 63 | EXPECT_TRUE(ull == 0x0123456789ABCDEFULL); |
| 64 | |
| 65 | Bitmap<24> bitmap24; |
| 66 | bitmap24.init(); |
| 67 | bitmap24.set_all(); |
| 68 | bitmap24.intersect(0x47BULL); |
| 69 | ulonglong ull24 = bitmap24.to_ulonglong(); |
| 70 | EXPECT_TRUE(ull24 == 0x47BULL); |
| 71 | } |
| 72 | |
| 73 | TEST_F(BitmapTest, GetFirstSet) { |
| 74 | const Bitmap<64> const64bitmap(0); |
| 75 | uint key = const64bitmap.get_first_set(); |
| 76 | EXPECT_EQ(MY_BIT_NONE, key); |
| 77 | const Bitmap<BITMAP_SIZE> const128bitmap(0); |
| 78 | key = const128bitmap.get_first_set(); |
| 79 | EXPECT_EQ(MY_BIT_NONE, key); |
| 80 | } |
| 81 | |
| 82 | } // namespace bitmap_unittest |