MathUtils.tests.js 5.9 KB

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