Kevin Lubick | 2541edf | 2018-01-11 10:27:14 -0500 | [diff] [blame] | 1 | /* |
Kevin Lubick | db1e5c6 | 2018-02-27 08:30:43 -0500 | [diff] [blame] | 2 | * Copyright 2018 Google, LLC |
Kevin Lubick | 2541edf | 2018-01-11 10:27:14 -0500 | [diff] [blame] | 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 | |
Cary Clark | 91390c8 | 2018-03-09 14:02:46 -0500 | [diff] [blame] | 8 | #ifndef FuzzCommon_DEFINED |
| 9 | #define FuzzCommon_DEFINED |
| 10 | |
Kevin Lubick | 2541edf | 2018-01-11 10:27:14 -0500 | [diff] [blame] | 11 | #include "Fuzz.h" |
| 12 | #include "SkPath.h" |
| 13 | #include "SkRegion.h" |
| 14 | |
| 15 | // We don't always want to test NaNs and infinities. |
Cary Clark | 91390c8 | 2018-03-09 14:02:46 -0500 | [diff] [blame] | 16 | static inline void fuzz_nice_float(Fuzz* fuzz, float* f) { |
Kevin Lubick | 2541edf | 2018-01-11 10:27:14 -0500 | [diff] [blame] | 17 | float v; |
| 18 | fuzz->next(&v); |
| 19 | constexpr float kLimit = 1.0e35f; // FLT_MAX? |
| 20 | *f = (v == v && v <= kLimit && v >= -kLimit) ? v : 0.0f; |
| 21 | } |
| 22 | |
| 23 | template <typename... Args> |
| 24 | inline void fuzz_nice_float(Fuzz* fuzz, float* f, Args... rest) { |
| 25 | fuzz_nice_float(fuzz, f); |
| 26 | fuzz_nice_float(fuzz, rest...); |
| 27 | } |
| 28 | |
Hal Canary | 13872dd | 2018-04-06 10:25:12 -0400 | [diff] [blame] | 29 | template <typename T, typename Min, typename Max> |
| 30 | inline void fuzz_enum_range(Fuzz* fuzz, T* value, Min rmin, Max rmax) { |
| 31 | using U = skstd::underlying_type_t<T>; |
| 32 | fuzz->nextRange((U*)value, (U)rmin, (U)rmax); |
| 33 | } |
| 34 | |
| 35 | inline void fuzz_region(Fuzz* fuzz, SkRegion* region, int maxN) { |
Kevin Lubick | 2541edf | 2018-01-11 10:27:14 -0500 | [diff] [blame] | 36 | uint8_t N; |
Hal Canary | 13872dd | 2018-04-06 10:25:12 -0400 | [diff] [blame] | 37 | fuzz->nextRange(&N, 0, maxN); |
Kevin Lubick | 2541edf | 2018-01-11 10:27:14 -0500 | [diff] [blame] | 38 | for (uint8_t i = 0; i < N; ++i) { |
| 39 | SkIRect r; |
Hal Canary | 13872dd | 2018-04-06 10:25:12 -0400 | [diff] [blame] | 40 | SkRegion::Op op; |
| 41 | // Avoid the sentinal value used by Region. |
| 42 | fuzz->nextRange(&r.fLeft, -2147483646, 2147483646); |
| 43 | fuzz->nextRange(&r.fTop, -2147483646, 2147483646); |
| 44 | fuzz->nextRange(&r.fRight, -2147483646, 2147483646); |
| 45 | fuzz->nextRange(&r.fBottom, -2147483646, 2147483646); |
Kevin Lubick | 2541edf | 2018-01-11 10:27:14 -0500 | [diff] [blame] | 46 | r.sort(); |
Hal Canary | 13872dd | 2018-04-06 10:25:12 -0400 | [diff] [blame] | 47 | fuzz_enum_range(fuzz, &op, (SkRegion::Op)0, SkRegion::kLastOp); |
| 48 | if (!region->op(r, op)) { |
Kevin Lubick | 2541edf | 2018-01-11 10:27:14 -0500 | [diff] [blame] | 49 | return; |
| 50 | } |
| 51 | } |
| 52 | } |
Cary Clark | 91390c8 | 2018-03-09 14:02:46 -0500 | [diff] [blame] | 53 | |
Hal Canary | 13872dd | 2018-04-06 10:25:12 -0400 | [diff] [blame] | 54 | template <> |
| 55 | inline void Fuzz::next(SkRegion* region) { fuzz_region(this, region, 10); } |
| 56 | |
Cary Clark | 91390c8 | 2018-03-09 14:02:46 -0500 | [diff] [blame] | 57 | // allows some float values for path points |
| 58 | void FuzzPath(Fuzz* fuzz, SkPath* path, int maxOps); |
| 59 | // allows all float values for path points |
| 60 | void BuildPath(Fuzz* fuzz, SkPath* path, int last_verb); |
| 61 | |
| 62 | #endif |