UNPKG

sku-pg

Version:

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

222 lines (161 loc) 4.92 kB
<!DOCTYPE html> <html> <!-- Copyright 2008 The Closure Library Authors. All Rights Reserved. Use of this source code is governed by the Apache License, Version 2.0. See the COPYING file for details. --> <!-- Vec3 Unit Tests --> <head> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Closure Unit Tests - goog.math.Vec3</title> <script src="../base.js"></script> <script> goog.require('goog.testing.jsunit'); goog.require('goog.math.Vec3'); goog.require('goog.math.Coordinate3'); </script> </head> <body> <script> function assertVec3Equals(a, b) { assertTrue(b + ' should be equal to ' + a, goog.math.Vec3.equals(a, b)); } function testVec3() { var v = new goog.math.Vec3(3.14, 2.78, -7.21); assertEquals(3.14, v.x); assertEquals(2.78, v.y); assertEquals(-7.21, v.z); } function testRandomUnit() { var a = goog.math.Vec3.randomUnit(); assertRoughlyEquals(1.0, a.magnitude(), 1e-10); } function testRandom() { var a = goog.math.Vec3.random(); assertTrue(a.magnitude() <= 1.0); } function testFromCoordinate3() { var a = new goog.math.Coordinate3(-2, 10, 4); var b = goog.math.Vec3.fromCoordinate3(a); assertEquals(-2, b.x); assertEquals(10, b.y); assertEquals(4, b.z); } function testClone() { var a = new goog.math.Vec3(1, 2, 5); var b = a.clone(); assertEquals(a.x, b.x); assertEquals(a.y, b.y); assertEquals(a.z, b.z); } function testMagnitude() { var a = new goog.math.Vec3(0, 10, 0); var b = new goog.math.Vec3(3, 4, 5); var c = new goog.math.Vec3(-4, 3, 8); assertEquals(10, a.magnitude()); assertEquals(Math.sqrt(50), b.magnitude()); assertEquals(Math.sqrt(89), c.magnitude()); } function testSquaredMagnitude() { var a = new goog.math.Vec3(-3, -4, -5); assertEquals(50, a.squaredMagnitude()); } function testScale() { var a = new goog.math.Vec3(1, 2, 3); a.scale(0.5); assertEquals(0.5, a.x); assertEquals(1, a.y); assertEquals(1.5, a.z); } function testInvert() { var a = new goog.math.Vec3(3, 4, 5); a.invert(); assertEquals(-3, a.x); assertEquals(-4, a.y); assertEquals(-5, a.z); } function testNormalize() { var a = new goog.math.Vec3(5, 5, 5); a.normalize(); assertRoughlyEquals(1.0, a.magnitude(), 1e-10); } function testAdd() { var a = new goog.math.Vec3(1, -1, 7); a.add(new goog.math.Vec3(3, 3, 3)); assertVec3Equals(new goog.math.Vec3(4, 2, 10), a); } function testSubtract() { var a = new goog.math.Vec3(1, -1, 4); a.subtract(new goog.math.Vec3(3, 3, 3)); assertVec3Equals(new goog.math.Vec3(-2, -4, 1), a); } function testEquals() { var a = new goog.math.Vec3(1, 2, 5); assertFalse(a.equals(null)); assertFalse(a.equals(new goog.math.Vec3(1, 3, 5))); assertFalse(a.equals(new goog.math.Vec3(2, 2, 2))); assertTrue(a.equals(a)); assertTrue(a.equals(new goog.math.Vec3(1, 2, 5))); } function testSum() { var a = new goog.math.Vec3(0.5, 0.25, 1.2); var b = new goog.math.Vec3(0.5, 0.75, -0.6); var c = goog.math.Vec3.sum(a, b); assertVec3Equals(new goog.math.Vec3(1, 1, 0.6), c); } function testDifference() { var a = new goog.math.Vec3(0.5, 0.25, 3); var b = new goog.math.Vec3(0.5, 0.75, 5); var c = goog.math.Vec3.difference(a, b); assertVec3Equals(new goog.math.Vec3(0, -0.5, -2), c); } function testDistance() { var a = new goog.math.Vec3(3, 4, 5); var b = new goog.math.Vec3(-3, -4, 5); assertEquals(10, goog.math.Vec3.distance(a, b)); } function testSquaredDistance() { var a = new goog.math.Vec3(3, 4, 5); var b = new goog.math.Vec3(-3, -4, 5); assertEquals(100, goog.math.Vec3.squaredDistance(a, b)); } function testVec3Equals() { assertTrue(goog.math.Vec3.equals(null, null, null)); assertFalse(goog.math.Vec3.equals(null, new goog.math.Vec3())); var a = new goog.math.Vec3(1, 3, 5); assertTrue(goog.math.Vec3.equals(a, a)); assertTrue(goog.math.Vec3.equals(a, new goog.math.Vec3(1, 3, 5))); assertFalse(goog.math.Vec3.equals(1, new goog.math.Vec3(3, 1, 5))); } function testDot() { var a = new goog.math.Vec3(0, 5, 2); var b = new goog.math.Vec3(3, 0, 5); assertEquals(10, goog.math.Vec3.dot(a, b)); var c = new goog.math.Vec3(-5, -5, 5); var d = new goog.math.Vec3(0, 7, -2); assertEquals(-45, goog.math.Vec3.dot(c, d)); } function testCross() { var a = new goog.math.Vec3(3, 0, 0); var b = new goog.math.Vec3(0, 2, 0); assertVec3Equals(new goog.math.Vec3(0, 0, 6), goog.math.Vec3.cross(a, b)); var c = new goog.math.Vec3(1, 2, 3); var d = new goog.math.Vec3(4, 5, 6); assertVec3Equals(new goog.math.Vec3(-3, 6, -3), goog.math.Vec3.cross(c, d)); } function testLerp() { var a = new goog.math.Vec3(0, 0, 0); var b = new goog.math.Vec3(10, 10, 10); for (var i = 0; i <= 10; i++) { var c = goog.math.Vec3.lerp(a, b, i / 10); assertEquals(i, c.x); assertEquals(i, c.y); assertEquals(i, c.z); } } </script> </body> </html>