Class QuantityTest
Quantity API using Length as a representative concrete type.
Goals and coverage:
- Display unit handling and SI value invariants
- Formatting and stringification variants
- Parsing helpers:
Quantity.valueOf(String, Quantity)andQuantity.of(double, String, Quantity) - Comparators and zero-comparison helpers
- Static helpers:
Quantity.interpolate(Quantity, Quantity, double),Quantity.max(Quantity, Quantity[]),Quantity.min(Quantity, Quantity[]),Quantity.sum(Quantity, Quantity[]),Quantity.mean(Quantity, Quantity[]) - Arithmetic producing
SIQuantity:Quantity.multiply(Quantity),Quantity.divide(Quantity),Quantity.reciprocal() - Conversion to a known quantity via
Quantity.as(org.djunits.unit.UnitInterface) equals/hashCodecontract- Special branch in
Quantity.valueOf(String, Quantity)forUnitless(empty unit string accepted), exercised usingDimensionless
Deliberately out of scope: We do not re-test domain specifics of Length or any other derived type;
they have their own dedicated unit tests. Here, Length merely provides a concrete vehicle for the abstract API.
Locale pinning: The suite pins Locale.Category.FORMAT to Locale.US to ensure deterministic
behavior of number formatting and parsing; the original locale is restored afterwards. Copyright (c) 2025-2026 Delft
University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information
https://djunits.org. The DJUNITS project is distributed under a
three-clause BSD-style license.
- Author:
- Alexander Verbraeck (specifications); Test implementation by Copilot.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static classQuantity class for test. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) voidVerifies abs and negate.(package private) voidVerifiesQuantity.as(org.djunits.unit.UnitInterface)converts to a correctly typed quantity while preserving the SI value and replacing the display unit with the requested unit.(package private) voidVerifies theComparablecontract forQuantity.compareTo(Quantity).(package private) voidVerifies pairwise comparisons (lt/le/gt/ge/eq/ne) based on SI values.(package private) voidVerifies that theQuantityconstructor rejects anulldisplay unit.(package private) voidVerifiesequals/hashCodeconsider the SI value (bits) and the display unit.(package private) voidVerifiesQuantity.getInUnit()using the current display unit.(package private) voidVerifiesQuantity.getInUnit(org.djunits.unit.UnitInterface)for explicit target units.(package private) voidTestsQuantity.getName()for both the simple case (single leading uppercase, e.g.(package private) voidConfirms that the pretty/localized name as returned byQuantity.getName()is non-empty.(package private) void(package private) voidmaxMin()VerifiesQuantity.max(Quantity, Quantity[])andQuantity.min(Quantity, Quantity[])selection.(package private) voidVerifies thatNumber-derived conversions reflect the SI value semantics (rounding for int/long).(package private) voidofParses()Verifies successful parsing viaQuantity.of(double, String, Quantity)and its error branches fornullparameters, empty unit string, and unknown unit.(package private) static voidPin the defaultLocalefor predictable formatting/parsing behavior.(package private) static voidRestore the defaultLocalefor the FORMAT category.(package private) voidVerifiesQuantity.multiply(Quantity),Quantity.divide(Quantity), andQuantity.reciprocal()produceSIQuantitywith correct SI values (unit algebra correctness is validated in SIQuantity/unit tests).(package private) voidEnsures thatQuantity.siUnit()returns the correct SI unit for the quantity family.(package private) voidsumMean()VerifiesQuantity.sum(Quantity, Quantity[])andQuantity.mean(Quantity, Quantity[])and that the display unit of the first argument is preserved in the result.voidTest multiple format settings combined.voidTest defaulttoString()for a simple length.voidTest magnitude change with decimals and width.voidTest locale influence on decimal separator.voidTest negative quantity formatting.voidTesttoString(Unit)converts the unit for display.(package private) voidUnitless special-case coverage: When the example isDimensionless, an empty (or whitespace-only) unit token must be accepted and interpreted asUnitless.BASE.(package private) voidVerifies error handling inQuantity.valueOf(String, Quantity).(package private) voidVerifies successful parsing viaQuantity.valueOf(String, Quantity)with and without a space between number and unit.(package private) voidVerifies zero-comparison helpers (lt0/le0/gt0/ge0/eq0/ne0).
-
Constructor Details
-
QuantityTest
public QuantityTest()
-
-
Method Details
-
pinLocale
@BeforeAll static void pinLocale()Pin the defaultLocalefor predictable formatting/parsing behavior. -
restoreLocale
@AfterAll static void restoreLocale()Restore the defaultLocalefor the FORMAT category. -
constructorNullDisplayUnitThrows
@Test void constructorNullDisplayUnitThrows() -
getInUnitCurrentDisplay
@Test void getInUnitCurrentDisplay()VerifiesQuantity.getInUnit()using the current display unit. -
getInUnitExplicitTarget
@Test void getInUnitExplicitTarget()VerifiesQuantity.getInUnit(org.djunits.unit.UnitInterface)for explicit target units. -
getNameIsNonEmpty
@Test void getNameIsNonEmpty()Confirms that the pretty/localized name as returned byQuantity.getName()is non-empty. -
siUnitMatches
@Test void siUnitMatches()Ensures thatQuantity.siUnit()returns the correct SI unit for the quantity family. -
numberConversions
@Test void numberConversions()Verifies thatNumber-derived conversions reflect the SI value semantics (rounding for int/long). -
comparisons
@Test void comparisons()Verifies pairwise comparisons (lt/le/gt/ge/eq/ne) based on SI values. -
zeroComparisons
@Test void zeroComparisons()Verifies zero-comparison helpers (lt0/le0/gt0/ge0/eq0/ne0). -
compareToContract
@Test void compareToContract()Verifies theComparablecontract forQuantity.compareTo(Quantity). -
valueOfParses
@Test void valueOfParses()Verifies successful parsing viaQuantity.valueOf(String, Quantity)with and without a space between number and unit. -
valueOfInvalids
@Test void valueOfInvalids()Verifies error handling inQuantity.valueOf(String, Quantity).nullinput or example- empty string
- missing unit (non-Unitless type)
- unknown unit
-
ofParses
@Test void ofParses()Verifies successful parsing viaQuantity.of(double, String, Quantity)and its error branches fornullparameters, empty unit string, and unknown unit. -
valueOfAcceptsEmptyUnitForDimensionless
@Test void valueOfAcceptsEmptyUnitForDimensionless()Unitless special-case coverage: When the example isDimensionless, an empty (or whitespace-only) unit token must be accepted and interpreted asUnitless.BASE.Also verifies that a non-empty, unknown unit still fails for dimensionless parsing.
-
testDefaultToString
@Test @DisplayName("Default toString() uses default number and display unit") public void testDefaultToString()Test defaulttoString()for a simple length. -
testToStringWithTargetUnit
@Test @DisplayName("toString(Unit) converts value and displays target unit") public void testToStringWithTargetUnit()TesttoString(Unit)converts the unit for display. -
testNegativeQuantity
@Test @DisplayName("Negative quantities keep sign and formatting") public void testNegativeQuantity()Test negative quantity formatting. -
testLargeMagnitudeFixed
@Test @DisplayName("Large magnitude with explicit number format") public void testLargeMagnitudeFixed()Test magnitude change with decimals and width. -
testCombinedFormat
@Test @DisplayName("Combined settings for number, unit and quantity") public void testCombinedFormat()Test multiple format settings combined. -
testLocaleFormat
@Test @DisplayName("Locale format affects decimal separator") public void testLocaleFormat()Test locale influence on decimal separator. -
interpolateQuantity
@Test void interpolateQuantity()VerifiesQuantity.interpolate(Quantity, Quantity, double). For:- a valid ratio in [0, 1] (result SI value and display unit of the first argument)
- out-of-bounds ratio (< 0 or > 1) raising
IllegalArgumentException
-
maxMin
@Test void maxMin()VerifiesQuantity.max(Quantity, Quantity[])andQuantity.min(Quantity, Quantity[])selection. -
sumMean
@Test void sumMean()VerifiesQuantity.sum(Quantity, Quantity[])andQuantity.mean(Quantity, Quantity[])and that the display unit of the first argument is preserved in the result. -
absNegate
@Test void absNegate()Verifies abs and negate. -
siQuantityArithmetic
@Test void siQuantityArithmetic()VerifiesQuantity.multiply(Quantity),Quantity.divide(Quantity), andQuantity.reciprocal()produceSIQuantitywith correct SI values (unit algebra correctness is validated in SIQuantity/unit tests). -
asKnownQuantity
@Test void asKnownQuantity()VerifiesQuantity.as(org.djunits.unit.UnitInterface)converts to a correctly typed quantity while preserving the SI value and replacing the display unit with the requested unit. -
equalsHashCode
@Test void equalsHashCode()Verifiesequals/hashCodeconsider the SI value (bits) and the display unit. -
getNameFormatsPrettyForQuantityAndSIQuantity
@Test void getNameFormatsPrettyForQuantityAndSIQuantity()TestsQuantity.getName()for both the simple case (single leading uppercase, e.g. "Length" -> "Length") and the camel-case/internal-uppercase case usingSIQuantity(e.g. "SIQuantity" -> "S i quantity").Why SIQuantity? The algorithm in
Quantity.getName()inserts a space before each uppercase letter after the first character and lower-cases that letter. UsingSIQuantityguarantees we hit that branch: 'S' (kept as-is), then 'I' (→ " i"), then 'Q' (→ " q"), then "uantity".Expected:
new Length(...).getName()yields"Length"(no spaces added)new SIQuantity(...).getName()yields"S i quantity"(spaces and lower-casing applied)
-