UNPKG

sku-pg

Version:

Skulpt is a Javascript implementation of Python 2.x. Python that runs in your browser!

203 lines (144 loc) 4.18 kB
<!DOCTYPE html> <html> <!-- Copyright 2006 The Closure Library Authors. All Rights Reserved. Use of this source code is governed by an Apache 2.0 License. See the COPYING file for details. --> <head> <title>Closure Unit Tests - goog.math.Vec2</title> <script src="../base.js"></script> <script> goog.require('goog.math.Coordinate'); goog.require('goog.math.Vec2'); goog.require('goog.testing.jsunit'); </script> </head> <body> <script> function assertVectorEquals(a, b) { assertTrue(b + ' should be equal to ' + a, goog.math.Vec2.equals(a, b)); } function testVec2() { var a = new goog.math.Vec2(); assertEquals(0, a.x); assertEquals(0, a.y); var b = new goog.math.Vec2(4); assertEquals(4, b.x); assertEquals(0, b.y); var c = new goog.math.Vec2(3.14, 2.78); assertEquals(3.14, c.x); assertEquals(2.78, c.y); } function testRandomUnit() { var a = goog.math.Vec2.randomUnit(); assertRoughlyEquals(1.0, a.magnitude(), 1e-10); } function testRandom() { var a = goog.math.Vec2.random(); assertTrue(a.magnitude() <= 1.0); } function testClone() { var a = new goog.math.Vec2(1, 2); var b = a.clone(); assertEquals(a.x, b.x); assertEquals(a.y, b.y); } function testMagnitude() { var a = new goog.math.Vec2(0, 10); var b = new goog.math.Vec2(3, 4); assertEquals(10, a.magnitude()); assertEquals(5, b.magnitude()); } function testSquaredMagnitude() { var a = new goog.math.Vec2(-3, -4); assertEquals(25, a.squaredMagnitude()); } function testScale() { var a = new goog.math.Vec2(1, 2); a.scale(0.5); assertEquals(0.5, a.x); assertEquals(1, a.y); } function testInvert() { var a = new goog.math.Vec2(3, 4); a.invert(); assertEquals(-3, a.x); assertEquals(-4, a.y); } function testNormalize() { var a = new goog.math.Vec2(5, 5); a.normalize(); assertRoughlyEquals(1.0, a.magnitude(), 1e-10); } function testAdd() { var a = new goog.math.Vec2(1, -1); a.add(new goog.math.Vec2(3, 3)); assertVectorEquals(new goog.math.Vec2(4, 2), a); } function testSubtract() { var a = new goog.math.Vec2(1, -1); a.subtract(new goog.math.Vec2(3, 3)); assertVectorEquals(new goog.math.Vec2(-2, -4), a); } function testEquals() { var a = new goog.math.Vec2(1, 2); assertFalse(a.equals(null)); assertFalse(a.equals(new goog.math.Vec2(1, 3))); assertFalse(a.equals(new goog.math.Vec2(2, 2))); assertTrue(a.equals(a)); assertTrue(a.equals(new goog.math.Vec2(1, 2))); } function testSum() { var a = new goog.math.Vec2(0.5, 0.25); var b = new goog.math.Vec2(0.5, 0.75); var c = goog.math.Vec2.sum(a, b); assertVectorEquals(new goog.math.Vec2(1, 1), c); } function testDifference() { var a = new goog.math.Vec2(0.5, 0.25); var b = new goog.math.Vec2(0.5, 0.75); var c = goog.math.Vec2.difference(a, b); assertVectorEquals(new goog.math.Vec2(0, -0.5), c); } function testDistance() { var a = new goog.math.Vec2(3, 4); var b = new goog.math.Vec2(-3, -4); assertEquals(10, goog.math.Vec2.distance(a, b)); } function testSquaredDistance() { var a = new goog.math.Vec2(3, 4); var b = new goog.math.Vec2(-3, -4); assertEquals(100, goog.math.Vec2.squaredDistance(a, b)); } function testVec2Equals() { assertTrue(goog.math.Vec2.equals(null, null)); assertFalse(goog.math.Vec2.equals(null, new goog.math.Vec2())); var a = new goog.math.Vec2(1, 3); assertTrue(goog.math.Vec2.equals(a, a)); assertTrue(goog.math.Vec2.equals(a, new goog.math.Vec2(1, 3))); assertFalse(goog.math.Vec2.equals(1, new goog.math.Vec2(3, 1))); } function testDot() { var a = new goog.math.Vec2(0, 5); var b = new goog.math.Vec2(3, 0); assertEquals(0, goog.math.Vec2.dot(a, b)); var c = new goog.math.Vec2(-5, -5); var d = new goog.math.Vec2(0, 7); assertEquals(-35, goog.math.Vec2.dot(c, d)); } function testLerp() { var a = new goog.math.Vec2(0, 0); var b = new goog.math.Vec2(10, 10); for (var i = 0; i <= 10; i++) { var c = goog.math.Vec2.lerp(a, b, i / 10); assertEquals(i, c.x); assertEquals(i, c.y); } } function testToString() { testEquals('(0, 0)', new goog.math.Vec2(0, 0).toString()); } </script> </body> </html>