vec4-spec.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. import * as vec3 from "../../src/gl-matrix/vec3"
  2. import * as vec4 from "../../src/gl-matrix/vec4"
  3. describe("vec4", function() {
  4. let out, vecA, vecB, result;
  5. beforeEach(function() { vecA = [1, 2, 3, 4]; vecB = [5, 6, 7, 8]; out = [0, 0, 0, 0]; });
  6. describe("create", function() {
  7. beforeEach(function() { result = vec4.create(); });
  8. it("should return a 4 element array initialized to 0s", function() { expect(result).toBeEqualish([0, 0, 0, 0]); });
  9. });
  10. describe("clone", function() {
  11. beforeEach(function() { result = vec4.clone(vecA); });
  12. it("should return a 4 element array initialized to the values in vecA", function() { expect(result).toBeEqualish(vecA); });
  13. });
  14. describe("fromValues", function() {
  15. beforeEach(function() { result = vec4.fromValues(1, 2, 3, 4); });
  16. it("should return a 4 element array initialized to the values passed", function() { expect(result).toBeEqualish([1, 2, 3, 4]); });
  17. });
  18. describe("copy", function() {
  19. beforeEach(function() { result = vec4.copy(out, vecA); });
  20. it("should place values into out", function() { expect(out).toBeEqualish([1, 2, 3, 4]); });
  21. it("should return out", function() { expect(result).toBe(out); });
  22. });
  23. describe("set", function() {
  24. beforeEach(function() { result = vec4.set(out, 1, 2, 3, 4); });
  25. it("should place values into out", function() { expect(out).toBeEqualish([1, 2, 3, 4]); });
  26. it("should return out", function() { expect(result).toBe(out); });
  27. });
  28. describe("add", function() {
  29. describe("with a separate output vector", function() {
  30. beforeEach(function() { result = vec4.add(out, vecA, vecB); });
  31. it("should place values into out", function() { expect(out).toBeEqualish([6, 8, 10, 12]); });
  32. it("should return out", function() { expect(result).toBe(out); });
  33. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3, 4]); });
  34. it("should not modify vecB", function() { expect(vecB).toBeEqualish([5, 6, 7, 8]); });
  35. });
  36. describe("when vecA is the output vector", function() {
  37. beforeEach(function() { result = vec4.add(vecA, vecA, vecB); });
  38. it("should place values into vecA", function() { expect(vecA).toBeEqualish([6, 8, 10, 12]); });
  39. it("should return vecA", function() { expect(result).toBe(vecA); });
  40. it("should not modify vecB", function() { expect(vecB).toBeEqualish([5, 6, 7, 8]); });
  41. });
  42. describe("when vecB is the output vector", function() {
  43. beforeEach(function() { result = vec4.add(vecB, vecA, vecB); });
  44. it("should place values into vecB", function() { expect(vecB).toBeEqualish([6, 8, 10, 12]); });
  45. it("should return vecB", function() { expect(result).toBe(vecB); });
  46. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3, 4]); });
  47. });
  48. });
  49. describe("subtract", function() {
  50. it("should have an alias called 'sub'", function() { expect(vec4.sub).toEqual(vec4.subtract); });
  51. describe("with a separate output vector", function() {
  52. beforeEach(function() { result = vec4.subtract(out, vecA, vecB); });
  53. it("should place values into out", function() { expect(out).toBeEqualish([-4, -4, -4, -4]); });
  54. it("should return out", function() { expect(result).toBe(out); });
  55. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3, 4]); });
  56. it("should not modify vecB", function() { expect(vecB).toBeEqualish([5, 6, 7, 8]); });
  57. });
  58. describe("when vecA is the output vector", function() {
  59. beforeEach(function() { result = vec4.subtract(vecA, vecA, vecB); });
  60. it("should place values into vecA", function() { expect(vecA).toBeEqualish([-4, -4, -4, -4]); });
  61. it("should return vecA", function() { expect(result).toBe(vecA); });
  62. it("should not modify vecB", function() { expect(vecB).toBeEqualish([5, 6, 7, 8]); });
  63. });
  64. describe("when vecB is the output vector", function() {
  65. beforeEach(function() { result = vec4.subtract(vecB, vecA, vecB); });
  66. it("should place values into vecB", function() { expect(vecB).toBeEqualish([-4, -4, -4, -4]); });
  67. it("should return vecB", function() { expect(result).toBe(vecB); });
  68. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3, 4]); });
  69. });
  70. });
  71. describe("multiply", function() {
  72. it("should have an alias called 'mul'", function() { expect(vec4.mul).toEqual(vec4.multiply); });
  73. describe("with a separate output vector", function() {
  74. beforeEach(function() { result = vec4.multiply(out, vecA, vecB); });
  75. it("should place values into out", function() { expect(out).toBeEqualish([5, 12, 21, 32]); });
  76. it("should return out", function() { expect(result).toBe(out); });
  77. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3, 4]); });
  78. it("should not modify vecB", function() { expect(vecB).toBeEqualish([5, 6, 7, 8]); });
  79. });
  80. describe("when vecA is the output vector", function() {
  81. beforeEach(function() { result = vec4.multiply(vecA, vecA, vecB); });
  82. it("should place values into vecA", function() { expect(vecA).toBeEqualish([5, 12, 21, 32]); });
  83. it("should return vecA", function() { expect(result).toBe(vecA); });
  84. it("should not modify vecB", function() { expect(vecB).toBeEqualish([5, 6, 7, 8]); });
  85. });
  86. describe("when vecB is the output vector", function() {
  87. beforeEach(function() { result = vec4.multiply(vecB, vecA, vecB); });
  88. it("should place values into vecB", function() { expect(vecB).toBeEqualish([5, 12, 21, 32]); });
  89. it("should return vecB", function() { expect(result).toBe(vecB); });
  90. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3, 4]); });
  91. });
  92. });
  93. describe("divide", function() {
  94. it("should have an alias called 'div'", function() { expect(vec4.div).toEqual(vec4.divide); });
  95. describe("with a separate output vector", function() {
  96. beforeEach(function() { result = vec4.divide(out, vecA, vecB); });
  97. it("should place values into out", function() { expect(out).toBeEqualish([0.2, 0.333333, 0.428571, 0.5]); });
  98. it("should return out", function() { expect(result).toBe(out); });
  99. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3, 4]); });
  100. it("should not modify vecB", function() { expect(vecB).toBeEqualish([5, 6, 7, 8]); });
  101. });
  102. describe("when vecA is the output vector", function() {
  103. beforeEach(function() { result = vec4.divide(vecA, vecA, vecB); });
  104. it("should place values into vecA", function() { expect(vecA).toBeEqualish([0.2, 0.333333, 0.428571, 0.5]); });
  105. it("should return vecA", function() { expect(result).toBe(vecA); });
  106. it("should not modify vecB", function() { expect(vecB).toBeEqualish([5, 6, 7, 8]); });
  107. });
  108. describe("when vecB is the output vector", function() {
  109. beforeEach(function() { result = vec4.divide(vecB, vecA, vecB); });
  110. it("should place values into vecB", function() { expect(vecB).toBeEqualish([0.2, 0.333333, 0.428571, 0.5]); });
  111. it("should return vecB", function() { expect(result).toBe(vecB); });
  112. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3, 4]); });
  113. });
  114. });
  115. describe("ceil", function() {
  116. beforeEach(function() { vecA = [Math.E, Math.PI, Math.SQRT2, Math.SQRT1_2]; });
  117. describe("with a separate output vector", function() {
  118. beforeEach(function() { result = vec4.ceil(out, vecA); });
  119. it("should place values into out", function() { expect(out).toBeEqualish([3, 4, 2, 1]); });
  120. it("should return out", function() { expect(result).toBe(out); });
  121. it("should not modify vecA", function() { expect(vecA).toBeEqualish([Math.E, Math.PI, Math.SQRT2, Math.SQRT1_2]); });
  122. });
  123. describe("when vecA is the output vector", function() {
  124. beforeEach(function() { result = vec4.ceil(vecA, vecA); });
  125. it("should place values into vecA", function() { expect(vecA).toBeEqualish([3, 4, 2, 1]); });
  126. it("should return vecA", function() { expect(result).toBe(vecA); });
  127. });
  128. });
  129. describe("floor", function() {
  130. beforeEach(function() { vecA = [Math.E, Math.PI, Math.SQRT2, Math.SQRT1_2]; });
  131. describe("with a separate output vector", function() {
  132. beforeEach(function() { result = vec4.floor(out, vecA); });
  133. it("should place values into out", function() { expect(out).toBeEqualish([2, 3, 1, 0]); });
  134. it("should return out", function() { expect(result).toBe(out); });
  135. it("should not modify vecA", function() { expect(vecA).toBeEqualish([Math.E, Math.PI, Math.SQRT2, Math.SQRT1_2]); });
  136. });
  137. describe("when vecA is the output vector", function() {
  138. beforeEach(function() { result = vec4.floor(vecA, vecA); });
  139. it("should place values into vecA", function() { expect(vecA).toBeEqualish([2, 3, 1, 0]); });
  140. it("should return vecA", function() { expect(result).toBe(vecA); });
  141. });
  142. });
  143. describe("min", function() {
  144. beforeEach(function() { vecA = [1, 3, 1, 3]; vecB = [3, 1, 3, 1]; });
  145. describe("with a separate output vector", function() {
  146. beforeEach(function() { result = vec4.min(out, vecA, vecB); });
  147. it("should place values into out", function() { expect(out).toBeEqualish([1, 1, 1, 1]); });
  148. it("should return out", function() { expect(result).toBe(out); });
  149. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 3, 1, 3]); });
  150. it("should not modify vecB", function() { expect(vecB).toBeEqualish([3, 1, 3, 1]); });
  151. });
  152. describe("when vecA is the output vector", function() {
  153. beforeEach(function() { result = vec4.min(vecA, vecA, vecB); });
  154. it("should place values into vecA", function() { expect(vecA).toBeEqualish([1, 1, 1, 1]); });
  155. it("should return vecA", function() { expect(result).toBe(vecA); });
  156. it("should not modify vecB", function() { expect(vecB).toBeEqualish([3, 1, 3, 1]); });
  157. });
  158. describe("when vecB is the output vector", function() {
  159. beforeEach(function() { result = vec4.min(vecB, vecA, vecB); });
  160. it("should place values into vecB", function() { expect(vecB).toBeEqualish([1, 1, 1, 1]); });
  161. it("should return vecB", function() { expect(result).toBe(vecB); });
  162. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 3, 1, 3]); });
  163. });
  164. });
  165. describe("max", function() {
  166. beforeEach(function() { vecA = [1, 3, 1, 3]; vecB = [3, 1, 3, 1]; });
  167. describe("with a separate output vector", function() {
  168. beforeEach(function() { result = vec4.max(out, vecA, vecB); });
  169. it("should place values into out", function() { expect(out).toBeEqualish([3, 3, 3, 3]); });
  170. it("should return out", function() { expect(result).toBe(out); });
  171. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 3, 1, 3]); });
  172. it("should not modify vecB", function() { expect(vecB).toBeEqualish([3, 1, 3, 1]); });
  173. });
  174. describe("when vecA is the output vector", function() {
  175. beforeEach(function() { result = vec4.max(vecA, vecA, vecB); });
  176. it("should place values into vecA", function() { expect(vecA).toBeEqualish([3, 3, 3, 3]); });
  177. it("should return vecA", function() { expect(result).toBe(vecA); });
  178. it("should not modify vecB", function() { expect(vecB).toBeEqualish([3, 1, 3, 1]); });
  179. });
  180. describe("when vecB is the output vector", function() {
  181. beforeEach(function() { result = vec4.max(vecB, vecA, vecB); });
  182. it("should place values into vecB", function() { expect(vecB).toBeEqualish([3, 3, 3, 3]); });
  183. it("should return vecB", function() { expect(result).toBe(vecB); });
  184. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 3, 1, 3]); });
  185. });
  186. });
  187. describe("round", function() {
  188. beforeEach(function() { vecA = [Math.E, Math.PI, Math.SQRT2, Math.SQRT1_2]; });
  189. describe("with a separate output vector", function() {
  190. beforeEach(function() { result = vec4.round(out, vecA); });
  191. it("should place values into out", function() { expect(out).toBeEqualish([3, 3, 1, 1]); });
  192. it("should return out", function() { expect(result).toBe(out); });
  193. it("should not modify vecA", function() { expect(vecA).toBeEqualish([Math.E, Math.PI, Math.SQRT2, Math.SQRT1_2]); });
  194. });
  195. describe("when vecA is the output vector", function() {
  196. beforeEach(function() { result = vec4.round(vecA, vecA); });
  197. it("should place values into vecA", function() { expect(vecA).toBeEqualish([3, 3, 1, 1]); });
  198. it("should return vecA", function() { expect(result).toBe(vecA); });
  199. });
  200. });
  201. describe("scale", function() {
  202. describe("with a separate output vector", function() {
  203. beforeEach(function() { result = vec4.scale(out, vecA, 2); });
  204. it("should place values into out", function() { expect(out).toBeEqualish([2, 4, 6, 8]); });
  205. it("should return out", function() { expect(result).toBe(out); });
  206. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3, 4]); });
  207. });
  208. describe("when vecA is the output vector", function() {
  209. beforeEach(function() { result = vec4.scale(vecA, vecA, 2); });
  210. it("should place values into vecA", function() { expect(vecA).toBeEqualish([2, 4, 6, 8]); });
  211. it("should return vecA", function() { expect(result).toBe(vecA); });
  212. });
  213. });
  214. describe("scaleAndAdd", function() {
  215. describe("with a separate output vector", function() {
  216. beforeEach(function() { result = vec4.scaleAndAdd(out, vecA, vecB, 0.5); });
  217. it("should place values into out", function() { expect(out).toBeEqualish([3.5, 5, 6.5, 8]); });
  218. it("should return out", function() { expect(result).toBe(out); });
  219. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3, 4]); });
  220. it("should not modify vecB", function() { expect(vecB).toBeEqualish([5, 6, 7, 8]); });
  221. });
  222. describe("when vecA is the output vector", function() {
  223. beforeEach(function() { result = vec4.scaleAndAdd(vecA, vecA, vecB, 0.5); });
  224. it("should place values into vecA", function() { expect(vecA).toBeEqualish([3.5, 5, 6.5, 8]); });
  225. it("should return vecA", function() { expect(result).toBe(vecA); });
  226. it("should not modify vecB", function() { expect(vecB).toBeEqualish([5, 6, 7, 8]); });
  227. });
  228. describe("when vecB is the output vector", function() {
  229. beforeEach(function() { result = vec4.scaleAndAdd(vecB, vecA, vecB, 0.5); });
  230. it("should place values into vecB", function() { expect(vecB).toBeEqualish([3.5, 5, 6.5, 8]); });
  231. it("should return vecB", function() { expect(result).toBe(vecB); });
  232. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3, 4]); });
  233. });
  234. });
  235. describe("distance", function() {
  236. it("should have an alias called 'dist'", function() { expect(vec4.dist).toEqual(vec4.distance); });
  237. beforeEach(function() { result = vec4.distance(vecA, vecB); });
  238. it("should return the distance", function() { expect(result).toBeEqualish(8); });
  239. });
  240. describe("squaredDistance", function() {
  241. it("should have an alias called 'sqrDist'", function() { expect(vec4.sqrDist).toEqual(vec4.squaredDistance); });
  242. beforeEach(function() { result = vec4.squaredDistance(vecA, vecB); });
  243. it("should return the squared distance", function() { expect(result).toEqual(64); });
  244. });
  245. describe("length", function() {
  246. it("should have an alias called 'len'", function() { expect(vec4.len).toEqual(vec4.length); });
  247. beforeEach(function() { result = vec4.len(vecA); });
  248. it("should return the length", function() { expect(result).toBeEqualish(5.477225); });
  249. });
  250. describe("squaredLength", function() {
  251. it("should have an alias called 'sqrLen'", function() { expect(vec4.sqrLen).toEqual(vec4.squaredLength); });
  252. beforeEach(function() { result = vec4.squaredLength(vecA); });
  253. it("should return the squared length", function() { expect(result).toEqual(30); });
  254. });
  255. describe("negate", function() {
  256. describe("with a separate output vector", function() {
  257. beforeEach(function() { result = vec4.negate(out, vecA); });
  258. it("should place values into out", function() { expect(out).toBeEqualish([-1, -2, -3, -4]); });
  259. it("should return out", function() { expect(result).toBe(out); });
  260. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3, 4]); });
  261. });
  262. describe("when vecA is the output vector", function() {
  263. beforeEach(function() { result = vec4.negate(vecA, vecA); });
  264. it("should place values into vecA", function() { expect(vecA).toBeEqualish([-1, -2, -3, -4]); });
  265. it("should return vecA", function() { expect(result).toBe(vecA); });
  266. });
  267. });
  268. describe("normalize", function() {
  269. beforeEach(function() { vecA = [5, 0, 0, 0]; });
  270. describe("with a separate output vector", function() {
  271. beforeEach(function() { result = vec4.normalize(out, vecA); });
  272. it("should place values into out", function() { expect(out).toBeEqualish([1, 0, 0, 0]); });
  273. it("should return out", function() { expect(result).toBe(out); });
  274. it("should not modify vecA", function() { expect(vecA).toBeEqualish([5, 0, 0, 0]); });
  275. });
  276. describe("when vecA is the output vector", function() {
  277. beforeEach(function() { result = vec4.normalize(vecA, vecA); });
  278. it("should place values into vecA", function() { expect(vecA).toBeEqualish([1, 0, 0, 0]); });
  279. it("should return vecA", function() { expect(result).toBe(vecA); });
  280. });
  281. });
  282. describe("dot", function() {
  283. beforeEach(function() { result = vec4.dot(vecA, vecB); });
  284. it("should return the dot product", function() { expect(result).toEqual(70); });
  285. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3, 4]); });
  286. it("should not modify vecB", function() { expect(vecB).toBeEqualish([5, 6, 7, 8]); });
  287. });
  288. describe("lerp", function() {
  289. describe("with a separate output vector", function() {
  290. beforeEach(function() { result = vec4.lerp(out, vecA, vecB, 0.5); });
  291. it("should place values into out", function() { expect(out).toBeEqualish([3, 4, 5, 6]); });
  292. it("should return out", function() { expect(result).toBe(out); });
  293. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3, 4]); });
  294. it("should not modify vecB", function() { expect(vecB).toBeEqualish([5, 6, 7, 8]); });
  295. });
  296. describe("when vecA is the output vector", function() {
  297. beforeEach(function() { result = vec4.lerp(vecA, vecA, vecB, 0.5); });
  298. it("should place values into vecA", function() { expect(vecA).toBeEqualish([3, 4, 5, 6]); });
  299. it("should return vecA", function() { expect(result).toBe(vecA); });
  300. it("should not modify vecB", function() { expect(vecB).toBeEqualish([5, 6, 7, 8]); });
  301. });
  302. describe("when vecB is the output vector", function() {
  303. beforeEach(function() { result = vec4.lerp(vecB, vecA, vecB, 0.5); });
  304. it("should place values into vecB", function() { expect(vecB).toBeEqualish([3, 4, 5, 6]); });
  305. it("should return vecB", function() { expect(result).toBe(vecB); });
  306. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3, 4]); });
  307. });
  308. });
  309. describe("random", function() {
  310. describe("with no scale", function() {
  311. beforeEach(function() { result = vec4.random(out); });
  312. it("should result in a unit length vector", function() { expect(vec4.len(out)).toBeEqualish(1.0); });
  313. it("should return out", function() { expect(result).toBe(out); });
  314. });
  315. describe("with a scale", function() {
  316. beforeEach(function() { result = vec4.random(out, 5.0); });
  317. it("should result in a unit length vector", function() { expect(vec4.len(out)).toBeEqualish(5.0); });
  318. it("should return out", function() { expect(result).toBe(out); });
  319. });
  320. });
  321. describe("forEach", function() {
  322. let vecArray;
  323. beforeEach(function() {
  324. vecArray = [
  325. 1, 2, 3, 4,
  326. 5, 6, 7, 8,
  327. 0, 0, 0, 0
  328. ];
  329. });
  330. describe("when performing operations that take no extra arguments", function() {
  331. beforeEach(function() { result = vec4.forEach(vecArray, 0, 0, 0, vec4.normalize); });
  332. it("should update all values", function() {
  333. expect(vecArray).toBeEqualish([
  334. 0.182574, 0.365148, 0.547722, 0.730296,
  335. 0.379049, 0.454858, 0.530668, 0.606478,
  336. 0, 0, 0, 0
  337. ]);
  338. });
  339. it("should return vecArray", function() { expect(result).toBe(vecArray); });
  340. });
  341. describe("when performing operations that takes one extra arguments", function() {
  342. beforeEach(function() { result = vec4.forEach(vecArray, 0, 0, 0, vec4.add, vecA); });
  343. it("should update all values", function() {
  344. expect(vecArray).toBeEqualish([
  345. 2, 4, 6, 8,
  346. 6, 8, 10, 12,
  347. 1, 2, 3, 4
  348. ]);
  349. });
  350. it("should return vecArray", function() { expect(result).toBe(vecArray); });
  351. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3, 4]); });
  352. });
  353. describe("when specifying an offset", function() {
  354. beforeEach(function() { result = vec4.forEach(vecArray, 0, 4, 0, vec4.add, vecA); });
  355. it("should update all values except the first vector", function() {
  356. expect(vecArray).toBeEqualish([
  357. 1, 2, 3, 4,
  358. 6, 8, 10, 12,
  359. 1, 2, 3, 4
  360. ]);
  361. });
  362. it("should return vecArray", function() { expect(result).toBe(vecArray); });
  363. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3, 4]); });
  364. });
  365. describe("when specifying a count", function() {
  366. beforeEach(function() { result = vec4.forEach(vecArray, 0, 0, 2, vec4.add, vecA); });
  367. it("should update all values except the last vector", function() {
  368. expect(vecArray).toBeEqualish([
  369. 2, 4, 6, 8,
  370. 6, 8, 10, 12,
  371. 0, 0, 0, 0
  372. ]);
  373. });
  374. it("should return vecArray", function() { expect(result).toBe(vecArray); });
  375. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3, 4]); });
  376. });
  377. describe("when specifying a stride", function() {
  378. beforeEach(function() { result = vec4.forEach(vecArray, 8, 0, 0, vec4.add, vecA); });
  379. it("should update all values except the second vector", function() {
  380. expect(vecArray).toBeEqualish([
  381. 2, 4, 6, 8,
  382. 5, 6, 7, 8,
  383. 1, 2, 3, 4
  384. ]);
  385. });
  386. it("should return vecArray", function() { expect(result).toBe(vecArray); });
  387. it("should not modify vecA", function() { expect(vecA).toBeEqualish([1, 2, 3, 4]); });
  388. });
  389. describe("when calling a function that does not modify the out variable", function() {
  390. beforeEach(function() {
  391. result = vec3.forEach(vecArray, 0, 0, 0, function(out, vec) {});
  392. });
  393. it("values should remain unchanged", function() {
  394. expect(vecArray).toBeEqualish([
  395. 1, 2, 3, 4,
  396. 5, 6, 7, 8,
  397. 0, 0, 0, 0
  398. ]);
  399. });
  400. it("should return vecArray", function() { expect(result).toBe(vecArray); });
  401. });
  402. });
  403. describe("str", function() {
  404. beforeEach(function() { result = vec4.str(vecA); });
  405. it("should return a string representation of the vector", function() { expect(result).toEqual("vec4(1, 2, 3, 4)"); });
  406. });
  407. describe("exactEquals", function() {
  408. let vecC, r0, r1;
  409. beforeEach(function() {
  410. vecA = [0, 1, 2, 3];
  411. vecB = [0, 1, 2, 3];
  412. vecC = [1, 2, 3, 4];
  413. r0 = vec4.exactEquals(vecA, vecB);
  414. r1 = vec4.exactEquals(vecA, vecC);
  415. });
  416. it("should return true for identical vectors", function() { expect(r0).toBe(true); });
  417. it("should return false for different vectors", function() { expect(r1).toBe(false); });
  418. it("should not modify vecA", function() { expect(vecA).toBeEqualish([0, 1, 2, 3]); });
  419. it("should not modify vecB", function() { expect(vecB).toBeEqualish([0, 1, 2, 3]); });
  420. });
  421. describe("equals", function() {
  422. let vecC, vecD, r0, r1, r2;
  423. beforeEach(function() {
  424. vecA = [0, 1, 2, 3];
  425. vecB = [0, 1, 2, 3];
  426. vecC = [1, 2, 3, 4];
  427. vecD = [1e-16, 1, 2, 3];
  428. r0 = vec4.equals(vecA, vecB);
  429. r1 = vec4.equals(vecA, vecC);
  430. r2 = vec4.equals(vecA, vecD);
  431. });
  432. it("should return true for identical vectors", function() { expect(r0).toBe(true); });
  433. it("should return false for different vectors", function() { expect(r1).toBe(false); });
  434. it("should return true for close but not identical vectors", function() { expect(r2).toBe(true); });
  435. it("should not modify vecA", function() { expect(vecA).toBeEqualish([0, 1, 2, 3]); });
  436. it("should not modify vecB", function() { expect(vecB).toBeEqualish([0, 1, 2, 3]); });
  437. });
  438. });