|
| 1 | +//! Tests for max_fuzz_int configuration limiting fuzz values. |
| 2 | +
|
| 3 | +use crate::test_helpers::TEST_DATA_DEFAULT; |
| 4 | +use alloy_primitives::U256; |
| 5 | +use forge::result::{SuiteResult, TestStatus}; |
| 6 | +use foundry_test_utils::Filter; |
| 7 | + |
| 8 | +/// Test that max_fuzz_int properly limits unsigned integers to [0, max]. |
| 9 | +/// With max_fuzz_int = 255, all uint256 values should be <= 255. |
| 10 | +#[tokio::test(flavor = "multi_thread")] |
| 11 | +async fn test_fuzz_max_int_uint_limited() { |
| 12 | + let max_value = U256::from(u8::MAX); |
| 13 | + |
| 14 | + let filter = |
| 15 | + Filter::new("testFuzzUint256Limited", "FuzzMaxIntTest", ".*/revive/FuzzMaxInt.t.sol"); |
| 16 | + let mut runner = TEST_DATA_DEFAULT.runner_with(|config| { |
| 17 | + config.fuzz.max_fuzz_int = Some(max_value); |
| 18 | + config.fuzz.runs = 1000; |
| 19 | + }); |
| 20 | + let results = runner.test_collect(&filter).unwrap(); |
| 21 | + |
| 22 | + for (_, SuiteResult { test_results, .. }) in results { |
| 23 | + for (test_name, result) in test_results { |
| 24 | + assert_eq!( |
| 25 | + result.status, |
| 26 | + TestStatus::Success, |
| 27 | + "Test {} should pass with max_fuzz_int limiting uint256 values to [0, 255].\nReason: {:?}", |
| 28 | + test_name, |
| 29 | + result.reason |
| 30 | + ); |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/// Test that max_fuzz_int properly limits signed integers to [-(max+1), max]. |
| 36 | +/// With max_fuzz_int = 255, signed integers should be in range [-256, 255]. |
| 37 | +#[tokio::test(flavor = "multi_thread")] |
| 38 | +async fn test_fuzz_max_int_int_limited() { |
| 39 | + let max_value = U256::from(u8::MAX); |
| 40 | + |
| 41 | + let filter = |
| 42 | + Filter::new("testFuzzInt256Limited", "FuzzMaxIntTest", ".*/revive/FuzzMaxInt.t.sol"); |
| 43 | + let mut runner = TEST_DATA_DEFAULT.runner_with(|config| { |
| 44 | + config.fuzz.max_fuzz_int = Some(max_value); |
| 45 | + config.fuzz.runs = 1000; |
| 46 | + }); |
| 47 | + let results = runner.test_collect(&filter).unwrap(); |
| 48 | + |
| 49 | + for (_, SuiteResult { test_results, .. }) in results { |
| 50 | + for (test_name, result) in test_results { |
| 51 | + assert_eq!( |
| 52 | + result.status, |
| 53 | + TestStatus::Success, |
| 54 | + "Test {} should pass with max_fuzz_int limiting int256 values to [-256, 255].\nReason: {:?}", |
| 55 | + test_name, |
| 56 | + result.reason |
| 57 | + ); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/// Test that without max_fuzz_int, uint256 values can exceed 255. |
| 63 | +#[tokio::test(flavor = "multi_thread")] |
| 64 | +async fn test_fuzz_no_limit_uint_exceeds() { |
| 65 | + let filter = |
| 66 | + Filter::new("testFuzzUint256Unlimited", "FuzzNoLimitTest", ".*/revive/FuzzMaxInt.t.sol"); |
| 67 | + let mut runner = TEST_DATA_DEFAULT.runner_with(|config| { |
| 68 | + config.fuzz.max_fuzz_int = None; // No limit |
| 69 | + config.fuzz.runs = 1000; |
| 70 | + config.fuzz.seed = Some(U256::from(42u32)); // Fixed seed for reproducibility |
| 71 | + }); |
| 72 | + let results = runner.test_collect(&filter).unwrap(); |
| 73 | + |
| 74 | + for (_, SuiteResult { test_results, .. }) in results { |
| 75 | + for (test_name, result) in test_results { |
| 76 | + assert_eq!( |
| 77 | + result.status, |
| 78 | + TestStatus::Failure, |
| 79 | + "Test {} should FAIL without max_fuzz_int as fuzzer generates values > 255.\nReason: {:?}", |
| 80 | + test_name, |
| 81 | + result.reason |
| 82 | + ); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/// Test that without max_fuzz_int, int256 values can exceed [-256, 255] range. |
| 88 | +#[tokio::test(flavor = "multi_thread")] |
| 89 | +async fn test_fuzz_no_limit_int_exceeds() { |
| 90 | + let filter = |
| 91 | + Filter::new("testFuzzInt256Unlimited", "FuzzNoLimitTest", ".*/revive/FuzzMaxInt.t.sol"); |
| 92 | + let mut runner = TEST_DATA_DEFAULT.runner_with(|config| { |
| 93 | + config.fuzz.max_fuzz_int = None; // No limit |
| 94 | + config.fuzz.runs = 1000; |
| 95 | + config.fuzz.seed = Some(U256::from(42u32)); // Fixed seed for reproducibility |
| 96 | + }); |
| 97 | + let results = runner.test_collect(&filter).unwrap(); |
| 98 | + |
| 99 | + for (_, SuiteResult { test_results, .. }) in results { |
| 100 | + for (test_name, result) in test_results { |
| 101 | + assert_eq!( |
| 102 | + result.status, |
| 103 | + TestStatus::Failure, |
| 104 | + "Test {} should FAIL without max_fuzz_int as fuzzer generates values outside [-256, 255].\nReason: {:?}", |
| 105 | + test_name, |
| 106 | + result.reason |
| 107 | + ); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments