Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 1 | // Copyright 2021 The Dawn & Tint Authors |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 3 | // Redistribution and use in source and binary forms, with or without |
| 4 | // modification, are permitted provided that the following conditions are met: |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 5 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 6 | // 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | // list of conditions and the following disclaimer. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 8 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 9 | // 2. Redistributions in binary form must reproduce the above copyright notice, |
| 10 | // this list of conditions and the following disclaimer in the documentation |
| 11 | // and/or other materials provided with the distribution. |
| 12 | // |
| 13 | // 3. Neither the name of the copyright holder nor the names of its |
| 14 | // contributors may be used to endorse or promote products derived from |
| 15 | // this software without specific prior written permission. |
| 16 | // |
| 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 21 | // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 23 | // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 24 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 25 | // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 27 | |
| 28 | #include "src/tint/fuzzers/random_generator.h" |
| 29 | |
| 30 | #include <memory> |
| 31 | |
| 32 | #include "gtest/gtest.h" |
| 33 | |
| 34 | #include "src/tint/fuzzers/mersenne_twister_engine.h" |
| 35 | |
dan sinclair | 62a1d71 | 2022-04-07 17:46:04 +0000 | [diff] [blame] | 36 | namespace tint::fuzzers { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 37 | namespace { |
| 38 | |
| 39 | /// Implementation of RandomGeneratorEngine that just returns a stream of |
| 40 | /// monotonically increasing numbers. |
| 41 | class MonotonicEngine : public RandomGeneratorEngine { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 42 | public: |
| 43 | uint32_t RandomUInt32(uint32_t, uint32_t) override { return next_++; } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 44 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 45 | uint64_t RandomUInt64(uint64_t, uint64_t) override { return next_++; } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 46 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 47 | void RandomNBytes(uint8_t*, size_t) override { |
| 48 | assert(false && "MonotonicDelegate does not implement RandomNBytes"); |
| 49 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 50 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 51 | private: |
| 52 | uint32_t next_ = 0; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | class RandomGeneratorTest : public testing::Test { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 56 | public: |
| 57 | void SetUp() override { rng_ = std::make_unique<RandomGenerator>(0); } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 58 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 59 | void TearDown() override {} |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 60 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 61 | protected: |
| 62 | std::unique_ptr<RandomGenerator> rng_; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | #ifndef NDEBUG |
| 66 | TEST_F(RandomGeneratorTest, GetUInt32ReversedBoundsCrashes) { |
Ian Vollick | c118734 | 2023-07-05 17:39:02 +0000 | [diff] [blame] | 67 | EXPECT_DEATH_IF_SUPPORTED(rng_->GetUInt32(10, 5), ".*"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | TEST_F(RandomGeneratorTest, GetUInt32EmptyBoundsCrashes) { |
Ian Vollick | c118734 | 2023-07-05 17:39:02 +0000 | [diff] [blame] | 71 | EXPECT_DEATH_IF_SUPPORTED(rng_->GetUInt32(5, 5), ".*"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | TEST_F(RandomGeneratorTest, GetUInt32ZeroBoundCrashes) { |
Ian Vollick | c118734 | 2023-07-05 17:39:02 +0000 | [diff] [blame] | 75 | EXPECT_DEATH_IF_SUPPORTED(rng_->GetUInt32(0u), ".*"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 76 | } |
| 77 | #endif // NDEBUG |
| 78 | |
| 79 | TEST_F(RandomGeneratorTest, GetUInt32SingularReturnsOneValue) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 80 | { |
| 81 | uint32_t result = rng_->GetUInt32(5u, 6u); |
| 82 | ASSERT_EQ(5u, result); |
| 83 | } |
| 84 | { |
| 85 | uint32_t result = rng_->GetUInt32(1u); |
| 86 | ASSERT_EQ(0u, result); |
| 87 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | TEST_F(RandomGeneratorTest, GetUInt32StaysInBounds) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 91 | { |
| 92 | uint32_t result = rng_->GetUInt32(5u, 10u); |
| 93 | ASSERT_LE(5u, result); |
| 94 | ASSERT_GT(10u, result); |
| 95 | } |
| 96 | { |
| 97 | uint32_t result = rng_->GetUInt32(10u); |
| 98 | ASSERT_LE(0u, result); |
| 99 | ASSERT_GT(10u, result); |
| 100 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | #ifndef NDEBUG |
| 104 | TEST_F(RandomGeneratorTest, GetUInt64ReversedBoundsCrashes) { |
Ian Vollick | c118734 | 2023-07-05 17:39:02 +0000 | [diff] [blame] | 105 | EXPECT_DEATH_IF_SUPPORTED(rng_->GetUInt64(10, 5), ".*"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | TEST_F(RandomGeneratorTest, GetUInt64EmptyBoundsCrashes) { |
Ian Vollick | c118734 | 2023-07-05 17:39:02 +0000 | [diff] [blame] | 109 | EXPECT_DEATH_IF_SUPPORTED(rng_->GetUInt64(5, 5), ".*"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | TEST_F(RandomGeneratorTest, GetUInt64ZeroBoundCrashes) { |
Ian Vollick | c118734 | 2023-07-05 17:39:02 +0000 | [diff] [blame] | 113 | EXPECT_DEATH_IF_SUPPORTED(rng_->GetUInt64(0u), ".*"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 114 | } |
| 115 | #endif // NDEBUG |
| 116 | |
| 117 | TEST_F(RandomGeneratorTest, GetUInt64SingularReturnsOneValue) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 118 | { |
| 119 | uint64_t result = rng_->GetUInt64(5u, 6u); |
| 120 | ASSERT_EQ(5u, result); |
| 121 | } |
| 122 | { |
| 123 | uint64_t result = rng_->GetUInt64(1u); |
| 124 | ASSERT_EQ(0u, result); |
| 125 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | TEST_F(RandomGeneratorTest, GetUInt64StaysInBounds) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 129 | { |
| 130 | uint64_t result = rng_->GetUInt64(5u, 10u); |
| 131 | ASSERT_LE(5u, result); |
| 132 | ASSERT_GT(10u, result); |
| 133 | } |
| 134 | { |
| 135 | uint64_t result = rng_->GetUInt64(10u); |
| 136 | ASSERT_LE(0u, result); |
| 137 | ASSERT_GT(10u, result); |
| 138 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | TEST_F(RandomGeneratorTest, GetByte) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 142 | rng_->GetByte(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | #ifndef NDEBUG |
| 146 | TEST_F(RandomGeneratorTest, GetNBytesNullDataBufferCrashes) { |
Ian Vollick | c118734 | 2023-07-05 17:39:02 +0000 | [diff] [blame] | 147 | EXPECT_DEATH_IF_SUPPORTED(rng_->GetNBytes(nullptr, 5), ".*"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 148 | } |
| 149 | #endif // NDEBUG |
| 150 | |
| 151 | TEST_F(RandomGeneratorTest, GetNBytes) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 152 | std::vector<uint8_t> data; |
| 153 | for (uint32_t i = 25; i < 1000u; i = i + 25) { |
| 154 | data.resize(i); |
| 155 | rng_->GetNBytes(data.data(), data.size()); |
| 156 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | TEST_F(RandomGeneratorTest, GetBool) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 160 | rng_->GetBool(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | TEST_F(RandomGeneratorTest, GetWeightedBoolZeroAlwaysFalse) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 164 | ASSERT_FALSE(rng_->GetWeightedBool(0)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | TEST_F(RandomGeneratorTest, GetWeightedBoolHundredAlwaysTrue) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 168 | ASSERT_TRUE(rng_->GetWeightedBool(100)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | #ifndef NDEBUG |
| 172 | TEST_F(RandomGeneratorTest, GetWeightedBoolAboveHundredCrashes) { |
Ian Vollick | c118734 | 2023-07-05 17:39:02 +0000 | [diff] [blame] | 173 | EXPECT_DEATH_IF_SUPPORTED(rng_->GetWeightedBool(101), ".*"); |
| 174 | EXPECT_DEATH_IF_SUPPORTED(rng_->GetWeightedBool(500), ".*"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 175 | } |
| 176 | #endif // NDEBUG |
| 177 | |
| 178 | TEST_F(RandomGeneratorTest, GetWeightedBool) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 179 | for (uint32_t i = 0; i <= 100; i++) { |
| 180 | rng_ = std::make_unique<RandomGenerator>(std::make_unique<MonotonicEngine>()); |
| 181 | for (uint32_t j = 0; j <= 100; j++) { |
| 182 | if (j < i) { |
| 183 | ASSERT_TRUE(rng_->GetWeightedBool(i)); |
| 184 | } else { |
| 185 | ASSERT_FALSE(rng_->GetWeightedBool(i)); |
| 186 | } |
| 187 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 188 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | #ifndef NDEBUG |
| 192 | TEST_F(RandomGeneratorTest, GetRandomElementEmptyVectorCrashes) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 193 | std::vector<uint8_t> v; |
Ian Vollick | c118734 | 2023-07-05 17:39:02 +0000 | [diff] [blame] | 194 | EXPECT_DEATH_IF_SUPPORTED(rng_->GetRandomElement(v), ".*"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 195 | } |
| 196 | #endif // NDEBUG |
| 197 | |
| 198 | TEST_F(RandomGeneratorTest, GetRandomElement) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 199 | std::vector<uint32_t> v; |
| 200 | for (uint32_t i = 25; i < 100u; i = i + 25) { |
| 201 | rng_ = std::make_unique<RandomGenerator>(std::make_unique<MonotonicEngine>()); |
| 202 | v.resize(i); |
| 203 | std::iota(v.begin(), v.end(), 0); |
| 204 | for (uint32_t j = 0; j < i; j++) { |
| 205 | EXPECT_EQ(j, rng_->GetRandomElement(v)); |
| 206 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 207 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | } // namespace |
dan sinclair | 62a1d71 | 2022-04-07 17:46:04 +0000 | [diff] [blame] | 211 | } // namespace tint::fuzzers |