vec4f_test.js 6.8 KB

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