vec2_test.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Copyright 2006 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.math.Vec2Test');
  15. goog.setTestOnly('goog.math.Vec2Test');
  16. goog.require('goog.math.Vec2');
  17. goog.require('goog.testing.jsunit');
  18. function assertVectorEquals(a, b) {
  19. assertTrue(b + ' should be equal to ' + a, goog.math.Vec2.equals(a, b));
  20. }
  21. function testVec2() {
  22. var v = new goog.math.Vec2(3.14, 2.78);
  23. assertEquals(3.14, v.x);
  24. assertEquals(2.78, v.y);
  25. }
  26. function testRandomUnit() {
  27. var a = goog.math.Vec2.randomUnit();
  28. assertRoughlyEquals(1.0, a.magnitude(), 1e-10);
  29. }
  30. function testRandom() {
  31. var a = goog.math.Vec2.random();
  32. assertTrue(a.magnitude() <= 1.0);
  33. }
  34. function testClone() {
  35. var a = new goog.math.Vec2(1, 2);
  36. var b = a.clone();
  37. assertEquals(a.x, b.x);
  38. assertEquals(a.y, b.y);
  39. }
  40. function testMagnitude() {
  41. var a = new goog.math.Vec2(0, 10);
  42. var b = new goog.math.Vec2(3, 4);
  43. assertEquals(10, a.magnitude());
  44. assertEquals(5, b.magnitude());
  45. }
  46. function testSquaredMagnitude() {
  47. var a = new goog.math.Vec2(-3, -4);
  48. assertEquals(25, a.squaredMagnitude());
  49. }
  50. function testScaleFactor() {
  51. var a = new goog.math.Vec2(1, 2);
  52. var scaled = a.scale(0.5);
  53. assertTrue(
  54. 'The type of the return value should be goog.math.Vec2',
  55. scaled instanceof goog.math.Vec2);
  56. assertVectorEquals(new goog.math.Vec2(0.5, 1), a);
  57. }
  58. function testScaleXY() {
  59. var a = new goog.math.Vec2(10, 15);
  60. var scaled = a.scale(2, 3);
  61. assertEquals('The function should return the target instance', a, scaled);
  62. assertTrue(
  63. 'The type of the return value should be goog.math.Vec2',
  64. scaled instanceof goog.math.Vec2);
  65. assertVectorEquals(new goog.math.Vec2(20, 45), a);
  66. }
  67. function testInvert() {
  68. var a = new goog.math.Vec2(3, 4);
  69. a.invert();
  70. assertEquals(-3, a.x);
  71. assertEquals(-4, a.y);
  72. }
  73. function testNormalize() {
  74. var a = new goog.math.Vec2(5, 5);
  75. a.normalize();
  76. assertRoughlyEquals(1.0, a.magnitude(), 1e-10);
  77. }
  78. function testAdd() {
  79. var a = new goog.math.Vec2(1, -1);
  80. a.add(new goog.math.Vec2(3, 3));
  81. assertVectorEquals(new goog.math.Vec2(4, 2), a);
  82. }
  83. function testSubtract() {
  84. var a = new goog.math.Vec2(1, -1);
  85. a.subtract(new goog.math.Vec2(3, 3));
  86. assertVectorEquals(new goog.math.Vec2(-2, -4), a);
  87. }
  88. function testRotate() {
  89. var a = new goog.math.Vec2(1, -1);
  90. a.rotate(Math.PI / 2);
  91. assertRoughlyEquals(1, a.x, 0.000001);
  92. assertRoughlyEquals(1, a.y, 0.000001);
  93. a.rotate(-Math.PI);
  94. assertRoughlyEquals(-1, a.x, 0.000001);
  95. assertRoughlyEquals(-1, a.y, 0.000001);
  96. }
  97. function testRotateAroundPoint() {
  98. var a = goog.math.Vec2.rotateAroundPoint(
  99. new goog.math.Vec2(1, -1), new goog.math.Vec2(1, 0), Math.PI / 2);
  100. assertRoughlyEquals(2, a.x, 0.000001);
  101. assertRoughlyEquals(0, a.y, 0.000001);
  102. }
  103. function testEquals() {
  104. var a = new goog.math.Vec2(1, 2);
  105. assertFalse(a.equals(null));
  106. assertFalse(a.equals({}));
  107. assertFalse(a.equals(new goog.math.Vec2(1, 3)));
  108. assertFalse(a.equals(new goog.math.Vec2(2, 2)));
  109. assertTrue(a.equals(a));
  110. assertTrue(a.equals(new goog.math.Vec2(1, 2)));
  111. }
  112. function testSum() {
  113. var a = new goog.math.Vec2(0.5, 0.25);
  114. var b = new goog.math.Vec2(0.5, 0.75);
  115. var c = goog.math.Vec2.sum(a, b);
  116. assertVectorEquals(new goog.math.Vec2(1, 1), c);
  117. }
  118. function testDifference() {
  119. var a = new goog.math.Vec2(0.5, 0.25);
  120. var b = new goog.math.Vec2(0.5, 0.75);
  121. var c = goog.math.Vec2.difference(a, b);
  122. assertVectorEquals(new goog.math.Vec2(0, -0.5), c);
  123. }
  124. function testDistance() {
  125. var a = new goog.math.Vec2(3, 4);
  126. var b = new goog.math.Vec2(-3, -4);
  127. assertEquals(10, goog.math.Vec2.distance(a, b));
  128. }
  129. function testSquaredDistance() {
  130. var a = new goog.math.Vec2(3, 4);
  131. var b = new goog.math.Vec2(-3, -4);
  132. assertEquals(100, goog.math.Vec2.squaredDistance(a, b));
  133. }
  134. function testVec2Equals() {
  135. assertTrue(goog.math.Vec2.equals(null, null));
  136. assertFalse(goog.math.Vec2.equals(null, new goog.math.Vec2()));
  137. var a = new goog.math.Vec2(1, 3);
  138. assertTrue(goog.math.Vec2.equals(a, a));
  139. assertTrue(goog.math.Vec2.equals(a, new goog.math.Vec2(1, 3)));
  140. assertFalse(goog.math.Vec2.equals(1, new goog.math.Vec2(3, 1)));
  141. }
  142. function testDot() {
  143. var a = new goog.math.Vec2(0, 5);
  144. var b = new goog.math.Vec2(3, 0);
  145. assertEquals(0, goog.math.Vec2.dot(a, b));
  146. var c = new goog.math.Vec2(-5, -5);
  147. var d = new goog.math.Vec2(0, 7);
  148. assertEquals(-35, goog.math.Vec2.dot(c, d));
  149. }
  150. function testDeterminant() {
  151. var a = new goog.math.Vec2(0, 5);
  152. var b = new goog.math.Vec2(0, 10);
  153. assertEquals(0, goog.math.Vec2.determinant(a, b));
  154. var c = new goog.math.Vec2(0, 5);
  155. var d = new goog.math.Vec2(10, 0);
  156. assertEquals(-50, goog.math.Vec2.determinant(c, d));
  157. var e = new goog.math.Vec2(-5, -5);
  158. var f = new goog.math.Vec2(0, 7);
  159. assertEquals(-35, goog.math.Vec2.determinant(e, f));
  160. }
  161. function testLerp() {
  162. var a = new goog.math.Vec2(0, 0);
  163. var b = new goog.math.Vec2(10, 10);
  164. for (var i = 0; i <= 10; i++) {
  165. var c = goog.math.Vec2.lerp(a, b, i / 10);
  166. assertEquals(i, c.x);
  167. assertEquals(i, c.y);
  168. }
  169. }
  170. function testToString() {
  171. testEquals('(0, 0)', new goog.math.Vec2(0, 0).toString());
  172. }