Skip to content
Myr

Merges new and validate methods of `RangeStep` into one method, renames error names according to code review.

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

authored by Steven Gu · committed by David Peter · parent d4a0e9a · Browse files · 3 files+51 added−52 removed
This commit

State

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

Diff stats

3 files+51 added−52 removed

  • other:+51 added−52 removed

3 files+51 added−52 removed

modified:src/hyperfine/error.rs+6 added-6 removed
10 unmodified lines
11 EmptyRange,12 TooLarge,13 ZeroStep,14 StepRequired,15 DifferentCommandNameCountWithParameters(usize, usize),15 UnexpectedCommandNameCount(usize, usize),16}1718impl From<num::ParseIntError> for ParameterScanError {19 fn from(e: num::ParseIntError) -> ParameterScanError {
20 unmodified lines
40 "A step size is required when the range bounds are \41 floating point numbers. The step size can be specified \42 with the '--parameter-step-size' parameter"43 ),44 ParameterScanError::DifferentCommandNameCountWithParameters(real, expected) => {44 ParameterScanError::UnexpectedCommandNameCount(real, expected) => {45 write!(46 f,47 "Current --command-name count is {}: expected {} as parameters",47 "'--command-name' has been specified {} times. It has to appear exactly once, or exactly {} times (number of benchmarks)",48 real, expected49 )50 }51 }
6 unmodified lines
58pub enum OptionsError<'a> {59 RunsBelowTwo,60 EmptyRunsRange,61 TooManyCommandNames(usize),62 DifferentCommandNameCountWithParameters(usize, usize),62 UnexpectedCommandNameCount(usize, usize),63 NumericParsingError(&'a str, ParseIntError),64}6566impl<'a> fmt::Display for OptionsError<'a> {
3 unmodified lines
70 OptionsError::RunsBelowTwo => write!(f, "Number of runs below two"),71 OptionsError::TooManyCommandNames(n) => {72 write!(f, "Too many --command-name options: expected {} at most", n)73 }74 OptionsError::DifferentCommandNameCountWithParameters(real, expected) => {74 OptionsError::UnexpectedCommandNameCount(real, expected) => {75 write!(76 f,77 "Current --command-name count is {}: expected {} as parameters",77 "'--command-name' has been specified {} times. It has to appear exactly once, or exactly {} times (number of benchmarks)",78 real, expected79 )80 }81 OptionsError::NumericParsingError(cmd, ref err) => write!(
10 unmodified lines
modified:src/hyperfine/parameter_range.rs+41 added-41 removed
30 unmodified lines
31 > Numeric for T32{33}3435#[derive(Debug)]36struct RangeStep<T> {37 state: T,38 end: T,39 step: T,40}4142impl<T: Numeric> RangeStep<T> {42 fn new(start: T, end: T, step: T) -> RangeStep<T> {43 RangeStep {44 state: start,45 end,46 step,47 }48 }4950 fn validate(&self) -> Result<(), ParameterScanError> {51 if self.end < self.state {43 fn new(start: T, end: T, step: T) -> Result<Self, ParameterScanError> {44 if end < start {45 return Err(ParameterScanError::EmptyRange);46 }4755 if self.step == T::from(0) {48 if step == T::from(0) {49 return Err(ParameterScanError::ZeroStep);50 }5152 const MAX_PARAMETERS: usize = 100_000;60 match self.size_hint() {61 (_, Some(size)) if size <= MAX_PARAMETERS => Ok(()),53 match range_step_size_hint(start, end, step) {54 (_, Some(size)) if size <= MAX_PARAMETERS => Ok(Self {55 state: start,56 end,57 step,58 }),59 _ => Err(ParameterScanError::TooLarge),60 }61 }62}
11 unmodified lines
74 Some(return_val)75 }7677 fn size_hint(&self) -> (usize, Option<usize>) {81 if self.step == T::from(0) {82 return (usize::MAX, None);83 }78 range_step_size_hint(self.state, self.end, self.step)79 }80}8185 let steps = (self.end - self.state + T::from(1)) / self.step;86 steps87 .into()88 .try_into()89 .map_or((usize::MAX, None), |u| (u, Some(u)))82fn range_step_size_hint<T: Numeric>(start: T, end: T, step: T) -> (usize, Option<usize>) {83 if step == T::from(0) {84 return (usize::MAX, None);85 }8687 let steps = (end - start + T::from(1)) / step;88 steps89 .into()90 .try_into()91 .map_or((usize::MAX, None), |u| (u, Some(u)))92}9394fn build_parameterized_commands<'a, T: Numeric>(95 param_min: T,
2 unmodified lines
98 command_names: Vec<&'a str>,99 command_strings: Vec<&'a str>,100 param_name: &'a str,101) -> Result<Vec<Command<'a>>, ParameterScanError> {101 let param_range = RangeStep::new(param_min, param_max, step);102 param_range.validate()?;102 let param_range = RangeStep::new(param_min, param_max, step)?;103 let param_count = param_range.size_hint().1.unwrap();104 let command_name_count = command_names.len();105106 // `--command-name` should appear exactly once or same count with parameters.106 // `--command-name` should appear exactly once or exactly B times,107 // where B is the total number of benchmarks.108 if command_name_count > 1 && command_name_count != param_count {108 return Err(ParameterScanError::DifferentCommandNameCountWithParameters(109 return Err(ParameterScanError::UnexpectedCommandNameCount(110 command_name_count,111 param_count,112 ));113 }
65 unmodified lines
179}180181#[test]182fn test_integer_range() {182 let param_range: Vec<i32> = RangeStep::new(0, 10, 3).collect();183 let param_range: Vec<i32> = RangeStep::new(0, 10, 3).unwrap().collect();184185 assert_eq!(param_range.len(), 4);186 assert_eq!(param_range[0], 0);187 assert_eq!(param_range[3], 9);
4 unmodified lines
192 let param_min = Decimal::from(0);193 let param_max = Decimal::from(1);194 let step = Decimal::from_str("0.1").unwrap();195195 let param_range: Vec<Decimal> = RangeStep::new(param_min, param_max, step).collect();196 let param_range: Vec<Decimal> = RangeStep::new(param_min, param_max, step)197 .unwrap()198 .collect();199200 assert_eq!(param_range.len(), 11);201 assert_eq!(param_range[0], Decimal::from(0));202 assert_eq!(param_range[10], Decimal::from(1));203}204205#[test]206fn test_range_step_validate() {204 let range_step = RangeStep::new(0, 10, 3);205 assert!(range_step.validate().is_ok());207 let result = RangeStep::new(0, 10, 3);208 assert!(result.is_ok());209207 let range_step = RangeStep::new(210 let result = RangeStep::new(211 Decimal::from(0),212 Decimal::from(1),213 Decimal::from_str("0.1").unwrap(),214 );212 assert!(range_step.validate().is_ok());215 assert!(result.is_ok());216214 let range_step = RangeStep::new(11, 10, 1);215 assert_eq!(216 format!("{}", range_step.validate().unwrap_err()),217 "Empty parameter range"218 );217 let result = RangeStep::new(11, 10, 1);218 assert_eq!(format!("{}", result.unwrap_err()), "Empty parameter range");219220 let range_step = RangeStep::new(0, 10, 0);220 let result = RangeStep::new(0, 10, 0);221 assert_eq!(222 format!("{}", range_step.validate().unwrap_err()),222 format!("{}", result.unwrap_err()),223 "Zero is not a valid parameter step"224 );225226 let range_step = RangeStep::new(0, 100_001, 1);226 let result = RangeStep::new(0, 100_001, 1);227 assert_eq!(228 format!("{}", range_step.validate().unwrap_err()),228 format!("{}", result.unwrap_err()),229 "Parameter range is too large"230 );231}232
75 unmodified lines
308 "val",309 );310 assert_eq!(311 format!("{}", result.unwrap_err()),312 "Current --command-name count is 2: expected 3 as parameters"312 "'--command-name' has been specified 2 times. It has to appear exactly once, or exactly 3 times (number of benchmarks)"313 );314}
modified:src/main.rs+4 added-5 removed
253 unmodified lines
254 if param_space_size == 0 {255 return Vec::new();256 }257258 // `--command-name` should appear exactly once or same count with parameters.258 // `--command-name` should appear exactly once or exactly B times,259 // where B is the total number of benchmarks.260 let command_name_count = command_names.len();261 if command_name_count > 1 && command_name_count != param_space_size {261 let err = OptionsError::DifferentCommandNameCountWithParameters(262 command_name_count,263 param_space_size,264 );262 let err =263 OptionsError::UnexpectedCommandNameCount(command_name_count, param_space_size);264 error(&err.to_string());265 }266267 let mut i = 0;
139 unmodified lines