Browse Source

Fixes #993. Ported MinMaxAccumulator and test cases.

http://source.rd.rcg.local/trac/eagle6/changeset/1313/Eagle6_SVN
Spencer Rathbun 13 years ago
parent
commit
9edf1eaf7b

+ 57 - 0
lib/pipeline/accumulators/MinMaxAccumulator.js

@@ -0,0 +1,57 @@
+var MinMaxAccumulator = module.exports = (function(){
+	// CONSTRUCTOR
+	/** 
+	* Constructor for MinMaxAccumulator, wraps SingleValueAccumulator's constructor and
+	* adds flag to track whether we have started or not
+	*
+	**/
+	var klass = module.exports = function MinMaxAccumulator(sense){
+		if(arguments.length > 1 ) throw new Error("expects a single value");
+		base.call(this);
+		this.sense = sense; /* 1 for min, -1 for max; used to "scale" comparison */
+		if ((this.sense !== 1) && (this.sense !== -1)) throw new Error("this should never happen");
+	}, base = require("./SingleValueAccumulator"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
+
+	// DEPENDENCIES
+	var Value = require("../Value");
+
+	// PROTOTYPE MEMBERS
+	proto.getOpName = function getOpName(){
+		if (this.sense == 1)
+			return "$min";
+		return "$max";
+	};
+
+	klass.createMin = function createMin(){
+		return new MinMaxAccumulator(1);
+	};
+
+	klass.createMax = function createMax(){
+		return new MinMaxAccumulator(-1);
+	};
+
+	/** 
+	* Takes a document and returns the first value in the document
+	*
+	* @param {Object} doc the document source
+	* @return the first value
+	**/
+	proto.evaluate = function evaluate(doc){
+		if (this.operands.length != 1) throw new Error("this should never happen");
+		var prhs = this.operands[0].evaluate(doc);
+
+		/* if this is the first value, just use it */
+		if (!base.prototype.getValue.call(this))
+			this.value = prhs;
+		else {
+			/* compare with the current value; swap if appropriate */
+			var cmp = Value.compare(this.value, prhs) * this.sense;
+			if (cmp > 0)
+				this.value = prhs;
+		}
+		return this.value;
+
+	};
+
+	return klass;
+})();

+ 80 - 0
test/lib/pipeline/accumulators/MaxAccumulator.js

@@ -0,0 +1,80 @@
+var assert = require("assert"),
+	MaxAccumulator = require("../../../../lib/pipeline/accumulators/MinMaxAccumulator"),
+	FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression");
+
+function createAccumulator(){
+	var maxAccumulator = MaxAccumulator.createMax();
+	maxAccumulator.addOperand(new FieldPathExpression("a") );
+	return maxAccumulator;
+}
+
+module.exports = {
+
+	"MaxAccumulator": {
+
+		"constructor()": {
+
+			"should not throw Error when constructing without args using createMax": function testConstructor(){
+				assert.doesNotThrow(function(){
+					MaxAccumulator.createMax();
+				});
+			},
+
+			"should throw Error when constructing without args using default constructor": function testConstructor(){
+				assert.throws(function(){
+					new MaxAccumulator();
+				});
+			}
+
+		},
+
+		"#getOpName()": {
+
+			"should return the correct op name; $max": function testOpName(){
+				var acc = createAccumulator();
+				assert.equal(acc.getOpName(), "$max");
+			}
+
+		},
+
+		"#evaluate()": {
+
+			"The accumulator evaluates no documents": function none() {
+				// The accumulator returns no value in this case.
+				var acc = createAccumulator();
+				assert.ok(!acc.getValue());
+			},
+
+			"The accumulator evaluates one document and retains its value": function one() {
+				var acc = createAccumulator();
+				acc.evaluate({a:5});
+				assert.strictEqual(acc.getValue(), 5);
+			},
+
+			"The accumulator evaluates one document with the field missing retains undefined": function missing() {
+				var acc = createAccumulator();
+				acc.evaluate({});
+				assert.strictEqual(acc.getValue(), undefined);
+			},
+
+			"The accumulator evaluates two documents and retains the maximum": function two() {
+				var acc = createAccumulator();
+				acc.evaluate({a:5});
+				acc.evaluate({a:7});
+				assert.strictEqual(acc.getValue(), 7);
+			},
+
+			"The accumulator evaluates two documents and retains the defined value in the first": function lastMissing() {
+				var acc = createAccumulator();
+				acc.evaluate({a:7});
+				acc.evaluate({});
+				assert.strictEqual(acc.getValue(), 7);
+			}
+		}
+
+	}
+
+};
+
+if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);
+

+ 80 - 0
test/lib/pipeline/accumulators/MinAccumulator.js

@@ -0,0 +1,80 @@
+var assert = require("assert"),
+	MinAccumulator = require("../../../../lib/pipeline/accumulators/MinMaxAccumulator"),
+	FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression");
+
+function createAccumulator(){
+	var minAccumulator = MinAccumulator.createMin();
+	minAccumulator.addOperand(new FieldPathExpression("a") );
+	return minAccumulator;
+}
+
+module.exports = {
+
+	"MinAccumulator": {
+
+		"constructor()": {
+
+			"should not throw Error when constructing without args using createMin": function testConstructor(){
+				assert.doesNotThrow(function(){
+					MinAccumulator.createMin();
+				});
+			},
+
+			"should throw Error when constructing without args using default constructor": function testConstructor(){
+				assert.throws(function(){
+					new MinAccumulator();
+				});
+			}
+
+		},
+
+		"#getOpName()": {
+
+			"should return the correct op name; $min": function testOpName(){
+				var acc = createAccumulator();
+				assert.equal(acc.getOpName(), "$min");
+			}
+
+		},
+
+		"#evaluate()": {
+
+			"The accumulator evaluates no documents": function none() {
+				// The accumulator returns no value in this case.
+				var acc = createAccumulator();
+				assert.ok(!acc.getValue());
+			},
+
+			"The accumulator evaluates one document and retains its value": function one() {
+				var acc = createAccumulator();
+				acc.evaluate({a:5});
+				assert.strictEqual(acc.getValue(), 5);
+			},
+
+			"The accumulator evaluates one document with the field missing retains undefined": function missing() {
+				var acc = createAccumulator();
+				acc.evaluate({});
+				assert.strictEqual(acc.getValue(), undefined);
+			},
+
+			"The accumulator evaluates two documents and retains the minimum": function two() {
+				var acc = createAccumulator();
+				acc.evaluate({a:5});
+				acc.evaluate({a:7});
+				assert.strictEqual(acc.getValue(), 5);
+			},
+
+			"The accumulator evaluates two documents and retains the undefined value in the last": function lastMissing() {
+				var acc = createAccumulator();
+				acc.evaluate({a:7});
+				acc.evaluate({});
+				assert.strictEqual(acc.getValue(), undefined);
+			}
+		}
+
+	}
+
+};
+
+if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);
+