vec2-spec.js 29 KB

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