package com.capgemini.framework.common;

import org.testng.annotations.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class DoubleFormatterTest {

  @Test
  public void toSignificantDigitsTest() {
    // Some Integers
    tryToFormat(0, 1, "0"); // Simple integer
    tryToFormat(20001054, 9, "20001054"); // Simple integer
    tryToFormat(20001054, 8, "20001054"); // Simple integer, exact size
    tryToFormat(20001054, 3, "20000000"); // Integer, with truncation of non-significant digits
    tryToFormat(20001054, 6, "20001100"); // Integer, with rounding of non-significant digits
    tryToFormat(-20001054, 9, "-20001054"); // [Negative] Simple integer
    tryToFormat(-20001054, 8, "-20001054"); // [Negative] Simple integer, exact size
    tryToFormat(-20001054, 3, "-20000000"); // [Negative] Integer, with truncation of non-significant digits
    tryToFormat(-20001054, 6, "-20001100"); // [Negative] Integer, with rounding of non-significant digits

    // Some positive powers
    tryToFormat(200010.54, 9, "200010.54"); // Simple real number
    tryToFormat(200010.54, 8, "200010.54"); // Simple real number, exact size
    tryToFormat(200010.54, 3, "200000"); // Real number, with truncation of non-significant digits before the coma
    tryToFormat(200010.54, 7, "200010.5"); // Real number, with truncation of non-significant digits after the coma
    tryToFormat(200010.56, 7, "200010.6"); // Real number, with rounding of non-significant digits after the coma
    tryToFormat(-200010.54, 9, "-200010.54"); // [Negative] Simple real number
    tryToFormat(-200010.54, 8, "-200010.54"); // [Negative] Simple real number, exact size
    tryToFormat(-200010.54, 3, "-200000"); // [Negative] Real number, with truncation of non-significant digits before the coma [NOTE the zeroes and the coma must go away]
    tryToFormat(-200010.54, 7, "-200010.5"); // [Negative] Real number, with truncation of non-significant digits after the coma
    tryToFormat(-200010.56, 7, "-200010.6"); // [Negative] Real number, with rounding of non-significant digits after the coma

    // Very high number of significant digits, must still be correct (16 is the max)
    tryToFormat(-100.0002005105412345, 16, "-100.0002005105412"); // [Negative]

    // Extremely high number of significant digits, must fail due to Long being too short :D
    tryToFormat(-100.0002005105412345, 25, "-100.0002005105412"); // [Negative]

    // Some negative powers
    tryToFormat(0.0002, 15, "0.0002"); // Test simple printing
    tryToFormat(0.00020051024, 6, "0.00020051"); // Test truncating [NOTE the zeroes after the last digit must go away]
    tryToFormat(0.00020051054, 3, "0.000201"); // Test rounding up the last significant digit
    tryToFormat(-0.0002, 6, "-0.0002"); // [Negative] Test simple printing
    tryToFormat(-0.00020051024, 6, "-0.00020051"); // [Negative] Test truncating [NOTE the zeroes after the last digit must go away]
    tryToFormat(-0.00020051054, 3, "-0.000201"); // [Negative] Test rounding up the last significant digit
  }

  private void tryToFormat(final double value, final int significantDigits, final String expectedResult) {
    assertThat(DoubleFormatter.toSignificantDigits(value, significantDigits)).isEqualTo(expectedResult);
  }
}
