You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

275 lines
7.6 KiB

  1. import Vector from "../src/05/vector";
  2. import { expect } from 'chai';
  3. describe('Vector', () => {
  4. it('can be initialized with 4 numbers', () => {
  5. const v = new Vector(1, 2, 3, 4);
  6. expect(v).to.be.an('object');
  7. expect(v.data[0]).to.be.a("number");
  8. expect(v.data[0]).to.equal(1);
  9. expect(v.data[1]).to.be.a("number");
  10. expect(v.data[1]).to.equal(2);
  11. expect(v.data[2]).to.be.a("number");
  12. expect(v.data[2]).to.equal(3);
  13. expect(v.data[3]).to.be.a("number");
  14. expect(v.data[3]).to.equal(4);
  15. });
  16. it('x component can be set and retrieved', () => {
  17. const v = new Vector(0, 0, 0, 0);
  18. expect(v).has.property('x');
  19. v.x = 42;
  20. expect(v.x).to.equal(42);
  21. });
  22. it('y component can be set and retrieved', () => {
  23. const v = new Vector(0, 0, 0, 0);
  24. expect(v).has.property('y');
  25. v.y = 42;
  26. expect(v.y).to.equal(42);
  27. });
  28. it('z component can be set and retrieved', () => {
  29. const v = new Vector(0, 0, 0, 0);
  30. expect(v).has.property('z');
  31. v.z = 42;
  32. expect(v.z).to.equal(42);
  33. });
  34. it('w component can be set and retrieved', () => {
  35. const v = new Vector(0, 0, 0, 0);
  36. expect(v).has.property('w');
  37. v.w = 42;
  38. expect(v.w).to.equal(42);
  39. });
  40. it('r component can be set and retrieved', () => {
  41. const v = new Vector(0, 0, 0, 0);
  42. expect(v).has.property('r');
  43. v.r = 42;
  44. expect(v.r).to.equal(42);
  45. });
  46. it('g component can be set and retrieved', () => {
  47. const v = new Vector(0, 0, 0, 0);
  48. expect(v).has.property('g');
  49. v.g = 42;
  50. expect(v.g).to.equal(42);
  51. });
  52. it('b component can be set and retrieved', () => {
  53. const v = new Vector(0, 0, 0, 0);
  54. expect(v).has.property('b');
  55. v.b = 42;
  56. expect(v.b).to.equal(42);
  57. });
  58. it('a component can be set and retrieved', () => {
  59. const v = new Vector(0, 0, 0, 0);
  60. expect(v).has.property('a');
  61. v.a = 42;
  62. expect(v.a).to.equal(42);
  63. });
  64. it('method add exists', () => {
  65. const v = new Vector(0, 0, 0, 0);
  66. expect(v).to.respondTo('add');
  67. });
  68. it('method sub exists', () => {
  69. const v = new Vector(0, 0, 0, 0);
  70. expect(v).to.respondTo('sub');
  71. });
  72. it('method mul exists', () => {
  73. const v = new Vector(0, 0, 0, 0);
  74. expect(v).to.respondTo('mul');
  75. });
  76. it('method div exists', () => {
  77. const v = new Vector(0, 0, 0, 0);
  78. expect(v).to.respondTo('div');
  79. });
  80. it('method normalize exists', () => {
  81. const v = new Vector(0, 0, 0, 0);
  82. expect(v).to.respondTo('normalize');
  83. });
  84. it('length returns the correct result', () => {
  85. const v = new Vector(0, 4, 3, 0);
  86. expect(v).has.property('length');
  87. expect(v.length).to.equal(5);
  88. });
  89. it('addition adds the other vector', () => {
  90. const a = new Vector(1, 2, 3, 1);
  91. const b = new Vector(1, -2, 0, 0);
  92. expect(a).to.respondTo('add');
  93. const c: Vector = a.add(b);
  94. expect(c).to.not.be.null;
  95. expect(c).to.be.an('object');
  96. expect(c.x).to.equal(2);
  97. expect(c.y).to.equal(0);
  98. expect(c.z).to.equal(3);
  99. expect(c.w).to.equal(1);
  100. });
  101. it('addition does not change the value of "this"', () => {
  102. const a = new Vector(1, 2, 3, 1);
  103. const b = new Vector(1, -2, 0, 0);
  104. expect(a).to.respondTo('add');
  105. const c: Vector = a.add(b);
  106. expect(a).to.not.be.null;
  107. expect(a).to.be.an('object');
  108. expect(a.x).to.equal(1);
  109. expect(a.y).to.equal(2);
  110. expect(a.z).to.equal(3);
  111. expect(a.w).to.equal(1);
  112. });
  113. it('subtraction subtracts the other vector', () => {
  114. const a = new Vector(1, 3, 3, 0);
  115. const b = new Vector(2, -2, 0, 0);
  116. expect(a).to.respondTo('sub');
  117. const c: Vector = a.sub(b);
  118. expect(c).to.not.be.null;
  119. expect(c).to.be.an('object');
  120. expect(c.x).to.equal(-1);
  121. expect(c.y).to.equal(5);
  122. expect(c.z).to.equal(3);
  123. expect(c.w).to.equal(0);
  124. });
  125. it('subtraction does not change the value of "this"', () => {
  126. const a = new Vector(1, 2, 3, 1);
  127. const b = new Vector(1, -2, 0, 0);
  128. expect(a).to.respondTo('sub');
  129. const c: Vector = a.sub(b);
  130. expect(a).to.not.be.null;
  131. expect(a).to.be.an('object');
  132. expect(a.x).to.equal(1);
  133. expect(a.y).to.equal(2);
  134. expect(a.z).to.equal(3);
  135. expect(a.w).to.equal(1);
  136. });
  137. it('multiplication with a scalar multiplies correctly', () => {
  138. const a = new Vector(1, 3, 3, 0);
  139. expect(a).to.respondTo('mul');
  140. const c: Vector = a.mul(4);
  141. expect(c.x).to.equal(4);
  142. expect(c.y).to.equal(12);
  143. expect(c.z).to.equal(12);
  144. expect(c.w).to.equal(0);
  145. });
  146. it('multiplication does not change the value of "this"', () => {
  147. const a = new Vector(1, 3, 3, 0);
  148. expect(a).to.respondTo('mul');
  149. const c: Vector = a.mul(4);
  150. expect(a.x).to.equal(1);
  151. expect(a.y).to.equal(3);
  152. expect(a.z).to.equal(3);
  153. expect(a.w).to.equal(0);
  154. });
  155. it('division by a scalar divides correctly', () => {
  156. const a = new Vector(3, 12, 6, 0);
  157. expect(a).to.respondTo('div');
  158. const c: Vector = a.div(3);
  159. expect(c.x).to.equal(1);
  160. expect(c.y).to.equal(4);
  161. expect(c.z).to.equal(2);
  162. expect(c.w).to.equal(0);
  163. });
  164. it('division does not change the value of "this"', () => {
  165. const a = new Vector(1, 3, 3, 0);
  166. expect(a).to.respondTo('div');
  167. const c: Vector = a.div(4);
  168. expect(a.x).to.equal(1);
  169. expect(a.y).to.equal(3);
  170. expect(a.z).to.equal(3);
  171. expect(a.w).to.equal(0);
  172. });
  173. it('cross product returns the correct result', () => {
  174. const a = new Vector(1, 3, 3, 0);
  175. const b = new Vector(2, -2, 0, 0);
  176. expect(a).to.respondTo('cross');
  177. const c = a.cross(b);
  178. expect(c.x).to.equal(6);
  179. expect(c.y).to.equal(6);
  180. expect(c.z).to.equal(-8);
  181. expect(c.w).to.equal(0);
  182. });
  183. it('cross product does not change the value of "this"', () => {
  184. const a = new Vector(1, 3, 3, 0);
  185. const b = new Vector(2, -2, 0, 0);
  186. expect(a).to.respondTo('cross');
  187. const c = a.cross(b);
  188. expect(a.x).to.equal(1);
  189. expect(a.y).to.equal(3);
  190. expect(a.z).to.equal(3);
  191. expect(a.w).to.equal(0);
  192. });
  193. it('dot product is correct', () => {
  194. const a = new Vector(1, 3, 3, 0);
  195. const b = new Vector(2, 2, 1, 0);
  196. const c = new Vector(2, -2, 0, 0);
  197. expect(a).to.respondTo('dot');
  198. const d = a.dot(b);
  199. const e = b.dot(c);
  200. expect(d).to.equal(11);
  201. expect(e).to.equal(0);
  202. });
  203. it('equality of different vectors returns false', () => {
  204. const a = new Vector(1, 3, 3, 1);
  205. const b = new Vector(2, -2, 0, 1);
  206. expect(a).to.respondTo('equals');
  207. expect(a.equals(b)).to.equal(false);
  208. });
  209. it('equality of equal vectors returns true', () => {
  210. const a = new Vector(1, 3, 3, 1);
  211. expect(a).to.respondTo('equals');
  212. expect(a.equals(a)).to.equal(true);
  213. });
  214. it('vectors very close to each other are equal', () => {
  215. const a = new Vector(1, 3, 3, 1);
  216. const b = new Vector(1.0000000000001, 3.000000000001, 2.9999999999999, 1);
  217. expect(a).to.respondTo('equals');
  218. expect(a.equals(b)).to.equal(true);
  219. });
  220. });