Vector4.tests.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /* global QUnit */
  2. import { Vector4 } from '../../../../src/math/Vector4.js';
  3. import { Matrix4 } from '../../../../src/math/Matrix4.js';
  4. import { BufferAttribute } from '../../../../src/core/BufferAttribute.js';
  5. import {
  6. x,
  7. y,
  8. z,
  9. w,
  10. eps
  11. } from '../../utils/math-constants.js';
  12. export default QUnit.module( 'Maths', () => {
  13. QUnit.module( 'Vector4', () => {
  14. // INSTANCING
  15. QUnit.test( 'Instancing', ( assert ) => {
  16. let a = new Vector4();
  17. assert.ok( a.x == 0, 'Passed!' );
  18. assert.ok( a.y == 0, 'Passed!' );
  19. assert.ok( a.z == 0, 'Passed!' );
  20. assert.ok( a.w == 1, 'Passed!' );
  21. a = new Vector4( x, y, z, w );
  22. assert.ok( a.x === x, 'Passed!' );
  23. assert.ok( a.y === y, 'Passed!' );
  24. assert.ok( a.z === z, 'Passed!' );
  25. assert.ok( a.w === w, 'Passed!' );
  26. } );
  27. // PUBLIC STUFF
  28. QUnit.test( 'isVector4', ( assert ) => {
  29. const object = new Vector4();
  30. assert.ok( object.isVector4, 'Vector4.isVector4 should be true' );
  31. } );
  32. QUnit.test( 'set', ( assert ) => {
  33. const a = new Vector4();
  34. assert.ok( a.x == 0, 'Passed!' );
  35. assert.ok( a.y == 0, 'Passed!' );
  36. assert.ok( a.z == 0, 'Passed!' );
  37. assert.ok( a.w == 1, 'Passed!' );
  38. a.set( x, y, z, w );
  39. assert.ok( a.x == x, 'Passed!' );
  40. assert.ok( a.y == y, 'Passed!' );
  41. assert.ok( a.z == z, 'Passed!' );
  42. assert.ok( a.w == w, 'Passed!' );
  43. } );
  44. QUnit.todo( 'setScalar', ( assert ) => {
  45. assert.ok( false, 'everything\'s gonna be alright' );
  46. } );
  47. QUnit.test( 'setX', ( assert ) => {
  48. const a = new Vector4();
  49. assert.ok( a.x == 0, 'Passed!' );
  50. a.setX( x );
  51. assert.ok( a.x == x, 'Passed!' );
  52. } );
  53. QUnit.test( 'setY', ( assert ) => {
  54. const a = new Vector4();
  55. assert.ok( a.y == 0, 'Passed!' );
  56. a.setY( y );
  57. assert.ok( a.y == y, 'Passed!' );
  58. } );
  59. QUnit.test( 'setZ', ( assert ) => {
  60. const a = new Vector4();
  61. assert.ok( a.z == 0, 'Passed!' );
  62. a.setZ( z );
  63. assert.ok( a.z == z, 'Passed!' );
  64. } );
  65. QUnit.test( 'setW', ( assert ) => {
  66. const a = new Vector4();
  67. assert.ok( a.w == 1, 'Passed!' );
  68. a.setW( w );
  69. assert.ok( a.w == w, 'Passed!' );
  70. } );
  71. QUnit.todo( 'setComponent', ( assert ) => {
  72. assert.ok( false, 'everything\'s gonna be alright' );
  73. } );
  74. QUnit.todo( 'getComponent', ( assert ) => {
  75. assert.ok( false, 'everything\'s gonna be alright' );
  76. } );
  77. QUnit.todo( 'clone', ( assert ) => {
  78. assert.ok( false, 'everything\'s gonna be alright' );
  79. } );
  80. QUnit.test( 'copy', ( assert ) => {
  81. const a = new Vector4( x, y, z, w );
  82. const b = new Vector4().copy( a );
  83. assert.ok( b.x == x, 'Passed!' );
  84. assert.ok( b.y == y, 'Passed!' );
  85. assert.ok( b.z == z, 'Passed!' );
  86. assert.ok( b.w == w, 'Passed!' );
  87. // ensure that it is a true copy
  88. a.x = 0;
  89. a.y = - 1;
  90. a.z = - 2;
  91. a.w = - 3;
  92. assert.ok( b.x == x, 'Passed!' );
  93. assert.ok( b.y == y, 'Passed!' );
  94. assert.ok( b.z == z, 'Passed!' );
  95. assert.ok( b.w == w, 'Passed!' );
  96. } );
  97. QUnit.test( 'add', ( assert ) => {
  98. const a = new Vector4( x, y, z, w );
  99. const b = new Vector4( - x, - y, - z, - w );
  100. a.add( b );
  101. assert.ok( a.x == 0, 'Passed!' );
  102. assert.ok( a.y == 0, 'Passed!' );
  103. assert.ok( a.z == 0, 'Passed!' );
  104. assert.ok( a.w == 0, 'Passed!' );
  105. } );
  106. QUnit.todo( 'addScalar', ( assert ) => {
  107. assert.ok( false, 'everything\'s gonna be alright' );
  108. } );
  109. QUnit.test( 'addVectors', ( assert ) => {
  110. const b = new Vector4( - x, - y, - z, - w );
  111. const c = new Vector4().addVectors( b, b );
  112. assert.ok( c.x == - 2 * x, 'Passed!' );
  113. assert.ok( c.y == - 2 * y, 'Passed!' );
  114. assert.ok( c.z == - 2 * z, 'Passed!' );
  115. assert.ok( c.w == - 2 * w, 'Passed!' );
  116. } );
  117. QUnit.test( 'addScaledVector', ( assert ) => {
  118. const a = new Vector4( x, y, z, w );
  119. const b = new Vector4( 6, 7, 8, 9 );
  120. const s = 3;
  121. a.addScaledVector( b, s );
  122. assert.strictEqual( a.x, x + b.x * s, 'Check x' );
  123. assert.strictEqual( a.y, y + b.y * s, 'Check y' );
  124. assert.strictEqual( a.z, z + b.z * s, 'Check z' );
  125. assert.strictEqual( a.w, w + b.w * s, 'Check w' );
  126. } );
  127. QUnit.test( 'sub', ( assert ) => {
  128. const a = new Vector4( x, y, z, w );
  129. const b = new Vector4( - x, - y, - z, - w );
  130. a.sub( b );
  131. assert.ok( a.x == 2 * x, 'Passed!' );
  132. assert.ok( a.y == 2 * y, 'Passed!' );
  133. assert.ok( a.z == 2 * z, 'Passed!' );
  134. assert.ok( a.w == 2 * w, 'Passed!' );
  135. } );
  136. QUnit.todo( 'subScalar', ( assert ) => {
  137. assert.ok( false, 'everything\'s gonna be alright' );
  138. } );
  139. QUnit.test( 'subVectors', ( assert ) => {
  140. const a = new Vector4( x, y, z, w );
  141. const c = new Vector4().subVectors( a, a );
  142. assert.ok( c.x == 0, 'Passed!' );
  143. assert.ok( c.y == 0, 'Passed!' );
  144. assert.ok( c.z == 0, 'Passed!' );
  145. assert.ok( c.w == 0, 'Passed!' );
  146. } );
  147. QUnit.todo( 'multiply', ( assert ) => {
  148. assert.ok( false, 'everything\'s gonna be alright' );
  149. } );
  150. QUnit.todo( 'multiplyScalar', ( assert ) => {
  151. assert.ok( false, 'everything\'s gonna be alright' );
  152. } );
  153. QUnit.test( 'applyMatrix4', ( assert ) => {
  154. const a = new Vector4( x, y, z, w );
  155. const m = new Matrix4().makeRotationX( Math.PI );
  156. const expected = new Vector4( 2, - 3, - 4, 5 );
  157. a.applyMatrix4( m );
  158. assert.ok( Math.abs( a.x - expected.x ) <= eps, 'Rotation matrix: check x' );
  159. assert.ok( Math.abs( a.y - expected.y ) <= eps, 'Rotation matrix: check y' );
  160. assert.ok( Math.abs( a.z - expected.z ) <= eps, 'Rotation matrix: check z' );
  161. assert.ok( Math.abs( a.w - expected.w ) <= eps, 'Rotation matrix: check w' );
  162. a.set( x, y, z, w );
  163. m.makeTranslation( 5, 7, 11 );
  164. expected.set( 27, 38, 59, 5 );
  165. a.applyMatrix4( m );
  166. assert.ok( Math.abs( a.x - expected.x ) <= eps, 'Translation matrix: check x' );
  167. assert.ok( Math.abs( a.y - expected.y ) <= eps, 'Translation matrix: check y' );
  168. assert.ok( Math.abs( a.z - expected.z ) <= eps, 'Translation matrix: check z' );
  169. assert.ok( Math.abs( a.w - expected.w ) <= eps, 'Translation matrix: check w' );
  170. a.set( x, y, z, w );
  171. m.set( 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0 );
  172. expected.set( 2, 3, 4, 4 );
  173. a.applyMatrix4( m );
  174. assert.ok( Math.abs( a.x - expected.x ) <= eps, 'Custom matrix: check x' );
  175. assert.ok( Math.abs( a.y - expected.y ) <= eps, 'Custom matrix: check y' );
  176. assert.ok( Math.abs( a.z - expected.z ) <= eps, 'Custom matrix: check z' );
  177. assert.ok( Math.abs( a.w - expected.w ) <= eps, 'Custom matrix: check w' );
  178. a.set( x, y, z, w );
  179. m.set( 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53 );
  180. expected.set( 68, 224, 442, 664 );
  181. a.applyMatrix4( m );
  182. assert.ok( Math.abs( a.x - expected.x ) <= eps, 'Bogus matrix: check x' );
  183. assert.ok( Math.abs( a.y - expected.y ) <= eps, 'Bogus matrix: check y' );
  184. assert.ok( Math.abs( a.z - expected.z ) <= eps, 'Bogus matrix: check z' );
  185. assert.ok( Math.abs( a.w - expected.w ) <= eps, 'Bogus matrix: check w' );
  186. } );
  187. QUnit.todo( 'divideScalar', ( assert ) => {
  188. assert.ok( false, 'everything\'s gonna be alright' );
  189. } );
  190. QUnit.todo( 'setAxisAngleFromQuaternion', ( assert ) => {
  191. assert.ok( false, 'everything\'s gonna be alright' );
  192. } );
  193. QUnit.todo( 'setAxisAngleFromRotationMatrix', ( assert ) => {
  194. assert.ok( false, 'everything\'s gonna be alright' );
  195. } );
  196. QUnit.todo( 'min', ( assert ) => {
  197. assert.ok( false, 'everything\'s gonna be alright' );
  198. } );
  199. QUnit.todo( 'max', ( assert ) => {
  200. assert.ok( false, 'everything\'s gonna be alright' );
  201. } );
  202. QUnit.todo( 'clamp', ( assert ) => {
  203. assert.ok( false, 'everything\'s gonna be alright' );
  204. } );
  205. QUnit.test( 'clampScalar', ( assert ) => {
  206. const a = new Vector4( - 0.1, 0.01, 0.5, 1.5 );
  207. const clamped = new Vector4( 0.1, 0.1, 0.5, 1.0 );
  208. a.clampScalar( 0.1, 1.0 );
  209. assert.ok( Math.abs( a.x - clamped.x ) <= eps, 'Check x' );
  210. assert.ok( Math.abs( a.y - clamped.y ) <= eps, 'Check y' );
  211. assert.ok( Math.abs( a.z - clamped.z ) <= eps, 'Check z' );
  212. assert.ok( Math.abs( a.w - clamped.w ) <= eps, 'Check w' );
  213. } );
  214. QUnit.todo( 'clampLength', ( assert ) => {
  215. assert.ok( false, 'everything\'s gonna be alright' );
  216. } );
  217. QUnit.todo( 'floor', ( assert ) => {
  218. assert.ok( false, 'everything\'s gonna be alright' );
  219. } );
  220. QUnit.todo( 'ceil', ( assert ) => {
  221. assert.ok( false, 'everything\'s gonna be alright' );
  222. } );
  223. QUnit.todo( 'round', ( assert ) => {
  224. assert.ok( false, 'everything\'s gonna be alright' );
  225. } );
  226. QUnit.todo( 'roundToZero', ( assert ) => {
  227. assert.ok( false, 'everything\'s gonna be alright' );
  228. } );
  229. QUnit.test( 'negate', ( assert ) => {
  230. const a = new Vector4( x, y, z, w );
  231. a.negate();
  232. assert.ok( a.x == - x, 'Passed!' );
  233. assert.ok( a.y == - y, 'Passed!' );
  234. assert.ok( a.z == - z, 'Passed!' );
  235. assert.ok( a.w == - w, 'Passed!' );
  236. } );
  237. QUnit.test( 'dot', ( assert ) => {
  238. const a = new Vector4( x, y, z, w );
  239. const b = new Vector4( - x, - y, - z, - w );
  240. const c = new Vector4( 0, 0, 0, 0 );
  241. let result = a.dot( b );
  242. assert.ok( result == ( - x * x - y * y - z * z - w * w ), 'Passed!' );
  243. result = a.dot( c );
  244. assert.ok( result == 0, 'Passed!' );
  245. } );
  246. QUnit.todo( 'lengthSq', ( assert ) => {
  247. assert.ok( false, 'everything\'s gonna be alright' );
  248. } );
  249. QUnit.todo( 'length', ( assert ) => {
  250. assert.ok( false, 'everything\'s gonna be alright' );
  251. } );
  252. QUnit.test( 'manhattanLength', ( assert ) => {
  253. const a = new Vector4( x, 0, 0, 0 );
  254. const b = new Vector4( 0, - y, 0, 0 );
  255. const c = new Vector4( 0, 0, z, 0 );
  256. const d = new Vector4( 0, 0, 0, w );
  257. const e = new Vector4( 0, 0, 0, 0 );
  258. assert.ok( a.manhattanLength() == x, 'Positive x' );
  259. assert.ok( b.manhattanLength() == y, 'Negative y' );
  260. assert.ok( c.manhattanLength() == z, 'Positive z' );
  261. assert.ok( d.manhattanLength() == w, 'Positive w' );
  262. assert.ok( e.manhattanLength() == 0, 'Empty initialization' );
  263. a.set( x, y, z, w );
  264. assert.ok(
  265. a.manhattanLength() == Math.abs( x ) + Math.abs( y ) + Math.abs( z ) + Math.abs( w ),
  266. 'All components'
  267. );
  268. } );
  269. QUnit.test( 'normalize', ( assert ) => {
  270. const a = new Vector4( x, 0, 0, 0 );
  271. const b = new Vector4( 0, - y, 0, 0 );
  272. const c = new Vector4( 0, 0, z, 0 );
  273. const d = new Vector4( 0, 0, 0, - w );
  274. a.normalize();
  275. assert.ok( a.length() == 1, 'Passed!' );
  276. assert.ok( a.x == 1, 'Passed!' );
  277. b.normalize();
  278. assert.ok( b.length() == 1, 'Passed!' );
  279. assert.ok( b.y == - 1, 'Passed!' );
  280. c.normalize();
  281. assert.ok( c.length() == 1, 'Passed!' );
  282. assert.ok( c.z == 1, 'Passed!' );
  283. d.normalize();
  284. assert.ok( d.length() == 1, 'Passed!' );
  285. assert.ok( d.w == - 1, 'Passed!' );
  286. } );
  287. QUnit.test( 'setLength', ( assert ) => {
  288. let a = new Vector4( x, 0, 0, 0 );
  289. assert.ok( a.length() == x, 'Passed!' );
  290. a.setLength( y );
  291. assert.ok( a.length() == y, 'Passed!' );
  292. a = new Vector4( 0, 0, 0, 0 );
  293. assert.ok( a.length() == 0, 'Passed!' );
  294. a.setLength( y );
  295. assert.ok( a.length() == 0, 'Passed!' );
  296. a.setLength();
  297. assert.ok( isNaN( a.length() ), 'Passed!' );
  298. } );
  299. QUnit.todo( 'lerp', ( assert ) => {
  300. assert.ok( false, 'everything\'s gonna be alright' );
  301. } );
  302. QUnit.todo( 'lerpVectors', ( assert ) => {
  303. assert.ok( false, 'everything\'s gonna be alright' );
  304. } );
  305. QUnit.test( 'equals', ( assert ) => {
  306. const a = new Vector4( x, 0, z, 0 );
  307. const b = new Vector4( 0, - y, 0, - w );
  308. assert.ok( a.x != b.x, 'Passed!' );
  309. assert.ok( a.y != b.y, 'Passed!' );
  310. assert.ok( a.z != b.z, 'Passed!' );
  311. assert.ok( a.w != b.w, 'Passed!' );
  312. assert.ok( ! a.equals( b ), 'Passed!' );
  313. assert.ok( ! b.equals( a ), 'Passed!' );
  314. a.copy( b );
  315. assert.ok( a.x == b.x, 'Passed!' );
  316. assert.ok( a.y == b.y, 'Passed!' );
  317. assert.ok( a.z == b.z, 'Passed!' );
  318. assert.ok( a.w == b.w, 'Passed!' );
  319. assert.ok( a.equals( b ), 'Passed!' );
  320. assert.ok( b.equals( a ), 'Passed!' );
  321. } );
  322. QUnit.test( 'fromArray', ( assert ) => {
  323. const a = new Vector4();
  324. const array = [ 1, 2, 3, 4, 5, 6, 7, 8 ];
  325. a.fromArray( array );
  326. assert.strictEqual( a.x, 1, 'No offset: check x' );
  327. assert.strictEqual( a.y, 2, 'No offset: check y' );
  328. assert.strictEqual( a.z, 3, 'No offset: check z' );
  329. assert.strictEqual( a.w, 4, 'No offset: check w' );
  330. a.fromArray( array, 4 );
  331. assert.strictEqual( a.x, 5, 'With offset: check x' );
  332. assert.strictEqual( a.y, 6, 'With offset: check y' );
  333. assert.strictEqual( a.z, 7, 'With offset: check z' );
  334. assert.strictEqual( a.w, 8, 'With offset: check w' );
  335. } );
  336. QUnit.test( 'toArray', ( assert ) => {
  337. const a = new Vector4( x, y, z, w );
  338. let array = a.toArray();
  339. assert.strictEqual( array[ 0 ], x, 'No array, no offset: check x' );
  340. assert.strictEqual( array[ 1 ], y, 'No array, no offset: check y' );
  341. assert.strictEqual( array[ 2 ], z, 'No array, no offset: check z' );
  342. assert.strictEqual( array[ 3 ], w, 'No array, no offset: check w' );
  343. array = [];
  344. a.toArray( array );
  345. assert.strictEqual( array[ 0 ], x, 'With array, no offset: check x' );
  346. assert.strictEqual( array[ 1 ], y, 'With array, no offset: check y' );
  347. assert.strictEqual( array[ 2 ], z, 'With array, no offset: check z' );
  348. assert.strictEqual( array[ 3 ], w, 'With array, no offset: check w' );
  349. array = [];
  350. a.toArray( array, 1 );
  351. assert.strictEqual( array[ 0 ], undefined, 'With array and offset: check [0]' );
  352. assert.strictEqual( array[ 1 ], x, 'With array and offset: check x' );
  353. assert.strictEqual( array[ 2 ], y, 'With array and offset: check y' );
  354. assert.strictEqual( array[ 3 ], z, 'With array and offset: check z' );
  355. assert.strictEqual( array[ 4 ], w, 'With array and offset: check w' );
  356. } );
  357. QUnit.test( 'fromBufferAttribute', ( assert ) => {
  358. const a = new Vector4();
  359. const attr = new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] ), 4 );
  360. a.fromBufferAttribute( attr, 0 );
  361. assert.strictEqual( a.x, 1, 'Offset 0: check x' );
  362. assert.strictEqual( a.y, 2, 'Offset 0: check y' );
  363. assert.strictEqual( a.z, 3, 'Offset 0: check z' );
  364. assert.strictEqual( a.w, 4, 'Offset 0: check w' );
  365. a.fromBufferAttribute( attr, 1 );
  366. assert.strictEqual( a.x, 5, 'Offset 1: check x' );
  367. assert.strictEqual( a.y, 6, 'Offset 1: check y' );
  368. assert.strictEqual( a.z, 7, 'Offset 1: check z' );
  369. assert.strictEqual( a.w, 8, 'Offset 1: check w' );
  370. } );
  371. // TODO (Itee) refactor/split
  372. QUnit.test( 'setX,setY,setZ,setW', ( assert ) => {
  373. const a = new Vector4();
  374. assert.ok( a.x == 0, 'Passed!' );
  375. assert.ok( a.y == 0, 'Passed!' );
  376. assert.ok( a.z == 0, 'Passed!' );
  377. assert.ok( a.w == 1, 'Passed!' );
  378. a.setX( x );
  379. a.setY( y );
  380. a.setZ( z );
  381. a.setW( w );
  382. assert.ok( a.x == x, 'Passed!' );
  383. assert.ok( a.y == y, 'Passed!' );
  384. assert.ok( a.z == z, 'Passed!' );
  385. assert.ok( a.w == w, 'Passed!' );
  386. } );
  387. QUnit.test( 'setComponent,getComponent', ( assert ) => {
  388. const a = new Vector4();
  389. assert.ok( a.x == 0, 'Passed!' );
  390. assert.ok( a.y == 0, 'Passed!' );
  391. assert.ok( a.z == 0, 'Passed!' );
  392. assert.ok( a.w == 1, 'Passed!' );
  393. a.setComponent( 0, 1 );
  394. a.setComponent( 1, 2 );
  395. a.setComponent( 2, 3 );
  396. a.setComponent( 3, 4 );
  397. assert.ok( a.getComponent( 0 ) == 1, 'Passed!' );
  398. assert.ok( a.getComponent( 1 ) == 2, 'Passed!' );
  399. assert.ok( a.getComponent( 2 ) == 3, 'Passed!' );
  400. assert.ok( a.getComponent( 3 ) == 4, 'Passed!' );
  401. } );
  402. QUnit.test( 'setComponent/getComponent exceptions', ( assert ) => {
  403. const a = new Vector4();
  404. assert.throws(
  405. function () {
  406. a.setComponent( 4, 0 );
  407. },
  408. /index is out of range/,
  409. 'setComponent with an out of range index throws Error'
  410. );
  411. assert.throws(
  412. function () {
  413. a.getComponent( 4 );
  414. },
  415. /index is out of range/,
  416. 'getComponent with an out of range index throws Error'
  417. );
  418. } );
  419. QUnit.test( 'setScalar/addScalar/subScalar', ( assert ) => {
  420. const a = new Vector4();
  421. const s = 3;
  422. a.setScalar( s );
  423. assert.strictEqual( a.x, s, 'setScalar: check x' );
  424. assert.strictEqual( a.y, s, 'setScalar: check y' );
  425. assert.strictEqual( a.z, s, 'setScalar: check z' );
  426. assert.strictEqual( a.w, s, 'setScalar: check w' );
  427. a.addScalar( s );
  428. assert.strictEqual( a.x, 2 * s, 'addScalar: check x' );
  429. assert.strictEqual( a.y, 2 * s, 'addScalar: check y' );
  430. assert.strictEqual( a.z, 2 * s, 'addScalar: check z' );
  431. assert.strictEqual( a.w, 2 * s, 'addScalar: check w' );
  432. a.subScalar( 2 * s );
  433. assert.strictEqual( a.x, 0, 'subScalar: check x' );
  434. assert.strictEqual( a.y, 0, 'subScalar: check y' );
  435. assert.strictEqual( a.z, 0, 'subScalar: check z' );
  436. assert.strictEqual( a.w, 0, 'subScalar: check w' );
  437. } );
  438. QUnit.test( 'multiplyScalar/divideScalar', ( assert ) => {
  439. const a = new Vector4( x, y, z, w );
  440. const b = new Vector4( - x, - y, - z, - w );
  441. a.multiplyScalar( - 2 );
  442. assert.ok( a.x == x * - 2, 'Passed!' );
  443. assert.ok( a.y == y * - 2, 'Passed!' );
  444. assert.ok( a.z == z * - 2, 'Passed!' );
  445. assert.ok( a.w == w * - 2, 'Passed!' );
  446. b.multiplyScalar( - 2 );
  447. assert.ok( b.x == 2 * x, 'Passed!' );
  448. assert.ok( b.y == 2 * y, 'Passed!' );
  449. assert.ok( b.z == 2 * z, 'Passed!' );
  450. assert.ok( b.w == 2 * w, 'Passed!' );
  451. a.divideScalar( - 2 );
  452. assert.ok( a.x == x, 'Passed!' );
  453. assert.ok( a.y == y, 'Passed!' );
  454. assert.ok( a.z == z, 'Passed!' );
  455. assert.ok( a.w == w, 'Passed!' );
  456. b.divideScalar( - 2 );
  457. assert.ok( b.x == - x, 'Passed!' );
  458. assert.ok( b.y == - y, 'Passed!' );
  459. assert.ok( b.z == - z, 'Passed!' );
  460. assert.ok( b.w == - w, 'Passed!' );
  461. } );
  462. QUnit.test( 'min/max/clamp', ( assert ) => {
  463. const a = new Vector4( x, y, z, w );
  464. const b = new Vector4( - x, - y, - z, - w );
  465. const c = new Vector4();
  466. c.copy( a ).min( b );
  467. assert.ok( c.x == - x, 'Passed!' );
  468. assert.ok( c.y == - y, 'Passed!' );
  469. assert.ok( c.z == - z, 'Passed!' );
  470. assert.ok( c.w == - w, 'Passed!' );
  471. c.copy( a ).max( b );
  472. assert.ok( c.x == x, 'Passed!' );
  473. assert.ok( c.y == y, 'Passed!' );
  474. assert.ok( c.z == z, 'Passed!' );
  475. assert.ok( c.w == w, 'Passed!' );
  476. c.set( - 2 * x, 2 * y, - 2 * z, 2 * w );
  477. c.clamp( b, a );
  478. assert.ok( c.x == - x, 'Passed!' );
  479. assert.ok( c.y == y, 'Passed!' );
  480. assert.ok( c.z == - z, 'Passed!' );
  481. assert.ok( c.w == w, 'Passed!' );
  482. } );
  483. QUnit.test( 'length/lengthSq', ( assert ) => {
  484. const a = new Vector4( x, 0, 0, 0 );
  485. const b = new Vector4( 0, - y, 0, 0 );
  486. const c = new Vector4( 0, 0, z, 0 );
  487. const d = new Vector4( 0, 0, 0, w );
  488. const e = new Vector4( 0, 0, 0, 0 );
  489. assert.ok( a.length() == x, 'Passed!' );
  490. assert.ok( a.lengthSq() == x * x, 'Passed!' );
  491. assert.ok( b.length() == y, 'Passed!' );
  492. assert.ok( b.lengthSq() == y * y, 'Passed!' );
  493. assert.ok( c.length() == z, 'Passed!' );
  494. assert.ok( c.lengthSq() == z * z, 'Passed!' );
  495. assert.ok( d.length() == w, 'Passed!' );
  496. assert.ok( d.lengthSq() == w * w, 'Passed!' );
  497. assert.ok( e.length() == 0, 'Passed!' );
  498. assert.ok( e.lengthSq() == 0, 'Passed!' );
  499. a.set( x, y, z, w );
  500. assert.ok( a.length() == Math.sqrt( x * x + y * y + z * z + w * w ), 'Passed!' );
  501. assert.ok( a.lengthSq() == ( x * x + y * y + z * z + w * w ), 'Passed!' );
  502. } );
  503. QUnit.test( 'lerp/clone', ( assert ) => {
  504. const a = new Vector4( x, 0, z, 0 );
  505. const b = new Vector4( 0, - y, 0, - w );
  506. assert.ok( a.lerp( a, 0 ).equals( a.lerp( a, 0.5 ) ), 'Passed!' );
  507. assert.ok( a.lerp( a, 0 ).equals( a.lerp( a, 1 ) ), 'Passed!' );
  508. assert.ok( a.clone().lerp( b, 0 ).equals( a ), 'Passed!' );
  509. assert.ok( a.clone().lerp( b, 0.5 ).x == x * 0.5, 'Passed!' );
  510. assert.ok( a.clone().lerp( b, 0.5 ).y == - y * 0.5, 'Passed!' );
  511. assert.ok( a.clone().lerp( b, 0.5 ).z == z * 0.5, 'Passed!' );
  512. assert.ok( a.clone().lerp( b, 0.5 ).w == - w * 0.5, 'Passed!' );
  513. assert.ok( a.clone().lerp( b, 1 ).equals( b ), 'Passed!' );
  514. } );
  515. // OTHERS
  516. QUnit.test( 'iterable', ( assert ) => {
  517. const v = new Vector4( 0, 0.3, 0.7, 1 );
  518. const array = [ ...v ];
  519. assert.strictEqual( array[ 0 ], 0, 'Vector4 is iterable.' );
  520. assert.strictEqual( array[ 1 ], 0.3, 'Vector4 is iterable.' );
  521. assert.strictEqual( array[ 2 ], 0.7, 'Vector4 is iterable.' );
  522. assert.strictEqual( array[ 3 ], 1, 'Vector4 is iterable.' );
  523. } );
  524. } );
  525. } );