vec2_test.js 6.3 KB

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