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

import java.util.IllegalFormatConversionException;
import static net.ranides.assira.junit.NewAssert.*;
import org.junit.Test;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class FormatNumberTest {
    
    public FormatNumberTest() {
    }

    @Test
    public void testGroup() {
        assertEquals("3", FormatNumber.group(3));
        assertEquals("12", FormatNumber.group(12));
        assertEquals("127", FormatNumber.group(127));
        assertEquals("1'270", FormatNumber.group(1270));
        assertEquals("1'270'000", FormatNumber.group(1270000));
    }
    
    @Test
    public void testGroup_long() {
        assertEquals("3", FormatNumber.group(3L));
        assertEquals("12", FormatNumber.group(12L));
        assertEquals("127", FormatNumber.group(127L));
        assertEquals("1'270", FormatNumber.group(1270L));
        assertEquals("1'270'000", FormatNumber.group(1270000L));
        assertEquals("1'270'000'542", FormatNumber.group(1270000542));
        assertEquals("1'270'000'542'300", FormatNumber.group(1270000542300L));
    }
    
    @Test
    public void testAsEngineerM() {
        assertEquals("1.20k", FormatNumber.asEngineer("%.2M", 1200));
        assertEquals("12.00", FormatNumber.asEngineer("%.2M", 12));
        assertEquals("100.00m", FormatNumber.asEngineer("%.2M", 0.1));
        assertEquals("120.00m", FormatNumber.asEngineer("%.2M", 0.12));
        assertEquals("51.00m", FormatNumber.asEngineer("%.2M", 0.051));
        assertEquals("5.10m", FormatNumber.asEngineer("%.2M", 0.0051));
        assertEquals("510.00μ", FormatNumber.asEngineer("%.2M", 0.00051));
        
        assertEquals("-1.20k", FormatNumber.asEngineer("%.2M", -1200));
        assertEquals("-12.00", FormatNumber.asEngineer("%.2M", -12));
        assertEquals("-100.00m", FormatNumber.asEngineer("%.2M", -0.1));
        assertEquals("-120.00m", FormatNumber.asEngineer("%.2M", -0.12));
        assertEquals("-51.00m", FormatNumber.asEngineer("%.2M", -0.051));
        assertEquals("-5.10m", FormatNumber.asEngineer("%.2M", -0.0051));
        assertEquals("-510.00μ", FormatNumber.asEngineer("%.2M", -0.00051));
        
        assertThrows(IllegalFormatConversionException.class, ()->{
            FormatNumber.asEngineer("%.2f", 0.00051);
        });
    }
    
    @Test
    public void testAsEngineerE() {
        assertEquals("1.20E3", FormatNumber.asEngineer("%.2E", 1200));
        assertEquals("12.00E0", FormatNumber.asEngineer("%.2E", 12));
        assertEquals("100.00E-3", FormatNumber.asEngineer("%.2E", 0.1));
        assertEquals("120.00E-3", FormatNumber.asEngineer("%.2E", 0.12));
        assertEquals("51.00E-3", FormatNumber.asEngineer("%.2E", 0.051));
        assertEquals("5.10E-3", FormatNumber.asEngineer("%.2E", 0.0051));
        assertEquals("510.00E-6", FormatNumber.asEngineer("%.2E", 0.00051));
        
        assertEquals("-1.20E3", FormatNumber.asEngineer("%.2E", -1200));
        assertEquals("-12.00E0", FormatNumber.asEngineer("%.2E", -12));
        assertEquals("-100.00E-3", FormatNumber.asEngineer("%.2E", -0.1));
        assertEquals("-120.00E-3", FormatNumber.asEngineer("%.2E", -0.12));
        assertEquals("-51.00E-3", FormatNumber.asEngineer("%.2E", -0.051));
        assertEquals("-5.10E-3", FormatNumber.asEngineer("%.2E", -0.0051));
        assertEquals("-510.00E-6", FormatNumber.asEngineer("%.2E", -0.00051));
        
        assertThrows(IllegalFormatConversionException.class, ()->{
            FormatNumber.asEngineer("%.2f", 0.00051);
        });
    }
    
    @Test
    public void testAsEngineerS() {
        assertEquals("12.53Mi", FormatNumber.asEngineer("%.2S", 12.527*1024*1024));
        assertEquals("11.72Ki", FormatNumber.asEngineer("%.2S", 12000));
        assertEquals("1.17Ki", FormatNumber.asEngineer("%.2S", 1200));
        assertEquals("12.00", FormatNumber.asEngineer("%.2S", 12));
        assertEquals("0.10", FormatNumber.asEngineer("%.2S", 0.1));
        assertEquals("0.12", FormatNumber.asEngineer("%.2S", 0.12));
        assertEquals("0.05", FormatNumber.asEngineer("%.2S", 0.051));
        assertEquals("0.01", FormatNumber.asEngineer("%.2S", 0.0051));
        assertEquals("0.00", FormatNumber.asEngineer("%.2S", 0.00051));
    }
    
    @Test
    public void testAsEngineerB() {
        assertEquals("12.53B6", FormatNumber.asEngineer("%.2B", 12.527*1024*1024));
        assertEquals("11.72B3", FormatNumber.asEngineer("%.2B", 12000));
        assertEquals("1.17B3", FormatNumber.asEngineer("%.2B", 1200));
        assertEquals("12.00B0", FormatNumber.asEngineer("%.2B", 12));
        assertEquals("0.01B0", FormatNumber.asEngineer("%.2B", 0.0051));
        assertEquals("0.00B0", FormatNumber.asEngineer("%.2B", 0.00051));
        assertEquals("0B0", FormatNumber.asEngineer("%.0B", 0.00051));
        assertEquals("72B6", FormatNumber.asEngineer("%.0B", 72*1024*1024));
        assertEquals("12B9", FormatNumber.asEngineer("%.0B", 12*1024*1024*1024L));
        assertEquals("12B12", FormatNumber.asEngineer("%.0B", 12*1024L*1024L*1024L*1024L));
    }
    
    @Test
    public void testAsEngineerK() {
        assertEquals("12.53MM", FormatNumber.asEngineer("%.2K", 12.527*1024*1024));
        assertEquals("11.72KK", FormatNumber.asEngineer("%.2K", 12000));
        assertEquals("1.17KK", FormatNumber.asEngineer("%.2K", 1200));
        assertEquals("12.00", FormatNumber.asEngineer("%.2K", 12));
        assertEquals("0.01", FormatNumber.asEngineer("%.2K", 0.0051));
        assertEquals("0.00", FormatNumber.asEngineer("%.2K", 0.00051));
        assertEquals("0", FormatNumber.asEngineer("%.0K", 0.00051));
        assertEquals("72MM", FormatNumber.asEngineer("%.0K", 72*1024*1024));
        assertEquals("12GG", FormatNumber.asEngineer("%.0K", 12*1024*1024*1024L));
        assertEquals("12TT", FormatNumber.asEngineer("%.0K", 12*1024L*1024L*1024L*1024L));
    }
    
    @Test
    public void testAsEngineerFormat() {
        assertEquals("001.27k", FormatNumber.asEngineer("%06.2M", 1273));
        assertEquals("01.273k", FormatNumber.asEngineer("%06.3M", 1273));
        assertEquals("1.273000k", FormatNumber.asEngineer("%06M", 1273));
        assertEquals("001.273000k", FormatNumber.asEngineer("%010M", 1273));
        
        assertEquals("1.273000k", FormatNumber.asEngineer("%M", 1273));
        assertEquals("1.273000E3", FormatNumber.asEngineer("%E", 1273));
        assertEquals("1.500000Ki", FormatNumber.asEngineer("%S", 1536));
    }
        
    
}
