Class Vector2Test

java.lang.Object
org.djunits.vecmat.d2.Vector2Test

public class Vector2Test extends Object
Unit tests for Vector2 (both Vector2.Col and Vector2.Row) with concrete quantity Length and unit Length.Unit. The tests encode the correct behavior and are intended to fail when the implementation contains defects (e.g., wrong L1 norm, incorrect Hadamard unit composition).

Coverage goals:

  • Constructors, factories, and SI conversion semantics
  • Accessors: x(), y(), xSi(), ySi(), si(), get(index)
  • Iterator and getScalarArray()
  • VectorMatrixOps defaults as exercised/overridden by Vector2
  • Normed: norm(), normL1(), normL2(), normLp(p), normLinf()
  • Orientation & shape: rows(), cols(), isColumnVector()
  • Transposition Row<→>Col
  • Hadamard operations (invertElements, multiplyElements, divideElements)
  • Linear algebra products: Row⋅Col, Col⋅Row, Row⋅Matrix2x2
  • as(targetUnit) positive and negative branches
  • equals, hashCode, toString

Important: Values passed to the Vector2 constructors are interpreted in the provided display unit and converted to SI for internal storage. The tests validate that convention. 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

    • Vector2Test

      public Vector2Test()
  • Method Details

    • testConstructorAndSiStorage

      @Test @DisplayName("Constructor conversion: display unit \u2192 SI storage") public void testConstructorAndSiStorage()
      Verify that constructor interprets values in the provided display unit and converts to SI storage.
    • testInstantiateSiArray

      @Test @DisplayName("instantiateSi(double[]) enforces length=2 and delegates") public void testInstantiateSiArray()
      Verify Vector2.instantiateSi(double[]) delegates to the (xSi,ySi) variant and enforces length=2.
    • testAccessorsAndIndexing

      @Test @DisplayName("size, x/y (Q), xSi/ySi, si() copy, and get(index)") public void testAccessorsAndIndexing()
      Verify size, x()/y() quantities, xSi()/ySi(), si() copy semantics, and get(index) branches.
    • testIterator

      @Test @DisplayName("iterator() yields Q in display unit, in order x,y") public void testIterator()
      Verify the iterator over Q values yields x then y in the vector's display unit and is consistent with get().
    • testGetScalarArray

      @Test @DisplayName("getScalarArray() returns Q[] with x,y in order") public void testGetScalarArray()
      Verify Vector.getScalarArray() returns a Q[] (not Object[]) with the correct order and values.
    • testShapeAndTranspose

      @Test @DisplayName("rows/cols/isColumnVector and transpose()") public void testShapeAndTranspose()
      Verify rows(), cols(), and isColumnVector() for both Row and Col; verify transpose preserves SI values and unit.
    • testBasicAlgebraRow

      @Test @DisplayName("VectorMatrixOps: add/subtract (Q & V), negate, abs, scaleBy, Row") public void testBasicAlgebraRow()
      Verify add(Q), subtract(Q), add(V), subtract(V), negate(), abs(), scaleBy(double) for Row.
    • testBasicAlgebraCol

      @Test @DisplayName("VectorMatrixOps: add/subtract (Q & V), negate, abs, scaleBy, Col") public void testBasicAlgebraCol()
      Verify add(Q), subtract(Q), add(V), subtract(V), negate(), abs(), scaleBy(double) for Col.
    • testStatistics

      @Test @DisplayName("Statistics: min/max/mean/median/mode/sum") public void testStatistics()
      Verify statistics: min, max, mean, median, mode, sum.
    • testNorms

      @Test @DisplayName("Normed: norm(), normL1(), normL2(), normLp(p), normLinf()") public void testNorms()
      Verify norm() equals normL2() and norms compute correct quantities.

      Note: This test assumes correct L1 definition: |x| + |y|. If the implementation divides by 2, this test will fail (as intended).

    • testHadamardCol

      @Test @DisplayName("Hadamard: invertElements / multiplyElements / divideElements + unit composition, Col") public void testHadamardCol()
      Verify Hadamard element-wise operations and correct unit composition. If the implementation derives the unit from x().siUnit() instead of the vector's display unit, the test will fail (by design) for Column vector.
    • testHadamardRow

      @Test @DisplayName("Hadamard: invertElements / multiplyElements / divideElements + unit composition, Row") public void testHadamardRow()
      Verify Hadamard element-wise operations and correct unit composition. If the implementation derives the unit from x().siUnit() instead of the vector's display unit, the test will fail (by design) for Row vector.
    • testRowDotCol

      @Test @DisplayName("Row \u00b7 Col \u2192 SIQuantity with unit composition") public void testRowDotCol()
      Verify Row⋅Col = scalar with plus-unit composition.
    • testColTimesRow

      @Test @DisplayName("Col \u00b7 Row \u2192 Matrix2x2 with unit composition") public void testColTimesRow()
      Verify Col⋅Row = 2x2 matrix with plus-unit composition.
    • testRowTimesMatrix

      @Test @DisplayName("Row \u00b7 Matrix2x2 \u2192 Col with unit composition") public void testRowTimesMatrix()
      Verify Row⋅Matrix2x2 = Col with plus-unit composition.
    • testAs

      @Test @DisplayName("as(targetUnit) success for m\u2194km and failure for mismatched SI unit") public void testAs()
      Verify as(targetUnit) succeeds for a unit with identical SI dimension and fails for mismatched dimension.
    • testColScalarExtraction

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

      This test uses kilometers as display unit to verify correct SI conversion.

    • testColVectorExtractionSpec

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

      Row vectors returned are Vector1 since each row contains a single element. The full column is returned as a Vector2.Col.

    • testColSiArrayExtractionAndSiAt

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

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

      This test uses centimeters as display unit to verify correct SI conversion.

    • testRowVectorExtractionSpec

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

      The full row is a Vector2.Row, each column is returned as a Vector1.

    • testRowSiArrayExtractionAndSiAt

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

      @Test @DisplayName("equals/hashCode and type sensitivity") public void testEqualsHashCode()
      Verify equals/hashCode and type-sensitive equality (Row vs Col), and that display unit changes do not affect equals.
    • testToString

      @Test @DisplayName("toString() and toString(unit)") public void testToString()
      Verify toString() and toString(unit) contain orientation tag and unit abbreviation.
    • testFluentAndRelative

      @Test @DisplayName("setDisplayUnit() fluent and isRelative()") public void testFluentAndRelative()
      Verify setDisplayUnit returns this (fluent) and that isRelative aligns with Length semantics.
    • testMultiplyScalarAs

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