Exercise 3 The following class provides the \"parseTimeToSeconds\" method, which
ID: 3715012 • Letter: E
Question
Exercise 3
The following class provides the "parseTimeToSeconds" method, which takes in a string representing a given time (e.g., "1:23:45 pm") and attempts to parse it and return the number of seconds past midnight that the specified time represents. Your job is to write test suites with various levels of coverage. Specifically, you should perform the following tasks:
Write a minimal test suite that provides full statement coverage (e.g., write the shortest test suite you can think of that provides full statement coverage).
Write a minimal test suite that provides full branch coverage.
Write a minimal test suite that provides full path coverage. If any paths are not possible to test, describe why.
public class TimeParser {
private static final int MINS_PER_HR = 60;
private static final int SECS_PER_MIN = 60;
private static final int MIN_HRS = 0;
private static final int MAX_HRS = 23;
private static final int MIN_MINS = 0;
private static final int MAX_MINS = 59;
private static final int MIN_SECS = 0;
private static final int MAX_SECS = 59; // ignore leap seconds
public static int parseTimeToSeconds(String time) throws NumberFormatException {
// Normalize the input (trim whitespace and make lower case)
time = time.trim().toLowerCase();
int firstColon = time.indexOf(':');
if (firstColon == -1) {
throw new NumberFormatException("Unrecognized time format");
}
int secondColon = time.indexOf(':', firstColon+1);
if (secondColon == -1) {
throw new NumberFormatException("Unrecognized time format");
}
// Interpret everything up to the first colon as the hour
int hours = Integer.valueOf(time.substring(0, firstColon));
// Interpret everything between the two colons as the minute
int minutes = Integer.valueOf(time.substring(firstColon+1, secondColon));
// Interpret the two characters after the second colon as the seconds
int seconds = Integer.valueOf(time.substring(secondColon+1, secondColon+3));
// Adjust hours if 'pm' specified
if (time.contains("pm")) {
hours += 12;
} else if (time.contains("am") && hours == 12) {
hours = 0;
}
// Range check the values
if ((hours < MIN_HRS || hours > MAX_HRS) ||
(minutes < MIN_MINS || minutes > MAX_MINS) ||
(seconds < MIN_SECS || seconds > MAX_SECS)) {
throw new IllegalArgumentException("Unacceptable time specified");
}
// Calculate number of seconds since midnight
return (((hours * MINS_PER_HR) + minutes) * SECS_PER_MIN) + seconds;
}
}
Explanation / Answer
//TimeParserTestPathCoverage
import org.junit.Test;
import static org.junit.Assert.*;
public class TimeParserTestPathCoverage {
@Test (expected = NumberFormatException.class)
public void testStatementCoverage() throws Exception {
TimeParser.parseTimeToSeconds("00");
}
@Test (expected = NumberFormatException.class)
public void testOneColon() throws Exception {
TimeParser.parseTimeToSeconds("1:00");
}
@Test (expected = IllegalArgumentException.class)
public void testInvalidHours() throws Exception {
TimeParser.parseTimeToSeconds("24:00:00");
}
@Test (expected = IllegalArgumentException.class)
public void testInvalidMinutes() throws Exception {
TimeParser.parseTimeToSeconds("00:61:00");
}
@Test (expected = IllegalArgumentException.class)
public void testInvalidSeconds() throws Exception {
TimeParser.parseTimeToSeconds("00:00:61");
}
@Test
public void testAM() throws Exception {
assertEquals(TimeParser.parseTimeToSeconds("12:00:00am"), 0);
}
@Test
public void testPM() throws Exception {
assertEquals(TimeParser.parseTimeToSeconds("1:00:00pm"), 46800);
}
@Test
public void test24Hour() throws Exception {
assertEquals(TimeParser.parseTimeToSeconds("13:00:00"), 46800);
}
}
----------------------------------------------------------------------------------------------
//TimeParserTestStatementCoverage
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.sql.Time;
import static org.junit.Assert.*;
public class TimeParserTestStatementCoverage {
@Test (expected = NumberFormatException.class)
public void testStatementCoverage() throws Exception {
TimeParser.parseTimeToSeconds("00");
}
@Test (expected = NumberFormatException.class)
public void testOneColon() throws Exception {
TimeParser.parseTimeToSeconds("1:00");
}
@Test (expected = IllegalArgumentException.class)
public void testInvalidTime() throws Exception {
TimeParser.parseTimeToSeconds("24:00:00");
}
@Test
public void testAM() throws Exception {
assertEquals(TimeParser.parseTimeToSeconds("12:00:00am"), 0);
}
@Test
public void testPM() throws Exception {
assertEquals(TimeParser.parseTimeToSeconds("1:00:00pm"), 46800);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.