number.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. describe('util/number', function () {
  2. var utHelper = window.utHelper;
  3. var testCase = utHelper.prepare(['echarts/util/number']);
  4. describe('linearMap', function () {
  5. testCase('accuracyError', function (numberUtil) {
  6. var range = [-15918.3, 17724.9];
  7. var result = numberUtil.linearMap(100, [0, 100], range, true);
  8. // Should not be 17724.899999999998.
  9. expect(result).toEqual(range[1]);
  10. var range = [-62.83, 83.56];
  11. var result = numberUtil.linearMap(100, [0, 100], range, true);
  12. // Should not be 83.55999999999999.
  13. expect(result).toEqual(range[1]);
  14. });
  15. testCase('clamp', function (numberUtil) {
  16. // (1) normal order.
  17. var range = [-15918.3, 17724.9];
  18. // bigger than max
  19. var result = numberUtil.linearMap(100.1, [0, 100], range, true);
  20. expect(result).toEqual(range[1]);
  21. // smaller than min
  22. var result = numberUtil.linearMap(-2, [0, 100], range, true);
  23. expect(result).toEqual(range[0]);
  24. // equals to max
  25. var result = numberUtil.linearMap(100, [0, 100], range, true);
  26. expect(result).toEqual(range[1]);
  27. // equals to min
  28. var result = numberUtil.linearMap(0, [0, 100], range, true);
  29. expect(result).toEqual(range[0]);
  30. // (2) inverse range
  31. var range = [17724.9, -15918.3];
  32. // bigger than max
  33. var result = numberUtil.linearMap(102, [0, 100], range, true);
  34. expect(result).toEqual(range[1]);
  35. // smaller than min
  36. var result = numberUtil.linearMap(-0.001, [0, 100], range, true);
  37. expect(result).toEqual(range[0]);
  38. // equals to max
  39. var result = numberUtil.linearMap(100, [0, 100], range, true);
  40. expect(result).toEqual(range[1]);
  41. // equals to min
  42. var result = numberUtil.linearMap(0, [0, 100], range, true);
  43. expect(result).toEqual(range[0]);
  44. // (2) inverse domain
  45. // bigger than max, inverse domain
  46. var range = [-15918.3, 17724.9];
  47. // bigger than max
  48. var result = numberUtil.linearMap(102, [100, 0], range, true);
  49. expect(result).toEqual(range[0]);
  50. // smaller than min
  51. var result = numberUtil.linearMap(-0.001, [100, 0], range, true);
  52. expect(result).toEqual(range[1]);
  53. // equals to max
  54. var result = numberUtil.linearMap(100, [100, 0], range, true);
  55. expect(result).toEqual(range[0]);
  56. // equals to min
  57. var result = numberUtil.linearMap(0, [100, 0], range, true);
  58. expect(result).toEqual(range[1]);
  59. // (3) inverse domain, inverse range
  60. var range = [17724.9, -15918.3];
  61. // bigger than max
  62. var result = numberUtil.linearMap(100.1, [100, 0], range, true);
  63. expect(result).toEqual(range[0]);
  64. // smaller than min
  65. var result = numberUtil.linearMap(-2, [100, 0], range, true);
  66. expect(result).toEqual(range[1]);
  67. // equals to max
  68. var result = numberUtil.linearMap(100, [100, 0], range, true);
  69. expect(result).toEqual(range[0]);
  70. // equals to min
  71. var result = numberUtil.linearMap(0, [100, 0], range, true);
  72. expect(result).toEqual(range[1]);
  73. });
  74. testCase('noClamp', function (numberUtil) {
  75. // (1) normal order.
  76. var range = [-15918.3, 17724.9];
  77. // bigger than max
  78. var result = numberUtil.linearMap(100.1, [0, 100], range, false);
  79. expect(result).toEqual(17758.543199999996);
  80. // smaller than min
  81. var result = numberUtil.linearMap(-2, [0, 100], range, false);
  82. expect(result).toEqual(-16591.164);
  83. // equals to max
  84. var result = numberUtil.linearMap(100, [0, 100], range, false);
  85. expect(result).toEqual(17724.9);
  86. // equals to min
  87. var result = numberUtil.linearMap(0, [0, 100], range, false);
  88. expect(result).toEqual(-15918.3);
  89. // (2) inverse range
  90. var range = [17724.9, -15918.3];
  91. // bigger than max
  92. var result = numberUtil.linearMap(102, [0, 100], range, false);
  93. expect(result).toEqual(-16591.163999999997);
  94. // smaller than min
  95. var result = numberUtil.linearMap(-0.001, [0, 100], range, false);
  96. expect(result).toEqual(17725.236432);
  97. // equals to max
  98. var result = numberUtil.linearMap(100, [0, 100], range, false);
  99. expect(result).toEqual(-15918.3);
  100. // equals to min
  101. var result = numberUtil.linearMap(0, [0, 100], range, false);
  102. expect(result).toEqual(17724.9);
  103. // (2) inverse domain
  104. // bigger than max, inverse domain
  105. var range = [-15918.3, 17724.9];
  106. // bigger than max
  107. var result = numberUtil.linearMap(102, [100, 0], range, false);
  108. expect(result).toEqual(-16591.164);
  109. // smaller than min
  110. var result = numberUtil.linearMap(-0.001, [100, 0], range, false);
  111. expect(result).toEqual(17725.236432);
  112. // equals to max
  113. var result = numberUtil.linearMap(100, [100, 0], range, false);
  114. expect(result).toEqual(-15918.3);
  115. // equals to min
  116. var result = numberUtil.linearMap(0, [100, 0], range, false);
  117. expect(result).toEqual(17724.9);
  118. // (3) inverse domain, inverse range
  119. var range = [17724.9, -15918.3];
  120. // bigger than max
  121. var result = numberUtil.linearMap(100.1, [100, 0], range, false);
  122. expect(result).toEqual(17758.5432);
  123. // smaller than min
  124. var result = numberUtil.linearMap(-2, [100, 0], range, false);
  125. expect(result).toEqual(-16591.163999999997);
  126. // equals to max
  127. var result = numberUtil.linearMap(100, [100, 0], range, false);
  128. expect(result).toEqual(17724.9);
  129. // equals to min
  130. var result = numberUtil.linearMap(0, [100, 0], range, false);
  131. expect(result).toEqual(-15918.3);
  132. });
  133. testCase('normal', function (numberUtil) {
  134. doTest(true);
  135. doTest(false);
  136. function doTest(clamp) {
  137. // normal
  138. var range = [444, 555];
  139. var result = numberUtil.linearMap(40, [0, 100], range, clamp);
  140. expect(result).toEqual(488.4);
  141. // inverse range
  142. var range = [555, 444];
  143. var result = numberUtil.linearMap(40, [0, 100], range, clamp);
  144. expect(result).toEqual(510.6);
  145. // inverse domain and range
  146. var range = [555, 444];
  147. var result = numberUtil.linearMap(40, [100, 0], range, clamp);
  148. expect(result).toEqual(488.4);
  149. // inverse domain
  150. var range = [444, 555];
  151. var result = numberUtil.linearMap(40, [100, 0], range, clamp);
  152. expect(result).toEqual(510.6);
  153. }
  154. });
  155. testCase('zeroInterval', function (numberUtil) {
  156. doTest(true);
  157. doTest(false);
  158. function doTest(clamp) {
  159. // zero domain interval
  160. var range = [444, 555];
  161. var result = numberUtil.linearMap(40, [1212222223.2323232, 1212222223.2323232], range, clamp);
  162. expect(result).toEqual(499.5); // half of range.
  163. // zero range interval
  164. var range = [1221212.1221372238, 1221212.1221372238];
  165. var result = numberUtil.linearMap(40, [0, 100], range, clamp);
  166. expect(result).toEqual(1221212.1221372238);
  167. // zero domain interval and range interval
  168. var range = [1221212.1221372238, 1221212.1221372238];
  169. var result = numberUtil.linearMap(40, [43.55454545, 43.55454545], range, clamp);
  170. expect(result).toEqual(1221212.1221372238);
  171. }
  172. })
  173. });
  174. });