Class QuantityTest

java.lang.Object
org.djunits.quantity.def.QuantityTest

public class QuantityTest extends Object
Unit tests for the abstract Quantity API using Length as a representative concrete type.

Goals and coverage:

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.
  • Constructor Details

    • QuantityTest

      public QuantityTest()
  • Method Details

    • pinLocale

      @BeforeAll static void pinLocale()
      Pin the default Locale for predictable formatting/parsing behavior.
    • restoreLocale

      @AfterAll static void restoreLocale()
      Restore the default Locale for the FORMAT category.
    • constructorNullDisplayUnitThrows

      @Test void constructorNullDisplayUnitThrows()
      Verifies that the Quantity constructor rejects a null display unit.

      Expected: NullPointerException.

    • displayUnitAccessors

      @Test void displayUnitAccessors()
      Verifies that Quantity.getDisplayUnit() and Quantity.setDisplayUnit(org.djunits.unit.UnitInterface) round-trip correctly and that the setter is fluent (returns this).
    • siValueInvariant

      @Test void siValueInvariant()
      Ensures that the SI value returned by Quantity.si() is invariant under changes to the display unit.
    • getInUnitCurrentDisplay

      @Test void getInUnitCurrentDisplay()
      Verifies Quantity.getInUnit() using the current display unit.

      Case: switch from meters to kilometers and check numeric conversion.

    • getInUnitExplicitTarget

      @Test void getInUnitExplicitTarget()
      Verifies Quantity.getInUnit(org.djunits.unit.UnitInterface) for explicit target units.
    • getNameIsNonEmpty

      @Test void getNameIsNonEmpty()
      Confirms that the pretty/localized name as returned by Quantity.getName() is non-empty.
    • siUnitMatches

      @Test void siUnitMatches()
      Ensures that Quantity.siUnit() returns the correct SI unit for the quantity family.
    • numberConversions

      @Test void numberConversions()
      Verifies that Number-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 the Comparable contract for Quantity.compareTo(Quantity).
    • valueOfParses

      @Test void valueOfParses()
      Verifies successful parsing via Quantity.valueOf(String, Quantity) with and without a space between number and unit.
    • valueOfInvalids

      @Test void valueOfInvalids()
      Verifies error handling in Quantity.valueOf(String, Quantity).
      • null input or example
      • empty string
      • missing unit (non-Unitless type)
      • unknown unit
    • ofParses

      @Test void ofParses()
      Verifies successful parsing via Quantity.of(double, String, Quantity) and its error branches for null parameters, empty unit string, and unknown unit.
    • valueOfAcceptsEmptyUnitForDimensionless

      @Test void valueOfAcceptsEmptyUnitForDimensionless()
      Unitless special-case coverage: When the example is Dimensionless, an empty (or whitespace-only) unit token must be accepted and interpreted as Unitless.BASE.

      Also verifies that a non-empty, unknown unit still fails for dimensionless parsing.

    • formatVariants

      @Test void formatVariants()
      Verifies Quantity.format(double) for the fixed-format range, E-notation range, and non-finite values.
    • toStringVariants

      @Test void toStringVariants()
      Verifies Quantity.toString() variants, including verbose/type flags and unit suppression.
    • toStringSIPrefixed

      @Test void toStringSIPrefixed()
      Verifies Quantity.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()
      Verifies Quantity.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()
    • sumMean

      @Test void sumMean()
      Verifies Quantity.sum(Quantity, Quantity[]) and Quantity.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()
      Verifies Quantity.multiply(Quantity), Quantity.divide(Quantity), and Quantity.reciprocal() produce SIQuantity with correct SI values (unit algebra correctness is validated in SIQuantity/unit tests).
    • asKnownQuantity

      @Test void asKnownQuantity()
      Verifies Quantity.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()
      Verifies equals/hashCode consider the SI value (bits) and the display unit.
    • getNameFormatsPrettyForQuantityAndSIQuantity

      @Test void getNameFormatsPrettyForQuantityAndSIQuantity()
      Tests Quantity.getName() for both the simple case (single leading uppercase, e.g. "Length" -> "Length") and the camel-case/internal-uppercase case using SIQuantity (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. Using SIQuantity guarantees 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)