referrerpolicy=no-referrer-when-downgrade

fixed_point/
fixed_point.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// 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.
17
18//! # 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/).
29
30use honggfuzz::fuzz;
31use sp_arithmetic::{traits::Saturating, FixedI64, FixedPointNumber};
32
33fn main() {
34	loop {
35		fuzz!(|data: (i32, i32)| {
36			let x: i128 = data.0.into();
37			let y: i128 = data.1.into();
38
39			// Check `from_rational` and division are consistent.
40			if y != 0 {
41				let f1 =
42					FixedI64::saturating_from_integer(x) / FixedI64::saturating_from_integer(y);
43				let f2 = FixedI64::saturating_from_rational(x, y);
44				assert_eq!(f1.into_inner(), f2.into_inner());
45			}
46
47			// Check `saturating_mul`.
48			let a = FixedI64::saturating_from_rational(2, 5);
49			let b = a.saturating_mul(FixedI64::saturating_from_integer(x));
50			let n = b.into_inner() as i128;
51			let m = 2i128 * x * FixedI64::accuracy() as i128 / 5i128;
52			assert_eq!(n, m);
53
54			// Check `saturating_mul` and division are inverse.
55			if x != 0 {
56				assert_eq!(a, b / FixedI64::saturating_from_integer(x));
57			}
58
59			// Check `reciprocal`.
60			let r = a.reciprocal().unwrap().reciprocal().unwrap();
61			assert_eq!(a, r);
62
63			// Check addition.
64			let a = FixedI64::saturating_from_integer(x);
65			let b = FixedI64::saturating_from_integer(y);
66			let c = FixedI64::saturating_from_integer(x.saturating_add(y));
67			assert_eq!(a.saturating_add(b), c);
68
69			// Check subtraction.
70			let a = FixedI64::saturating_from_integer(x);
71			let b = FixedI64::saturating_from_integer(y);
72			let c = FixedI64::saturating_from_integer(x.saturating_sub(y));
73			assert_eq!(a.saturating_sub(b), c);
74
75			// Check `saturating_mul_acc_int`.
76			let a = FixedI64::saturating_from_rational(2, 5);
77			let b = a.saturating_mul_acc_int(x);
78			let xx = FixedI64::saturating_from_integer(x);
79			let d = a.saturating_mul(xx).saturating_add(xx).into_inner() as i128 /
80				FixedI64::accuracy() as i128;
81			assert_eq!(b, d);
82		});
83	}
84}