Best EvoMaster code snippet using org.evomaster.client.java.instrumentation.coverage.methodreplacement.DistanceHelper.distanceToRange
Source:DateTimeParsingUtils.java
...76 char c = input.charAt(i);77 //format HH:MM:SS78 //let's simplify and only allow 00:00:00 to 19:59:5979 if (i == 0) {80 distance += distanceToRange(c, '0', '1');81 } else if (i == 1 || i == 4 || i == 7) {82 distance += distanceToRange(c, '0', '9');83 } else if (i == 2 || i == 5) {84 distance += distanceToChar(c, ':');85 } else if (i == 3 || i == 6) {86 distance += distanceToRange(c, '0', '5');87 } else {88 distance += MAX_CHAR_DISTANCE;89 }90 }91 if (input.length() < ISO_LOCAL_TIME_LENGTH) {92 //too short93 distance += (MAX_CHAR_DISTANCE * (ISO_LOCAL_TIME_LENGTH - input.length()));94 }95 if(distance < 0){96 distance = Long.MAX_VALUE; // overflow97 }98 //recall h in [0,1] where the highest the distance the closer to 099 double h = DistanceHelper.heuristicFromScaledDistanceWithBase(base, distance);100 return h;101 }102 /**103 * returns a value that represents how close is the value to the format YYYY-MM-DD104 *105 * @param input106 * @return107 */108 public static double getHeuristicToISOLocalDateTimeParsing(CharSequence input) {109 if (input == null) {110 return H_REACHED_BUT_NULL;111 }112 try {113 LocalDateTime.parse(input);114 return H_PARSED_OK;115 } catch (DateTimeParseException ex) {116 return getHeuristicToISOLocalDateTime(input);117 }118 }119 /**120 * Returns the approximate (i.e. simplified) distance of a string121 * to the format YYYY-MM-DDTHH:MM (T is case insensitive)122 *123 * For simplification, only the range of days between 01 and 28,124 * and months between 01 and 09, and hours between 00 and 19.125 *126 * @param input a non-null string that fails to parse in127 * the YYYY-MM-DDTHH:MM format128 * @return129 */130 private static double getHeuristicToISOLocalDateTime(CharSequence input) {131 Objects.requireNonNull(input);132 final double base = H_NOT_NULL;133 long distance = 0;134 for (int i = 0; i < input.length(); i++) {135 char c = input.charAt(i);136 // TODO: The code below can be refactored with class DateFormatClassReplacement137 //format YYYY-MM-DDT138 if (i >= 0 && i <= 3) {139 //any Y value is ok140 distance += distanceToDigit(c);141 } else if (i == 4 || i == 7) {142 distance += distanceToChar(c, '-');143 } else if (i == 5) {144 //let's simplify and only allow 01 to 09 for MM145 distance += distanceToChar(c, '0');146 } else if (i == 6) {147 distance += distanceToRange(c, '1', '9');148 } else if (i == 8) {149 //let's simplify and only allow 01 to 28150 distance += distanceToRange(c, '0', '2');151 } else if (i == 9) {152 distance += distanceToRange(c, '1', '8');153 } else if (i == 10) {154 // The letter 'T'. Parsing is case insensitive.155 distance += Math.min(distanceToChar(c, 'T'), distanceToChar(c, 't'));156 } else if (i == 11) {157 distance += distanceToRange(c, '0', '1');158 } else if (i == 12 || i == 15 || i == 18) {159 distance += distanceToRange(c, '0', '9');160 } else if (i == 13 || i == 16) {161 distance += distanceToChar(c, ':');162 } else if (i == 14 || i == 17) {163 distance += distanceToRange(c, '0', '5');164 } else {165 distance += MAX_CHAR_DISTANCE;166 }167 }168 if (input.length() < ISO_LOCAL_DATE_TIME_LENGTH) {169 //too short170 distance += (MAX_CHAR_DISTANCE * (ISO_LOCAL_DATE_TIME_LENGTH - input.length()));171 }172 if(distance < 0){173 distance = Long.MAX_VALUE; // overflow174 }175 //recall h in [0,1] where the highest the distance the closer to 0176 double h = DistanceHelper.heuristicFromScaledDistanceWithBase(base, distance);177 return h;178 }179 /**180 * returns how close the input was to HH:MM or HH:MM:SS.181 * If the input fails to parse in any of those formats, the182 * distance only considers hours in the 00 to 19 range.183 *184 * @param input185 * @return186 */187 public static double getHeuristicToISOLocalTimeParsing(CharSequence input) {188 if (input == null) {189 return H_REACHED_BUT_NULL;190 }191 /*192 * returns the highest value (i.e. closer) to actually193 * satisfy the parsing with or without seconds194 */195 return Math.max(196 getHeuristicToLocalTimeWithoutSecondsParsing(input),197 getHeuristicToLocalTimeWithSecondsParsing(input));198 }199 /**200 * returns the heuristic value in [0,1] that201 * represents how close is the value to the format YYYY-MM-DD202 *203 * @param input204 * @return205 */206 public static double getHeuristicToISOLocalDateParsing(CharSequence input) {207 if (input == null) {208 return H_REACHED_BUT_NULL;209 }210 try {211 /*212 due to the simplification later on (i.e. not all valid local dates213 are considered, only a subrange), still must make sure to get a 1214 if no exception is thrown215 */216 LocalDate.parse(input);217 return H_PARSED_OK;218 } catch (DateTimeParseException e) {219 return getHeuristicToISOLocalDate(input);220 }221 }222 /**223 * Returns the approximate (i.e. simplified) heuristic value of a string224 * to the format YYYY-MM-DD.225 * For simplification, only the range of days between 01 and 28,226 * and months between 01 and 09.227 *228 * @param input a non-null string that fails to parse in229 * the YYYY-MM-DD format230 * @return231 */232 private static double getHeuristicToISOLocalDate(CharSequence input) {233 Objects.requireNonNull(input);234 //nothing to do235 final double base = H_NOT_NULL;236 long distance = 0;237 for (int i = 0; i < input.length(); i++) {238 char c = input.charAt(i);239 // TODO: The code below can be refactored with class DateFormatClassReplacement240 //format YYYY-MM-DD241 if (i >= 0 && i <= 3) {242 //any Y value is ok243 distance += distanceToDigit(c);244 } else if (i == 4 || i == 7) {245 distance += distanceToChar(c, '-');246 } else if (i == 5) {247 //let's simplify and only allow 01 to 09 for MM248 distance += distanceToChar(c, '0');249 } else if (i == 6) {250 distance += distanceToRange(c, '1', '9');251 } else if (i == 8) {252 //let's simplify and only allow 01 to 28253 distance += distanceToRange(c, '0', '2');254 } else if (i == 9) {255 distance += distanceToRange(c, '1', '8');256 } else {257 distance += MAX_CHAR_DISTANCE;258 }259 }260 if (input.length() < ISO_LOCAL_DATE_LENGTH) {261 //too short262 distance += (MAX_CHAR_DISTANCE * (ISO_LOCAL_DATE_LENGTH - input.length()));263 }264 if(distance < 0){265 distance = Long.MAX_VALUE; // overflow266 }267 //recall h in [0,1] where the highest the distance the closer to 0268 double h = DistanceHelper.heuristicFromScaledDistanceWithBase(base, distance);269 return h;270 }271 /**272 * returns a value that represents how close is the value to the format YYYY-MM-DD273 *274 * @param input275 * @return276 */277 public static double getHeuristicToDateTimeParsing(CharSequence input) {278 if (input == null) {279 return H_REACHED_BUT_NULL;280 }281 try {282 /*283 due to the simplification later on (i.e. only some valid dates are considered,284 but not all), still must make sure to get a 1 if no exception is thrown285 */286 new SimpleDateFormat(DATE_TIME_NO_SECONDS_FORMAT).parse(input.toString());287 return H_PARSED_OK;288 } catch (ParseException e) {289 return getHeuristicToDateTime(input);290 }291 }292 /**293 * Returns the approximate (i.e. simplified) heuristic value for a string294 * to the format YYYY-MM-DD HH:MM.295 *296 * For simplification, only the range of days between 01 and 28,297 * and months between 01 and 09, and hours between 00 and 19.298 *299 * @param input a non-null string that fails to parse in300 * the YYYY-MM-DD HH:MM format301 * @return302 */303 private static double getHeuristicToDateTime(CharSequence input) {304 Objects.requireNonNull(input);305 final double base = H_NOT_NULL;306 long distance = 0;307 for (int i = 0; i < input.length(); i++) {308 char c = input.charAt(i);309 //format YYYY-MM-DD HH:MM310 if (i >= 0 && i <= 3) {311 //any Y value is ok312 distance += distanceToDigit(c);313 } else if (i == 4 || i == 7) {314 distance += distanceToChar(c, '-');315 } else if (i == 5) {316 //let's simplify and only allow 01 to 09 for MM317 distance += distanceToChar(c, '0');318 } else if (i == 6) {319 distance += distanceToRange(c, '1', '9');320 } else if (i == 8) {321 //let's simplify and only allow 01 to 28 for DD322 distance += distanceToRange(c, '0', '2');323 } else if (i == 9) {324 distance += distanceToRange(c, '1', '8');325 } else if (i == 10) {326 distance += distanceToChar(c, ' ');327 } else if (i == 11) {328 // let's simplify and only allow 00 to 19 for HH329 distance += distanceToRange(c, '0', '1');330 } else if (i == 12) {331 distance += distanceToRange(c, '0', '9');332 } else if (i == 13) {333 distance += distanceToChar(c, ':');334 } else if (i == 14) {335 // allow 00 to 59 for MM336 distance += distanceToRange(c, '0', '5');337 } else if (i == 15) {338 distance += distanceToRange(c, '0', '9');339 } else {340 distance += MAX_CHAR_DISTANCE;341 }342 }343 int requiredLength = "YYYY-MM-DD HH:MM".length();344 if (input.length() < requiredLength) {345 //too short346 distance += (MAX_CHAR_DISTANCE * (requiredLength - input.length()));347 }348 if(distance < 0){349 distance = Long.MAX_VALUE; // overflow350 }351 //recall h in [0,1] where the highest the distance the closer to 0352 double h = DistanceHelper.heuristicFromScaledDistanceWithBase(base, distance);...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!