Android Open Source - AstroPhysCalc Unit Util Test






From Project

Back to project page AstroPhysCalc.

License

The source code is released under:

Apache License

If you think the Android project AstroPhysCalc listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.astrophyscalc;
/*  w w w.  ja va2  s .c om*/
import java.util.HashSet;
import java.util.Set;

import org.junit.Assert;
import org.junit.Test;

public class UnitUtilTest extends Assert {

  @Test
  public void getBestUnitEmptyUnits() {

    // Empty unit set
    UnitExpression expr = UnitExpression.create(new HashSet<UnitAndDim>());
    ValueAndUnits vu = ValueAndUnits.create(1d, expr);

    Unit unit = UnitUtil.getBestUnit(vu, new HashSet<Unit>());;
    assertEquals(null, unit);

    // Not single unit of dim one
    expr = UnitExpression.create(TimeUnit.DAYS, 1, 2);
    vu = ValueAndUnits.create(0d, expr);

    Set<Unit> units = new HashSet<Unit>();
    units.add(TimeUnit.DAYS);
    units.add(TimeUnit.YEARS);

    String exceptionMsg = "";
    try {
      UnitUtil.getBestUnit(vu, units);
    }
    catch (IllegalArgumentException ex) {
      exceptionMsg = ex.getMessage();
    }
    assertEquals(UnitUtil.UNIT_EXPR_NOT_SINGLE_UNIT_DIM_ONE, exceptionMsg);
  }

  @Test
  public void getBestUnit() {

    UnitExpression expr = UnitExpression.createFromUnit(TimeUnit.DAYS);
    final ValueAndUnits vu = ValueAndUnits.create(0d, expr);

    final Set<Unit> unitSet = new HashSet<Unit>();
    unitSet.add(TimeUnit.DAYS);
    unitSet.add(TimeUnit.S);
    unitSet.add(TimeUnit.YEARS);

    final Unit bestUnit = UnitUtil.getBestUnit(vu, unitSet);

    assertEquals(TimeUnit.S, bestUnit);
  }

}




Java Source Code List

org.astrophyscalc.AstroPhysCalcActivity.java
org.astrophyscalc.CalcPage.java
org.astrophyscalc.CalcRow.java
org.astrophyscalc.Calculator.java
org.astrophyscalc.Constants.java
org.astrophyscalc.DimensionTest.java
org.astrophyscalc.Dimension.java
org.astrophyscalc.FractionTest.java
org.astrophyscalc.Fraction.java
org.astrophyscalc.LengthUnit.java
org.astrophyscalc.MassUnit.java
org.astrophyscalc.TimeUnitTest.java
org.astrophyscalc.TimeUnit.java
org.astrophyscalc.UnitAndDimTest.java
org.astrophyscalc.UnitAndDim.java
org.astrophyscalc.UnitExpressionTest.java
org.astrophyscalc.UnitExpression.java
org.astrophyscalc.UnitSelectionRule.java
org.astrophyscalc.UnitSelector.java
org.astrophyscalc.UnitSpinnerItem.java
org.astrophyscalc.UnitUtilTest.java
org.astrophyscalc.UnitUtil.java
org.astrophyscalc.Unit.java
org.astrophyscalc.ValueAndUnitsTest.java
org.astrophyscalc.ValueAndUnits.java