SumAccumulator.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. "use strict";
  2. var assert = require("assert"),
  3. SumAccumulator = require("../../../../lib/pipeline/accumulators/SumAccumulator");
  4. // Mocha one-liner to make these tests self-hosted
  5. if(!module.parent)return(require.cache[__filename]=null,(new(require("mocha"))({ui:"exports",reporter:"spec",grep:process.env.TEST_GREP})).addFile(__filename).run(process.exit));
  6. exports.SumAccumulator = {
  7. ".constructor()": {
  8. "should create instance of Accumulator": function() {
  9. assert(new SumAccumulator() instanceof SumAccumulator);
  10. },
  11. "should throw error if called with args": function() {
  12. assert.throws(function() {
  13. new SumAccumulator(123);
  14. });
  15. },
  16. },
  17. ".create()": {
  18. "should return an instance of the accumulator": function() {
  19. assert(SumAccumulator.create() instanceof SumAccumulator);
  20. },
  21. },
  22. "#process()": {
  23. "should return 0 if no inputs evaluated": function testNone() {
  24. var acc = SumAccumulator.create();
  25. assert.strictEqual(acc.getValue(), 0);
  26. },
  27. "should return value for one int input": function testOneInt() {
  28. var acc = SumAccumulator.create();
  29. acc.process(5);
  30. assert.strictEqual(acc.getValue(), 5);
  31. },
  32. "should return value for one long input": function testOneLong() {
  33. var acc = SumAccumulator.create();
  34. acc.process(6e24);
  35. assert.strictEqual(acc.getValue(), 6e24);
  36. },
  37. "should return value for one large long input": function testOneLargeLong() {
  38. var acc = SumAccumulator.create();
  39. acc.process(6e42);
  40. assert.strictEqual(acc.getValue(), 6e42);
  41. },
  42. "should return value for one double input": function testOneDouble() {
  43. var acc = SumAccumulator.create();
  44. acc.process(7.0);
  45. assert.strictEqual(acc.getValue(), 7.0);
  46. },
  47. "should return value for one fractional double input": function testNanDouble() {
  48. var acc = SumAccumulator.create();
  49. acc.process(NaN);
  50. assert.notEqual(acc.getValue(), acc.getValue()); // NaN is unequal to itself.
  51. },
  52. beforeEach: function() { // used in the tests below
  53. this.getSumValueFor = function(first, second) { // kind of like TwoOperandBase
  54. var acc = SumAccumulator.create();
  55. for (var i = 0, l = arguments.length; i < l; i++) {
  56. acc.process(arguments[i]);
  57. }
  58. return acc.getValue();
  59. };
  60. },
  61. "should return sum for two ints": function testIntInt() {
  62. var summand1 = 4,
  63. summand2 = 5,
  64. expected = 9;
  65. assert.strictEqual(this.getSumValueFor(summand1, summand2), expected);
  66. assert.strictEqual(this.getSumValueFor(summand2, summand1), expected);
  67. },
  68. "should return sum for two ints (overflow)": function testIntIntOverflow() {
  69. var summand1 = 32767,
  70. summand2 = 10,
  71. expected = 32767 + 10;
  72. assert.strictEqual(this.getSumValueFor(summand1, summand2), expected);
  73. assert.strictEqual(this.getSumValueFor(summand2, summand1), expected);
  74. },
  75. "should return sum for two ints (negative overflow)": function testIntIntNegativeOverflow() {
  76. var summand1 = 32767,
  77. summand2 = -10,
  78. expected = 32767 + -10;
  79. assert.strictEqual(this.getSumValueFor(summand1, summand2), expected);
  80. assert.strictEqual(this.getSumValueFor(summand2, summand1), expected);
  81. },
  82. "should return sum for int and long": function testIntLong() {
  83. var summand1 = 4,
  84. summand2 = 5e24,
  85. expected = 4 + 5e24;
  86. assert.strictEqual(this.getSumValueFor(summand1, summand2), expected);
  87. assert.strictEqual(this.getSumValueFor(summand2, summand1), expected);
  88. },
  89. "should return sum for max int and long (no int overflow)": function testIntLongNoIntOverflow() {
  90. var summand1 = 32767,
  91. summand2 = 1e24,
  92. expected = 32767 + 1e24;
  93. assert.strictEqual(this.getSumValueFor(summand1, summand2), expected);
  94. assert.strictEqual(this.getSumValueFor(summand2, summand1), expected);
  95. },
  96. "should return sum for int and max long (long overflow)": function testIntLongLongOverflow() {
  97. var summand1 = 1,
  98. summand2 = 9223372036854775807,
  99. expected = 1 + 9223372036854775807;
  100. assert.strictEqual(this.getSumValueFor(summand1, summand2), expected);
  101. assert.strictEqual(this.getSumValueFor(summand2, summand1), expected);
  102. },
  103. "should return sum for long and long": function testLongLong() {
  104. var summand1 = 4e24,
  105. summand2 = 5e24,
  106. expected = 4e24 + 5e24;
  107. assert.strictEqual(this.getSumValueFor(summand1, summand2), expected);
  108. assert.strictEqual(this.getSumValueFor(summand2, summand1), expected);
  109. },
  110. "should return sum for max long and max long (overflow)": function testLongLongOverflow() {
  111. var summand1 = 9223372036854775807,
  112. summand2 = 9223372036854775807,
  113. expected = 9223372036854775807 + 9223372036854775807;
  114. assert.strictEqual(this.getSumValueFor(summand1, summand2), expected);
  115. assert.strictEqual(this.getSumValueFor(summand2, summand1), expected);
  116. },
  117. "should return sum for int and double": function testIntDouble() {
  118. var summand1 = 4,
  119. summand2 = 5.5,
  120. expected = 9.5;
  121. assert.strictEqual(this.getSumValueFor(summand1, summand2), expected);
  122. assert.strictEqual(this.getSumValueFor(summand2, summand1), expected);
  123. },
  124. "should return sum for int and NaN as NaN": function testIntNanDouble() {
  125. var summand1 = 4,
  126. summand2 = NaN,
  127. expected = NaN;
  128. assert(isNaN(this.getSumValueFor(summand1, summand2)));
  129. assert(isNaN(this.getSumValueFor(summand2, summand1)));
  130. },
  131. "should return sum for int and double (no int overflow)": function testIntDoubleNoIntOverflow() {
  132. var summand1 = 32767,
  133. summand2 = 1.0,
  134. expected = 32767 + 1.0;
  135. assert.strictEqual(this.getSumValueFor(summand1, summand2), expected);
  136. assert.strictEqual(this.getSumValueFor(summand2, summand1), expected);
  137. },
  138. "should return sum for long and double": function testLongDouble() {
  139. var summand1 = 4e24,
  140. summand2 = 5.5,
  141. expected = 4e24 + 5.5;
  142. assert.strictEqual(this.getSumValueFor(summand1, summand2), expected);
  143. assert.strictEqual(this.getSumValueFor(summand2, summand1), expected);
  144. },
  145. "should return sum for max long and double (no long overflow)": function testLongDoubleNoLongOverflow() {
  146. var summand1 = 9223372036854775807,
  147. summand2 = 1.0,
  148. expected = 9223372036854775807 + 1.0;
  149. assert.strictEqual(this.getSumValueFor(summand1, summand2), expected);
  150. assert.strictEqual(this.getSumValueFor(summand2, summand1), expected);
  151. },
  152. "should return sum for double and double": function testDoubleDouble() {
  153. var summand1 = 2.5,
  154. summand2 = 5.5,
  155. expected = 8.0;
  156. assert.strictEqual(this.getSumValueFor(summand1, summand2), expected);
  157. assert.strictEqual(this.getSumValueFor(summand2, summand1), expected);
  158. },
  159. "should return sum for double and double (overflow)": function testDoubleDoubleOverflow() {
  160. var summand1 = Number.MAX_VALUE,
  161. summand2 = Number.MAX_VALUE,
  162. expected = Infinity;
  163. assert.strictEqual(this.getSumValueFor(summand1, summand2), expected);
  164. assert.strictEqual(this.getSumValueFor(summand2, summand1), expected);
  165. },
  166. "should return sum for int and long and double": function testIntLongDouble() {
  167. assert.strictEqual(this.getSumValueFor(5, 99, 0.2), 104.2);
  168. },
  169. "should return sum for a negative value": function testNegative() {
  170. var summand1 = 5,
  171. summand2 = -8.8,
  172. expected = 5 - 8.8;
  173. assert.strictEqual(this.getSumValueFor(summand1, summand2), expected);
  174. assert.strictEqual(this.getSumValueFor(summand2, summand1), expected);
  175. },
  176. "should return sum for long and negative int": function testLongIntNegative() {
  177. var summand1 = 5e24,
  178. summand2 = -6,
  179. expected = 5e24 - 6;
  180. assert.strictEqual(this.getSumValueFor(summand1, summand2), expected);
  181. assert.strictEqual(this.getSumValueFor(summand2, summand1), expected);
  182. },
  183. "should return sum for int and null": function testIntNull() {
  184. var summand1 = 5,
  185. summand2 = null,
  186. expected = 5;
  187. assert.strictEqual(this.getSumValueFor(summand1, summand2), expected);
  188. assert.strictEqual(this.getSumValueFor(summand2, summand1), expected);
  189. },
  190. "should return sum for int and undefined": function testIntUndefined() {
  191. var summand1 = 9,
  192. summand2, // = undefined,
  193. expected = 9;
  194. assert.strictEqual(this.getSumValueFor(summand1, summand2), expected);
  195. assert.strictEqual(this.getSumValueFor(summand2, summand1), expected);
  196. },
  197. "should return sum for long long max and long long max and 1": function testNoOverflowBeforeDouble() {
  198. var actual = this.getSumValueFor(9223372036854775807, 9223372036854775807, 1.0),
  199. expected = 9223372036854775807 + 9223372036854775807;
  200. assert.strictEqual(actual, expected);
  201. },
  202. },
  203. "#getValue()": {
  204. "should get value the same for shard and router": function() {
  205. var acc = SumAccumulator.create();
  206. assert.strictEqual(acc.getValue(false), acc.getValue(true));
  207. acc.process(123);
  208. assert.strictEqual(acc.getValue(false), acc.getValue(true));
  209. },
  210. },
  211. "#reset()": {
  212. "should reset to 0": function() {
  213. var acc = SumAccumulator.create();
  214. assert.strictEqual(acc.getValue(), 0);
  215. acc.process(123);
  216. assert.notEqual(acc.getValue(), 0);
  217. acc.reset();
  218. assert.strictEqual(acc.getValue(), 0);
  219. assert.strictEqual(acc.getValue(true), 0);
  220. },
  221. },
  222. "#getOpName()": {
  223. "should return the correct op name; $sum": function() {
  224. assert.equal(SumAccumulator.create().getOpName(), "$sum");
  225. }
  226. },
  227. };