test.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. var test = require('tap').test;
  2. var summary = require('./summary.js');
  3. test('testing data method', function (t) {
  4. t.deepEqual(
  5. summary([2, 3, 7, -2, 0, 1, 1]).data(),
  6. [2, 3, 7, -2, 0, 1, 1]
  7. );
  8. t.end();
  9. });
  10. test('testing sort method', function (t) {
  11. const data = [2, 3, 7, -2, 0, 1, 1];
  12. t.deepEqual(
  13. summary(data, true).sort(),
  14. [2, 3, 7, -2, 0, 1, 1]
  15. );
  16. t.deepEqual(data, [2, 3, 7, -2, 0, 1, 1], 'data shouldn\'t mutate');
  17. t.deepEqual(
  18. summary(data, false).sort(),
  19. [-2, 0, 1, 1, 2, 3, 7]
  20. );
  21. t.deepEqual(data, [2, 3, 7, -2, 0, 1, 1], 'data shouldn\'t mutate');
  22. t.deepEqual(
  23. summary(data).sort(),
  24. [-2, 0, 1, 1, 2, 3, 7]
  25. );
  26. t.deepEqual(data, [2, 3, 7, -2, 0, 1, 1], 'data shouldn\'t mutate');
  27. t.end();
  28. });
  29. test('testing sort method when cached', function (t) {
  30. const s = summary([2, 3, 7, -2, 0, 1, 1]);
  31. t.deepEqual(s.sort(), [-2, 0, 1, 1, 2, 3, 7]);
  32. t.deepEqual(s.sort(), [-2, 0, 1, 1, 2, 3, 7]);
  33. t.end();
  34. });
  35. test('testing size method', function (t) {
  36. t.equal(
  37. summary([1, 10]).size(),
  38. 2
  39. );
  40. t.end();
  41. });
  42. test('testing sum method', function (t) {
  43. t.equal(
  44. summary([1, 10]).sum(),
  45. 11
  46. );
  47. t.end();
  48. });
  49. test('testing sum for variable lengths', function (t) {
  50. const data = [];
  51. for (let i = 0; i < 256; i++) {
  52. data.push(i);
  53. t.equal(summary(data).sum(), (i**2 + i) / 2);
  54. }
  55. t.end();
  56. });
  57. test('testing sum method when cached', function (t) {
  58. const s = summary([1, 10]);
  59. t.equal(s.sum(), 11);
  60. t.equal(s.sum(), 11);
  61. t.end();
  62. });
  63. test('testing mode method', function (t) {
  64. t.equal(
  65. summary([10, 11, 12, 11, 12, 7, 12]).mode(),
  66. 12
  67. );
  68. t.equal(
  69. summary([10, 13, 12, 13, 12, 13, 12]).mode(),
  70. 13
  71. );
  72. t.equal(
  73. summary([1, 2, 2, 3, 3, 3, 4]).mode(),
  74. 3
  75. );
  76. t.equal(
  77. summary([1, 2, 2, 3, 3, 4]).mode(),
  78. 3
  79. );
  80. t.equal(
  81. summary([1, 2, 2, 2, 3, 3, 4]).mode(),
  82. 2
  83. );
  84. t.equal(
  85. summary([1, 2, 3]).mode(),
  86. 3
  87. );
  88. t.end();
  89. });
  90. test('testing mode method when cached', function (t) {
  91. const s = summary([1, 2, 3]);
  92. t.equal(s.mode(), 3);
  93. t.equal(s.mode(), 3);
  94. t.end();
  95. });
  96. test('testing mean method', function (t) {
  97. t.equal(
  98. summary([2, 4]).mean(),
  99. 3
  100. );
  101. t.end();
  102. });
  103. test('testing mean method when cached', function (t) {
  104. const s = summary([2, 4]);
  105. t.equal(s.mean(), 3);
  106. t.equal(s.mean(), 3);
  107. t.end();
  108. });
  109. test('testing quartile method', function (t) {
  110. var data = summary([
  111. 2, 27, 10, 29, 16, 8, 5, 19, 2, 2, 18,28,
  112. 7, 28, 28, 25, 19, 14, 18, 21, 25, 29, 7,
  113. 3, 21, 3, 24, 18, 12, 25
  114. ]);
  115. t.equal(data.quartile(0.00), 2.00);
  116. t.equal(data.quartile(0.13), 3.00);
  117. t.equal(data.quartile(0.26), 7.00);
  118. t.equal(data.quartile(0.39), 14.0);
  119. t.equal(data.quartile(0.52), 18.0);
  120. t.equal(data.quartile(0.65), 21.0);
  121. t.equal(data.quartile(0.78), 25.0);
  122. t.equal(data.quartile(0.91), 28.0);
  123. t.equal(data.quartile(1.00), 29.0);
  124. // when cached
  125. t.equal(data.quartile(0.00), 2.00);
  126. t.equal(data.quartile(0.13), 3.00);
  127. t.equal(data.quartile(0.26), 7.00);
  128. t.equal(data.quartile(0.39), 14.0);
  129. t.equal(data.quartile(0.52), 18.0);
  130. t.equal(data.quartile(0.65), 21.0);
  131. t.equal(data.quartile(0.78), 25.0);
  132. t.equal(data.quartile(0.91), 28.0);
  133. t.equal(data.quartile(1.00), 29.0);
  134. t.end();
  135. });
  136. test('testing median method', function (t) {
  137. var data = summary([
  138. 2, 27, 10, 29, 16, 8, 5, 19, 2, 2, 18,28,
  139. 7, 28, 28, 25, 19, 14, 18, 21, 25, 29, 7,
  140. 3, 21, 3, 24, 18, 12, 25
  141. ]);
  142. t.equal(data.median(), 18);
  143. // when cached
  144. t.equal(data.median(), 18);
  145. t.end();
  146. });
  147. test('testing variance method', function (t) {
  148. t.equal(summary([-2, -1, 0, 1, 2]).variance(), 2.5);
  149. t.end();
  150. });
  151. test('testing variance method when cached', function (t) {
  152. const s = summary([-2, -1, 0, 1, 2]);
  153. t.equal(s.variance(), 2.5);
  154. t.equal(s.variance(), 2.5);
  155. t.end();
  156. });
  157. test('testing sd method', function (t) {
  158. t.equal(summary([-2, -1, 0, 1, 2]).sd(), Math.sqrt(2.5));
  159. t.end();
  160. });
  161. test('testing sd method when cached', function (t) {
  162. const s = summary([-2, -1, 0, 1, 2]);
  163. t.equal(s.sd(), Math.sqrt(2.5));
  164. t.equal(s.sd(), Math.sqrt(2.5));
  165. t.end();
  166. });
  167. test('testing max method', function (t) {
  168. t.equal(summary([6, 10, 2, 5]).max(), 10);
  169. t.end();
  170. });
  171. test('testing max method when cached', function (t) {
  172. const s = summary([6, 10, 2, 5]);
  173. t.equal(s.max(), 10);
  174. t.equal(s.max(), 10);
  175. t.end();
  176. });
  177. test('testing min method', function (t) {
  178. t.equal(summary([6, 10, 2, 5]).min(), 2);
  179. t.end();
  180. });
  181. test('testing min method when cached', function (t) {
  182. const s = summary([6, 10, 2, 5]);
  183. t.equal(s.min(), 2);
  184. t.equal(s.min(), 2);
  185. t.end();
  186. });
  187. test('typed array', function (t) {
  188. t.equal(summary(new Int8Array([2, 4])).mean(), 3);
  189. t.end();
  190. });
  191. test('not array', function (t) {
  192. t.throws(() => summary({ length: 0 }), TypeError);
  193. t.end();
  194. });