mat2d-spec.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. import * as mat2d from "../../src/gl-matrix/mat2d"
  2. describe("mat2d", function() {
  3. let out, matA, matB, oldA, oldB, identity, result;
  4. beforeEach(function() {
  5. matA = [1, 2,
  6. 3, 4,
  7. 5, 6];
  8. oldA = [1, 2,
  9. 3, 4,
  10. 5, 6];
  11. matB = [7, 8,
  12. 9, 10,
  13. 11, 12];
  14. oldB = [7, 8,
  15. 9, 10,
  16. 11, 12];
  17. out = [0, 0,
  18. 0, 0,
  19. 0, 0];
  20. identity = [1, 0,
  21. 0, 1,
  22. 0, 0];
  23. });
  24. describe("create", function() {
  25. beforeEach(function() { result = mat2d.create(); });
  26. it("should return a 6 element array initialized to a 2x3 identity matrix", function() { expect(result).toBeEqualish(identity); });
  27. });
  28. describe("clone", function() {
  29. beforeEach(function() { result = mat2d.clone(matA); });
  30. it("should return a 6 element array initialized to the values in matA", function() { expect(result).toBeEqualish(matA); });
  31. });
  32. describe("copy", function() {
  33. beforeEach(function() { result = mat2d.copy(out, matA); });
  34. it("should place values into out", function() { expect(out).toBeEqualish(matA); });
  35. it("should return out", function() { expect(result).toBe(out); });
  36. });
  37. describe("identity", function() {
  38. beforeEach(function() { result = mat2d.identity(out); });
  39. it("should place values into out", function() { expect(result).toBeEqualish(identity); });
  40. it("should return out", function() { expect(result).toBe(out); });
  41. });
  42. describe("invert", function() {
  43. describe("with a separate output matrix", function() {
  44. beforeEach(function() { result = mat2d.invert(out, matA); });
  45. it("should place values into out", function() { expect(out).toBeEqualish([ -2, 1, 1.5, -0.5, 1, -2 ]); });
  46. it("should return out", function() { expect(result).toBe(out); });
  47. it("should not modify matA", function() { expect(matA).toBeEqualish(oldA); });
  48. });
  49. describe("when matA is the output matrix", function() {
  50. beforeEach(function() { result = mat2d.invert(matA, matA); });
  51. it("should place values into matA", function() { expect(matA).toBeEqualish([ -2, 1, 1.5, -0.5, 1, -2 ]); });
  52. it("should return matA", function() { expect(result).toBe(matA); });
  53. });
  54. });
  55. describe("determinant", function() {
  56. beforeEach(function() { result = mat2d.determinant(matA); });
  57. it("should return the determinant", function() { expect(result).toEqual(-2); });
  58. });
  59. describe("multiply", function() {
  60. it("should have an alias called 'mul'", function() { expect(mat2d.mul).toEqual(mat2d.multiply); });
  61. describe("with a separate output matrix", function() {
  62. beforeEach(function() { result = mat2d.multiply(out, matA, matB); });
  63. it("should place values into out", function() { expect(out).toBeEqualish([31, 46, 39, 58, 52, 76]); });
  64. it("should return out", function() { expect(result).toBe(out); });
  65. it("should not modify matA", function() { expect(matA).toBeEqualish(oldA); });
  66. it("should not modify matB", function() { expect(matB).toBeEqualish(oldB); });
  67. });
  68. describe("when matA is the output matrix", function() {
  69. beforeEach(function() { result = mat2d.multiply(matA, matA, matB); });
  70. it("should place values into matA", function() { expect(matA).toBeEqualish([31, 46, 39, 58, 52, 76]); });
  71. it("should return matA", function() { expect(result).toBe(matA); });
  72. it("should not modify matB", function() { expect(matB).toBeEqualish(oldB); });
  73. });
  74. describe("when matB is the output matrix", function() {
  75. beforeEach(function() { result = mat2d.multiply(matB, matA, matB); });
  76. it("should place values into matB", function() { expect(matB).toBeEqualish([31, 46, 39, 58, 52, 76]); });
  77. it("should return matB", function() { expect(result).toBe(matB); });
  78. it("should not modify matA", function() { expect(matA).toBeEqualish(oldA); });
  79. });
  80. });
  81. describe("rotate", function() {
  82. describe("with a separate output matrix", function() {
  83. beforeEach(function() { result = mat2d.rotate(out, matA, Math.PI * 0.5); });
  84. it("should place values into out", function() { expect(out).toBeEqualish([3, 4, -1, -2, 5, 6]); });
  85. it("should return out", function() { expect(result).toBe(out); });
  86. it("should not modify matA", function() { expect(matA).toBeEqualish(oldA); });
  87. });
  88. describe("when matA is the output matrix", function() {
  89. beforeEach(function() { result = mat2d.rotate(matA, matA, Math.PI * 0.5); });
  90. it("should place values into matA", function() { expect(matA).toBeEqualish([3, 4, -1, -2, 5, 6]); });
  91. it("should return matA", function() { expect(result).toBe(matA); });
  92. });
  93. });
  94. describe("scale", function() {
  95. let vecA;
  96. beforeEach(function() { vecA = [2, 3]; });
  97. describe("with a separate output matrix", function() {
  98. beforeEach(function() { result = mat2d.scale(out, matA, vecA); });
  99. it("should place values into out", function() { expect(out).toBeEqualish([2, 4, 9, 12, 5, 6]); });
  100. it("should return out", function() { expect(result).toBe(out); });
  101. it("should not modify matA", function() { expect(matA).toBeEqualish(oldA); });
  102. });
  103. describe("when matA is the output matrix", function() {
  104. beforeEach(function() { result = mat2d.scale(matA, matA, vecA); });
  105. it("should place values into matA", function() { expect(matA).toBeEqualish([2, 4, 9, 12, 5, 6]); });
  106. it("should return matA", function() { expect(result).toBe(matA); });
  107. });
  108. });
  109. describe("translate", function() {
  110. let vecA;
  111. beforeEach(function() { vecA = [2, 3]; });
  112. describe("with a separate output matrix", function() {
  113. beforeEach(function() { result = mat2d.translate(out, matA, vecA); });
  114. it("should place values into out", function() { expect(out).toBeEqualish([1, 2, 3, 4, 16, 22]); });
  115. it("should return out", function() { expect(result).toBe(out); });
  116. it("should not modify matA", function() { expect(matA).toBeEqualish(oldA); });
  117. });
  118. describe("when matA is the output matrix", function() {
  119. beforeEach(function() { result = mat2d.translate(matA, matA, vecA); });
  120. it("should place values into matA", function() { expect(matA).toBeEqualish([1, 2, 3, 4, 16, 22]); });
  121. it("should return matA", function() { expect(result).toBe(matA); });
  122. });
  123. });
  124. describe("str", function() {
  125. beforeEach(function() { result = mat2d.str(matA); });
  126. it("should return a string representation of the matrix", function() { expect(result).toEqual("mat2d(1, 2, 3, 4, 5, 6)"); });
  127. });
  128. describe("frob", function() {
  129. beforeEach(function() { result = mat2d.frob(matA); });
  130. it("should return the Frobenius Norm of the matrix", function() { expect(result).toEqual( Math.sqrt(Math.pow(1, 2) + Math.pow(2, 2) + Math.pow(3, 2) + Math.pow(4, 2) + Math.pow(5, 2) + Math.pow(6, 2) + 1)); });
  131. });
  132. describe("add", function() {
  133. describe("with a separate output matrix", function() {
  134. beforeEach(function() { result = mat2d.add(out, matA, matB); });
  135. it("should place values into out", function() { expect(out).toBeEqualish([8, 10, 12, 14, 16, 18]); });
  136. it("should return out", function() { expect(result).toBe(out); });
  137. it("should not modify matA", function() { expect(matA).toBeEqualish(oldA); });
  138. it("should not modify matB", function() { expect(matB).toBeEqualish(oldB); });
  139. });
  140. describe("when matA is the output matrix", function() {
  141. beforeEach(function() { result = mat2d.add(matA, matA, matB); });
  142. it("should place values into matA", function() { expect(matA).toBeEqualish([8, 10, 12, 14, 16, 18]); });
  143. it("should return matA", function() { expect(result).toBe(matA); });
  144. it("should not modify matB", function() { expect(matB).toBeEqualish(oldB); });
  145. });
  146. describe("when matB is the output matrix", function() {
  147. beforeEach(function() { result = mat2d.add(matB, matA, matB); });
  148. it("should place values into matB", function() { expect(matB).toBeEqualish([8, 10, 12, 14, 16, 18]); });
  149. it("should return matB", function() { expect(result).toBe(matB); });
  150. it("should not modify matA", function() { expect(matA).toBeEqualish(oldA); });
  151. });
  152. });
  153. describe("subtract", function() {
  154. it("should have an alias called 'sub'", function() { expect(mat2d.sub).toEqual(mat2d.subtract); });
  155. describe("with a separate output matrix", function() {
  156. beforeEach(function() { result = mat2d.subtract(out, matA, matB); });
  157. it("should place values into out", function() { expect(out).toBeEqualish([-6, -6, -6, -6, -6, -6]); });
  158. it("should return out", function() { expect(result).toBe(out); });
  159. it("should not modify matA", function() { expect(matA).toBeEqualish(oldA); });
  160. it("should not modify matB", function() { expect(matB).toBeEqualish(oldB); });
  161. });
  162. describe("when matA is the output matrix", function() {
  163. beforeEach(function() { result = mat2d.subtract(matA, matA, matB); });
  164. it("should place values into matA", function() { expect(matA).toBeEqualish([-6, -6, -6, -6, -6, -6]); });
  165. it("should return matA", function() { expect(result).toBe(matA); });
  166. it("should not modify matB", function() { expect(matB).toBeEqualish(oldB); });
  167. });
  168. describe("when matB is the output matrix", function() {
  169. beforeEach(function() { result = mat2d.subtract(matB, matA, matB); });
  170. it("should place values into matB", function() { expect(matB).toBeEqualish([-6, -6, -6, -6, -6, -6]); });
  171. it("should return matB", function() { expect(result).toBe(matB); });
  172. it("should not modify matA", function() { expect(matA).toBeEqualish(oldA); });
  173. });
  174. });
  175. describe("fromValues", function() {
  176. beforeEach(function() { result = mat2d.fromValues(1, 2, 3, 4, 5, 6); });
  177. it("should return a 6 element array initialized to the values passed", function() { expect(result).toBeEqualish([1, 2, 3, 4, 5, 6]); });
  178. });
  179. describe("set", function() {
  180. beforeEach(function() { result = mat2d.set(out, 1, 2, 3, 4, 5, 6); });
  181. it("should place values into out", function() { expect(out).toBeEqualish([1, 2, 3, 4, 5, 6]); });
  182. it("should return out", function() { expect(result).toBe(out); });
  183. });
  184. describe("multiplyScalar", function() {
  185. describe("with a separate output matrix", function() {
  186. beforeEach(function() { result = mat2d.multiplyScalar(out, matA, 2); });
  187. it("should place values into out", function() { expect(out).toBeEqualish([2, 4, 6, 8, 10, 12]); });
  188. it("should return out", function() { expect(result).toBe(out); });
  189. it("should not modify matA", function() { expect(matA).toBeEqualish([1, 2, 3, 4, 5, 6]); });
  190. });
  191. describe("when matA is the output matrix", function() {
  192. beforeEach(function() { result = mat2d.multiplyScalar(matA, matA, 2); });
  193. it("should place values into matA", function() { expect(matA).toBeEqualish([2, 4, 6, 8, 10, 12]); });
  194. it("should return matA", function() { expect(result).toBe(matA); });
  195. });
  196. });
  197. describe("multiplyScalarAndAdd", function() {
  198. describe("with a separate output matrix", function() {
  199. beforeEach(function() { result = mat2d.multiplyScalarAndAdd(out, matA, matB, 0.5); });
  200. it("should place values into out", function() { expect(out).toBeEqualish([4.5, 6, 7.5, 9, 10.5, 12]); });
  201. it("should return out", function() { expect(result).toBe(out); });
  202. it("should not modify matA", function() { expect(matA).toBeEqualish([1, 2, 3, 4, 5, 6]); });
  203. it("should not modify matB", function() { expect(matB).toBeEqualish([7, 8, 9, 10, 11, 12]); });
  204. });
  205. describe("when matA is the output matrix", function() {
  206. beforeEach(function() { result = mat2d.multiplyScalarAndAdd(matA, matA, matB, 0.5); });
  207. it("should place values into matA", function() { expect(matA).toBeEqualish([4.5, 6, 7.5, 9, 10.5, 12]); });
  208. it("should return matA", function() { expect(result).toBe(matA); });
  209. it("should not modify matB", function() { expect(matB).toBeEqualish([7, 8, 9, 10, 11, 12]); });
  210. });
  211. describe("when matB is the output matrix", function() {
  212. beforeEach(function() { result = mat2d.multiplyScalarAndAdd(matB, matA, matB, 0.5); });
  213. it("should place values into matB", function() { expect(matB).toBeEqualish([4.5, 6, 7.5, 9, 10.5, 12]); });
  214. it("should return matB", function() { expect(result).toBe(matB); });
  215. it("should not modify matA", function() { expect(matA).toBeEqualish([1, 2, 3, 4, 5, 6]); });
  216. });
  217. });
  218. describe("exactEquals", function() {
  219. let matC, r0, r1;
  220. beforeEach(function() {
  221. matA = [0, 1, 2, 3, 4, 5];
  222. matB = [0, 1, 2, 3, 4, 5];
  223. matC = [1, 2, 3, 4, 5, 6];
  224. r0 = mat2d.exactEquals(matA, matB);
  225. r1 = mat2d.exactEquals(matA, matC);
  226. });
  227. it("should return true for identical matrices", function() { expect(r0).toBe(true); });
  228. it("should return false for different matrices", function() { expect(r1).toBe(false); });
  229. it("should not modify matA", function() { expect(matA).toBeEqualish([0, 1, 2, 3, 4, 5]); });
  230. it("should not modify matB", function() { expect(matB).toBeEqualish([0, 1, 2, 3, 4, 5]); });
  231. });
  232. describe("equals", function() {
  233. let matC, matD, r0, r1, r2;
  234. beforeEach(function() {
  235. matA = [0, 1, 2, 3, 4, 5];
  236. matB = [0, 1, 2, 3, 4, 5];
  237. matC = [1, 2, 3, 4, 5, 6];
  238. matD = [1e-16, 1, 2, 3, 4, 5];
  239. r0 = mat2d.equals(matA, matB);
  240. r1 = mat2d.equals(matA, matC);
  241. r2 = mat2d.equals(matA, matD);
  242. });
  243. it("should return true for identical matrices", function() { expect(r0).toBe(true); });
  244. it("should return false for different matrices", function() { expect(r1).toBe(false); });
  245. it("should return true for close but not identical matrices", function() { expect(r2).toBe(true); });
  246. it("should not modify matA", function() { expect(matA).toBeEqualish([0, 1, 2, 3, 4, 5]); });
  247. it("should not modify matB", function() { expect(matB).toBeEqualish([0, 1, 2, 3, 4, 5]); });
  248. });
  249. });