Class QuantityTableTest

java.lang.Object
org.djunits.vecmat.table.QuantityTableTest

public class QuantityTableTest extends Object
Unit tests for QuantityTable. This test covers all behavior specifically introduced in QuantityTable.

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 Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Test as(): converting the table to a different display unit but identical SI unit.
    void
    Verify that asMatrixNxN() preserves SI data and display unit for square matrices (tested with 1x1 and 4x4), and throws IllegalStateException for non-square shapes (tested with 2x3 and 3x2).
    void
    Verify that all asXXX() methods correctly preserve SI values and correctly assign display units.
    void
    Verify that asVectorNCol(): Preserves SI data and display unit for N x 1 matrices (tested with N=4 and N=1), Throws IllegalStateException when the matrix has more than one column (e.g., N x 2). SI correctness is validated via both the row-major si() array and element access on the returned vector.
    void
    Verify that asVectorNRow(): Preserves SI data and display unit for 1 x N matrices (tested with N=4 and N=1), Throws IllegalStateException when the matrix has more than one row (e.g., 2 x N). SI correctness is validated via both the row-major si() array and element access on the returned vector.
    void
    Test basic constructor behavior: correct dimensions and display unit.
    void
    Test divideElements(): Hadamard division of two tables.
    void
    Verify equals/hashCode.
    void
    Verify of(double[], rows, cols, U) rejects nulls/bad sizes and converts display→SI.
    void
    Verify of(double[][],U) checks rectangular shape and converts display→SI.
    void
    Verify of(Q[][],U) accepts per-cell units via DenseDoubleData and sets display unit.
    void
    Test instantiateSi(double[]): produces new instance with SI data replaced.
    void
    Test invertElements(): works elementwise, produces SIQuantity output.
    void
    Test multiplyElements(): Hadamard product of two tables.
    void
    Test multiply/divide by scalar and as() method.
    void
    Verify QuantityTable conversions to fixed-size matrices preserve SI data and display unit, and that shape checks throw IllegalStateException for the mismatch branches covered.
    void
    Verify QuantityTable conversions to quantity table preserve SI data and display unit, and that shape checks throw IllegalStateException for the mismatch branches covered.
    void
    Verify QuantityTable conversions to fixed-size vectors and matrices preserve SI data and display unit, and that shape checks throw IllegalStateException with both row- and column-mismatch branches covered.
    void
    Verify scalar extraction helpers.
    void
    Verify double extraction helpers.
    void
    Verify vector extraction helpers.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • QuantityTableTest

      public QuantityTableTest()
  • Method Details

    • testConstructor

      @Test @DisplayName("ctor: stores datagrid and display unit correctly") public void testConstructor()
      Test basic constructor behavior: correct dimensions and display unit.
    • testFactoryArray

      @Test @DisplayName("of(double[],rows,cols,U): nulls/bad sizes/SI convert") public void testFactoryArray()
      Verify of(double[], rows, cols, U) rejects nulls/bad sizes and converts display→SI.
    • testFactoryGrid

      @Test @DisplayName("of(double[][],U): rectangular shape & SI convert") public void testFactoryGrid()
      Verify of(double[][],U) checks rectangular shape and converts display→SI.
    • testFactoryQuantityGrid

      @Test @DisplayName("of(Q[][],U): quantity grid accepted") public void testFactoryQuantityGrid()
      Verify of(Q[][],U) accepts per-cell units via DenseDoubleData and sets display unit.
    • testInstantiateSi

      @Test @DisplayName("instantiateSi(): replaces SI data correctly and preserves display unit") public void testInstantiateSi()
      Test instantiateSi(double[]): produces new instance with SI data replaced.
    • testInvertElements

      @Test @DisplayName("invertElements(): reciprocal of each SI value") public void testInvertElements()
      Test invertElements(): works elementwise, produces SIQuantity output.
    • testMultiplyElements

      @Test @DisplayName("multiplyElements(): Hadamard multiplication") public void testMultiplyElements()
      Test multiplyElements(): Hadamard product of two tables.
    • testDivideElements

      @Test @DisplayName("divideElements(): Hadamard division") public void testDivideElements()
      Test divideElements(): Hadamard division of two tables.
    • testEqualsHash

      @Test @DisplayName("equals / hashCode") public void testEqualsHash()
      Verify equals/hashCode.
    • testAs

      @Test @DisplayName("as(): convert display unit when SI units match") public void testAs()
      Test as(): converting the table to a different display unit but identical SI unit.
    • testQuantityTableAsVectorConversions

      @Test @DisplayName("QuantityTable: asVector preserve SI and unit; row/col mismatch branches throw") public void testQuantityTableAsVectorConversions()
      Verify QuantityTable conversions to fixed-size vectors and matrices preserve SI data and display unit, and that shape checks throw IllegalStateException with both row- and column-mismatch branches covered.

      Uses kilometers for column-shaped tests and centimeters for row-shaped tests to exercise SI conversion.

    • testQuantityTableAsMatrixConversions

      @Test @DisplayName("QuantityTable: asMatrix preserve SI and unit") public void testQuantityTableAsMatrixConversions()
      Verify QuantityTable conversions to fixed-size matrices preserve SI data and display unit, and that shape checks throw IllegalStateException for the mismatch branches covered.
    • testQuantityTableAsMatrixNxM

      @Test @DisplayName("QuantityTable: asMatrixNxM preserve SI and unit") public void testQuantityTableAsMatrixNxM()
      Verify QuantityTable conversions to quantity table preserve SI data and display unit, and that shape checks throw IllegalStateException for the mismatch branches covered.
    • testAsMatrixNxN

      @Test @DisplayName("QuantityTable: asMatrixNxN preserves SI & unit (square) and throws for non-square shapes") public void testAsMatrixNxN()
      Verify that asMatrixNxN() preserves SI data and display unit for square matrices (tested with 1x1 and 4x4), and throws IllegalStateException for non-square shapes (tested with 2x3 and 3x2). SI correctness is checked via both the row-major si() array and si(row, col) with 0-based indices.
    • testAsVectorNCol

      @Test @DisplayName("QuantityTable: asVectorNCol preserves SI & unit (Nx1) and throws for Nx2") public void testAsVectorNCol()
      Verify that asVectorNCol():
      • Preserves SI data and display unit for N x 1 matrices (tested with N=4 and N=1),
      • Throws IllegalStateException when the matrix has more than one column (e.g., N x 2).
      SI correctness is validated via both the row-major si() array and element access on the returned vector.
    • testAsVectorNRow

      @Test @DisplayName("QuantityTable: asVectorNRow preserves SI & unit (1xN) and throws for 2xN") public void testAsVectorNRow()
      Verify that asVectorNRow():
      • Preserves SI data and display unit for 1 x N matrices (tested with N=4 and N=1),
      • Throws IllegalStateException when the matrix has more than one row (e.g., 2 x N).
      SI correctness is validated via both the row-major si() array and element access on the returned vector.
    • testAsMethodsUnitConversion

      @Test @DisplayName("asXXX(): SI consistency and correct display-unit conversion") public void testAsMethodsUnitConversion()
      Verify that all asXXX() methods correctly preserve SI values and correctly assign display units. This test detects subtle SI/display-unit conversion mistakes such as interpreting SI values as display-unit values or performing double conversion.
    • testScalarExtraction

      @Test @DisplayName("getScalars / getRowScalars / getColumnScalars") public void testScalarExtraction()
      Verify scalar extraction helpers.
    • testVectorExtraction

      @Test @DisplayName("getRowVector / getColumnVector") public void testVectorExtraction()
      Verify vector extraction helpers.
    • testSiArrayExtraction

      @Test @DisplayName("getRowSi / getColumnSi") public void testSiArrayExtraction()
      Verify double extraction helpers.
    • testMultiplyScalarAs

      @Test public void testMultiplyScalarAs()
      Test multiply/divide by scalar and as() method.