isBigNumber.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. if (typeof Test === 'undefined') require('../tester');
  2. Test('isBigNumber', function () {
  3. function t(expected, value){
  4. Test.areEqual(expected, BigNumber.isBigNumber(value));
  5. }
  6. function tx(fn, msg){
  7. Test.isException(fn, msg);
  8. }
  9. t(false, void 0);
  10. t(false, null);
  11. t(false, '0');
  12. t(false, 0);
  13. t(false, 1);
  14. t(false, NaN);
  15. t(false, []);
  16. t(false, {});
  17. t(true, new BigNumber(0));
  18. t(true, new BigNumber('0'));
  19. t(true, new BigNumber(1));
  20. t(true, new BigNumber('1'));
  21. var AnotherBigNumber = BigNumber.clone();
  22. t(true, new AnotherBigNumber(0));
  23. t(true, new AnotherBigNumber('0'));
  24. t(true, new AnotherBigNumber(1));
  25. t(true, new AnotherBigNumber('1'));
  26. t(false, {c: null, e: null, s: null});
  27. t(false, {c: null, e: null, s: 1});
  28. t(false, {c: null, e: null, s: -1});
  29. t(true, {c: null, e: null, s: null, _isBigNumber: true}); // NaN
  30. t(true, {c: null, e: null, s: 1, _isBigNumber: true}); // Infinity
  31. t(true, {c: null, e: null, s: -1, _isBigNumber: true}); // -Infinity
  32. tx(function () {BigNumber.isBigNumber({c: undefined, e: null, s: null, _isBigNumber: true})}, "{c: undefined, e: null, s: null, _isBigNumber: true}");
  33. tx(function () {BigNumber.isBigNumber({c: undefined, e: null, s: null, _isBigNumber: true})}, "{c: undefined, e: null, s: null, _isBigNumber: true}");
  34. tx(function () {BigNumber.isBigNumber({c: null, e: undefined, s: null, _isBigNumber: true})}, "{c: null, e: undefined, s: null, _isBigNumber: true}");
  35. tx(function () {BigNumber.isBigNumber({c: null, e: null, s: undefined, _isBigNumber: true})}, "{c: null, e: null, s: undefined, _isBigNumber: true}");
  36. tx(function () {BigNumber.isBigNumber({c: null, e: 1, s: 0, _isBigNumber: true})}, "{c: null, e: 1, s: 0, _isBigNumber: true}");
  37. tx(function () {BigNumber.isBigNumber({c: null, e: 1, s: null, _isBigNumber: true})}, "{c: null, e: 1, s: null, _isBigNumber: true}");
  38. tx(function () {BigNumber.isBigNumber({c: [1], e: 1, s: null, _isBigNumber: true})}, "{c: [1], e: 1, s: null, _isBigNumber: true}");
  39. tx(function () {BigNumber.isBigNumber({c: [1], e: null, s: null, _isBigNumber: true})}, "{c: [1], e: null, s: null, _isBigNumber: true}");
  40. tx(function () {BigNumber.isBigNumber({c: [0, 1], e: 0, s: 1, _isBigNumber: true})}, "{c: [0, 1], e: 0, s: 1, _isBigNumber: true}");
  41. tx(function () {BigNumber.isBigNumber({c: [0, 0], e: 0, s: 1, _isBigNumber: true})}, "{c: [0, 0], e: 0, s: 1, _isBigNumber: true}");
  42. tx(function () {BigNumber.isBigNumber({c: [0, 0, 1], e: 0, s: 1, _isBigNumber: true})}, "{c: [0, 0, 1], e: 0, s: 1, _isBigNumber: true}");
  43. tx(function () {BigNumber.isBigNumber({c: [1, 0], e: 0, s: 1, _isBigNumber: true})}, "{c: [1, 0], e: 0, s: 1, _isBigNumber: true}");
  44. t(true, {c: [0], e: 0, s: 1, _isBigNumber: true}); // 0
  45. tx(function () {BigNumber.isBigNumber({c: [0], e: 1, s: 1, _isBigNumber: true})}, "{c: [0], e: 1, s: 1, _isBigNumber: true}");
  46. tx(function () {BigNumber.isBigNumber({c: [0], e: -1, s: 1, _isBigNumber: true})}, "{c: [0], e: -1, s: 1, _isBigNumber: true}");
  47. t(true, {c: [1], e: 0, s: 1, _isBigNumber: true}); // 1
  48. t(true, {c: [1], e: 0, s: -1, _isBigNumber: true}); // -1
  49. tx(function () {BigNumber.isBigNumber({c: [1], e: 1, s: 1, _isBigNumber: true})}, "{c: [1], e: 1, s: 1, _isBigNumber: true}");
  50. tx(function () {BigNumber.isBigNumber({c: [1], e: 1, s: 1, _isBigNumber: true})}, "{c: [1], e: 1, s: 1, _isBigNumber: true}");
  51. tx(function () {BigNumber.isBigNumber({c: ['1'], e: 0, s: 1, _isBigNumber: true})}, "{c: ['1'], e: 0, s: 1, _isBigNumber: true}");
  52. tx(function () {BigNumber.isBigNumber({c: [1], e: '0', s: 1, _isBigNumber: true})}, "{c: [1], e: '0', s: 1, _isBigNumber: true}");
  53. tx(function () {BigNumber.isBigNumber({c: [1], e: 0, s: '0', _isBigNumber: true})}, "{c: [1], e: 0, s: '0', _isBigNumber: true}");
  54. tx(function () {BigNumber.isBigNumber({c: ['1'], e: undefined, s: 1, _isBigNumber: true})}, "{c: ['1'], e: undefined, s: 1, _isBigNumber: true}");
  55. tx(function () {BigNumber.isBigNumber({c: [1.1], e: 0, s: 1, _isBigNumber: true})}, "{c: [1.1], e: 0, s: 1, _isBigNumber: true}");
  56. tx(function () {BigNumber.isBigNumber({c: [1], e: 0.1, s: 1, _isBigNumber: true})}, "{c: [1], e: 0.1, s: 1, _isBigNumber: true}");
  57. tx(function () {BigNumber.isBigNumber({c: [1], e: 0, s: 1.1, _isBigNumber: true})}, "{c: [1], e: 0, s: 1.1, _isBigNumber: true}");
  58. tx(function () {BigNumber.isBigNumber({c: [1], e: 0, s: -1.1, _isBigNumber: true})}, "{c: [1], e: 0, s: -1.1, _isBigNumber: true}");
  59. t(true, {c: [10], e: 1, s: 1, _isBigNumber: true}); // 10
  60. tx(function () {BigNumber.isBigNumber({c: [10], e: 0, s: 1, _isBigNumber: true})}, "{c: [10], e: 0, s: 1, _isBigNumber: true}");
  61. tx(function () {BigNumber.isBigNumber({c: [1], e: 1, s: 1, _isBigNumber: true})}, "{c: [1], e: 1, s: 1, _isBigNumber: true}");
  62. t(true, {c: [10000000000000], e: 13, s: 1, _isBigNumber: true}); // 1e13
  63. tx(function () {BigNumber.isBigNumber({c: [1], e: 13, s: 1, _isBigNumber: true})}, "{c: [1], e: 13, s: 1, _isBigNumber: true}");
  64. t(true, {c: [99999999999999], e: 13, s: 1, _isBigNumber: true}); // 99999999999999
  65. t(true, {c: [1], e: 14, s: 1, _isBigNumber: true}); // 100000000000000
  66. tx(function () {BigNumber.isBigNumber({c: [100000000000000], e: 14, s: 1, _isBigNumber: true})}, "{c: [100000000000000], e: 14, s: 1, _isBigNumber: true}");
  67. tx(function () {BigNumber.isBigNumber({c: [100000000000000], e: 0, s: 1, _isBigNumber: true})}, "{c: [100000000000000], e: 0, s: 1, _isBigNumber: true}");
  68. t(true, {c: [1e13], e: -1, s: -1, _isBigNumber: true}); // 0.1
  69. tx(function () {BigNumber.isBigNumber({c: [1], e: -1, s: -1, _isBigNumber: true})}, "{c: [1], e: -1, s: -1, _isBigNumber: true}");
  70. t(true, {c: [98700000], e: -7, s: -1, _isBigNumber: true}); // -0.000000987
  71. tx(function () {BigNumber.isBigNumber({c: [987], e: -7, s: -1, _isBigNumber: true})}, "{c: [987], e: -7, s: -1, _isBigNumber: true}");
  72. t(true, {c: [9, 9, 9], e: 14, s: 1, _isBigNumber: true}); // 900000000000009.00000000000009
  73. tx(function () {BigNumber.isBigNumber({c: [900000000000009, 9], e: 14, s: 1, _isBigNumber: true})}, "{c: [900000000000009, 9], e: 14, s: 1, _isBigNumber: true}");
  74. });