/src/skia/fuzz/FuzzQuadRoots.cpp
Line | Count | Source |
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 | 20 | static void fuzz_quad_real_roots(double A, double B, double C) { |
18 | 20 | double roots[2]; |
19 | 20 | const int numSolutions = SkQuads::RootsReal(A, B, C, roots); |
20 | 20 | SkASSERT_RELEASE(numSolutions >= 0 && numSolutions <= 2); |
21 | 39 | for (int i = 0; i < numSolutions; i++) { |
22 | 19 | 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 | 19 | } |
30 | 20 | if (numSolutions == 2) { |
31 | | // Roots should not be duplicated |
32 | 5 | SkASSERT_RELEASE(!sk_doubles_nearly_equal_ulps(roots[0], roots[1])); |
33 | 5 | } |
34 | 20 | } |
35 | | |
36 | 20 | DEF_FUZZ(QuadRoots, fuzz) { |
37 | 20 | double A, B, C; |
38 | 20 | fuzz->next(&A); |
39 | 20 | fuzz->next(&B); |
40 | 20 | 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 | 20 | fuzz_quad_real_roots(A, B, C); |
48 | 20 | } |