vec4_test.js 6.0 KB

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