vec2_test.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // Copyright 2012 The Closure Library Authors. All Rights Reserved.
  2. // Use of this source code is governed by the Apache License, Version 2.0.
  3. goog.provide('goog.vec.Vec2Test');
  4. goog.setTestOnly('goog.vec.Vec2Test');
  5. goog.require('goog.vec.Float32Array');
  6. goog.require('goog.vec.Vec2');
  7. goog.require('goog.testing.jsunit');
  8. function testConstructor() {
  9. var v = goog.vec.Vec2.createFloat32();
  10. assertElementsEquals(0, v[0]);
  11. assertEquals(0, v[1]);
  12. assertElementsEquals([0, 0], goog.vec.Vec2.createFloat32());
  13. goog.vec.Vec2.setFromValues(v, 1, 2);
  14. assertElementsEquals([1, 2], v);
  15. var w = goog.vec.Vec2.createFloat64();
  16. assertElementsEquals(0, w[0]);
  17. assertEquals(0, w[1]);
  18. assertElementsEquals([0, 0], goog.vec.Vec2.createFloat64());
  19. goog.vec.Vec2.setFromValues(w, 1, 2);
  20. assertElementsEquals([1, 2], w);
  21. }
  22. function testSet() {
  23. var v = goog.vec.Vec2.createFloat32();
  24. goog.vec.Vec2.setFromValues(v, 1, 2);
  25. assertElementsEquals([1, 2], v);
  26. goog.vec.Vec2.setFromArray(v, [4, 5]);
  27. assertElementsEquals([4, 5], v);
  28. var w = goog.vec.Vec2.createFloat32();
  29. goog.vec.Vec2.setFromValues(w, 1, 2);
  30. assertElementsEquals([1, 2], w);
  31. goog.vec.Vec2.setFromArray(w, [4, 5]);
  32. assertElementsEquals([4, 5], w);
  33. }
  34. function testAdd() {
  35. var v0 = goog.vec.Vec2.createFloat32FromArray([1, 2]);
  36. var v1 = goog.vec.Vec2.createFloat32FromArray([4, 5]);
  37. var v2 = goog.vec.Vec2.cloneFloat32(v0);
  38. goog.vec.Vec2.add(v2, v1, v2);
  39. assertElementsEquals([1, 2], v0);
  40. assertElementsEquals([4, 5], v1);
  41. assertElementsEquals([5, 7], v2);
  42. goog.vec.Vec2.add(goog.vec.Vec2.add(v0, v1, v2), v0, v2);
  43. assertElementsEquals([6, 9], v2);
  44. }
  45. function testSubtract() {
  46. var v0 = goog.vec.Vec2.createFloat32FromArray([1, 2]);
  47. var v1 = goog.vec.Vec2.createFloat32FromArray([4, 5]);
  48. var v2 = goog.vec.Vec2.cloneFloat32(v0);
  49. goog.vec.Vec2.subtract(v2, v1, v2);
  50. assertElementsEquals([1, 2], v0);
  51. assertElementsEquals([4, 5], v1);
  52. assertElementsEquals([-3, -3], v2);
  53. goog.vec.Vec2.setFromValues(v2, 0, 0, 0);
  54. goog.vec.Vec2.subtract(v1, v0, v2);
  55. assertElementsEquals([3, 3], v2);
  56. v2 = goog.vec.Vec2.cloneFloat32(v0);
  57. goog.vec.Vec2.subtract(v2, v1, v2);
  58. assertElementsEquals([-3, -3], v2);
  59. goog.vec.Vec2.subtract(goog.vec.Vec2.subtract(v1, v0, v2), v0, v2);
  60. assertElementsEquals([2, 1], v2);
  61. }
  62. function testNegate() {
  63. var v0 = goog.vec.Vec2.createFloat32FromArray([1, 2]);
  64. var v1 = goog.vec.Vec2.createFloat32();
  65. goog.vec.Vec2.negate(v0, v1);
  66. assertElementsEquals([-1, -2], v1);
  67. assertElementsEquals([1, 2], v0);
  68. goog.vec.Vec2.negate(v0, v0);
  69. assertElementsEquals([-1, -2], v0);
  70. }
  71. function testAbs() {
  72. var v0 = goog.vec.Vec2.createFloat32FromValues(-1, -2);
  73. var v1 = goog.vec.Vec2.createFloat32();
  74. goog.vec.Vec2.abs(v0, v1);
  75. assertElementsEquals([1, 2], v1);
  76. assertElementsEquals([-1, -2], v0);
  77. goog.vec.Vec2.abs(v0, v0);
  78. assertElementsEquals([1, 2], v0);
  79. }
  80. function testScale() {
  81. var v0 = goog.vec.Vec2.createFloat32FromArray([1, 2]);
  82. var v1 = goog.vec.Vec2.createFloat32();
  83. goog.vec.Vec2.scale(v0, 4, v1);
  84. assertElementsEquals([4, 8], v1);
  85. assertElementsEquals([1, 2], v0);
  86. goog.vec.Vec2.setFromArray(v1, v0);
  87. goog.vec.Vec2.scale(v1, 5, v1);
  88. assertElementsEquals([5, 10], v1);
  89. }
  90. function testMagnitudeSquared() {
  91. var v0 = goog.vec.Vec2.createFloat32FromArray([1, 2]);
  92. assertEquals(5, goog.vec.Vec2.magnitudeSquared(v0));
  93. }
  94. function testMagnitude() {
  95. var v0 = goog.vec.Vec2.createFloat32FromArray([1, 2]);
  96. assertEquals(Math.sqrt(5), goog.vec.Vec2.magnitude(v0));
  97. }
  98. function testNormalize() {
  99. var v0 = goog.vec.Vec2.createFloat32FromArray([2, 3]);
  100. var v1 = goog.vec.Vec2.createFloat32();
  101. var v2 = goog.vec.Vec2.createFloat32();
  102. goog.vec.Vec2.scale(
  103. v0, 1 / goog.vec.Vec2.magnitude(v0), v2);
  104. goog.vec.Vec2.normalize(v0, v1);
  105. assertElementsEquals(v2, v1);
  106. assertElementsEquals([2, 3], v0);
  107. goog.vec.Vec2.setFromArray(v1, v0);
  108. goog.vec.Vec2.normalize(v1, v1);
  109. assertElementsEquals(v2, v1);
  110. }
  111. function testDot() {
  112. var v0 = goog.vec.Vec2.createFloat32FromArray([1, 2]);
  113. var v1 = goog.vec.Vec2.createFloat32FromArray([4, 5]);
  114. assertEquals(14, goog.vec.Vec2.dot(v0, v1));
  115. assertEquals(14, goog.vec.Vec2.dot(v1, v0));
  116. }
  117. function testDistanceSquared() {
  118. var v0 = goog.vec.Vec2.createFloat32FromValues(1, 2);
  119. var v1 = goog.vec.Vec2.createFloat32FromValues(1, 2);
  120. assertEquals(0, goog.vec.Vec2.distanceSquared(v0, v1));
  121. goog.vec.Vec2.setFromValues(v0, 1, 2);
  122. goog.vec.Vec2.setFromValues(v1, -1, -2);
  123. assertEquals(20, goog.vec.Vec2.distanceSquared(v0, v1));
  124. }
  125. function testDistance() {
  126. var v0 = goog.vec.Vec2.createFloat32FromValues(1, 2);
  127. var v1 = goog.vec.Vec2.createFloat32FromValues(1, 2);
  128. assertEquals(0, goog.vec.Vec2.distance(v0, v1));
  129. goog.vec.Vec2.setFromValues(v0, 2, 3);
  130. goog.vec.Vec2.setFromValues(v1, -2, 0);
  131. assertEquals(5, goog.vec.Vec2.distance(v0, v1));
  132. }
  133. function testDirection() {
  134. var v0 = goog.vec.Vec2.createFloat32FromValues(1, 2);
  135. var v1 = goog.vec.Vec2.createFloat32FromValues(1, 2);
  136. var dirVec = goog.vec.Vec2.createFloat32FromValues(4, 5);
  137. goog.vec.Vec2.direction(v0, v1, dirVec);
  138. assertElementsEquals([0, 0], dirVec);
  139. goog.vec.Vec2.setFromValues(v0, 0, 0);
  140. goog.vec.Vec2.setFromValues(v1, 1, 0);
  141. goog.vec.Vec2.direction(v0, v1, dirVec);
  142. assertElementsEquals([1, 0], dirVec);
  143. goog.vec.Vec2.setFromValues(v0, 1, 1);
  144. goog.vec.Vec2.setFromValues(v1, 0, 0);
  145. goog.vec.Vec2.direction(v0, v1, dirVec);
  146. assertElementsRoughlyEqual(
  147. [-0.707106781, -0.707106781],
  148. dirVec, goog.vec.EPSILON);
  149. }
  150. function testLerp() {
  151. var v0 = goog.vec.Vec2.createFloat32FromValues(1, 2);
  152. var v1 = goog.vec.Vec2.createFloat32FromValues(10, 20);
  153. var v2 = goog.vec.Vec2.cloneFloat32(v0);
  154. goog.vec.Vec2.lerp(v2, v1, 0, v2);
  155. assertElementsEquals([1, 2], v2);
  156. goog.vec.Vec2.lerp(v2, v1, 1, v2);
  157. assertElementsEquals([10, 20], v2);
  158. goog.vec.Vec2.lerp(v0, v1, .5, v2);
  159. assertElementsEquals([5.5, 11], v2);
  160. }
  161. function testMax() {
  162. var v0 = goog.vec.Vec2.createFloat32FromValues(10, 20);
  163. var v1 = goog.vec.Vec2.createFloat32FromValues(5, 25);
  164. var v2 = goog.vec.Vec2.createFloat32();
  165. goog.vec.Vec2.max(v0, v1, v2);
  166. assertElementsEquals([10, 25], v2);
  167. goog.vec.Vec2.max(v1, v0, v1);
  168. assertElementsEquals([10, 25], v1);
  169. goog.vec.Vec2.max(v2, 20, v2);
  170. assertElementsEquals([20, 25], v2);
  171. }
  172. function testMin() {
  173. var v0 = goog.vec.Vec2.createFloat32FromValues(10, 20);
  174. var v1 = goog.vec.Vec2.createFloat32FromValues(5, 25);
  175. var v2 = goog.vec.Vec2.createFloat32();
  176. goog.vec.Vec2.min(v0, v1, v2);
  177. assertElementsEquals([5, 20], v2);
  178. goog.vec.Vec2.min(v1, v0, v1);
  179. assertElementsEquals([5, 20], v1);
  180. goog.vec.Vec2.min(v2, 10, v2);
  181. assertElementsEquals([5, 10], v2);
  182. }
  183. function testEquals() {
  184. var v0 = goog.vec.Vec2.createFloat32FromValues(1, 2);
  185. var v1 = goog.vec.Vec2.cloneFloat32(v0);
  186. assertElementsEquals(v0, v1);
  187. v1[0] = 4;
  188. assertFalse(goog.vec.Vec2.equals(v0, v1));
  189. v1 = goog.vec.Vec2.cloneFloat32(v0);
  190. v1[1] = 4;
  191. assertFalse(goog.vec.Vec2.equals(v0, v1));
  192. }