vec4_test.js 6.6 KB

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