Skip to content
Myr

Merge pull request #951 from Likio3000/codex/reject-negative-parameter-steps

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Reject negative parameter steps before expanding ranges

authored by David Peter · committed by GitHub · parents d3d67fc, 31ae72b · Browse files · 5 files+54 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+54 added−2 removed

  • tests:+16 added−0 removed
  • other:+38 added−2 removed

5 files+54 added−2 removed

modified:doc/hyperfine.1+1 added-1 removed
178 unmodified lines
179.HP180\fB\-D\fR, \fB\-\-parameter\-step\-size\fR \fIDELTA\fP181.IP182This argument requires \fB\-\-parameter\-scan\fR to be specified as well. Traverse the183range \fIMIN..MAX\fP in steps of \fIDELTA\fP.183range \fIMIN..MAX\fP in steps of \fIDELTA\fP, which must be positive.184.IP185.RS186Example:187.RS
216 unmodified lines
modified:src/parameter/range_step.rs+34 added
45 unmodified lines
46 if step == T::from(0) {47 return Err(ParameterScanError::ZeroStep);48 }4950 if step < T::from(0) {51 return Err(ParameterScanError::NegativeStep);52 }5354 const MAX_PARAMETERS: usize = 100_000;55 match range_step_size_hint(start, end, step) {56 (_, Some(size)) if size <= MAX_PARAMETERS => Ok(Self {57 state: start,
66 unmodified lines
124 assert_eq!(param_range[10], Decimal::from(1));125 }126127 #[test]128 fn test_negative_integer_step() {129 for step in [-2, -3, i32::MIN, -1] {130 for end in [0, 1, 10] {131 let result = RangeStep::new(0, end, step);132 assert_eq!(133 result.unwrap_err().to_string(),134 "Parameter step size must be positive"135 );136 }137 }138 }139140 #[test]141 fn test_negative_decimal_step() {142 for step in ["-2.0", "-0.1"] {143 for end in [0, 1, 10] {144 let result = RangeStep::new(145 Decimal::ZERO,146 Decimal::from(end),147 Decimal::from_str(step).unwrap(),148 );149 assert_eq!(150 result.unwrap_err().to_string(),151 "Parameter step size must be positive"152 );153 }154 }155 }156157 #[test]158 fn test_range_step_validate() {159 let result = RangeStep::new(0, 10, 3);160 assert!(result.is_ok());161
23 unmodified lines
modified:src/cli.rs+1 added-1 removed
180 unmodified lines
181 .value_names(["DELTA"])182 .requires("parameter-scan")183 .help(184 "This argument requires --parameter-scan to be specified as well. \185 Traverse the range MIN..MAX in steps of DELTA.\n\n \185 Traverse the range MIN..MAX in steps of DELTA, which must be positive.\n\n \186 Example: hyperfine -P delay 0.3 0.7 -D 0.2 'sleep {delay}'\n\n\187 This performs benchmarks for 'sleep 0.3', 'sleep 0.5' and 'sleep 0.7'.",188 ),189 )
234 unmodified lines
modified:src/error.rs+2 added
17 unmodified lines
18 #[error("Parameter range is too large")]19 TooLarge,20 #[error("Zero is not a valid parameter step")]21 ZeroStep,22 #[error("Parameter step size must be positive")]23 NegativeStep,24 #[error("A step size is required when the range bounds are floating point numbers. The step size can be specified with the '-D/--parameter-step-size <DELTA>' parameter")]25 StepRequired,26 #[error("'--command-name' has been specified {0} times. It has to appear exactly once, or exactly {1} times (number of benchmarks)")]27 UnexpectedCommandNameCount(usize, usize),
37 unmodified lines
modified:tests/integration_tests.rs+16 added
778 unmodified lines
779 ");780}781782#[test]783fn rejects_negative_parameter_steps() {784 for step in ["-1", "-1.0"] {785 hyperfine()786 .args(["--parameter-scan", "n", "0", "1"])787 .arg(format!("--parameter-step-size={step}"))788 .arg("echo {n}")789 .assert()790 .code(1)791 .stdout(predicate::str::is_empty())792 .stderr(predicate::str::contains(793 "Parameter step size must be positive",794 ));795 }796}797798#[test]799fn performs_reference_and_all_benchmarks_in_parameter_scan() {800 let _settings = snapshot_settings().bind_to_scope();801 assert_cmd_snapshot!(hyperfine_debug()802 .arg("--style=basic")
216 unmodified lines