SumAccumulator_test.js 9.0 KB

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