| 1 |
/* |
| 2 |
* Copyright 2023 Google LLC |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "fuzz/Fuzz.h" |
| 9 |
#include "include/private/base/SkAssert.h" |
| 10 |
#include "include/private/base/SkFloatingPoint.h" |
| 11 |
#include "src/base/SkCubics.h" |
| 12 |
#include "src/base/SkQuads.h" |
| 13 |
#include "src/base/SkUtils.h" |
| 14 |
|
| 15 |
#include <cmath> |
| 16 |
|
| 17 |
static void fuzz_quad_real_roots(double A, double B, double C) { |
| 18 |
double roots[2]; |
| 19 |
const int numSolutions = SkQuads::RootsReal(A, B, C, roots); |
| 20 |
SkASSERT_RELEASE(numSolutions >= 0 && numSolutions <= 2); |
| 21 |
for (int i = 0; i < numSolutions; i++) { |
| 22 |
SkASSERT_RELEASE(std::isfinite(roots[i])); |
| 23 |
// You may be tempted to add assertions that plug the provided solutions into |
| 24 |
// the quadratic equation and verify that the result is zero. Be advised |
| 25 |
// that the fuzzer is very good at finding float values that result in |
| 26 |
// seemingly arbitrarily large errors, due to the imprecision of floating |
| 27 |
// point math. Unless the input range is sufficiently small, such an |
| 28 |
// effort seems fruitless. |
| 29 |
} |
| 30 |
if (numSolutions == 2) { |
| 31 |
// Roots should not be duplicated |
| 32 |
SkASSERT_RELEASE(!sk_doubles_nearly_equal_ulps(roots[0], roots[1])); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
DEF_FUZZ(QuadRoots, fuzz) { |
| 37 |
double A, B, C; |
| 38 |
fuzz->next(&A); |
| 39 |
fuzz->next(&B); |
| 40 |
fuzz->next(&C); |
| 41 |
|
| 42 |
// Uncomment for easy test case creation |
| 43 |
// SkDebugf("A %16e (0x%lx) B %16e (0x%lx) C %16e (0x%lx)\n", |
| 44 |
// A, sk_bit_cast<uint64_t>(A), B, sk_bit_cast<uint64_t>(B), |
| 45 |
// C, sk_bit_cast<uint64_t>(C)); |
| 46 |
|
| 47 |
fuzz_quad_real_roots(A, B, C); |
| 48 |
} |