Package org.djunits.vecmat.d2
Class Vector2Test
java.lang.Object
org.djunits.vecmat.d2.Vector2Test
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(), getSiArray(), 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 (invertEntries, multiplyEntries, divideEntries)
- 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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidVerify size, x()/y() quantities, xSi()/ySi(), getSiArray() copy semantics, and get(index) branches.voidtestAs()Verify as(targetUnit) succeeds for a unit with identical SI dimension and fails for mismatched dimension.voidVerify add(Q), subtract(Q), add(V), subtract(V), negate(), abs(), scaleBy(double) for Col.voidVerify add(Q), subtract(Q), add(V), subtract(V), negate(), abs(), scaleBy(double) for Row.voidCol.of(double[],unit) – happy path.voidCol.of(double[],unit) – null array.voidCol.of(double[],unit) – wrong length.voidCol.of(double,double,unit) – happy path.voidCol.of(double,double,null) – null unit.voidCol.of(Q,Q) – happy path.voidCol.of(Q,Q) – null x.voidCol.of(Q,Q) – null y.voidCol.of(Q[]) – happy path.voidCol.of(Q[]) – null array.voidCol.of(Q[]) – wrong length.voidCol.ofSi(double[],null).voidCol.ofSi(double[],unit) – happy path.voidCol.ofSi(double[],unit) – wrong length.voidCol.ofSi(double,double,unit) – happy path.voidVerify Col⋅Row = 2x2 matrix with plus-unit composition.voidVerify that constructor interprets values in the provided display unit and converts to SI storage.voidVerify equals/hashCode and type-sensitive equality (Row vs Col), and that display unit changes do not affect equals.voidVerify setDisplayUnit returnsthis(fluent) and that isRelative aligns with Length semantics.voidVerifyVector.getScalarArray()returns a Q[] (not Object[]) with the correct order and values.voidVerify Hadamard element-wise operations and correct unit composition.voidVerify Hadamard element-wise operations and correct unit composition.voidVerifyVector2.instantiateSi(double[])delegates to the (xSi,ySi) variant and enforces length=2.voidVerify the iterator over Q values yields x then y in the vector's display unit and is consistent with get().voidTest multiply/divide by scalar and as() method.voidVerifynorm()equalsnormL2()and norms compute correct quantities.voidVerify Row⋅Col = scalar with plus-unit composition.voidRow.of(double[],unit) – happy path.voidRow.of(double[],unit) – null array.voidRow.of(double[],unit) – wrong length.voidRow.of(double,double,unit) – happy path.voidRow.of(double,double,null) – null unit.voidRow.of(Q,Q) – happy path.voidRow.of(Q,Q) – null x.voidRow.of(Q,Q) – null y.voidRow.of(Q[]) – happy path.voidRow.of(Q[]) – null array.voidRow.of(Q[]) – wrong length.voidRow.ofSi(double[],unit) – happy path.voidRow.ofSi(double[],unit) – wrong length.voidRow.ofSi(double,double,unit) – happy path.voidRow.ofSi(double,double,null).voidVerify Row⋅Matrix2x2 = Col with plus-unit composition.voidVerify rows(), cols(), and isColumnVector() for both Row and Col; verify transpose preserves SI values and unit.voidVerify statistics: min, max, mean, median, sum.voidVerify toString() and toString(unit) contain orientation tag and unit abbreviation.voidTest as() functions for Col vector.voidTest as() functions for Row vector.
-
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()VerifyVector2.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(), getSiArray() 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()VerifyVector.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/sum") public void testStatistics()Verify statistics: min, max, mean, median, sum. -
testNorms
@Test @DisplayName("Normed: norm(), normL1(), normL2(), normLp(p), normLinf()") public void testNorms()Verifynorm()equalsnormL2()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: invertEntries / multiplyEntries / divideEntries + 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: invertEntries / multiplyEntries / divideEntries + 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. -
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 returnsthis(fluent) and that isRelative aligns with Length semantics. -
testMultiplyScalarAs
@Test public void testMultiplyScalarAs()Test multiply/divide by scalar and as() method. -
testColOfDoubleDoubleUnitHappy
@Test public void testColOfDoubleDoubleUnitHappy()Col.of(double,double,unit) – happy path. -
testRowOfDoubleDoubleUnitHappy
@Test public void testRowOfDoubleDoubleUnitHappy()Row.of(double,double,unit) – happy path. -
testColOfDoubleDoubleUnitNull
@Test public void testColOfDoubleDoubleUnitNull()Col.of(double,double,null) – null unit. -
testRowOfDoubleDoubleUnitNull
@Test public void testRowOfDoubleDoubleUnitNull()Row.of(double,double,null) – null unit. -
testColOfQQHappy
@Test public void testColOfQQHappy()Col.of(Q,Q) – happy path. -
testRowOfQQHappy
@Test public void testRowOfQQHappy()Row.of(Q,Q) – happy path. -
testColOfQQNullX
@Test public void testColOfQQNullX()Col.of(Q,Q) – null x. -
testColOfQQNullY
@Test public void testColOfQQNullY()Col.of(Q,Q) – null y. -
testRowOfQQNullX
@Test public void testRowOfQQNullX()Row.of(Q,Q) – null x. -
testRowOfQQNullY
@Test public void testRowOfQQNullY()Row.of(Q,Q) – null y. -
testColOfArrayUnitHappy
@Test public void testColOfArrayUnitHappy()Col.of(double[],unit) – happy path. -
testRowOfArrayUnitHappy
@Test public void testRowOfArrayUnitHappy()Row.of(double[],unit) – happy path. -
testColOfArrayUnitNullArray
@Test public void testColOfArrayUnitNullArray()Col.of(double[],unit) – null array. -
testRowOfArrayUnitNullArray
@Test public void testRowOfArrayUnitNullArray()Row.of(double[],unit) – null array. -
testColOfArrayUnitWrongLength
@Test public void testColOfArrayUnitWrongLength()Col.of(double[],unit) – wrong length. -
testRowOfArrayUnitWrongLength
@Test public void testRowOfArrayUnitWrongLength()Row.of(double[],unit) – wrong length. -
testColOfSiArrayUnitHappy
@Test public void testColOfSiArrayUnitHappy()Col.ofSi(double[],unit) – happy path. -
testRowOfSiArrayUnitHappy
@Test public void testRowOfSiArrayUnitHappy()Row.ofSi(double[],unit) – happy path. -
testColOfSiArrayWrongLength
@Test public void testColOfSiArrayWrongLength()Col.ofSi(double[],unit) – wrong length. -
testRowOfSiArrayWrongLength
@Test public void testRowOfSiArrayWrongLength()Row.ofSi(double[],unit) – wrong length. -
testColOfSiArrayNullUnit
@Test public void testColOfSiArrayNullUnit()Col.ofSi(double[],null). -
testColOfSiScalarsHappy
@Test public void testColOfSiScalarsHappy()Col.ofSi(double,double,unit) – happy path. -
testRowOfSiScalarsHappy
@Test public void testRowOfSiScalarsHappy()Row.ofSi(double,double,unit) – happy path. -
testRowOfSiScalarsNullUnit
@Test public void testRowOfSiScalarsNullUnit()Row.ofSi(double,double,null). -
testColOfQuantityArrayHappy
@Test public void testColOfQuantityArrayHappy()Col.of(Q[]) – happy path. -
testRowOfQuantityArrayHappy
@Test public void testRowOfQuantityArrayHappy()Row.of(Q[]) – happy path. -
testColOfQuantityArrayNull
@Test public void testColOfQuantityArrayNull()Col.of(Q[]) – null array. -
testRowOfQuantityArrayNull
@Test public void testRowOfQuantityArrayNull()Row.of(Q[]) – null array. -
testColOfQuantityArrayWrongLength
@Test public void testColOfQuantityArrayWrongLength()Col.of(Q[]) – wrong length. -
testRowOfQuantityArrayWrongLength
@Test public void testRowOfQuantityArrayWrongLength()Row.of(Q[]) – wrong length. -
testVector2ColAsFunctions
@Test @DisplayName("Vector2.Col as() functions test") public void testVector2ColAsFunctions()Test as() functions for Col vector. -
testVector2RowAsFunctions
@Test @DisplayName("Vector2.Row as() functions test") public void testVector2RowAsFunctions()Test as() functions for Row vector.
-