Package org.djunits.vecmat.d2
Class Matrix2x2Test
java.lang.Object
org.djunits.vecmat.d2.Matrix2x2Test
Unit tests for
Matrix2x2 with concrete quantity Length and unit Length.Unit.
The tests aim for 100% method and branch coverage in Matrix2x2 and the inherited default functionality from:
VectorMatrixSquareDenseMatrixSquareMatrixHadamard- and the value interfaces used by
MatrixOps(Additive, Scalable, Value)
The assertions are written to reflect the intended functional specification. If any method in the class hierarchy is incorrectly implemented (including TODOs not implemented), tests will fail, as desired.
Conventions used in these tests
- Quantities:
Length - Units under test:
Length.Unit.m(SI base),Length.Unit.km(scale 1000), and occasionallyLength.Unit.cm - Numerical comparisons on SI arrays use a small epsilon
- Author:
- Alexander Verbraeck (specifications); Test implementation by Copilot.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidVerifyVectorMatrix.add(Quantity)andsubtract(VM)with another matrix.voidVerifyVectorMatrix.add(Quantity)andsubtract(Q)with quantities.voidVerifyMatrix2x2.adjugate()for a generic 2x2 matrix.voidtestAs()VerifyMatrix2x2.as(Unit)succeeds when SI units match (e.g., m ↔ km), and throws when SI units mismatch (e.g., length ↔ time).voidTest as() functions.voidvoidVerify determinant as scalar and as quantity, and trace.voidvoidVerifyMatrix2x2.of(double[], Unit)rejects nulls and wrong sizes, and converts using the display unit.voidVerifyMatrix2x2.of(double[][], Unit)validation and SI conversion.voidVerify grid extraction helpers onVectorMatrix.voidVerify Hadamard element-wise operations: invert, multiply, divide.voidEnsure index-related methods do not throw for valid bounds and that value retrieval uses display unit.voidVerifyMatrix2x2.instantiateSi(double[])creates a new matrix with the same display unit and the provided SI values.voidVerifyMatrix2x2.inverse()and multiplication with its inverse gives the identity (numerically).voidVerify thatMatrix2x2.inverse()throws on singular matrices.voidVerify standard 2x2 matrix multiplication.voidVerify multiplication with a column vector.voidTest multiply/divide by scalar and as() method.voidvoidVerify Frobenius norm and defaultnorm().voidTestMatrix2x2.of(double[], Unit)for nulls, wrong sizes, and unit conversion.voidExhaustive test ofMatrix2x2.of(double[][], Unit)grid validation and conversion.voidTestMatrix2x2.of(Quantity[])for nulls, wrong size, and conversion.voidExhaustive test ofMatrix2x2.of(Quantity[][])grid validation.voidTestMatrix2x2.ofSi(double[], Unit)for nulls, wrong sizes, and display unit handling.voidExhaustive test ofMatrix2x2.ofSi(double[][], Unit)grid validation.voidVerify scalar array extraction helpers onVectorMatrix.voidVerify thatVectorMatrix.setDisplayUnit(Unit)only affects presentation and not SI storage.voidVerify si array extraction helpers onVectorMatrix.voidVerifymean,median,min,max, andsum.voidVerify symmetry and skew-symmetry checks with and without tolerances.voidCallVectorMatrix.toString()andValue.format(Unit)for coverage.voidVerify transpose on a 2x2 matrix.voidVerify vector extraction helpers (getRow,getColumn,getDiagonal) behave per spec.
-
Constructor Details
-
Matrix2x2Test
public Matrix2x2Test()
-
-
Method Details
-
testFactoryOfArray
@Test @DisplayName("Factory of(double[]) \u2014 nulls, length check, and SI conversion") public void testFactoryOfArray()VerifyMatrix2x2.of(double[], Unit)rejects nulls and wrong sizes, and converts using the display unit. -
testFactoryOfGrid
@Test @DisplayName("Factory of(double[][]) \u2014 nulls, 2x2 shape, and SI conversion") public void testFactoryOfGrid()VerifyMatrix2x2.of(double[][], Unit)validation and SI conversion. -
testInstantiate
@Test @DisplayName("instantiate(double[]) \u2014 uses provided SI data and keeps display unit") public void testInstantiate()VerifyMatrix2x2.instantiateSi(double[])creates a new matrix with the same display unit and the provided SI values. -
testBasicShapeAndValue
@Test @DisplayName("rows/cols/value/isRelative") public void testBasicShapeAndValue() -
testSetDisplayUnit
@Test @DisplayName("setDisplayUnit() only changes presentation") public void testSetDisplayUnit()Verify thatVectorMatrix.setDisplayUnit(Unit)only affects presentation and not SI storage. -
testToString
@Test @DisplayName("toString() and toString(unit) contain unit abbreviation") public void testToString()CallVectorMatrix.toString()andValue.format(Unit)for coverage. We assert the unit abbreviation is present; we do not depend on exact formatting of numbers. -
testAddSubtractQuantity
@Test @DisplayName("add(Q) / subtract(Q) apply element-wise SI increments") public void testAddSubtractQuantity()VerifyVectorMatrix.add(Quantity)andsubtract(Q)with quantities. -
testAddSubtractMatrix
@Test @DisplayName("add(VM) / subtract(VM) element-wise") public void testAddSubtractMatrix()VerifyVectorMatrix.add(Quantity)andsubtract(VM)with another matrix. -
testNegateAbsScaleBy
@Test @DisplayName("negate / abs / scaleBy") public void testNegateAbsScaleBy() -
testStats
@Test @DisplayName("mean / median / min / max / sum") public void testStats()Verifymean,median,min,max, andsum. -
testTranspose
@Test @DisplayName("transpose() swaps off-diagonals") public void testTranspose()Verify transpose on a 2x2 matrix. -
testDeterminantAndTrace
@Test @DisplayName("determinantScalar(), determinant(), trace()") public void testDeterminantAndTrace()Verify determinant as scalar and as quantity, and trace. -
testNorms
@Test @DisplayName("normFrobenius and default norm()") public void testNorms()Verify Frobenius norm and defaultnorm(). -
testSymmetry
@Test @DisplayName("isSymmetric / isSkewSymmetric with and without tolerance") public void testSymmetry()Verify symmetry and skew-symmetry checks with and without tolerances. -
testInverseAndIdentity
@Test @DisplayName("inverse() and A * A^{-1} \u2248 I") public void testInverseAndIdentity() throws NonInvertibleMatrixExceptionVerifyMatrix2x2.inverse()and multiplication with its inverse gives the identity (numerically).- Throws:
NonInvertibleMatrixException- if inversion fails (test expects success)
-
testInverseThrowsOnSingular
@Test @DisplayName("inverse() throws on singular matrix") public void testInverseThrowsOnSingular()Verify thatMatrix2x2.inverse()throws on singular matrices. -
testAdjugate
@Test @DisplayName("adjugate() for [[a,b],[c,d]] equals [[d,-b],[-c,a]]") public void testAdjugate()VerifyMatrix2x2.adjugate()for a generic 2x2 matrix. -
testMatrixMultiply
@Test @DisplayName("matrix x matrix multiplication") public void testMatrixMultiply()Verify standard 2x2 matrix multiplication. -
testMatrixTimesVector
@Test @DisplayName("matrix x vector (column)") public void testMatrixTimesVector()Verify multiplication with a column vector. Assumes aVector2.Colwith constructor (double v1, double v2, SIUnit unit) is available. -
testHadamard
@Test @DisplayName("Hadamard: invertElements / multiplyElements / divideElements") public void testHadamard()Verify Hadamard element-wise operations: invert, multiply, divide. -
testAs
@Test @DisplayName("as(targetUnit) success (m\u2194km) and failure (length\u2194time)") public void testAs()VerifyMatrix2x2.as(Unit)succeeds when SI units match (e.g., m ↔ km), and throws when SI units mismatch (e.g., length ↔ time). -
testGridExtraction
@Test @DisplayName("getScalarGrid / getSiGrid") public void testGridExtraction()Verify grid extraction helpers onVectorMatrix. -
testScalarExtraction
@Test @DisplayName("getScalars / getRowScalars / getColumnScalars / getDiagonalScalars") public void testScalarExtraction()Verify scalar array extraction helpers onVectorMatrix. -
testVectorExtractionSpec
@Test @DisplayName("getRowVector / getColumnVector / getDiagonalVector return the expected vectors (spec)") public void testVectorExtractionSpec()Verify vector extraction helpers (getRow,getColumn,getDiagonal) behave per spec.This test expects non-null vectors with the correct content.
-
testSiArrayExtraction
@Test @DisplayName("getRowSi / getColumnSi / getDiagonalSi") public void testSiArrayExtraction()Verify si array extraction helpers onVectorMatrix. -
testEqualsHashCode
@Test @DisplayName("equals / hashCode") public void testEqualsHashCode() -
testIndexingAndValues
@Test @DisplayName("Indexing within bounds and value retrieval") public void testIndexingAndValues()Ensure index-related methods do not throw for valid bounds and that value retrieval uses display unit. -
testMultiplyScalarAs
@Test public void testMultiplyScalarAs()Test multiply/divide by scalar and as() method. -
testOfDoubleArray
@Test @DisplayName("of(double[], Unit): nulls, size checks, unit conversion") public void testOfDoubleArray()TestMatrix2x2.of(double[], Unit)for nulls, wrong sizes, and unit conversion. -
testOfSiDoubleArray
@Test @DisplayName("ofSi(double[], Unit): nulls, size checks, display unit") public void testOfSiDoubleArray()TestMatrix2x2.ofSi(double[], Unit)for nulls, wrong sizes, and display unit handling. -
testOfDoubleGridAllCornerCases
@Test @DisplayName("of(double[][], Unit): exhaustive null and shape validation") public void testOfDoubleGridAllCornerCases()Exhaustive test ofMatrix2x2.of(double[][], Unit)grid validation and conversion. -
testOfSiDoubleGridAllCornerCases
@Test @DisplayName("ofSi(double[][], Unit): exhaustive null and shape validation") public void testOfSiDoubleGridAllCornerCases()Exhaustive test ofMatrix2x2.ofSi(double[][], Unit)grid validation. -
testOfQuantityArray
@Test @DisplayName("of(Q[]): null elements, wrong length, unit conversion") public void testOfQuantityArray()TestMatrix2x2.of(Quantity[])for nulls, wrong size, and conversion. -
testOfQuantityGridAllCornerCases
@Test @DisplayName("of(Q[][]): exhaustive null and shape validation (full branch coverage)") public void testOfQuantityGridAllCornerCases()Exhaustive test ofMatrix2x2.of(Quantity[][])grid validation. Covers all branches of:grid == null grid.length != 2 grid[0] == null grid[1] == null grid[0].length != 2 grid[1].length != 2 grid[r][c] == null
-
testAsFunctions
@Test @DisplayName("Matrix2x2 as() functions test") public void testAsFunctions()Test as() functions.
-