Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #23 from devongovett/master

Avoid relying on toString in the tests
  • Loading branch information...
commit 26182868e655a00bf370c9bbd639d6a45a398974 2 parents 7baa4df + a37cdd6
@slightlyoff authored
View
2  src/Constraint.js
@@ -182,7 +182,7 @@ c.Equation = c.inherit({
} else if ((a1 instanceof c.AbstractVariable) &&
(a2 instanceof c.Expression)) {
var cv = a1, cle = a2, strength = a3, weight = a4;
- lc.call(this, cle, strength, weight);
+ lc.call(this, cle.clone(), strength, weight);
this.expression.addVariable(cv, -1);
} else if ((a1 instanceof c.AbstractVariable) &&
(typeof(a2) == 'number')) {
View
10 src/Expression.js
@@ -242,6 +242,16 @@ c.Expression = c.inherit({
});
return bstr;
},
+
+ equals: function(other) {
+ if (other === this) {
+ return true;
+ }
+
+ return other instanceof c.Expression &&
+ other.constant === this.constant &&
+ other.terms.equals(this.terms);
+ },
Plus: function(e1 /*c.Expression*/, e2 /*c.Expression*/) {
return e1.plus(e2);
View
21 src/HashTable.js
@@ -154,7 +154,28 @@ c.HashTable = c.inherit({
copyOwn(this._keyStrMap, n._keyStrMap);
}
return n;
+ },
+
+ equals: function(other) {
+ if (other === this) {
+ return true;
+ }
+
+ if (!(other instanceof c.HashTable) || other._size !== this._size) {
+ return false;
+ }
+
+ var codes = Object.keys(this._keyStrMap);
+ for (var i = 0; i < codes.length; i++) {
+ var code = codes[i];
+ if (this._keyStrMap[code] !== other._keyStrMap[code] || this._store[code] !== other._store[code]) {
+ return false;
+ }
+ }
+
+ return true;
}
+
});
})(c);
View
32 tests/Constraint-test.js
@@ -10,7 +10,7 @@ doh.add("c.Constraint", [
function equationFromExpression(t) {
var ex = new c.Expression(10);
var c1 = new c.Equation(ex);
- t.is(c1.expression, '10');
+ t.is(c1.expression, ex);
},
function expressionFromVars(t) {
@@ -24,7 +24,7 @@ doh.add("c.Constraint", [
var x = new c.Variable('x', 167);
var cly = new c.Expression(2);
var eq = new c.Equation(x, cly);
- t.is('2 + -1*[x:167]', eq.expression);
+ t.t(eq.expression.equals(cly.minus(x)));
},
function equationStrengthTest(t) {
@@ -60,7 +60,7 @@ doh.add("c.Constraint", [
function equation_variable_number(t) {
var v = new c.Variable('v', 22);
var eq = new c.Equation(v, 5);
- t.is(eq.expression, '5 + -1*[v:22]');
+ t.t(eq.expression.equals(c.Minus(5, v)));
},
function equation_expression_variable(t) {
@@ -68,7 +68,7 @@ doh.add("c.Constraint", [
var v = new c.Variable('v', 22);
var eq = new c.Equation(e, v);
- t.is(eq.expression, '10 + -1*[v:22]');
+ t.t(eq.expression.equals(c.Minus(10, v)));
},
function equation_expression_x2(t) {
@@ -76,14 +76,14 @@ doh.add("c.Constraint", [
var e2 = new c.Expression(new c.Variable('z', 10), 2, 4);
var eq = new c.Equation(e1, e2);
- t.is(eq.expression, '6 + -2*[z:10]');
+ t.t(eq.expression.equals(e1.minus(e2)));
},
function inequality_expression(t) {
var e = new c.Expression(10);
var ieq = new c.Inequality(e);
- t.is(ieq.expression, '10');
+ t.is(ieq.expression, e);
},
function inequality_var_op_var(t) {
@@ -91,20 +91,20 @@ doh.add("c.Constraint", [
var v2 = new c.Variable('v2', 5);
var ieq = new c.Inequality(v1, c.GEQ, v2);
- t.is(ieq.expression, '1*[v1:10] + -1*[v2:5]');
+ t.t(ieq.expression.equals(c.Minus(v1, v2)));
ieq = new c.Inequality(v1, c.LEQ, v2);
- t.is(ieq.expression, '-1*[v1:10] + 1*[v2:5]');
+ t.t(ieq.expression.equals(c.Minus(v2, v1)));
},
function inequality_var_op_num(t) {
var v = new c.Variable('v', 10);
var ieq = new c.Inequality(v, c.GEQ, 5);
- t.is(ieq.expression, '-5 + 1*[v:10]');
+ t.t(ieq.expression.equals(c.Minus(v, 5)));
ieq = new c.Inequality(v, c.LEQ, 5);
- t.is(ieq.expression, '5 + -1*[v:10]');
+ t.t(ieq.expression.equals(c.Minus(5, v)));
},
function inequality_expression_x2(t) {
@@ -112,10 +112,10 @@ doh.add("c.Constraint", [
var e2 = new c.Expression(new c.Variable('c', 10), 2, 4);
var ieq = new c.Inequality(e1, c.GEQ, e2);
- t.is(ieq.expression, '6 + -2*[c:10]');
+ t.t(ieq.expression.equals(e1.minus(e2)));
ieq = new c.Inequality(e1, c.LEQ, e2);
- t.is(ieq.expression, '-6 + 2*[c:10]');
+ t.t(ieq.expression.equals(e2.minus(e1)));
},
function inequality_var_op_exp(t) {
@@ -123,10 +123,10 @@ doh.add("c.Constraint", [
var e = new c.Expression(new c.Variable('x', 5), 2, 4);
var ieq = new c.Inequality(v, c.GEQ, e);
- t.is(ieq.expression, '-4 + 1*[v:10] + -2*[x:5]');
+ t.t(ieq.expression.equals(c.Minus(v, e)));
ieq = new c.Inequality(v, c.LEQ, e);
- t.is(ieq.expression, '4 + -1*[v:10] + 2*[x:5]');
+ t.t(ieq.expression.equals(e.minus(v)));
},
function inequality_exp_op_var(t) {
@@ -134,9 +134,9 @@ doh.add("c.Constraint", [
var e = new c.Expression(new c.Variable('x', 5), 2, 4);
var ieq = new c.Inequality(e, c.GEQ, v);
- t.is(ieq.expression, '4 + -1*[v:10] + 2*[x:5]');
+ t.t(ieq.expression.equals(e.minus(v)));
ieq = new c.Inequality(e, c.LEQ, v);
- t.is(ieq.expression, '-4 + 1*[v:10] + -2*[x:5]');
+ t.t(ieq.expression.equals(c.Minus(v, e)));
}
]);
View
9 tests/Expression-test.js
@@ -337,6 +337,15 @@ doh.add("c.Expression", [
t.is(e.toString(), '10 + 2*[v:5] + 4*[b:2]');
},
+ function equals(t) {
+ var v = new c.Variable('v', 5);
+
+ t.t(new c.Expression(10).equals(new c.Expression(10)));
+ t.f(new c.Expression(10).equals(new c.Expression(1)));
+ t.t(new c.Expression(v, 2, -1).equals(new c.Expression(v, 2, -1)));
+ t.f(new c.Expression(v, -2, 5).equals(new c.Expression(v, 3, 6)));
+ },
+
function Plus(t) {
var x = new c.Variable('x', 167);
var y = new c.Variable('y', 10);
Please sign in to comment.
Something went wrong with that request. Please try again.