Math.tests.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * @author humbletim / https://github.com/humbletim
  3. * @author TristanVALCKE / https://github.com/Itee
  4. */
  5. /* global QUnit */
  6. import { _Math as ThreeMath } from '../../../../src/math/Math';
  7. export default QUnit.module( 'Maths', () => {
  8. QUnit.module( 'Math', () => {
  9. // PUBLIC STUFF
  10. QUnit.test( "generateUUID", ( assert ) => {
  11. var a = ThreeMath.generateUUID();
  12. var regex = /[A-Z0-9]{8}-[A-Z0-9]{4}-4[A-Z0-9]{3}-[A-Z0-9]{4}-[A-Z0-9]{12}/i;
  13. // note the fixed '4' here ----------^
  14. assert.ok( regex.test( a ), "Generated UUID matches the expected pattern" );
  15. } );
  16. QUnit.test( "clamp", ( assert ) => {
  17. assert.strictEqual( ThreeMath.clamp( 0.5, 0, 1 ), 0.5, "Value already within limits" );
  18. assert.strictEqual( ThreeMath.clamp( 0, 0, 1 ), 0, "Value equal to one limit" );
  19. assert.strictEqual( ThreeMath.clamp( - 0.1, 0, 1 ), 0, "Value too low" );
  20. assert.strictEqual( ThreeMath.clamp( 1.1, 0, 1 ), 1, "Value too high" );
  21. } );
  22. QUnit.test( "euclideanModulo", ( assert ) => {
  23. assert.ok( isNaN( ThreeMath.euclideanModulo( 6, 0 ) ), "Division by zero returns NaN" );
  24. assert.strictEqual( ThreeMath.euclideanModulo( 6, 1 ), 0, "Divison by trivial divisor" );
  25. assert.strictEqual( ThreeMath.euclideanModulo( 6, 2 ), 0, "Divison by non-trivial divisor" );
  26. assert.strictEqual( ThreeMath.euclideanModulo( 6, 5 ), 1, "Divison by itself - 1" );
  27. assert.strictEqual( ThreeMath.euclideanModulo( 6, 6 ), 0, "Divison by itself" );
  28. assert.strictEqual( ThreeMath.euclideanModulo( 6, 7 ), 6, "Divison by itself + 1" );
  29. } );
  30. QUnit.test( "mapLinear", ( assert ) => {
  31. assert.strictEqual( ThreeMath.mapLinear( 0.5, 0, 1, 0, 10 ), 5, "Value within range" );
  32. assert.strictEqual( ThreeMath.mapLinear( 0.0, 0, 1, 0, 10 ), 0, "Value equal to lower boundary" );
  33. assert.strictEqual( ThreeMath.mapLinear( 1.0, 0, 1, 0, 10 ), 10, "Value equal to upper boundary" );
  34. } );
  35. QUnit.todo( "lerp", ( assert ) => {
  36. assert.ok( false, "everything's gonna be alright" );
  37. } );
  38. QUnit.test( "smoothstep", ( assert ) => {
  39. assert.strictEqual( ThreeMath.smoothstep( - 1, 0, 2 ), 0, "Value lower than minimum" );
  40. assert.strictEqual( ThreeMath.smoothstep( 0, 0, 2 ), 0, "Value equal to minimum" );
  41. assert.strictEqual( ThreeMath.smoothstep( 0.5, 0, 2 ), 0.15625, "Value within limits" );
  42. assert.strictEqual( ThreeMath.smoothstep( 1, 0, 2 ), 0.5, "Value within limits" );
  43. assert.strictEqual( ThreeMath.smoothstep( 1.5, 0, 2 ), 0.84375, "Value within limits" );
  44. assert.strictEqual( ThreeMath.smoothstep( 2, 0, 2 ), 1, "Value equal to maximum" );
  45. assert.strictEqual( ThreeMath.smoothstep( 3, 0, 2 ), 1, "Value highter than maximum" );
  46. } );
  47. QUnit.todo( "smootherstep", ( assert ) => {
  48. assert.ok( false, "everything's gonna be alright" );
  49. } );
  50. QUnit.test( "randInt", ( assert ) => {
  51. var low = 1, high = 3;
  52. var a = ThreeMath.randInt( low, high );
  53. assert.ok( a >= low, "Value equal to or higher than lower limit" );
  54. assert.ok( a <= high, "Value equal to or lower than upper limit" );
  55. } );
  56. QUnit.test( "randFloat", ( assert ) => {
  57. var low = 1, high = 3;
  58. var a = ThreeMath.randFloat( low, high );
  59. assert.ok( a >= low, "Value equal to or higher than lower limit" );
  60. assert.ok( a <= high, "Value equal to or lower than upper limit" );
  61. } );
  62. QUnit.test( "randFloatSpread", ( assert ) => {
  63. var a = ThreeMath.randFloatSpread( 3 );
  64. assert.ok( a > - 3 / 2, "Value higher than lower limit" );
  65. assert.ok( a < 3 / 2, "Value lower than upper limit" );
  66. } );
  67. QUnit.test( "degToRad", ( assert ) => {
  68. assert.strictEqual( ThreeMath.degToRad( 0 ), 0, "0 degrees" );
  69. assert.strictEqual( ThreeMath.degToRad( 90 ), Math.PI / 2, "90 degrees" );
  70. assert.strictEqual( ThreeMath.degToRad( 180 ), Math.PI, "180 degrees" );
  71. assert.strictEqual( ThreeMath.degToRad( 360 ), Math.PI * 2, "360 degrees" );
  72. } );
  73. QUnit.test( "radToDeg", ( assert ) => {
  74. assert.strictEqual( ThreeMath.radToDeg( 0 ), 0, "0 radians" );
  75. assert.strictEqual( ThreeMath.radToDeg( Math.PI / 2 ), 90, "Math.PI / 2 radians" );
  76. assert.strictEqual( ThreeMath.radToDeg( Math.PI ), 180, "Math.PI radians" );
  77. assert.strictEqual( ThreeMath.radToDeg( Math.PI * 2 ), 360, "Math.PI * 2 radians" );
  78. } );
  79. QUnit.test( "isPowerOfTwo", ( assert ) => {
  80. assert.strictEqual( ThreeMath.isPowerOfTwo( 0 ), false, "0 is not a PoT" );
  81. assert.strictEqual( ThreeMath.isPowerOfTwo( 1 ), true, "1 is a PoT" );
  82. assert.strictEqual( ThreeMath.isPowerOfTwo( 2 ), true, "2 is a PoT" );
  83. assert.strictEqual( ThreeMath.isPowerOfTwo( 3 ), false, "3 is not a PoT" );
  84. assert.strictEqual( ThreeMath.isPowerOfTwo( 4 ), true, "4 is a PoT" );
  85. } );
  86. QUnit.test( "ceilPowerOfTwo", ( assert ) => {
  87. assert.strictEqual( ThreeMath.ceilPowerOfTwo( 1 ), 1, "Closest higher PoT to 1 is 1" );
  88. assert.strictEqual( ThreeMath.ceilPowerOfTwo( 3 ), 4, "Closest higher PoT to 3 is 4" );
  89. assert.strictEqual( ThreeMath.ceilPowerOfTwo( 4 ), 4, "Closest higher PoT to 4 is 4" );
  90. } );
  91. QUnit.test( "floorPowerOfTwo", ( assert ) => {
  92. assert.strictEqual( ThreeMath.floorPowerOfTwo( 1 ), 1, "Closest lower PoT to 1 is 1" );
  93. assert.strictEqual( ThreeMath.floorPowerOfTwo( 3 ), 2, "Closest lower PoT to 3 is 2" );
  94. assert.strictEqual( ThreeMath.floorPowerOfTwo( 4 ), 4, "Closest lower PoT to 4 is 4" );
  95. } );
  96. } );
  97. } );
粤ICP备19079148号