|
| 1 | +// |
| 2 | +// Copyright (c) 2026 Vladislav Soulgard (vsoulgard at gmail dot com) |
| 3 | +// |
| 4 | +// Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | +// |
| 7 | + |
| 8 | +#include <boost/mysql/impl/internal/next_power_of_two.hpp> |
| 9 | + |
| 10 | +#include <boost/test/unit_test.hpp> |
| 11 | + |
| 12 | +#include <cstdint> |
| 13 | + |
| 14 | +using namespace boost::mysql::detail; |
| 15 | + |
| 16 | +BOOST_AUTO_TEST_SUITE(test_next_power_of_two) |
| 17 | + |
| 18 | +BOOST_AUTO_TEST_CASE(basic) |
| 19 | +{ |
| 20 | + // n = 0 (special case) |
| 21 | + BOOST_TEST(next_power_of_two<std::size_t>(0u) == 1u); |
| 22 | + |
| 23 | + // n is already power of two |
| 24 | + BOOST_TEST(next_power_of_two<std::size_t>(1u) == 1u); |
| 25 | + BOOST_TEST(next_power_of_two<std::size_t>(2u) == 2u); |
| 26 | + BOOST_TEST(next_power_of_two<std::size_t>(4u) == 4u); |
| 27 | + BOOST_TEST(next_power_of_two<std::size_t>(8u) == 8u); |
| 28 | + BOOST_TEST(next_power_of_two<std::size_t>(16u) == 16u); |
| 29 | + BOOST_TEST(next_power_of_two<std::size_t>(32u) == 32u); |
| 30 | + BOOST_TEST(next_power_of_two<std::size_t>(64u) == 64u); |
| 31 | + BOOST_TEST(next_power_of_two<std::size_t>(128u) == 128u); |
| 32 | + |
| 33 | + // n just below power of two |
| 34 | + BOOST_TEST(next_power_of_two<std::size_t>(3u) == 4u); |
| 35 | + BOOST_TEST(next_power_of_two<std::size_t>(7u) == 8u); |
| 36 | + BOOST_TEST(next_power_of_two<std::size_t>(15u) == 16u); |
| 37 | + BOOST_TEST(next_power_of_two<std::size_t>(31u) == 32u); |
| 38 | + BOOST_TEST(next_power_of_two<std::size_t>(63u) == 64u); |
| 39 | + BOOST_TEST(next_power_of_two<std::size_t>(127u) == 128u); |
| 40 | + |
| 41 | + // n just above power of two |
| 42 | + BOOST_TEST(next_power_of_two<std::size_t>(5u) == 8u); |
| 43 | + BOOST_TEST(next_power_of_two<std::size_t>(9u) == 16u); |
| 44 | + BOOST_TEST(next_power_of_two<std::size_t>(17u) == 32u); |
| 45 | + BOOST_TEST(next_power_of_two<std::size_t>(33u) == 64u); |
| 46 | + BOOST_TEST(next_power_of_two<std::size_t>(65u) == 128u); |
| 47 | + BOOST_TEST(next_power_of_two<std::size_t>(129u) == 256u); |
| 48 | + |
| 49 | + // n is random value |
| 50 | + BOOST_TEST(next_power_of_two<std::size_t>(6u) == 8u); |
| 51 | + BOOST_TEST(next_power_of_two<std::size_t>(13u) == 16u); |
| 52 | + BOOST_TEST(next_power_of_two<std::size_t>(21u) == 32u); |
| 53 | + BOOST_TEST(next_power_of_two<std::size_t>(45u) == 64u); |
| 54 | + BOOST_TEST(next_power_of_two<std::size_t>(89u) == 128u); |
| 55 | + BOOST_TEST(next_power_of_two<std::size_t>(200u) == 256u); |
| 56 | + BOOST_TEST(next_power_of_two<std::size_t>(300u) == 512u); |
| 57 | + BOOST_TEST(next_power_of_two<std::size_t>(400u) == 512u); |
| 58 | + BOOST_TEST(next_power_of_two<std::size_t>(505u) == 512u); |
| 59 | + BOOST_TEST(next_power_of_two<std::size_t>(888u) == 1024u); |
| 60 | +} |
| 61 | + |
| 62 | +BOOST_AUTO_TEST_CASE(different_types) |
| 63 | +{ |
| 64 | + // uint8_t |
| 65 | + BOOST_TEST(next_power_of_two<std::uint8_t>(0u) == 1u); |
| 66 | + BOOST_TEST(next_power_of_two<std::uint8_t>(62u) == 64u); |
| 67 | + BOOST_TEST(next_power_of_two<std::uint8_t>(100u) == 128u); |
| 68 | + BOOST_TEST(next_power_of_two<std::uint8_t>(128u) == 128u); |
| 69 | + |
| 70 | + // uint16_t |
| 71 | + BOOST_TEST(next_power_of_two<std::uint16_t>(0u) == 1u); |
| 72 | + BOOST_TEST(next_power_of_two<std::uint16_t>(1000u) == 1024u); |
| 73 | + BOOST_TEST(next_power_of_two<std::uint16_t>(16383u) == 16384u); |
| 74 | + BOOST_TEST(next_power_of_two<std::uint16_t>(32768u) == 32768u); |
| 75 | + |
| 76 | + // uint32_t |
| 77 | + BOOST_TEST(next_power_of_two<std::uint32_t>(0u) == 1u); |
| 78 | + BOOST_TEST(next_power_of_two<std::uint32_t>(100000u) == 131072u); |
| 79 | + BOOST_TEST(next_power_of_two<std::uint32_t>(1u << 30) == 1u << 30); |
| 80 | + BOOST_TEST(next_power_of_two<std::uint32_t>((1u << 30) + 1) == 1u << 31); |
| 81 | + |
| 82 | + // uint64_t |
| 83 | + BOOST_TEST(next_power_of_two<std::uint64_t>(0u) == 1u); |
| 84 | + BOOST_TEST(next_power_of_two<std::uint64_t>(1ull << 40) == 1ull << 40); |
| 85 | + BOOST_TEST(next_power_of_two<std::uint64_t>((1ull << 40) + 1) == 1ull << 41); |
| 86 | + BOOST_TEST(next_power_of_two<std::uint64_t>(1ull << 62) == 1ull << 62); |
| 87 | + BOOST_TEST(next_power_of_two<std::uint64_t>((1ull << 62) + 1) == 1ull << 63); |
| 88 | +} |
| 89 | + |
| 90 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments