Class VectorNTest

java.lang.Object
org.djunits.vecmat.dn.VectorNTest

public final class VectorNTest extends Object
Comprehensive unit tests for VectorN, validating both VectorN.Row and VectorN.Col implementations with concrete quantity Length and display units from Length.Unit.

The tests encode the intended functional specification. They will fail when the implementation deviates (e.g., incorrect addressing in get(index) or wrong unit composition in Hadamard operations).

Coverage scope:

  • Construction via a DataGridSi-backed SI store
  • Accessors: size(), rows(), cols(), isColumnVector(), si(), get(index)
  • Iteration and getScalarArray() correctness (types, order, unit)
  • Fluent display-unit switching (setDisplayUnit) and stringification (toString())
  • Vector algebra: add/subtract(Q), add/subtract(V), negate(), abs(), scaleBy(double)
  • Norms: norm(), normL1(), normL2(), normLp(int), normLinf()
  • Hadamard operations: invertElements(), multiplyElements(V), divideElements(V) with unit composition
  • Statistics: min(), max(), mean(), median(), mode(), sum()
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

    • VectorNTest

      public VectorNTest()
  • Method Details

    • testConstructorAndShapeCol

      @Test @DisplayName("Constructor SI storage & shape (Col)") public void testConstructorAndShapeCol()
      Verify that a VectorN.Col constructed from display values stores SI internally and reports correct shape properties (rows, cols, size, orientation).
    • testConstructorAndShapeRow

      @Test @DisplayName("Constructor SI storage & shape (Row)") public void testConstructorAndShapeRow()
      Verify that a VectorN.Row constructed from display values stores SI internally and reports correct shape properties (rows, cols, size, orientation).
    • testInstantiateSiArrayCol

      @Test @DisplayName("instantiateSi(double[]) enforces length=3 and delegates (Col)") public void testInstantiateSiArrayCol()
      Verify instantiateSi(double[]) enforces length=3 and delegates (Col).
    • testInstantiateSiArrayRow

      @Test @DisplayName("instantiateSi(double[]) enforces length=3 and delegates (Row)") public void testInstantiateSiArrayRow()
      Verify instantiateSi(double[]) enforces length=3 and delegates (Row).
    • testOfCol

      @Test public void testOfCol()
      Test the of() methods for Col.
    • testOfRow

      @Test public void testOfRow()
      Test the of() methods for Row.
    • testIndexing

      @Test @DisplayName("get(index), addressing semantics (Row & Col)") public void testIndexing()
      Verify Vector.get(int) and Vector.get(int) with valid and invalid indices; this test ensures the index addressing follows the intended semantics: a row vector reads from (0, index-1), a column vector from (index-1, 0).
    • testIterator

      @Test @DisplayName("iterator() yields Q in display unit, in natural order") public void testIterator()
      Verify that the Iterable interface is implemented correctly: the iterator produces Q values in natural order and throws NoSuchElementException at the end.
    • testGetScalarArray

      @Test @DisplayName("getScalarArray() returns Q[] with correct order and types") public void testGetScalarArray()
      Verify that VectorN.getScalarArray() returns a Q[] of correct length and component types, with entries in the expected x..n order.
    • testFluentAndToString

      @Test @DisplayName("setDisplayUnit() is fluent; toString contains orientation & unit") public void testFluentAndToString()
      Verify that VectorMatrix.setDisplayUnit(org.djunits.unit.UnitInterface) returns this for fluent usage and that VectorN.toString() and VectorN.toString(org.djunits.unit.UnitInterface) contain correct orientation and unit abbreviation.
    • testAlgebraRow

      @Test @DisplayName("Vector algebra (Row)") public void testAlgebraRow()
      Verify vector algebra operations for VectorN.Row: element-wise add/subtract(V), add/subtract(Q), negate(), abs(), and scaleBy(double).
    • testAlgebraCol

      @Test @DisplayName("Vector algebra (Col)") public void testAlgebraCol()
      Verify vector algebra operations for VectorN.Col: element-wise add/subtract(V), add/subtract(Q), negate(), abs(), and scaleBy(double).
    • testNorms

      @Test @DisplayName("Normed: norm(), normL1(), normL2(), normLp(p), normLinf()") public void testNorms()
      Verify norm(), normL1(), normL2(), normLp(int), and normLinf() for both Row and Col vectors; norm() is expected to equal normL2() per the Normed default.
    • testHadamardRow

      @Test @DisplayName("Hadamard: invert/multiply/divide + unit composition (Row)") public void testHadamardRow()
      Verify element-wise Hadamard operations on a VectorN.Row: invertElements(), multiplyElements(V), divideElements(V) and validate result-unit composition via SIUnit.add(SIUnit, SIUnit) and SIUnit.subtract(SIUnit, SIUnit).
    • testHadamardCol

      @Test @DisplayName("Hadamard: invert/multiply/divide + unit composition (Col)") public void testHadamardCol()
      Verify element-wise Hadamard operations on a VectorN.Col: invertElements(), multiplyElements(V), divideElements(V) and validate result-unit composition.
    • testStatisticsRow

      @Test @DisplayName("Statistics: min/max/mean/median/mode/sum (Row)") public void testStatisticsRow()
      Verify min, max, mean, median, mode, and sum on an unsorted odd-length VectorN.Row input (to avoid even-median ambiguity).
    • testStatisticsCol

      @Test @DisplayName("Statistics: min/max/mean/median/mode/sum (Col)") public void testStatisticsCol()
      Verify min, max, mean, median, mode, and sum on a VectorN.Col with inputs in kilometers; assertions are made against SI values in meters.
    • testColNScalarExtraction

      @Test @DisplayName("VectorN.Col: getScalars helpers (Row/Column) incl. 1-based, lengths, values, and bounds") public void testColNScalarExtraction()
      Verify scalar array extraction helpers on a VectorN.Col (4x1) and their 1-based variants.

      Uses kilometers as display unit to verify correct SI conversion.

    • testColNVectorExtractionSpec

      @Test @DisplayName("VectorN.Col: getRowVector / getColumnVector incl. 1-based and bounds") public void testColNVectorExtractionSpec()
      Verify vector extraction helpers on a VectorN.Col (4x1) for both 0-based and 1-based variants.

      Row vectors returned are Vector1 (1 element). The full column is returned as VectorN.Col.

    • testColNSiArrayExtractionAndSiAt

      @Test @DisplayName("VectorN.Col: getRowSi / getColumnSi incl. 1-based + si(r,c)/msi(r,c)") public void testColNSiArrayExtractionAndSiAt()
      Verify SI-array extraction helpers on a VectorN.Col and direct SI element access using si(row, col) and msi(row, col) including bounds.
    • testRowNScalarExtraction

      @Test @DisplayName("VectorN.Row: getScalars helpers (Row/Column) incl. 1-based, lengths, values, and bounds") public void testRowNScalarExtraction()
      Verify scalar array extraction helpers on a VectorN.Row (1x4) and their 1-based variants.

      Uses centimeters as display unit to verify correct SI conversion.

    • testRowNVectorExtractionSpec

      @Test @DisplayName("VectorN.Row: getRowVector / getColumnVector incl. 1-based and bounds") public void testRowNVectorExtractionSpec()
      Verify vector extraction helpers on a VectorN.Row (1x4) for both 0-based and 1-based variants.

      Full row returns VectorN.Row; each column returns Vector1.

    • testRowNSiArrayExtractionAndSiAt

      @Test @DisplayName("VectorN.Row: getRowSi / getColumnSi incl. 1-based + si(r,c)/msi(r,c)") public void testRowNSiArrayExtractionAndSiAt()
      Verify SI-array extraction helpers on a VectorN.Row and direct SI element access using si(row, col) and msi(row, col) including bounds.
    • testVectorNasVectorConversionsRowAndCol

      @Test @DisplayName("VectorN.{Row,Col}: asVector1 / asVector2* / asVector3* preserve SI & unit; illegal shapes throw") public void testVectorNasVectorConversionsRowAndCol()
      Verify that VectorN.Col and VectorN.Row convert to Vector1/Vector2/Vector3 variants with preserved SI data and display unit; and that illegal shapes throw IllegalStateException.

      Uses kilometers for column vectors and centimeters for row vectors to ensure SI conversion is correct.

    • testEqualsHashCode

      @Test @DisplayName("Equals and hashCode") public void testEqualsHashCode()
      Verify equals() and hashCode() methods.
    • testTranspose

      @Test @DisplayName("Transpose") public void testTranspose()
      Verify transpose() method.
    • testMultiplyScalarAs

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