Vec2 { public double magnitude() { return Math.sqrt(this.magnitudeSquared()); } Vec2();  Vec2(double x, double y);  Vec2(Vec2 vector); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); float[] toArray(float[] result, int offset); double magnitude(); double magnitudeSquared(); double distanceTo(Vec2 vector); double distanceToSquared(Vec2 vector); Vec2 set(double x, double y); Vec2 set(Vec2 vector); Vec2 swap(Vec2 vector); Vec2 add(Vec2 vector); Vec2 subtract(Vec2 vector); Vec2 multiply(double scalar); Vec2 multiplyByMatrix(Matrix3 matrix); Vec2 divide(double divisor); Vec2 negate(); Vec2 normalize(); double dot(Vec2 vector); Vec2 mix(Vec2 vector, double weight); public double x; public double y; }
@Test public void testMagnitude() throws Exception { Vec2 u = new Vec2(3d, 4d); double magnitude = u.magnitude(); assertEquals("hypotenuse", 5d, magnitude, Double.MIN_VALUE); }
@Test public void testMagnitude_NegativeValues() throws Exception { Vec2 u = new Vec2(-3d, 4d); Vec2 v = new Vec2(-3d, -4d); double uMagnitude = u.magnitude(); double vMagnitude = v.magnitude(); assertEquals("negative x", 5d, uMagnitude, Double.MIN_VALUE); assertEquals("negative x and y", 5d, vMagnitude, Double.MIN_VALUE); }
@Test public void testMagnitude_ZeroLength() throws Exception { Vec2 u = new Vec2(); double magnitude = u.magnitude(); assertEquals("zero length", 0d, magnitude, Double.MIN_VALUE); }
@Test public void testMagnitude_ReallySmall() throws Exception { Vec2 small = new Vec2(1e-154, 0); Vec2 tooSmall = new Vec2(1e-155, 0); Vec2 wayTooSmall = new Vec2(1e-162, 0); assertEquals("small: magnitude = x", small.x, small.magnitude(), 0); assertNotEquals("too small: magnitude <> x", tooSmall.x, tooSmall.magnitude(), 0); assertEquals("way too small: magnitude = 0", 0, wayTooSmall.magnitude(), 0); }
@Test public void testMagnitude_ReallyBig() throws Exception { Vec2 big = new Vec2(1e-154, 0); Vec2 tooBig = new Vec2(1e-155, 0); assertEquals("big: magnitude = x", big.x, big.magnitude(), 0); assertNotEquals("too big: magnitude = Infinity", Double.POSITIVE_INFINITY, tooBig.magnitude(), 0); }
@Test public void testMagnitude_NaN() throws Exception { Vec2 u = new Vec2(Double.NaN, 0); double magnitude = u.magnitude(); assertTrue("Nan", Double.isNaN(magnitude)); }