1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
/*
* Copyright (C) 2020 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include <wtf/CryptographicallyRandomNumber.h>
#include <wtf/MathExtras.h>
#include <wtf/StdLibExtras.h>
#if !USE(SYSTEM_MALLOC)
#include <bmalloc/Algorithm.h>
#endif
namespace TestWebKitAPI {
template<typename WordType>
void testFindBitInWord()
{
constexpr size_t bitsInWord = sizeof(WordType) * 8;
constexpr size_t numberOfShiftValues = bitsInWord + 1;
constexpr size_t testPermutationsPerShift = 100;
constexpr size_t numberOfTestValues = numberOfShiftValues * testPermutationsPerShift;
uint8_t startIndex[numberOfTestValues];
uint8_t endIndex[numberOfTestValues];
auto initTestValues = [&] () {
// Set some internal and boundary cases.
uint8_t specialCases[] = {
0,
bitsInWord / 2,
bitsInWord - 1,
bitsInWord,
};
constexpr size_t numberOfSpecialCases = sizeof(specialCases) / sizeof(specialCases[0]);
size_t nextTestValue = 0;
for (size_t i = 0; i < numberOfSpecialCases; ++i) {
for (size_t j = 0; j < numberOfSpecialCases; ++j) {
startIndex[nextTestValue] = specialCases[i];
endIndex[nextTestValue++] = specialCases[j];
}
}
// Fill in some random cases.
for (size_t i = nextTestValue; i < numberOfTestValues; ++i) {
startIndex[i] = static_cast<uint8_t>(cryptographicallyRandomUnitInterval() * bitsInWord);
uint8_t remainingBits = bitsInWord - startIndex[i];
endIndex[i] = static_cast<uint8_t>(cryptographicallyRandomUnitInterval() * remainingBits) + startIndex[i];
}
};
auto expectedBitInWord = [] (WordType word, size_t& index, size_t endIndex, bool value) -> bool {
word >>= index;
while (index < endIndex) {
if ((word & 1) == static_cast<WordType>(value))
return true;
index++;
word >>= 1;
}
index = endIndex;
return false;
};
auto test = [&] (bool value, size_t shift) {
constexpr uint64_t baseWord = std::numeric_limits<uint64_t>::max();
uint64_t word = (shift < bitsInWord) ? baseWord << shift : 0;
for (size_t i = 0; i < numberOfTestValues; ++i) {
size_t index = startIndex[i];
bool result = findBitInWord(word, index, endIndex[i], value);
size_t expectedIndex = startIndex[i];
bool expectedResult = expectedBitInWord(word, expectedIndex, endIndex[i], value);
ASSERT_EQ(result, expectedResult);
ASSERT_EQ(index, expectedIndex);
}
};
initTestValues();
// Testing find a set bit.
for (size_t i = 0; i < numberOfShiftValues; ++i)
test(true, i);
// Testing find a cleared bit.
for (size_t i = 0; i < numberOfShiftValues; ++i)
test(false, i);
}
TEST(WTF_StdLibExtras, findBitInWord_uint32_t) { testFindBitInWord<uint32_t>(); }
TEST(WTF_StdLibExtras, findBitInWord_uint64_t) { testFindBitInWord<uint64_t>(); }
// Tests that function-local types can be instantiated with makeUnique.
// Style check would complain about use of std::make_unique, enforcing use of
// makeUnique. The makeUnique needs WTF_..._MAKE_FAST_ALLOCATED.
// There used to be a warn-unused-typedef errors when using these.
TEST(WTF_StdLibExtras, MakeUniqueFunctionLocalTypeCompiles)
{
struct LocalStruct {
WTF_MAKE_STRUCT_FAST_ALLOCATED;
};
IGNORE_CLANG_WARNINGS_BEGIN("unused-local-typedef")
class LocalClass {
WTF_MAKE_FAST_ALLOCATED;
};
IGNORE_CLANG_WARNINGS_END
auto s = makeUnique<LocalStruct>();
auto c = makeUnique<LocalClass>();
}
TEST(WTF_StdLibExtras, RoundUpToMultipleOfWorks)
{
EXPECT_EQ(2u, roundUpToMultipleOf(static_cast<uint8_t>(2), static_cast<uint8_t>(1)));
EXPECT_EQ(254u, roundUpToMultipleOf(static_cast<uint8_t>(2), static_cast<uint8_t>(254)));
}
TEST(WTF_StdLibExtras, RoundUpToMultipleOfNonPowerOfTwoWorks)
{
{
uint16_t x = 65534;
uint16_t divisor = 7;
EXPECT_EQ(x, roundUpToMultipleOfNonPowerOfTwo(divisor, x));
EXPECT_EQ(x, roundUpToMultipleOfNonPowerOfTwo(CheckedUint16 { divisor }, CheckedUint16 { x }));
}
{
uint32_t x = std::numeric_limits<uint32_t>::max() - 2;
uint32_t divisor = 9241;
EXPECT_EQ(x, roundUpToMultipleOfNonPowerOfTwo(divisor, x));
EXPECT_EQ(x, roundUpToMultipleOfNonPowerOfTwo(CheckedUint32 { divisor }, CheckedUint32 { x }));
}
{
uint64_t x = std::numeric_limits<uint64_t>::max() - 2;
uint64_t divisor = 13;
EXPECT_EQ(x, roundUpToMultipleOfNonPowerOfTwo(divisor, x));
EXPECT_EQ(x, roundUpToMultipleOfNonPowerOfTwo(CheckedUint64 { divisor }, CheckedUint64 { x }));
}
#if !USE(SYSTEM_MALLOC)
// Test that bmalloc::roundUpToMultipleOfNonPowerOfTwo does not have the bug. The function uses size_t.
{
size_t x = std::numeric_limits<size_t>::max() - 2;
size_t divisor = sizeof(size_t) == 4 ? 9241 : 13;
EXPECT_EQ(x, roundUpToMultipleOfNonPowerOfTwo(divisor, x));
EXPECT_EQ(x, roundUpToMultipleOfNonPowerOfTwo(CheckedSize { divisor }, CheckedSize { x }));
EXPECT_EQ(x, bmalloc::roundUpToMultipleOfNonPowerOfTwo(divisor, x));
}
#endif
{
size_t x = std::numeric_limits<size_t>::max();
EXPECT_TRUE(roundUpToMultipleOfNonPowerOfTwo(CheckedSize { 2 }, CheckedSize { x }).hasOverflowed());
}
EXPECT_TRUE(roundUpToMultipleOfNonPowerOfTwo(CheckedSize { WTF::ResultOverflowed }, CheckedSize { 78 }).hasOverflowed());
EXPECT_TRUE(roundUpToMultipleOfNonPowerOfTwo(CheckedSize { 78 }, CheckedSize { WTF::ResultOverflowed }).hasOverflowed());
}
} // namespace TestWebKitAPI
|