### Question:
TensorFactory { public static Tensor<?> constant(double v, int[] dimensions) { if (dimensions.length == 1 && dimensions[ROWS_INDEX] == 1) { return new Scalar(v); } else if (dimensions.length == 1 && dimensions[ROWS_INDEX] > 1) { return Vector.fill(v, dimensions[ROWS_INDEX]); } else if (dimensions.length == 2 && dimensions[ROWS_INDEX] > 0 && dimensions[COLUMNS_INDEX] > 0) { return Matrix.fill(v, dimensions[ROWS_INDEX], dimensions[COLUMNS_INDEX]); } else { throw new IllegalArgumentException(formatWithLocale( "Tensor of dimensions greater than 2 are not supported, got %d dimensions", dimensions.length )); } } private  TensorFactory(); static Tensor<?> constant(double v, int[] dimensions);  }### Answer:
@Test void canBuildTensors() { Tensor<?> scalar = TensorFactory.constant(1, new int[]{1}); Tensor<?> vector = TensorFactory.constant(1, new int[]{2}); Tensor<?> matrix = TensorFactory.constant(1, new int[]{3, 4}); assertTrue(scalar instanceof Scalar); assertArrayEquals(new int[]{1}, scalar.dimensions); assertTrue(vector instanceof Vector); assertArrayEquals(new int[]{2}, vector.dimensions); assertTrue(matrix instanceof Matrix); assertArrayEquals(new int[]{3, 4}, matrix.dimensions); }
@Test void throwsOnWrongDimensions() { assertThrows(IllegalArgumentException.class, () -> TensorFactory.constant(1, new int[]{1, 2, 3})); assertThrows(IllegalArgumentException.class, () -> TensorFactory.constant(1, new int[]{-1})); assertThrows(IllegalArgumentException.class, () -> TensorFactory.constant(1, new int[]{-1, 0})); assertThrows(IllegalArgumentException.class, () -> TensorFactory.constant(1, new int[]{10, 0})); }