Coverage Report

Created: 2024-09-14 07:19

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