shiftedBy.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. if (typeof Test === 'undefined') require('../tester');
  2. Test('shiftedBy', function () {
  3. function t(expected, n, k) {
  4. Test.areEqual(String(expected), String(new BigNumber(n).shiftedBy(k)));
  5. }
  6. BigNumber.config({
  7. DECIMAL_PLACES: 20,
  8. ROUNDING_MODE: 4,
  9. EXPONENTIAL_AT: [-7, 21],
  10. RANGE: 1e9,
  11. POW_PRECISION: 0
  12. });
  13. t(0, 0, 0);
  14. t(10, 1, 1);
  15. t(0.1, 1, -1);
  16. t(200, 2, 2);
  17. t(2e+31, 2, 31);
  18. t(0.02, 2, -2);
  19. t(0.0002, 2, -4);
  20. t(1e+100, 1, 100);
  21. t(9999990, 9999.99, 3);
  22. t(NaN, NaN, 0);
  23. t(NaN, NaN, -1);
  24. t(NaN, NaN, 1);
  25. t(NaN, NaN, 2);
  26. t(NaN, NaN, -2);
  27. t(Infinity, Infinity, 0);
  28. t(-Infinity, -Infinity, -1);
  29. t(Infinity, Infinity, 1);
  30. t(-Infinity, -Infinity, 2);
  31. t(Infinity, Infinity, -2);
  32. t(0, 0, 1000);
  33. t(0, 0, 2);
  34. t(0, 0, -2);
  35. t(2, 2, 0);
  36. Test.areEqual('0', new BigNumber(0).shiftedBy(0).valueOf());
  37. Test.areEqual('-0', new BigNumber(-0).shiftedBy(0).valueOf());
  38. Test.areEqual('0', new BigNumber(0).shiftedBy(-0).valueOf());
  39. Test.areEqual('-0', new BigNumber(-0).shiftedBy(-0).valueOf());
  40. Test.areEqual('0', new BigNumber(0).shiftedBy(1000).valueOf());
  41. t('1e+1000000', 1, 1e6)
  42. t(1, '1e-1000000', 1e6)
  43. t('9.9e+999999999', 0.99, 1e+9);
  44. Test.isException(function () {new BigNumber('12.345').shiftedBy(true)}, ".shiftedBy(true)");
  45. Test.isException(function () {new BigNumber('12.345').shiftedBy(false)}, ".shiftedBy(false)");
  46. Test.isException(function () {new BigNumber('12.345').shiftedBy([])}, ".shiftedBy([])");
  47. Test.isException(function () {new BigNumber('12.345').shiftedBy({})}, ".shiftedBy({})");
  48. Test.isException(function () {new BigNumber('12.345').shiftedBy('')}, ".shiftedBy('')");
  49. Test.isException(function () {new BigNumber('12.345').shiftedBy(' ')}, ".shiftedBy(' ')");
  50. Test.isException(function () {new BigNumber('12.345').shiftedBy('4e')}, ".shiftedBy('4e')");
  51. Test.isException(function () {new BigNumber('12.345').shiftedBy('hello')}, ".shiftedBy('hello')");
  52. Test.isException(function () {new BigNumber('12.345').shiftedBy('\t')}, ".shiftedBy('\t')");
  53. Test.isException(function () {new BigNumber('12.345').shiftedBy(new Date)}, ".shiftedBy(new Date)");
  54. Test.isException(function () {new BigNumber('12.345').shiftedBy(new RegExp)}, ".shiftedBy(new RegExp)");
  55. Test.isException(function () {new BigNumber('12.345').shiftedBy(function (){})}, ".shiftedBy(function (){})");
  56. t('3.45345e+196', 0.000345345, 200);
  57. t('3.45345e-14', 0.000345345, -10);
  58. });