if (source.complexity == UMeasureUnitComplexity::UMEASURE_UNIT_MIXED ||
target.complexity == UMeasureUnitComplexity::UMEASURE_UNIT_MIXED) {
- status = U_INTERNAL_PROGRAM_ERROR;
+ status = U_ARGUMENT_TYPE_MISMATCH;
return UNCONVERTIBLE;
}
if (this->conversionRate_.source.complexity == UMeasureUnitComplexity::UMEASURE_UNIT_MIXED ||
this->conversionRate_.target.complexity == UMeasureUnitComplexity::UMEASURE_UNIT_MIXED) {
- status = U_INTERNAL_PROGRAM_ERROR;
+ status = U_ARGUMENT_TYPE_MISMATCH;
return;
}
this->conversionRate_.target, ratesInfo, status);
if (U_FAILURE(status)) return;
if (unitsState == Convertibility::UNCONVERTIBLE) {
- status = U_INTERNAL_PROGRAM_ERROR;
+ status = U_ARGUMENT_TYPE_MISMATCH;
return;
}
if (firstUnit.complexity == UMeasureUnitComplexity::UMEASURE_UNIT_MIXED ||
secondUnit.complexity == UMeasureUnitComplexity::UMEASURE_UNIT_MIXED) {
- status = U_INTERNAL_PROGRAM_ERROR;
+ status = U_ARGUMENT_TYPE_MISMATCH;
return 0;
}
}
if (unitsState == Convertibility::UNCONVERTIBLE || unitsState == Convertibility::RECIPROCAL) {
- status = U_INTERNAL_PROGRAM_ERROR;
+ status = U_ARGUMENT_TYPE_MISMATCH;
return 0;
}
void unitCurrency();
void unitInflections();
void unitGender();
+ void unitNotConvertible();
void unitPercent();
void percentParity();
void roundingFraction();
TESTCASE_AUTO(unitCurrency);
TESTCASE_AUTO(unitInflections);
TESTCASE_AUTO(unitGender);
+ TESTCASE_AUTO(unitNotConvertible);
TESTCASE_AUTO(unitPercent);
if (!quick) {
// Slow test: run in exhaustive mode only
assertEquals("getGender for a genderless language", "", fn.getGender(status));
}
+void NumberFormatterApiTest::unitNotConvertible() {
+ IcuTestErrorCode status(*this, "unitNotConvertible");
+ const double randomNumber = 1234;
+
+ NumberFormatter::with()
+ .unit(MeasureUnit::forIdentifier("meter-and-liter", status))
+ .locale("en_US")
+ .formatDouble(randomNumber, status);
+ assertEquals(u"error must be returned", status.errorName(), u"U_ARGUMENT_TYPE_MISMATCH");
+
+ status.reset();
+ NumberFormatter::with()
+ .unit(MeasureUnit::forIdentifier("month-and-week", status))
+ .locale("en_US")
+ .formatDouble(randomNumber, status);
+ assertEquals(u"error must be returned", status.errorName(), u"U_ARGUMENT_TYPE_MISMATCH");
+
+ status.reset();
+ NumberFormatter::with()
+ .unit(MeasureUnit::forIdentifier("week-and-day", status))
+ .locale("en_US")
+ .formatDouble(randomNumber, status);
+ assertTrue(u"no error", !U_FAILURE(status));
+}
+
void NumberFormatterApiTest::unitPercent() {
assertFormatDescending(
u"Percent",
import java.util.HashMap;
import java.util.regex.Pattern;
+import com.ibm.icu.impl.IllegalIcuArgumentException;
import com.ibm.icu.util.MeasureUnit;
public class UnitsConverter {
*/
public UnitsConverter(MeasureUnitImpl source, MeasureUnitImpl target, ConversionRates conversionRates) {
Convertibility convertibility = extractConvertibility(source, target, conversionRates);
- // TODO(icu-units#82): throw exception if conversion between incompatible types was requested?
- assert (convertibility == Convertibility.CONVERTIBLE || convertibility == Convertibility.RECIPROCAL);
+ if (convertibility != Convertibility.CONVERTIBLE && convertibility != Convertibility.RECIPROCAL) {
+ throw new IllegalIcuArgumentException("input units must be convertible or reciprocal");
+ }
Factor sourceToBase = conversionRates.getFactorToBase(source);
Factor targetToBase = conversionRates.getFactorToBase(target);
assertEquals("getGender for a genderless language", "", fn.getGender());
}
+ @Test
+ public void unitNotConvertible() {
+ final double randomNumber = 1234;
+
+ try {
+ NumberFormatter.with()
+ .unit(MeasureUnit.forIdentifier("meter-and-liter"))
+ .locale(new ULocale("en_US"))
+ .format(randomNumber);
+ } catch (Exception e) {
+ assertEquals("error must be thrown", "class com.ibm.icu.impl.IllegalIcuArgumentException", e.getClass().toString());
+ }
+
+ try {
+ NumberFormatter.with()
+ .unit(MeasureUnit.forIdentifier("month-and-week"))
+ .locale(new ULocale("en_US"))
+ .format(randomNumber);
+ } catch (Exception e) {
+ assertEquals("error must be thrown", "class com.ibm.icu.impl.IllegalIcuArgumentException", e.getClass().toString());
+ }
+
+ try {
+ NumberFormatter.with()
+ .unit(MeasureUnit.forIdentifier("day-and-hour"))
+ .locale(new ULocale("en_US"))
+ .format(2.5);
+ } catch (Exception e) {
+ // No errors.
+ assert false;
+ }
+
+ }
+
@Test
public void unitPercent() {
assertFormatDescending(