config.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. if (typeof Test === 'undefined') require('../tester');
  2. Test('config', function () {
  3. var MAX = 1e9;
  4. function t(expected, value){
  5. Test.areEqual(expected, value);
  6. }
  7. function tx(fn, msg){
  8. Test.isException(fn, msg);
  9. }
  10. t(BigNumber.config, BigNumber.set);
  11. var obj = BigNumber.config({
  12. DECIMAL_PLACES: 100,
  13. ROUNDING_MODE: 0,
  14. EXPONENTIAL_AT: 50,
  15. RANGE: 500
  16. });
  17. Test.isTrue(
  18. obj.DECIMAL_PLACES === 100 &&
  19. obj.ROUNDING_MODE === 0 &&
  20. obj.EXPONENTIAL_AT[0] === -50 &&
  21. obj.EXPONENTIAL_AT[1] === 50 &&
  22. obj.RANGE[0] === -500 &&
  23. obj.RANGE[1] === 500
  24. );
  25. obj = BigNumber.config({
  26. DECIMAL_PLACES: 40,
  27. ROUNDING_MODE: 4,
  28. EXPONENTIAL_AT: 1E9,
  29. RANGE: 1E9
  30. });
  31. t('object', typeof obj);
  32. t(40, obj.DECIMAL_PLACES);
  33. t(4, obj.ROUNDING_MODE);
  34. t('object', typeof obj.EXPONENTIAL_AT);
  35. t(2, obj.EXPONENTIAL_AT.length);
  36. t(-1e9, obj.EXPONENTIAL_AT[0]);
  37. t(1e9, obj.EXPONENTIAL_AT[1]);
  38. t('object', typeof obj.RANGE);
  39. t(2, obj.RANGE.length);
  40. t(-1e9, obj.RANGE[0]);
  41. t(1e9, obj.RANGE[1]);
  42. obj = BigNumber.config({EXPONENTIAL_AT: [-7, 21], RANGE: [-324, 308]});
  43. // DECIMAL_PLACES
  44. t(0, BigNumber.config({DECIMAL_PLACES: 0}).DECIMAL_PLACES);
  45. t(1, BigNumber.config({DECIMAL_PLACES: 1}).DECIMAL_PLACES);
  46. t(20, BigNumber.config({DECIMAL_PLACES: 20}).DECIMAL_PLACES);
  47. t(300000, BigNumber.config({DECIMAL_PLACES: 300000}).DECIMAL_PLACES);
  48. t(4e+8, BigNumber.config({DECIMAL_PLACES: 4e8}).DECIMAL_PLACES);
  49. t(123456789, BigNumber.config({DECIMAL_PLACES: 123456789}).DECIMAL_PLACES);
  50. t(2000, BigNumber.config({DECIMAL_PLACES: 2e+3}).DECIMAL_PLACES);
  51. t(MAX, BigNumber.config({DECIMAL_PLACES: MAX}).DECIMAL_PLACES);
  52. tx(function () {BigNumber.config({DECIMAL_PLACES: -1})}, "DECIMAL_PLACES: -1");
  53. tx(function () {BigNumber.config({DECIMAL_PLACES: 0.1})}, "DECIMAL_PLACES: 0.1");
  54. tx(function () {BigNumber.config({DECIMAL_PLACES: 1.1})}, "DECIMAL_PLACES: 1.1");
  55. tx(function () {BigNumber.config({DECIMAL_PLACES: -1.1})}, "DECIMAL_PLACES: -1.1");
  56. tx(function () {BigNumber.config({DECIMAL_PLACES: 8.1})}, "DECIMAL_PLACES: 8.1");
  57. tx(function () {BigNumber.config({DECIMAL_PLACES: MAX + 1})}, "DECIMAL_PLACES: MAX + 1");
  58. tx(function () {BigNumber.config({DECIMAL_PLACES: []})}, "DECIMAL_PLACES: []");
  59. tx(function () {BigNumber.config({DECIMAL_PLACES: {}})}, "DECIMAL_PLACES: {}");
  60. tx(function () {BigNumber.config({DECIMAL_PLACES: ''})}, "DECIMAL_PLACES: ''");
  61. tx(function () {BigNumber.config({DECIMAL_PLACES: ' '})}, "DECIMAL_PLACES: ' '");
  62. tx(function () {BigNumber.config({DECIMAL_PLACES: 'hi'})}, "DECIMAL_PLACES: 'hi'");
  63. tx(function () {BigNumber.config({DECIMAL_PLACES: '1e+999'})}, "DECIMAL_PLACES: '1e+999'");
  64. tx(function () {BigNumber.config({DECIMAL_PLACES: NaN})}, "DECIMAL_PLACES: NaN");
  65. tx(function () {BigNumber.config({DECIMAL_PLACES: Infinity})}, "DECIMAL_PLACES: Infinity");
  66. tx(function () {BigNumber.config({DECIMAL_PLACES: null})}, "DECIMAL_PLACES: null");
  67. tx(function () {BigNumber.config({DECIMAL_PLACES: undefined})}, "DECIMAL_PLACES: undefined");
  68. BigNumber.config({DECIMAL_PLACES: 40});
  69. // ROUNDING_MODE
  70. t(0, BigNumber.config({ROUNDING_MODE: 0}).ROUNDING_MODE);
  71. t(1, BigNumber.config({ROUNDING_MODE: 1}).ROUNDING_MODE);
  72. t(2, BigNumber.config({ROUNDING_MODE: 2}).ROUNDING_MODE);
  73. t(3, BigNumber.config({ROUNDING_MODE: 3}).ROUNDING_MODE);
  74. t(4, BigNumber.config({ROUNDING_MODE: 4}).ROUNDING_MODE);
  75. t(5, BigNumber.config({ROUNDING_MODE: 5}).ROUNDING_MODE);
  76. t(6, BigNumber.config({ROUNDING_MODE: 6}).ROUNDING_MODE);
  77. t(7, BigNumber.config({ROUNDING_MODE: 7}).ROUNDING_MODE);
  78. t(8, BigNumber.config({ROUNDING_MODE: 8}).ROUNDING_MODE);
  79. t(8, BigNumber.config(null).ROUNDING_MODE);
  80. t(8, BigNumber.config(undefined).ROUNDING_MODE);
  81. tx(function () {BigNumber.config({ROUNDING_MODE: -1})}, "ROUNDING_MODE: -1");
  82. tx(function () {BigNumber.config({ROUNDING_MODE: 0.1})}, "ROUNDING_MODE: 0.1");
  83. tx(function () {BigNumber.config({ROUNDING_MODE: 1.1})}, "ROUNDING_MODE: 1.1");
  84. tx(function () {BigNumber.config({ROUNDING_MODE: -1.1})}, "ROUNDING_MODE: -1.1");
  85. tx(function () {BigNumber.config({ROUNDING_MODE: 8.1})}, "ROUNDING_MODE: 8.1");
  86. tx(function () {BigNumber.config({ROUNDING_MODE: 9})}, "ROUNDING_MODE: 9");
  87. tx(function () {BigNumber.config({ROUNDING_MODE: 11})}, "ROUNDING_MODE: 11");
  88. tx(function () {BigNumber.config({ROUNDING_MODE: []})}, "ROUNDING_MODE: []");
  89. tx(function () {BigNumber.config({ROUNDING_MODE: {}})}, "ROUNDING_MODE: {}");
  90. tx(function () {BigNumber.config({ROUNDING_MODE: ''})}, "ROUNDING_MODE: ''");
  91. tx(function () {BigNumber.config({ROUNDING_MODE: ' '})}, "ROUNDING_MODE: ' '");
  92. tx(function () {BigNumber.config({ROUNDING_MODE: 'hi'})}, "ROUNDING_MODE: 'hi'");
  93. tx(function () {BigNumber.config({ROUNDING_MODE: NaN})}, "ROUNDING_MODE: NaN");
  94. tx(function () {BigNumber.config({ROUNDING_MODE: Infinity})}, "ROUNDING_MODE: Infinity");
  95. tx(function () {BigNumber.config({ROUNDING_MODE: null})}, "ROUNDING_MODE: null");
  96. tx(function () {BigNumber.config({ROUNDING_MODE: undefined})}, "ROUNDING_MODE: undefined");
  97. // EXPONENTIAL_AT
  98. t(-7, obj.EXPONENTIAL_AT[0]);
  99. t(21, obj.EXPONENTIAL_AT[1]);
  100. tx(function () {BigNumber.config({EXPONENTIAL_AT: [0.1, 1]})}, "EXPONENTIAL_AT: [0.1, 1]");
  101. tx(function () {BigNumber.config({EXPONENTIAL_AT: [-1, -0.1]})}, "EXPONENTIAL_AT: [-1, -0.1]");
  102. tx(function () {BigNumber.config({EXPONENTIAL_AT: [1, 1]})}, "EXPONENTIAL_AT: [1, 1]");
  103. tx(function () {BigNumber.config({EXPONENTIAL_AT: [-1, -1]})}, "EXPONENTIAL_AT: [-1, -1]");
  104. tx(function () {BigNumber.config({EXPONENTIAL_AT: MAX + 1})}, "EXPONENTIAL_AT: MAX + 1");
  105. tx(function () {BigNumber.config({EXPONENTIAL_AT: -MAX - 1})}, "EXPONENTIAL_AT: -MAX - 1");
  106. tx(function () {BigNumber.config({EXPONENTIAL_AT: [-MAX - 1, MAX]})}, "EXPONENTIAL_AT: [-MAX - 1, MAX]");
  107. tx(function () {BigNumber.config({EXPONENTIAL_AT: [-MAX, MAX + 1]})}, "EXPONENTIAL_AT: [-MAX, MAX + 1]");
  108. tx(function () {BigNumber.config({EXPONENTIAL_AT: [MAX + 1, -MAX - 1]})}, "EXPONENTIAL_AT: [MAX + 1, -MAX - 1]");
  109. tx(function () {BigNumber.config({EXPONENTIAL_AT: [-Infinity, Infinity]})}, "EXPONENTIAL_AT: [Infinity, -Infinity]");
  110. tx(function () {BigNumber.config({EXPONENTIAL_AT: [Infinity, -Infinity]})}, "EXPONENTIAL_AT: [Infinity, -Infinity]");
  111. obj = BigNumber.config();
  112. t(-7, obj.EXPONENTIAL_AT[0]);
  113. t(21, obj.EXPONENTIAL_AT[1]);
  114. t(1, BigNumber.config({EXPONENTIAL_AT: 1}).EXPONENTIAL_AT[1]);
  115. t(-1, BigNumber.config({EXPONENTIAL_AT: 1}).EXPONENTIAL_AT[0]);
  116. obj = BigNumber.config({EXPONENTIAL_AT: 0});
  117. Test.isTrue(obj.EXPONENTIAL_AT[0] === 0 && obj.EXPONENTIAL_AT[1] === 0);
  118. obj = BigNumber.config({EXPONENTIAL_AT: -1});
  119. Test.isTrue(obj.EXPONENTIAL_AT[0] === -1 && obj.EXPONENTIAL_AT[1] === 1);
  120. // RANGE
  121. BigNumber.config({EXPONENTIAL_AT: [-7, 21], RANGE: [-324, 308]});
  122. t(-324, obj.RANGE[0]);
  123. t(308, obj.RANGE[1]);
  124. tx(function () {BigNumber.config({RANGE: [-0.9, 1]})}, "RANGE: [-0.9, 1]");
  125. tx(function () {BigNumber.config({RANGE: [-1, 0.9]})}, "RANGE: [-1, 0.9]");
  126. tx(function () {BigNumber.config({RANGE: [0, 1]})}, "RANGE: [0, 1]");
  127. tx(function () {BigNumber.config({RANGE: [-1, 0]})}, "RANGE: [-1, 0]");
  128. tx(function () {BigNumber.config({RANGE: 0})}, "RANGE: 0");
  129. tx(function () {BigNumber.config({RANGE: MAX + 1})}, "RANGE: MAX + 1");
  130. tx(function () {BigNumber.config({RANGE: -MAX - 1})}, "RANGE: -MAX - 1");
  131. tx(function () {BigNumber.config({RANGE: [-MAX - 1, MAX + 1]})}, "RANGE: [-MAX - 1, MAX + 1]");
  132. tx(function () {BigNumber.config({RANGE: [MAX + 1, -MAX - 1]})}, "RANGE: [MAX + 1, -MAX - 1]");
  133. tx(function () {BigNumber.config({RANGE: Infinity})}, "RANGE: Infinity");
  134. tx(function () {BigNumber.config({RANGE: "-Infinity"})}, "RANGE: '-Infinity'");
  135. tx(function () {BigNumber.config({RANGE: [-Infinity, Infinity]})}, "RANGE: [-Infinity, Infinity]");
  136. tx(function () {BigNumber.config({RANGE: [Infinity, -Infinity]})}, "RANGE: [Infinity, -Infinity]");
  137. obj = BigNumber.config();
  138. t(-324, obj.RANGE[0]);
  139. t(308, obj.RANGE[1]);
  140. var hundred = new BigNumber(100);
  141. t('100', hundred.toString());
  142. t('100', new BigNumber(hundred).toString());
  143. t(1, BigNumber.config({RANGE: 1}).RANGE[1]);
  144. t(-1, BigNumber.config({RANGE: 1}).RANGE[0]);
  145. obj = BigNumber.config({RANGE: 1});
  146. Test.isTrue(obj.RANGE[0] === -1 && obj.RANGE[1] === 1);
  147. obj = BigNumber.config({RANGE: -1});
  148. Test.isTrue(obj.RANGE[0] === -1 && obj.RANGE[1] === 1);
  149. t('1', new BigNumber(1).toString());
  150. t('99', new BigNumber(99).toString());
  151. t('-99', new BigNumber(-99).toString());
  152. t('Infinity', new BigNumber(100).toString());
  153. t('-Infinity', new BigNumber(-100).toString());
  154. t('0.99', new BigNumber(0.99).toString());
  155. t('0.1', new BigNumber(0.1).toString());
  156. t('0', new BigNumber(0.09).toString());
  157. t('-0', new BigNumber(-0.09).valueOf());
  158. t('100', hundred.toString());
  159. t('Infinity', new BigNumber(hundred).toString());
  160. t('-Infinity', hundred.negated().toString());
  161. // FORMAT
  162. tx(function () {BigNumber.config({FORMAT: ''})}, "FORMAT: ''");
  163. tx(function () {BigNumber.config({FORMAT: 1})}, "FORMAT: 1");
  164. obj = {
  165. decimalSeparator: '.',
  166. groupSeparator: ',',
  167. groupSize: 3,
  168. secondaryGroupSize: 0,
  169. fractionGroupSeparator: '\xA0',
  170. fractionGroupSize: 0
  171. };
  172. t(obj, BigNumber.config({FORMAT: obj}).FORMAT);
  173. t('.', BigNumber.config().FORMAT.decimalSeparator);
  174. obj.decimalSeparator = ',';
  175. t(',', BigNumber.config().FORMAT.decimalSeparator);
  176. // ALPHABET
  177. BigNumber.config({ALPHABET: '0123456789abcdefghijklmnopqrstuvwxyz'});
  178. tx(function () {BigNumber.config({ALPHABET: ''})}, "ALPHABET: ''");
  179. tx(function () {BigNumber.config({ALPHABET: '1'})}, "ALPHABET: '1'");
  180. tx(function () {BigNumber.config({ALPHABET: 2})}, "ALPHABET: 2");
  181. tx(function () {BigNumber.config({ALPHABET: true})}, "ALPHABET: true");
  182. tx(function () {BigNumber.config({ALPHABET: 'aba'})}, "ALPHABET: 'aba'");
  183. tx(function () {BigNumber.config({ALPHABET: '0.'})}, "ALPHABET: '0.'");
  184. tx(function () {BigNumber.config({ALPHABET: '0-'})}, "ALPHABET: '0-'");
  185. tx(function () {BigNumber.config({ALPHABET: '0+'})}, "ALPHABET: '0+'");
  186. tx(function () {BigNumber.config({ALPHABET: '0123456789.'})}, "ALPHABET: '0123456789.'");
  187. BigNumber.config({ALPHABET: '0,'});
  188. t('0,', BigNumber.config().ALPHABET);
  189. BigNumber.config({ALPHABET: 'xy'});
  190. t('xy', BigNumber.config().ALPHABET);
  191. BigNumber.config({ALPHABET: '0123456789TE'});
  192. t('0123456789TE', BigNumber.config().ALPHABET);
  193. BigNumber.config({ALPHABET: '9876543210'});
  194. t('9876543210', BigNumber.config().ALPHABET);
  195. BigNumber.config({ALPHABET: '0123456789abcdefghijklmnopqrstuvwxyz'});
  196. });