1// This file is part of Substrate.
23// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
56// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
1718//! # Running
19//! Running this fuzzer can be done with `cargo hfuzz run fixed_point`. `honggfuzz` CLI options can
20//! be used by setting `HFUZZ_RUN_ARGS`, such as `-n 4` to use 4 threads.
21//!
22//! # Debugging a panic
23//! Once a panic is found, it can be debugged with
24//! `cargo hfuzz run-debug fixed_point hfuzz_workspace/fixed_point/*.fuzz`.
25//!
26//! # More information
27//! More information about `honggfuzz` can be found
28//! [here](https://docs.rs/honggfuzz/).
2930use honggfuzz::fuzz;
31use sp_arithmetic::{traits::Saturating, FixedI64, FixedPointNumber};
3233fn main() {
34loop {
35fuzz!(|data: (i32, i32)| {
36let x: i128 = data.0.into();
37let y: i128 = data.1.into();
3839// Check `from_rational` and division are consistent.
40if y != 0 {
41let f1 =
42 FixedI64::saturating_from_integer(x) / FixedI64::saturating_from_integer(y);
43let f2 = FixedI64::saturating_from_rational(x, y);
44assert_eq!(f1.into_inner(), f2.into_inner());
45 }
4647// Check `saturating_mul`.
48let a = FixedI64::saturating_from_rational(2, 5);
49let b = a.saturating_mul(FixedI64::saturating_from_integer(x));
50let n = b.into_inner() as i128;
51let m = 2i128 * x * FixedI64::accuracy() as i128 / 5i128;
52assert_eq!(n, m);
5354// Check `saturating_mul` and division are inverse.
55if x != 0 {
56assert_eq!(a, b / FixedI64::saturating_from_integer(x));
57 }
5859// Check `reciprocal`.
60let r = a.reciprocal().unwrap().reciprocal().unwrap();
61assert_eq!(a, r);
6263// Check addition.
64let a = FixedI64::saturating_from_integer(x);
65let b = FixedI64::saturating_from_integer(y);
66let c = FixedI64::saturating_from_integer(x.saturating_add(y));
67assert_eq!(a.saturating_add(b), c);
6869// Check subtraction.
70let a = FixedI64::saturating_from_integer(x);
71let b = FixedI64::saturating_from_integer(y);
72let c = FixedI64::saturating_from_integer(x.saturating_sub(y));
73assert_eq!(a.saturating_sub(b), c);
7475// Check `saturating_mul_acc_int`.
76let a = FixedI64::saturating_from_rational(2, 5);
77let b = a.saturating_mul_acc_int(x);
78let xx = FixedI64::saturating_from_integer(x);
79let d = a.saturating_mul(xx).saturating_add(xx).into_inner() as i128 /
80 FixedI64::accuracy() as i128;
81assert_eq!(b, d);
82 });
83 }
84}