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 concise textual and display strings (abbreviation correctness and spacing).(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) voidVerifies thatQuantity.getDisplayUnit()andQuantity.setDisplayUnit(org.djunits.unit.UnitInterface)round-trip correctly and that the setter is fluent (returnsthis).(package private) voidVerifiesequals/hashCodeconsider the SI value (bits) and the display unit.(package private) voidVerifiesQuantity.format(double)for the fixed-format range, E-notation range, and non-finite values.(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) voidEnsures that the SI value returned byQuantity.si()is invariant under changes to the display unit.(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.(package private) voidVerifiesQuantity.toStringSIPrefixed()both for an in-range SI-prefix selection and an out-of-range fallback to E-notation.(package private) voidVerifiesQuantity.toString()variants, including verbose/type flags and unit suppression.(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() -
displayUnitAccessors
@Test void displayUnitAccessors()Verifies thatQuantity.getDisplayUnit()andQuantity.setDisplayUnit(org.djunits.unit.UnitInterface)round-trip correctly and that the setter is fluent (returnsthis). -
siValueInvariant
@Test void siValueInvariant()Ensures that the SI value returned byQuantity.si()is invariant under changes to the display unit. -
getInUnitCurrentDisplay
@Test void getInUnitCurrentDisplay()VerifiesQuantity.getInUnit()using the current display unit.Case: switch from meters to kilometers and check numeric conversion.
-
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.
-
formatVariants
@Test void formatVariants()VerifiesQuantity.format(double)for the fixed-format range, E-notation range, and non-finite values. -
toStringVariants
@Test void toStringVariants()VerifiesQuantity.toString()variants, including verbose/type flags and unit suppression. -
toStringSIPrefixed
@Test void toStringSIPrefixed()VerifiesQuantity.toStringSIPrefixed()both for an in-range SI-prefix selection and an out-of-range fallback to E-notation. -
compactStrings
@Test void compactStrings()Verifies concise textual and display strings (abbreviation correctness and spacing). -
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)
-