vec4f_test.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Copyright 2013 The Closure Library Authors. All Rights Reserved.
  2. // Use of this source code is governed by the Apache License, Version 2.0.
  3. ////////////////////////// NOTE ABOUT EDITING THIS FILE ///////////////////////
  4. // //
  5. // Any edits to this file must be applied to vec4d_test.js by running: //
  6. // swap_type.sh vec4f_test.js > vec4d_test.js //
  7. // //
  8. ////////////////////////// NOTE ABOUT EDITING THIS FILE ///////////////////////
  9. goog.provide('goog.vec.vec4fTest');
  10. goog.setTestOnly('goog.vec.vec4fTest');
  11. goog.require('goog.vec.Float32Array');
  12. goog.require('goog.vec.vec4f');
  13. goog.require('goog.testing.jsunit');
  14. function testCreate() {
  15. var v = goog.vec.vec4f.create();
  16. assertElementsEquals([0, 0, 0, 0], v);
  17. }
  18. function testCreateFromArray() {
  19. var v = goog.vec.vec4f.createFromArray([1, 2, 3, 4]);
  20. assertElementsEquals([1, 2, 3, 4], v);
  21. }
  22. function testCreateFromValues() {
  23. var v = goog.vec.vec4f.createFromValues(1, 2, 3, 4);
  24. assertElementsEquals([1, 2, 3, 4], v);
  25. }
  26. function testClone() {
  27. var v0 = goog.vec.vec4f.createFromValues(1, 2, 3, 4);
  28. var v1 = goog.vec.vec4f.clone(v0);
  29. assertElementsEquals([1, 2, 3, 4], v1);
  30. }
  31. function testSet() {
  32. var v = goog.vec.vec4f.create();
  33. goog.vec.vec4f.setFromValues(v, 1, 2, 3, 4);
  34. assertElementsEquals([1, 2, 3, 4], v);
  35. goog.vec.vec4f.setFromArray(v, [4, 5, 6, 7]);
  36. assertElementsEquals([4, 5, 6, 7], v);
  37. }
  38. function testAdd() {
  39. var v0 = goog.vec.vec4f.setFromArray(goog.vec.vec4f.create(), [1, 2, 3, 4]);
  40. var v1 = goog.vec.vec4f.setFromArray(goog.vec.vec4f.create(), [5, 6, 7, 8]);
  41. var v2 = goog.vec.vec4f.setFromVec4f(goog.vec.vec4f.create(), v0);
  42. goog.vec.vec4f.add(v2, v1, v2);
  43. assertElementsEquals([1, 2, 3, 4], v0);
  44. assertElementsEquals([5, 6, 7, 8], v1);
  45. assertElementsEquals([6, 8, 10, 12], v2);
  46. goog.vec.vec4f.add(goog.vec.vec4f.add(v0, v1, v2), v0, v2);
  47. assertElementsEquals([7, 10, 13, 16], v2);
  48. }
  49. function testSubtract() {
  50. var v0 = goog.vec.vec4f.setFromArray(goog.vec.vec4f.create(), [4, 3, 2, 1]);
  51. var v1 = goog.vec.vec4f.setFromArray(goog.vec.vec4f.create(), [5, 6, 7, 8]);
  52. var v2 = goog.vec.vec4f.setFromVec4f(goog.vec.vec4f.create(), v0);
  53. goog.vec.vec4f.subtract(v2, v1, v2);
  54. assertElementsEquals([4, 3, 2, 1], v0);
  55. assertElementsEquals([5, 6, 7, 8], v1);
  56. assertElementsEquals([-1, -3, -5, -7], v2);
  57. goog.vec.vec4f.setFromValues(v2, 0, 0, 0, 0);
  58. goog.vec.vec4f.subtract(v1, v0, v2);
  59. assertElementsEquals([1, 3, 5, 7], v2);
  60. goog.vec.vec4f.subtract(goog.vec.vec4f.subtract(v1, v0, v2), v0, v2);
  61. assertElementsEquals([-3, 0, 3, 6], v2);
  62. }
  63. function testNegate() {
  64. var v0 = goog.vec.vec4f.setFromArray(goog.vec.vec4f.create(), [1, 2, 3, 4]);
  65. var v1 = goog.vec.vec4f.create();
  66. goog.vec.vec4f.negate(v0, v1);
  67. assertElementsEquals([-1, -2, -3, -4], v1);
  68. assertElementsEquals([1, 2, 3, 4], v0);
  69. goog.vec.vec4f.negate(v0, v0);
  70. assertElementsEquals([-1, -2, -3, -4], v0);
  71. }
  72. function testAbs() {
  73. var v0 = goog.vec.vec4f.setFromArray(goog.vec.vec4f.create(), [-1, -2, -3, -4]);
  74. var v1 = goog.vec.vec4f.create();
  75. goog.vec.vec4f.abs(v0, v1);
  76. assertElementsEquals([1, 2, 3, 4], v1);
  77. assertElementsEquals([-1, -2, -3, -4], v0);
  78. goog.vec.vec4f.abs(v0, v0);
  79. assertElementsEquals([1, 2, 3, 4], v0);
  80. }
  81. function testScale() {
  82. var v0 = goog.vec.vec4f.setFromArray(goog.vec.vec4f.create(), [1, 2, 3, 4]);
  83. var v1 = goog.vec.vec4f.create();
  84. goog.vec.vec4f.scale(v0, 4, v1);
  85. assertElementsEquals([4, 8, 12, 16], v1);
  86. assertElementsEquals([1, 2, 3, 4], v0);
  87. goog.vec.vec4f.setFromArray(v1, v0);
  88. goog.vec.vec4f.scale(v1, 5, v1);
  89. assertElementsEquals([5, 10, 15, 20], v1);
  90. }
  91. function testMagnitudeSquared() {
  92. var v0 = goog.vec.vec4f.setFromArray(goog.vec.vec4f.create(), [1, 2, 3, 4]);
  93. assertEquals(30, goog.vec.vec4f.magnitudeSquared(v0));
  94. }
  95. function testMagnitude() {
  96. var v0 = goog.vec.vec4f.setFromArray(goog.vec.vec4f.create(), [1, 2, 3, 4]);
  97. assertEquals(Math.sqrt(30), goog.vec.vec4f.magnitude(v0));
  98. }
  99. function testNormalize() {
  100. var v0 = goog.vec.vec4f.setFromArray(goog.vec.vec4f.create(), [2, 3, 4, 5]);
  101. var v1 = goog.vec.vec4f.create();
  102. var v2 = goog.vec.vec4f.create();
  103. goog.vec.vec4f.scale(v0, 1 / goog.vec.vec4f.magnitude(v0), v2);
  104. goog.vec.vec4f.normalize(v0, v1);
  105. assertElementsEquals(v2, v1);
  106. assertElementsEquals([2, 3, 4, 5], v0);
  107. goog.vec.vec4f.setFromArray(v1, v0);
  108. goog.vec.vec4f.normalize(v1, v1);
  109. assertElementsEquals(v2, v1);
  110. }
  111. function testDot() {
  112. var v0 = goog.vec.vec4f.setFromArray(goog.vec.vec4f.create(), [1, 2, 3, 4]);
  113. var v1 = goog.vec.vec4f.setFromArray(goog.vec.vec4f.create(), [5, 6, 7, 8]);
  114. assertEquals(70, goog.vec.vec4f.dot(v0, v1));
  115. assertEquals(70, goog.vec.vec4f.dot(v1, v0));
  116. }
  117. function testLerp() {
  118. var v0 = goog.vec.vec4f.setFromValues(goog.vec.vec4f.create(), 1, 2, 3, 4);
  119. var v1 = goog.vec.vec4f.setFromValues(goog.vec.vec4f.create(), 10, 20, 30, 40);
  120. var v2 = goog.vec.vec4f.setFromVec4f(goog.vec.vec4f.create(), v0);
  121. goog.vec.vec4f.lerp(v2, v1, 0, v2);
  122. assertElementsEquals([1, 2, 3, 4], v2);
  123. goog.vec.vec4f.lerp(v2, v1, 1, v2);
  124. assertElementsEquals([10, 20, 30, 40], v2);
  125. goog.vec.vec4f.lerp(v0, v1, .5, v2);
  126. assertElementsEquals([5.5, 11, 16.5, 22], v2);
  127. }
  128. function testMax() {
  129. var v0 = goog.vec.vec4f.setFromValues(goog.vec.vec4f.create(), 10, 20, 30, 40);
  130. var v1 = goog.vec.vec4f.setFromValues(goog.vec.vec4f.create(), 5, 25, 35, 30);
  131. var v2 = goog.vec.vec4f.create();
  132. goog.vec.vec4f.max(v0, v1, v2);
  133. assertElementsEquals([10, 25, 35, 40], v2);
  134. goog.vec.vec4f.max(v1, v0, v1);
  135. assertElementsEquals([10, 25, 35, 40], v1);
  136. goog.vec.vec4f.max(v2, 20, v2);
  137. assertElementsEquals([20, 25, 35, 40], v2);
  138. }
  139. function testMin() {
  140. var v0 = goog.vec.vec4f.setFromValues(goog.vec.vec4f.create(), 10, 20, 30, 40);
  141. var v1 = goog.vec.vec4f.setFromValues(goog.vec.vec4f.create(), 5, 25, 35, 30);
  142. var v2 = goog.vec.vec4f.create();
  143. goog.vec.vec4f.min(v0, v1, v2);
  144. assertElementsEquals([5, 20, 30, 30], v2);
  145. goog.vec.vec4f.min(v1, v0, v1);
  146. assertElementsEquals([5, 20, 30, 30], v1);
  147. goog.vec.vec4f.min(v2, 20, v2);
  148. assertElementsEquals([5, 20, 20, 20], v2);
  149. }
  150. function testEquals() {
  151. var v0 = goog.vec.vec4f.setFromValues(goog.vec.vec4f.create(), 1, 2, 3, 4);
  152. var v1 = goog.vec.vec4f.setFromVec4f(goog.vec.vec4f.create(), v0);
  153. assertElementsEquals(v0, v1);
  154. v1[0] = 5;
  155. assertFalse(goog.vec.vec4f.equals(v0, v1));
  156. v1 = goog.vec.vec4f.setFromVec4f(goog.vec.vec4f.create(), v0);
  157. v1[1] = 5;
  158. assertFalse(goog.vec.vec4f.equals(v0, v1));
  159. v1 = goog.vec.vec4f.setFromVec4f(goog.vec.vec4f.create(), v0);
  160. v1[2] = 5;
  161. assertFalse(goog.vec.vec4f.equals(v0, v1));
  162. v1 = goog.vec.vec4f.setFromVec4f(goog.vec.vec4f.create(), v0);
  163. v1[3] = 5;
  164. assertFalse(goog.vec.vec4f.equals(v0, v1));
  165. }