package com.atlassian.bitbucket.mesh.util;

import com.google.common.collect.ImmutableMap;
import org.junit.Test;

import java.time.Duration;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class NumberUtilsTest {

    private static final Map<Duration, String> TEST_DURATION_FORMATS = ImmutableMap.<Duration, String>builder()
            .put(Duration.ZERO, "0ms")
            .put(Duration.ofMillis(23L), "23ms")
            .put(Duration.ofMillis(3023L), "3s23ms")
            .put(Duration.ofMillis(30023L), "30s23ms")
            .put(Duration.ofMillis(90023L), "1m30s23ms")
            .put(Duration.ofMillis(7136913L), "1h58m56s")
            .put(Duration.ofMillis(279291023L), "3d5h34m")
            .build();

    private static final Map<Long, String> TEST_SIZE_FORMATS = ImmutableMap.<Long, String>builder()
            .put(0L, "0 B")
            .put(20L, "20 B")
            .put(2L * 1024L, "2.00 KB")
            .put(20L * 1024L, "20.00 KB")
            .put(20L * 1024L + 512L, "20.50 KB")
            .put(20L * 1024L * 1024L, "20.00 MB")
            .put(20L * 1024L * 1024L + 512L * 1024L, "20.50 MB")
            .put(20L * 1024L * 1024L * 1024L, "20.00 GB")
            .put(20L * 1024L * 1024L * 1024L * 1024L, "20480.00 GB")
            .build();

    @Test
    public void testFormatDuration() {
        for (Map.Entry<Duration, String> test : TEST_DURATION_FORMATS.entrySet()) {
            assertEquals(test.getValue(), NumberUtils.formatDuration(test.getKey()));
        }
    }

    @Test
    public void testFormatSize() {
        for (Map.Entry<Long, String> test : TEST_SIZE_FORMATS.entrySet()) {
            assertEquals(test.getValue(), NumberUtils.formatSize(test.getKey()));
        }
    }

}