| 1 | /* Copyright (c) 2021, 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 | /* See http://code.google.com/p/googletest/wiki/Primer */ |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | |
| 28 | #include "storage/innobase/include/ut0bitset.h" |
| 29 | |
| 30 | namespace innodb_ut0bitset_unittest { |
| 31 | |
| 32 | TEST(ut0bitset, find_set_handles_misaligned_ranges) { |
| 33 | alignas(64) byte base_data[1000] = {}; |
| 34 | for (int a = 0; a < 8; ++a) { |
| 35 | for (int len = 0; len < 10; ++len) { |
| 36 | Bitset bs(base_data + a, len); |
| 37 | for (int b = 0; b < len * 8; ++b) { |
| 38 | assert(!bs.test(b)); |
| 39 | base_data[a + b / 8] ^= 1U << b % 8; |
| 40 | assert(bs.test(b)); |
| 41 | for (int q = 0; q < len * 8; ++q) { |
| 42 | auto found = bs.find_set(q); |
| 43 | if (q <= b) { |
| 44 | ASSERT_EQ(found, b); |
| 45 | } else { |
| 46 | ASSERT_EQ(found, bs.NOT_FOUND); |
| 47 | } |
| 48 | } |
| 49 | base_data[a + b / 8] ^= 1U << b % 8; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | } // namespace innodb_ut0bitset_unittest |