Coverage Report

Created: 2024-09-14 07:19

/src/skia/fuzz/FuzzCubicRoots.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
92
static void fuzz_cubic_real_roots(double A, double B, double C, double D) {
18
92
    double roots[3];
19
92
    const int numSolutions = SkCubics::RootsReal(A, B, C, D, roots);
20
92
    SkASSERT_RELEASE(numSolutions >= 0 && numSolutions <= 3);
21
244
    for (int i = 0; i < numSolutions; i++) {
22
152
        SkASSERT_RELEASE(std::isfinite(roots[i]));
23
152
    }
24
    // Roots should not be duplicated
25
92
    if (numSolutions >= 2) {
26
48
        SkASSERT_RELEASE(!sk_doubles_nearly_equal_ulps(roots[0], roots[1]));
27
48
    }
28
92
    if (numSolutions == 3) {
29
17
        SkASSERT_RELEASE(!sk_doubles_nearly_equal_ulps(roots[1], roots[2]));
30
17
        SkASSERT_RELEASE(!sk_doubles_nearly_equal_ulps(roots[0], roots[2]));
31
17
    }
32
92
}
33
34
92
static void fuzz_cubic_roots_valid_t(double A, double B, double C, double D) {
35
92
    double roots[3];
36
92
    const int numSolutions = SkCubics::RootsValidT(A, B, C, D, roots);
37
92
    SkASSERT_RELEASE(numSolutions >= 0 && numSolutions <= 3);
38
215
    for (int i = 0; i < numSolutions; i++) {
39
123
        SkASSERT_RELEASE(std::isfinite(roots[i]));
40
123
        SkASSERT_RELEASE(roots[i] >= 0.0);
41
123
        SkASSERT_RELEASE(roots[i] <= 1.0);
42
123
    }
43
    // Roots should not be duplicated
44
92
    if (numSolutions >= 2) {
45
35
        SkASSERT_RELEASE(!sk_doubles_nearly_equal_ulps(roots[0], roots[1]));
46
35
    }
47
92
    if (numSolutions == 3) {
48
7
        SkASSERT_RELEASE(!sk_doubles_nearly_equal_ulps(roots[1], roots[2]));
49
7
        SkASSERT_RELEASE(!sk_doubles_nearly_equal_ulps(roots[0], roots[2]));
50
7
    }
51
92
}
52
53
92
static void fuzz_cubic_roots_binary_search(double A, double B, double C, double D) {
54
92
    double roots[3];
55
92
    const int numSolutions = SkCubics::BinarySearchRootsValidT(A, B, C, D, roots);
56
92
    SkASSERT_RELEASE(numSolutions >= 0 && numSolutions <= 3);
57
193
    for (int i = 0; i < numSolutions; i++) {
58
101
        SkASSERT_RELEASE(std::isfinite(roots[i]));
59
101
        SkASSERT_RELEASE(roots[i] >= 0.0);
60
101
        SkASSERT_RELEASE(roots[i] <= 1.0);
61
101
        double actual = SkCubics::EvalAt(A, B, C, D, roots[i]);
62
        // The binary search algorithm *should* be accurate regardless of the inputs.
63
101
        SkASSERT_RELEASE(std::abs(actual) < 0.001);
64
101
    }
65
    // Roots should not be duplicated
66
92
    if (numSolutions >= 2) {
67
23
        SkASSERT_RELEASE(!sk_doubles_nearly_equal_ulps(roots[0], roots[1]));
68
23
    }
69
92
    if (numSolutions == 3) {
70
2
        SkASSERT_RELEASE(!sk_doubles_nearly_equal_ulps(roots[1], roots[2]));
71
2
        SkASSERT_RELEASE(!sk_doubles_nearly_equal_ulps(roots[0], roots[2]));
72
2
    }
73
92
}
74
75
92
DEF_FUZZ(CubicRoots, fuzz) {
76
92
    double A, B, C, D;
77
92
    fuzz->next(&A);
78
92
    fuzz->next(&B);
79
92
    fuzz->next(&C);
80
92
    fuzz->next(&D);
81
82
    // Uncomment for easy test case creation
83
//    SkDebugf("A %16e (0x%lx) B %16e (0x%lx) C %16e (0x%lx) D %16e (0x%lx)\n",
84
//             A, sk_bit_cast<uint64_t>(A), B, sk_bit_cast<uint64_t>(B),
85
//             C, sk_bit_cast<uint64_t>(C), D, sk_bit_cast<uint64_t>(D));
86
92
    fuzz_cubic_real_roots(A, B, C, D);
87
88
92
    fuzz_cubic_roots_valid_t(A, B, C, D);
89
90
92
    fuzz_cubic_roots_binary_search(A, B, C, D);
91
92
}