116 lines
2.4 KiB
Java
Raw Normal View History

package machine_learning;
2019-06-20 13:17:23 +02:00
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class VectorTest
{
@Test
void shouldInitializeZeroVector()
{
var v = new Vector(3);
var expected = new Vector(0d, 0d, 0d);
2019-06-20 13:17:23 +02:00
assertEquals(3, v.dimension());
assertEquals(expected, v);
}
@Test
void shouldReturnCorrectVectorWhenAdding()
{
var v1 = new Vector(1d, 2d);
var v2 = new Vector(3d, 4d);
2019-06-20 13:17:23 +02:00
2019-06-30 23:59:49 +02:00
var actual = v1.add(v2);
var expected = new Vector(4d, 6d);
2019-06-20 13:17:23 +02:00
2019-06-30 23:59:49 +02:00
assertEquals(expected, actual);
2019-06-20 13:17:23 +02:00
}
@Test
void shouldReturnCorrectVectorWhenSubtracting()
{
var v1 = new Vector(1d, 2d);
var v2 = new Vector(3d, 4d);
2019-06-20 13:17:23 +02:00
2019-06-30 23:59:49 +02:00
var actual = v1.subtract(v2);
var expected = new Vector(-2d, -2d);
2019-06-20 13:17:23 +02:00
2019-06-30 23:59:49 +02:00
assertEquals(expected, actual);
2019-06-20 13:17:23 +02:00
}
@Test
2019-06-24 00:23:19 +02:00
void shouldReturnCorrectVectorWhenScalarMultiplying()
2019-06-20 13:17:23 +02:00
{
var v1 = new Vector(1d, 2d);
var v2 = new Vector(3d, 4d);
2019-06-20 13:17:23 +02:00
2019-06-30 23:59:49 +02:00
var actual = v1.scalar(v2);
2019-06-20 13:17:23 +02:00
var expected = 11d;
2019-06-30 23:59:49 +02:00
assertEquals(expected, actual);
2019-06-20 13:17:23 +02:00
}
2019-06-24 00:23:19 +02:00
@Test
void shouldReturnCorrectVectorWhenEuclid()
{
var v1 = new Vector(1d, 2d);
2019-06-24 00:23:19 +02:00
2019-06-30 23:59:49 +02:00
var actual = v1.euclid();
2019-06-24 00:23:19 +02:00
var expected = Math.sqrt(5);
2019-06-30 23:59:49 +02:00
assertEquals(expected, actual);
2019-06-24 00:23:19 +02:00
}
@Test
void shouldReturnCorrectDistance()
{
var v1 = new Vector(1d, 2d);
var v2 = new Vector(3d, 4d);
2019-06-30 23:59:49 +02:00
var actual = v1.distance(v2);
var expected = Math.sqrt(8);
2019-06-30 23:59:49 +02:00
assertEquals(expected, actual);
}
2019-06-24 00:23:19 +02:00
@Test
void shouldReturnCorrectVectorWhenDividing()
{
var v1 = new Vector(1d, 2d);
2019-06-24 00:23:19 +02:00
var div = 2d;
2019-06-30 23:59:49 +02:00
var actual = v1.divide(div);
var expected = new Vector(0.5d, 1d);
2019-06-24 00:23:19 +02:00
2019-06-30 23:59:49 +02:00
assertEquals(expected, actual);
}
@Test
void shouldDecreaseDimensionCorrect()
{
var v = new Vector(1d, 2d, 3d, 4d);
var decreasedDimensionVector = v.decreasedDimension();
var actual = decreasedDimensionVector.dimension();
var expected = 3;
assertEquals(expected, actual);
}
@Test
void shouldNormalizeCorrect()
{
var v = new Vector(4d, 4d, 4d, 4d);
var actual = v.normalized();
var expected = new Vector(0.5d, 0.5d, 0.5d, 0.5d);
assertEquals(expected, actual);
2019-06-24 00:23:19 +02:00
}
2019-06-20 13:17:23 +02:00
}