/*
 * @author Ranides Atterwim {@literal <ranides@gmail.com>}
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.time;

import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import static net.ranides.assira.junit.NewAssert.*;
import net.ranides.assira.text.StringUtils;

/**
 *
 * @author Ranides Atterwim {@literal <ranides@gmail.com>}
 */
public class TimeMetricTest {

    public static final double DELTA = 50.0;

    @Test
    public void testStop() throws InterruptedException {
        TimeMetric tm = new TimeMetric(TimeUnit.MILLISECONDS);
        tm.start();
        
        Thread.sleep(100);
        assertTimerText(100, "ms", tm);
        assertEquals(100.0, tm.stop(), DELTA);
        
        Thread.sleep(100);
        assertTimerText(200, "ms", tm);
        assertEquals(200.0, tm.stop(), DELTA);
        
        assertThrows(NoSuchElementException.class, ()->{
            System.out.printf("%d%n", tm.stop());
        });
    }
    
    private static void assertTimerText(int value, String unit, TimeMetric tm) {
        String text = tm.toString();
        assertTrue("Invalid unit: " + text, text.endsWith("ms"));
        int number = Integer.parseInt(StringUtils.before(text, "ms"));
        assertEquals(value, number, DELTA);
    }
    
}
