VectorScalarProduct { public int calculateScalarProduct(Vector v1, Vector v2) { validateInput(v1, v2); int v1Size = v1.size(); int v2Size = v2.size(); if (v1Size != v2Size) { throw new IllegalArgumentException("Vectors should be contains the same number of elements."); } int result = 0; for (int i = 0; i < v1Size; i++) { result += v1.getAt(i) * v2.getAt(i); } return result; }  int calculateScalarProduct(Vector v1, Vector v2);  }
@Test(expected = IllegalArgumentException.class) public void differentSizeVectorsShouldThrowException() { Vector v1 = new Vector(1, 2, 3, 4); Vector v2 = new Vector(1, 2); vectorScalarProduct.calculateScalarProduct(v1, v2); }
@Test(expected = IllegalArgumentException.class) public void nullVectorsShouldThrowException() { Vector v1 = new Vector(1, 2, 3, 4); vectorScalarProduct.calculateScalarProduct(v1, null); }
@Test public void shoudlReturnZeroIfVectorsAreEmpty() { assertEquals(0, vectorScalarProduct.calculateScalarProduct(new Vector(null), new Vector(null))); }
@Test public void shouldReturnZeroIfOneVectorIsFullOfZeros() { Vector v1 = new Vector(1, 2, 3, 4); Vector v2 = new Vector(0, 0, 0, 0); int result = vectorScalarProduct.calculateScalarProduct(v1, v2); assertEquals(0, result); }
@Test public void shouldReturnSumOfOneVectorIfOtherIsFullOfOnes() { Vector v1 = new Vector(2, 2, 2, 2); Vector v2 = new Vector(1, 1, 1, 1); int result = vectorScalarProduct.calculateScalarProduct(v1, v2); assertEquals(8, result); }
@Test public void shouldTakeIntoAccountNegativeElements() { Vector v1 = new Vector(2, 2, 2, 2); Vector v2 = new Vector(-1, 1, -1, -1); int result = vectorScalarProduct.calculateScalarProduct(v1, v2); assertEquals(-4, result); }