Math.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * @author humbletim / https://github.com/humbletim
  3. */
  4. QUnit.module( "Math" );
  5. //https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign
  6. //http://people.mozilla.org/~jorendorff/es6-draft.html#sec-math.sign
  7. /*
  8. 20.2.2.29 Math.sign(x)
  9. Returns the sign of the x, indicating whether x is positive, negative or zero.
  10. If x is NaN, the result is NaN.
  11. If x is -0, the result is -0.
  12. If x is +0, the result is +0.
  13. If x is negative and not -0, the result is -1.
  14. If x is positive and not +0, the result is +1.
  15. */
  16. QUnit.test( "Math.sign/polyfill", function( assert ) {
  17. assert.ok( isNaN( Math.sign(NaN) ) , "If x is NaN<NaN>, the result is NaN.");
  18. assert.ok( isNaN( Math.sign(new THREE.Vector3()) ) , "If x is NaN<object>, the result is NaN.");
  19. assert.ok( isNaN( Math.sign() ) , "If x is NaN<undefined>, the result is NaN.");
  20. assert.ok( isNaN( Math.sign('--3') ) , "If x is NaN<'--3'>, the result is NaN.");
  21. assert.ok( isNegativeZero( Math.sign(-0) ) , "If x is -0, the result is -0.");
  22. assert.ok( Math.sign(+0) === +0 , "If x is +0, the result is +0.");
  23. assert.ok( Math.sign(-Infinity) === -1 , "If x is negative<-Infinity> and not -0, the result is -1.");
  24. assert.ok( Math.sign('-3') === -1 , "If x is negative<'-3'> and not -0, the result is -1.");
  25. assert.ok( Math.sign('-1e-10') === -1 , "If x is negative<'-1e-10'> and not -0, the result is -1.");
  26. assert.ok( Math.sign(+Infinity) === +1 , "If x is positive<+Infinity> and not +0, the result is +1.");
  27. assert.ok( Math.sign('+3') === +1 , "If x is positive<'+3'> and not +0, the result is +1.");
  28. // Comparing with -0 is tricky because 0 === -0. But
  29. // luckily 1 / -0 === -Infinity so we can use that.
  30. function isNegativeZero( value ) {
  31. return value === 0 && 1 / value < 0;
  32. }
  33. });
  34. QUnit.test( "generateUUID", function ( assert ) {
  35. var a = THREE.Math.generateUUID();
  36. var regex = /[A-Z0-9]{8}-[A-Z0-9]{4}-4[A-Z0-9]{3}-[A-Z0-9]{4}-[A-Z0-9]{12}/i;
  37. // note the fixed '4' here ----------^
  38. assert.ok( regex.test( a ), "Generated UUID matches the expected pattern" );
  39. } );
  40. QUnit.test( "clamp", function ( assert ) {
  41. assert.strictEqual( THREE.Math.clamp( 0.5, 0, 1 ), 0.5, "Value already within limits" );
  42. assert.strictEqual( THREE.Math.clamp( 0, 0, 1 ), 0, "Value equal to one limit" );
  43. assert.strictEqual( THREE.Math.clamp( - 0.1, 0, 1 ), 0, "Value too low" );
  44. assert.strictEqual( THREE.Math.clamp( 1.1, 0, 1 ), 1, "Value too high" );
  45. } );
  46. QUnit.test( "euclideanModulo", function ( assert ) {
  47. assert.ok( isNaN( THREE.Math.euclideanModulo( 6, 0 ) ), "Division by zero returns NaN" );
  48. assert.strictEqual( THREE.Math.euclideanModulo( 6, 1 ), 0, "Divison by trivial divisor" );
  49. assert.strictEqual( THREE.Math.euclideanModulo( 6, 2 ), 0, "Divison by non-trivial divisor" );
  50. assert.strictEqual( THREE.Math.euclideanModulo( 6, 5 ), 1, "Divison by itself - 1" );
  51. assert.strictEqual( THREE.Math.euclideanModulo( 6, 6 ), 0, "Divison by itself" );
  52. assert.strictEqual( THREE.Math.euclideanModulo( 6, 7 ), 6, "Divison by itself + 1" );
  53. } );
  54. QUnit.test( "mapLinear", function ( assert ) {
  55. assert.strictEqual( THREE.Math.mapLinear( 0.5, 0, 1, 0, 10 ), 5, "Value within range" );
  56. assert.strictEqual( THREE.Math.mapLinear( 0.0, 0, 1, 0, 10 ), 0, "Value equal to lower boundary" );
  57. assert.strictEqual( THREE.Math.mapLinear( 1.0, 0, 1, 0, 10 ), 10, "Value equal to upper boundary" );
  58. } );
  59. QUnit.test( "smoothstep", function ( assert ) {
  60. assert.strictEqual( THREE.Math.smoothstep( - 1, 0, 2 ), 0, "Value lower than minimum" );
  61. assert.strictEqual( THREE.Math.smoothstep( 0, 0, 2 ), 0, "Value equal to minimum" );
  62. assert.strictEqual( THREE.Math.smoothstep( 0.5, 0, 2 ), 0.15625, "Value within limits" );
  63. assert.strictEqual( THREE.Math.smoothstep( 1, 0, 2 ), 0.5, "Value within limits" );
  64. assert.strictEqual( THREE.Math.smoothstep( 1.5, 0, 2 ), 0.84375, "Value within limits" );
  65. assert.strictEqual( THREE.Math.smoothstep( 2, 0, 2 ), 1, "Value equal to maximum" );
  66. assert.strictEqual( THREE.Math.smoothstep( 3, 0, 2 ), 1, "Value highter than maximum" );
  67. } );
  68. QUnit.test( "randInt", function ( assert ) {
  69. var low = 1, high = 3;
  70. var a = THREE.Math.randInt( low, high );
  71. assert.ok( a >= low, "Value equal to or higher than lower limit" );
  72. assert.ok( a <= high, "Value equal to or lower than upper limit" );
  73. } );
  74. QUnit.test( "randFloat", function ( assert ) {
  75. var low = 1, high = 3;
  76. var a = THREE.Math.randFloat( low, high );
  77. assert.ok( a >= low, "Value equal to or higher than lower limit" );
  78. assert.ok( a <= high, "Value equal to or lower than upper limit" );
  79. } );
  80. QUnit.test( "randFloatSpread", function ( assert ) {
  81. var a = THREE.Math.randFloatSpread( 3 );
  82. assert.ok( a > - 3 / 2, "Value higher than lower limit" );
  83. assert.ok( a < 3 / 2, "Value lower than upper limit" );
  84. } );
  85. QUnit.test( "degToRad", function ( assert ) {
  86. assert.strictEqual( THREE.Math.degToRad( 0 ), 0, "0 degrees" );
  87. assert.strictEqual( THREE.Math.degToRad( 90 ), Math.PI / 2, "90 degrees" );
  88. assert.strictEqual( THREE.Math.degToRad( 180 ), Math.PI, "180 degrees" );
  89. assert.strictEqual( THREE.Math.degToRad( 360 ), Math.PI * 2, "360 degrees" );
  90. } );
  91. QUnit.test( "radToDeg", function ( assert ) {
  92. assert.strictEqual( THREE.Math.radToDeg( 0 ), 0, "0 radians" );
  93. assert.strictEqual( THREE.Math.radToDeg( Math.PI / 2 ), 90, "Math.PI / 2 radians" );
  94. assert.strictEqual( THREE.Math.radToDeg( Math.PI ), 180, "Math.PI radians" );
  95. assert.strictEqual( THREE.Math.radToDeg( Math.PI * 2 ), 360, "Math.PI * 2 radians" );
  96. } );
  97. QUnit.test( "isPowerOfTwo", function ( assert ) {
  98. assert.strictEqual( THREE.Math.isPowerOfTwo( 0 ), false, "0 is not a PoT" );
  99. assert.strictEqual( THREE.Math.isPowerOfTwo( 1 ), true, "1 is a PoT" );
  100. assert.strictEqual( THREE.Math.isPowerOfTwo( 2 ), true, "2 is a PoT" );
  101. assert.strictEqual( THREE.Math.isPowerOfTwo( 3 ), false, "3 is not a PoT" );
  102. assert.strictEqual( THREE.Math.isPowerOfTwo( 4 ), true, "4 is a PoT" );
  103. } );
  104. QUnit.test( "ceilPowerOfTwo", function ( assert ) {
  105. assert.strictEqual( THREE.Math.ceilPowerOfTwo( 1 ), 1, "Closest higher PoT to 1 is 1" );
  106. assert.strictEqual( THREE.Math.ceilPowerOfTwo( 3 ), 4, "Closest higher PoT to 3 is 4" );
  107. assert.strictEqual( THREE.Math.ceilPowerOfTwo( 4 ), 4, "Closest higher PoT to 4 is 4" );
  108. } );
  109. QUnit.test( "floorPowerOfTwo", function ( assert ) {
  110. assert.strictEqual( THREE.Math.floorPowerOfTwo( 1 ), 1, "Closest lower PoT to 1 is 1" );
  111. assert.strictEqual( THREE.Math.floorPowerOfTwo( 3 ), 2, "Closest lower PoT to 3 is 2" );
  112. assert.strictEqual( THREE.Math.floorPowerOfTwo( 4 ), 4, "Closest lower PoT to 4 is 4" );
  113. } );