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
Provenance
- Commit
28e8c31- Tree
8946a9a- Author
Steven Gu <asongala@163.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
d4a0e9a
3 files changed+51 added−52 removed
modified:src/hyperfine/error.rs+6 added-6 removed
10 unmodified lines11 EmptyRange,12 TooLarge,13 ZeroStep,14 StepRequired,15- DifferentCommandNameCountWithParameters(usize, usize),15+ UnexpectedCommandNameCount(usize, usize),16 }17 18 impl From<num::ParseIntError> for ParameterScanError {19 fn from(e: num::ParseIntError) -> ParameterScanError {20 unmodified lines40 "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 lines58 pub enum OptionsError<'a> {59 RunsBelowTwo,60 EmptyRunsRange,61 TooManyCommandNames(usize),62- DifferentCommandNameCountWithParameters(usize, usize),62+ UnexpectedCommandNameCount(usize, usize),63 NumericParsingError(&'a str, ParseIntError),64 }65 66 impl<'a> fmt::Display for OptionsError<'a> {3 unmodified lines70 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 linesmodified:src/hyperfine/parameter_range.rs+41 added-41 removed
30 unmodified lines31 > Numeric for T32 {33 }34 35+#[derive(Debug)]36 struct RangeStep<T> {37 state: T,38 end: T,39 step: T,40 }41 42 impl<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- }49-50- 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 }47 55- if self.step == T::from(0) {48+ if step == T::from(0) {49 return Err(ParameterScanError::ZeroStep);50 }51 52 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 lines74 Some(return_val)75 }76 77 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+}81 85- 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)))82+fn 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 }86+87+ 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 }93 94 fn build_parameterized_commands<'a, T: Numeric>(95 param_min: T,2 unmodified lines98 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();105 106- // `--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 lines179 }180 181 #[test]182 fn 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();184 185 assert_eq!(param_range.len(), 4);186 assert_eq!(param_range[0], 0);187 assert_eq!(param_range[3], 9);4 unmodified lines192 let param_min = Decimal::from(0);193 let param_max = Decimal::from(1);194 let step = Decimal::from_str("0.1").unwrap();195 195- 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();199 200 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 }204 205 #[test]206 fn 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());209 207- 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());216 214- 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");219 220- 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 );225 226- 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 lines308 "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 lines254 if param_space_size == 0 {255 return Vec::new();256 }257 258- // `--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 }266 267 let mut i = 0;139 unmodified lines