forked from code-clash/look-and-say
track total test duration
parent
e0d527e202
commit
8315022d0d
@ -1,54 +1,70 @@
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
import org.junit.jupiter.params.provider.CsvFileSource;
|
import org.junit.jupiter.params.provider.CsvFileSource;
|
||||||
|
|
||||||
|
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
class SolutionTest {
|
class SolutionTest {
|
||||||
|
|
||||||
|
static long totalDuration;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void beforeAll() {
|
||||||
|
totalDuration = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
static void afterAll() {
|
||||||
|
System.out.println( String.format( "All solutions took %01d.%03d secs", totalDuration / 1000, totalDuration % 1000 ));
|
||||||
|
}
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@CsvFileSource( resources = "testdata.csv" )
|
@CsvFileSource( resources = "testdata.csv" )
|
||||||
void main( final String input, final String expected ) throws IOException {
|
void main( final String input, final String expected ) throws IOException {
|
||||||
|
// keep original streams
|
||||||
InputStream oldIn = System.in;
|
InputStream oldIn = System.in;
|
||||||
PrintStream oldOut = System.out;
|
PrintStream oldOut = System.out;
|
||||||
PrintStream oldErr = System.err;
|
PrintStream oldErr = System.err;
|
||||||
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
InputStream in = new ByteArrayInputStream( input.getBytes() );
|
redirectStreams(
|
||||||
System.setIn( in );
|
new ByteArrayInputStream( input.getBytes( UTF_8 )),
|
||||||
|
new PrintStream( bos, true, UTF_8 ),
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
new PrintStream( new ByteArrayOutputStream(), true, UTF_8 )
|
||||||
System.setOut( new PrintStream( out ) );
|
);
|
||||||
|
|
||||||
ByteArrayOutputStream err = new ByteArrayOutputStream();
|
|
||||||
System.setErr( new PrintStream( err ) );
|
|
||||||
|
|
||||||
// start time tracking
|
// start time tracking
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
|
|
||||||
Solution.main( new String[0] );
|
Solution.main( new String[0] );
|
||||||
out.flush();
|
|
||||||
|
|
||||||
// stop time tracking
|
// stop time tracking
|
||||||
long end = System.currentTimeMillis();
|
long duration = System.currentTimeMillis() - start;
|
||||||
|
totalDuration += duration;
|
||||||
|
|
||||||
// restore streams
|
// restore streams
|
||||||
System.setIn( oldIn );
|
redirectStreams( oldIn, oldOut, oldErr );
|
||||||
System.setOut( oldOut );
|
|
||||||
System.setErr( oldErr );
|
|
||||||
|
|
||||||
System.out.println( String.format( "Solution took %01d.%03d secs", ( end - start ) / 1000, ( end - start ) % 1000 ) );
|
System.out.println( String.format( "Solution took %01d.%03d secs", duration / 1000, duration % 1000 ));
|
||||||
|
|
||||||
try (BufferedReader chk = new BufferedReader( new InputStreamReader( new ByteArrayInputStream( out.toByteArray() )))) {
|
try (BufferedReader chk = new BufferedReader( new InputStreamReader( new ByteArrayInputStream( bos.toByteArray() )))) {
|
||||||
assertEquals( expected, chk.readLine() );
|
assertEquals( expected, chk.readLine() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
// restore streams
|
// restore streams
|
||||||
System.setIn( oldIn );
|
redirectStreams( oldIn, oldOut, oldErr );
|
||||||
System.setOut( oldOut );
|
|
||||||
System.setErr( oldErr );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void redirectStreams( final InputStream input, final PrintStream output, final PrintStream error ) {
|
||||||
|
System.setIn( input );
|
||||||
|
System.setOut( output );
|
||||||
|
System.setErr( error );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue