/** * Tests the {@link FormatUtils#formatTimeTakenNs(long, String)} method. */ @Test public void formatTimeTakenNs() { class TestCase { Pattern mExpected; String mInputMessage; public TestCase(String expectedRE, String inputMessage) { mExpected = Pattern.compile(expectedRE); mInputMessage = inputMessage; } } List<TestCase> testCases = new ArrayList<>(); testCases.add(new TestCase("^Task A took (.*) ns.$", "Task A")); testCases.add(new TestCase("^Task B took (.*) ns.$", "Task B")); long delta = 100000000; for (TestCase testCase : testCases) { String result = FormatUtils.formatTimeTakenNs(System.nanoTime() - delta, testCase.mInputMessage); Matcher match = testCase.mExpected.matcher(result); Assert.assertTrue(match.matches()); Assert.assertTrue(delta <= Long.parseLong(match.group(1))); Assert.assertTrue(Long.parseLong(match.group(1)) <= 2 * delta); } }