mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-10 07:39:16 +00:00
ICU-22781 Fix and Add unit tests for withConstantDenominator in MeasureUnit
See #3342
This commit is contained in:
parent
2c667e31cf
commit
036919214c
2 changed files with 48 additions and 1 deletions
icu4j/main/core/src
|
@ -555,6 +555,10 @@ public class MeasureUnit implements Serializable {
|
|||
* @draft ICU 77
|
||||
*/
|
||||
public MeasureUnit withConstantDenominator(long denominator) {
|
||||
if (denominator < 0) {
|
||||
throw new IllegalArgumentException("Denominator cannot be negative");
|
||||
}
|
||||
|
||||
if (this.getComplexity() != Complexity.COMPOUND && this.getComplexity() != Complexity.SINGLE) {
|
||||
throw new UnsupportedOperationException(
|
||||
"Constant denominator can only be applied to COMPOUND & SINGLE units");
|
||||
|
@ -563,7 +567,7 @@ public class MeasureUnit implements Serializable {
|
|||
MeasureUnitImpl measureUnitImpl = getCopyOfMeasureUnitImpl();
|
||||
measureUnitImpl.setConstantDenominator(denominator);
|
||||
|
||||
measureUnitImpl.setComplexity(denominator == 0 && measureUnitImpl.getSingleUnits().size() == 1
|
||||
measureUnitImpl.setComplexity(denominator == 0 && measureUnitImpl.getSingleUnits().size() < 2
|
||||
? Complexity.SINGLE
|
||||
: Complexity.COMPOUND);
|
||||
|
||||
|
|
|
@ -27,6 +27,7 @@ import com.ibm.icu.impl.units.UnitsRouter;
|
|||
import com.ibm.icu.util.Measure;
|
||||
import com.ibm.icu.util.MeasureUnit;
|
||||
import com.ibm.icu.util.ULocale;
|
||||
import com.ibm.icu.util.MeasureUnit.Complexity;
|
||||
|
||||
public class UnitsTest {
|
||||
|
||||
|
@ -817,4 +818,46 @@ public class UnitsTest {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithConstantDenominator() {
|
||||
class TestCase {
|
||||
String unitIdentifier;
|
||||
long constantDenominator;
|
||||
Complexity expectedComplexity;
|
||||
|
||||
TestCase(String unitIdentifier, long constantDenominator, Complexity expectedComplexity) {
|
||||
this.unitIdentifier = unitIdentifier;
|
||||
this.constantDenominator = constantDenominator;
|
||||
this.expectedComplexity = expectedComplexity;
|
||||
}
|
||||
}
|
||||
|
||||
TestCase[] testCases = {
|
||||
new TestCase("meter-per-second", 100, Complexity.COMPOUND),
|
||||
new TestCase("meter-per-100-second", 0, Complexity.COMPOUND),
|
||||
new TestCase("portion", 100, Complexity.COMPOUND),
|
||||
new TestCase("portion-per-100", 0, Complexity.SINGLE),
|
||||
};
|
||||
|
||||
for (TestCase testCase : testCases) {
|
||||
MeasureUnit unit = MeasureUnit.forIdentifier(testCase.unitIdentifier);
|
||||
unit = unit.withConstantDenominator(testCase.constantDenominator);
|
||||
|
||||
long actualDenominator = unit.getConstantDenominator();
|
||||
Complexity actualComplexity = unit.getComplexity();
|
||||
|
||||
assertEquals(testCase.constantDenominator, actualDenominator);
|
||||
assertEquals(testCase.expectedComplexity, actualComplexity);
|
||||
}
|
||||
|
||||
// Test invalid withConstantDenominator
|
||||
MeasureUnit unit = MeasureUnit.forIdentifier("meter-per-second");
|
||||
try {
|
||||
unit = unit.withConstantDenominator(-1);
|
||||
fail("Expected IllegalArgumentException");
|
||||
} catch (IllegalArgumentException e) {
|
||||
// Expected exception
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue