Flutter Engine
The Flutter Engine
FuzzCubicRoots.cpp
Go to the documentation of this file.
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"
11#include "src/base/SkCubics.h"
12#include "src/base/SkQuads.h"
13#include "src/base/SkUtils.h"
14
15#include <cmath>
16
17static void fuzz_cubic_real_roots(double A, double B, double C, double D) {
18 double roots[3];
19 const int numSolutions = SkCubics::RootsReal(A, B, C, D, roots);
20 SkASSERT_RELEASE(numSolutions >= 0 && numSolutions <= 3);
21 for (int i = 0; i < numSolutions; i++) {
23 }
24 // Roots should not be duplicated
25 if (numSolutions >= 2) {
27 }
28 if (numSolutions == 3) {
31 }
32}
33
34static void fuzz_cubic_roots_valid_t(double A, double B, double C, double D) {
35 double roots[3];
36 const int numSolutions = SkCubics::RootsValidT(A, B, C, D, roots);
37 SkASSERT_RELEASE(numSolutions >= 0 && numSolutions <= 3);
38 for (int i = 0; i < numSolutions; i++) {
40 SkASSERT_RELEASE(roots[i] >= 0.0);
41 SkASSERT_RELEASE(roots[i] <= 1.0);
42 }
43 // Roots should not be duplicated
44 if (numSolutions >= 2) {
46 }
47 if (numSolutions == 3) {
50 }
51}
52
53static void fuzz_cubic_roots_binary_search(double A, double B, double C, double D) {
54 double roots[3];
55 const int numSolutions = SkCubics::BinarySearchRootsValidT(A, B, C, D, roots);
56 SkASSERT_RELEASE(numSolutions >= 0 && numSolutions <= 3);
57 for (int i = 0; i < numSolutions; i++) {
59 SkASSERT_RELEASE(roots[i] >= 0.0);
60 SkASSERT_RELEASE(roots[i] <= 1.0);
61 double actual = SkCubics::EvalAt(A, B, C, D, roots[i]);
62 // The binary search algorithm *should* be accurate regardless of the inputs.
63 SkASSERT_RELEASE(std::abs(actual) < 0.001);
64 }
65 // Roots should not be duplicated
66 if (numSolutions >= 2) {
68 }
69 if (numSolutions == 3) {
72 }
73}
74
75DEF_FUZZ(CubicRoots, fuzz) {
76 double A, B, C, D;
77 fuzz->next(&A);
78 fuzz->next(&B);
79 fuzz->next(&C);
80 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));
87
89
91}
static void fuzz_cubic_roots_binary_search(double A, double B, double C, double D)
static void fuzz_cubic_real_roots(double A, double B, double C, double D)
static void fuzz_cubic_roots_valid_t(double A, double B, double C, double D)
DEF_FUZZ(CubicRoots, fuzz)
#define SkASSERT_RELEASE(cond)
Definition: SkAssert.h:100
bool sk_doubles_nearly_equal_ulps(double a, double b, uint8_t maxUlpsDiff=16)
static int BinarySearchRootsValidT(double A, double B, double C, double D, double solution[3])
Definition: SkCubics.cpp:208
static double EvalAt(double A, double B, double C, double D, double t)
Definition: SkCubics.h:51
static int RootsValidT(double A, double B, double C, double D, double solution[3])
Definition: SkCubics.cpp:127
static int RootsReal(double A, double B, double C, double D, double solution[3])
Definition: SkCubics.cpp:38
#define C(TEST_CATEGORY)
Definition: colrv1.cpp:248
#define B
SINT bool isfinite(const Vec< N, T > &v)
Definition: SkVx.h:1003
SIN Vec< N, float > abs(const Vec< N, float > &x)
Definition: SkVx.h:707