vec4_test.js 6.5 KB

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