Commit d29a0b52 authored by Chinmay Garde's avatar Chinmay Garde

Address initial code review concerns

parent 99dc91eb
......@@ -14,6 +14,6 @@ class ConstantMember extends _EquationMember {
Expression asExpression() => new Expression([], this.value);
}
ConstantMember CM(num value) {
ConstantMember cm(double value) {
return new ConstantMember(value);
}
......@@ -12,8 +12,8 @@ class Param extends _EquationMember {
variable._owner = this;
}
Param.withContext(ctx)
: variable = new Variable(0.0),
Param.withContext(ctx, [double value = 0.0])
: variable = new Variable(value),
context = ctx {
variable._owner = this;
}
......
......@@ -5,25 +5,25 @@
part of cassowary;
class Result {
final bool error;
final String message;
final bool error;
const Result(this.message, this.error);
const Result(this.message, { bool isError: true }) : error = isError;
static final Result success = const Result("Success", false);
static final Result unimplemented = const Result("Unimplemented", true);
static final Result success = const Result("Success", isError: false);
static final Result unimplemented = const Result("Unimplemented");
static final Result duplicateConstraint =
const Result("Duplicate Constraint", true);
const Result("Duplicate Constraint");
static final Result unsatisfiableConstraint =
const Result("Unsatisfiable Constraint", true);
const Result("Unsatisfiable Constraint");
static final Result unknownConstraint =
const Result("Unknown Constraint", true);
const Result("Unknown Constraint");
static final Result duplicateEditVariable =
const Result("Duplicate Edit Variable", true);
const Result("Duplicate Edit Variable");
static final Result badRequiredStrength =
const Result("Bad Required Strength", true);
const Result("Bad Required Strength");
static final Result unknownEditVariable =
const Result("Unknown Edit Variable", true);
const Result("Unknown Edit Variable");
static final Result internalSolverError =
const Result("Internal Solver Error", true);
const Result("Internal Solver Error");
}
......@@ -8,7 +8,7 @@ enum _SymbolType { invalid, external, slack, error, dummy, }
class _Symbol {
final _SymbolType type;
int tick;
final int tick;
_Symbol(this.type, this.tick);
......
......@@ -5,12 +5,12 @@
part of cassowary;
class Variable {
double value = 0.0;
double value;
String name;
Param _owner;
int _tick;
final int _tick;
static int _total = 0;
Variable(this.value) : _tick = _total++;
......
......@@ -16,8 +16,8 @@ void main() {
test('variable1', () {
var v = new Param(22.0);
expect((v + CM(22.0)).value, 44.0);
expect((v - CM(20.0)).value, 2.0);
expect((v + cm(22.0)).value, 44.0);
expect((v - cm(20.0)).value, 2.0);
});
test('term', () {
......@@ -40,7 +40,7 @@ void main() {
var v3 = new Param(22.0);
expect(v1 is Param, true);
expect(v1 + CM(20.0) is Expression, true);
expect(v1 + cm(20.0) is Expression, true);
expect(v1 + v2 is Expression, true);
expect((v1 + v2).value, 20.0);
......@@ -51,15 +51,15 @@ void main() {
});
test('expression2', () {
var e = new Param(10.0) + CM(5.0);
var e = new Param(10.0) + cm(5.0);
expect(e.value, 15.0);
expect(e is Expression, true);
// Constant
expect((e + CM(2.0)) is Expression, true);
expect((e + CM(2.0)).value, 17.0);
expect((e - CM(2.0)) is Expression, true);
expect((e - CM(2.0)).value, 13.0);
expect((e + cm(2.0)) is Expression, true);
expect((e + cm(2.0)).value, 17.0);
expect((e - cm(2.0)) is Expression, true);
expect((e - cm(2.0)).value, 13.0);
expect(e.value, 15.0);
......@@ -95,7 +95,7 @@ void main() {
var t = new Term(new Variable(12.0), 1.0);
// Constant
var c = CM(2.0);
var c = cm(2.0);
expect((t + c) is Expression, true);
expect((t + c).value, 14.0);
expect((t - c) is Expression, true);
......@@ -116,7 +116,7 @@ void main() {
expect((t - t2).value, 10.0);
// Expression
var exp = new Param(1.0) + CM(1.0);
var exp = new Param(1.0) + cm(1.0);
expect((t + exp) is Expression, true);
expect((t + exp).value, 14.0);
expect((t - exp) is Expression, true);
......@@ -127,7 +127,7 @@ void main() {
var v = new Param(3.0);
// Constant
var c = CM(2.0);
var c = cm(2.0);
expect((v + c) is Expression, true);
expect((v + c).value, 5.0);
expect((v - c) is Expression, true);
......@@ -148,7 +148,7 @@ void main() {
expect((v - t2).value, 1.0);
// Expression
var exp = new Param(1.0) + CM(1.0);
var exp = new Param(1.0) + cm(1.0);
expect(exp.terms.length, 1);
expect((v + exp) is Expression, true);
......@@ -158,10 +158,10 @@ void main() {
});
test('constantmember', () {
var c = CM(3.0);
var c = cm(3.0);
// Constant
var c2 = CM(2.0);
var c2 = cm(2.0);
expect((c + c2) is Expression, true);
expect((c + c2).value, 5.0);
expect((c - c2) is Expression, true);
......@@ -182,7 +182,7 @@ void main() {
expect((c - t2).value, 1.0);
// Expression
var exp = new Param(1.0) + CM(1.0);
var exp = new Param(1.0) + cm(1.0);
expect((c + exp) is Expression, true);
expect((c + exp).value, 5.0);
......@@ -194,61 +194,61 @@ void main() {
var left = new Param(10.0);
var right = new Param(100.0);
var c = right - left >= CM(25.0);
var c = right - left >= cm(25.0);
expect(c is Constraint, true);
});
test('simple_multiplication', () {
// Constant
var c = CM(20.0);
expect((c * CM(2.0)).value, 40.0);
var c = cm(20.0);
expect((c * cm(2.0)).value, 40.0);
// Variable
var v = new Param(20.0);
expect((v * CM(2.0)).value, 40.0);
expect((v * cm(2.0)).value, 40.0);
// Term
var t = new Term(v.variable, 1.0);
expect((t * CM(2.0)).value, 40.0);
expect((t * cm(2.0)).value, 40.0);
// Expression
var e = new Expression([t], 0.0);
expect((e * CM(2.0)).value, 40.0);
expect((e * cm(2.0)).value, 40.0);
});
test('simple_division', () {
// Constant
var c = CM(20.0);
expect((c / CM(2.0)).value, 10.0);
var c = cm(20.0);
expect((c / cm(2.0)).value, 10.0);
// Variable
var v = new Param(20.0);
expect((v / CM(2.0)).value, 10.0);
expect((v / cm(2.0)).value, 10.0);
// Term
var t = new Term(v.variable, 1.0);
expect((t / CM(2.0)).value, 10.0);
expect((t / cm(2.0)).value, 10.0);
// Expression
var e = new Expression([t], 0.0);
expect((e / CM(2.0)).value, 10.0);
expect((e / cm(2.0)).value, 10.0);
});
test('full_constraints_setup', () {
var left = new Param(2.0);
var right = new Param(10.0);
var c1 = right - left >= CM(20.0);
var c1 = right - left >= cm(20.0);
expect(c1 is Constraint, true);
expect(c1.expression.constant, -20.0);
expect(c1.relation, Relation.greaterThanOrEqualTo);
var c2 = (right - left == CM(30.0)) as Constraint;
var c2 = (right - left == cm(30.0)) as Constraint;
expect(c2 is Constraint, true);
expect(c2.expression.constant, -30.0);
expect(c2.relation, Relation.equalTo);
var c3 = right - left <= CM(30.0);
var c3 = right - left <= cm(30.0);
expect(c3 is Constraint, true);
expect(c3.expression.constant, -30.0);
expect(c3.relation, Relation.lessThanOrEqualTo);
......@@ -258,7 +258,7 @@ void main() {
var left = new Param(2.0);
var right = new Param(10.0);
var c = (right - left >= CM(200.0)) | 750.0;
var c = (right - left >= cm(200.0)) | 750.0;
expect(c is Constraint, true);
expect(c.expression.terms.length, 2);
expect(c.expression.constant, -200.0);
......@@ -271,7 +271,7 @@ void main() {
var left = new Param(2.0);
var right = new Param(100.0);
var c1 = right - left >= CM(200.0);
var c1 = right - left >= cm(200.0);
expect((right >= left) is Constraint, true);
......@@ -282,7 +282,7 @@ void main() {
var e = new Param(200.0) - new Param(100.0);
// Constant
var c1 = e >= CM(50.0);
var c1 = e >= cm(50.0);
expect(c1 is Constraint, true);
expect(c1.expression.terms.length, 2);
expect(c1.expression.constant, -50.0);
......@@ -308,7 +308,7 @@ void main() {
test('constraint_complex_non_exprs', () {
// Constant
var c1 = CM(100.0) >= CM(50.0);
var c1 = cm(100.0) >= cm(50.0);
expect(c1 is Constraint, true);
expect(c1.expression.terms.length, 0);
expect(c1.expression.constant, 50.0);
......@@ -340,7 +340,7 @@ void main() {
var left = new Param(2.0);
var right = new Param(100.0);
var c1 = right - left >= CM(200.0);
var c1 = right - left >= cm(200.0);
var c2 = right >= right;
expect(s.addConstraint(c1), Result.success);
......@@ -351,38 +351,38 @@ void main() {
});
test('test_multiplication_division_override', () {
var c = CM(10.0);
var c = cm(10.0);
var v = new Param(c.value);
var t = new Term(v.variable, 1.0);
var e = new Expression([t], 0.0);
// Constant
expect((c * CM(10.0)).value, 100);
expect((c * cm(10.0)).value, 100);
// Variable
expect((v * CM(10.0)).value, 100);
expect((v * cm(10.0)).value, 100);
// Term
expect((t * CM(10.0)).value, 100);
expect((t * cm(10.0)).value, 100);
// Expression
expect((e * CM(10.0)).value, 100);
expect((e * cm(10.0)).value, 100);
// Constant
expect((c / CM(10.0)).value, 1);
expect((c / cm(10.0)).value, 1);
// Variable
expect((v / CM(10.0)).value, 1);
expect((v / cm(10.0)).value, 1);
// Term
expect((t / CM(10.0)).value, 1);
expect((t / cm(10.0)).value, 1);
// Expression
expect((e / CM(10.0)).value, 1);
expect((e / cm(10.0)).value, 1);
});
test('test_multiplication_division_exceptions', () {
var c = CM(10.0);
var c = cm(10.0);
var v = new Param(c.value);
var t = new Term(v.variable, 1.0);
var e = new Expression([t], 0.0);
......@@ -405,7 +405,7 @@ void main() {
var right = new Param(100.0);
var mid = new Param(0.0);
Constraint c = left + right >= CM(2.0) * mid;
Constraint c = left + right >= cm(2.0) * mid;
expect(s.addConstraint(c), Result.success);
expect(s.addEditVariable(mid.variable, 999.0), Result.success);
......@@ -420,13 +420,13 @@ void main() {
var right = new Param(100.0);
var mid = new Param(0.0);
expect(((left + right) >= (CM(2.0) * mid)) is Constraint, true);
expect(((left + right) >= (cm(2.0) * mid)) is Constraint, true);
});
test('single_item', () {
var left = new Param(-20.0);
Solver s = new Solver();
s.addConstraint(left >= CM(0.0));
s.addConstraint(left >= cm(0.0));
s.flushUpdates();
expect(left.value, 0.0);
});
......@@ -438,10 +438,10 @@ void main() {
Solver s = new Solver();
expect(s.addConstraint((right + left == mid * CM(2.0)) as Constraint),
expect(s.addConstraint((right + left == mid * cm(2.0)) as Constraint),
Result.success);
expect(s.addConstraint(right - left >= CM(100.0)), Result.success);
expect(s.addConstraint(left >= CM(0.0)), Result.success);
expect(s.addConstraint(right - left >= cm(100.0)), Result.success);
expect(s.addConstraint(left >= cm(0.0)), Result.success);
s.flushUpdates();
......@@ -457,15 +457,15 @@ void main() {
Solver s = new Solver();
var c = (left >= CM(0.0));
var c = (left >= cm(0.0));
expect(s.addConstraints([
(left + right == CM(2.0) * mid) as Constraint,
(right - left >= CM(100.0)),
(left + right == cm(2.0) * mid) as Constraint,
(right - left >= cm(100.0)),
c
]), Result.success);
expect(s.addConstraints([(right >= CM(-20.0)), c]),
expect(s.addConstraints([(right >= cm(-20.0)), c]),
Result.duplicateConstraint);
});
......@@ -476,10 +476,10 @@ void main() {
Solver s = new Solver();
expect(s.addConstraint((right + left == mid * CM(2.0)) as Constraint),
expect(s.addConstraint((right + left == mid * cm(2.0)) as Constraint),
Result.success);
expect(s.addConstraint(right - left >= CM(100.0)), Result.success);
expect(s.addConstraint(left >= CM(0.0)), Result.success);
expect(s.addConstraint(right - left >= cm(100.0)), Result.success);
expect(s.addConstraint(left >= cm(0.0)), Result.success);
expect(s.addEditVariable(mid.variable, Priority.strong), Result.success);
expect(s.suggestValueForVariable(mid.variable, 300.0), Result.success);
......@@ -518,9 +518,9 @@ void main() {
solver.addEditVariable(container.variable, Priority.strong);
solver.suggestValueForVariable(container.variable, 100.0);
solver.addConstraint((p1 >= CM(30.0)) | Priority.strong);
solver.addConstraint((p1 >= cm(30.0)) | Priority.strong);
solver.addConstraint(((p1 == p3) as Constraint) | Priority.medium);
solver.addConstraint((p2 == CM(2.0) * p1) as Constraint);
solver.addConstraint((p2 == cm(2.0) * p1) as Constraint);
solver.addConstraint((container == (p1 + p2 + p3)) as Constraint);
solver.flushUpdates();
......@@ -541,9 +541,9 @@ void main() {
expect(s.addEditVariable(mid.variable, Priority.strong), Result.success);
expect(s.addConstraint((mid * CM(2.0) == left + right) as Constraint),
expect(s.addConstraint((mid * cm(2.0) == left + right) as Constraint),
Result.success);
expect(s.addConstraint(left >= CM(0.0)), Result.success);
expect(s.addConstraint(left >= cm(0.0)), Result.success);
expect(s.suggestValueForVariable(mid.variable, 50.0), Result.success);
......@@ -565,9 +565,9 @@ void main() {
expect(s.addEditVariable(mid.variable, Priority.strong), Result.success);
expect(s.addConstraint((mid * CM(2.0) == left + right) as Constraint),
expect(s.addConstraint((mid * cm(2.0) == left + right) as Constraint),
Result.success);
expect(s.addConstraint(left >= CM(10.0)), Result.success);
expect(s.addConstraint(left >= cm(10.0)), Result.success);
expect(s.suggestValueForVariable(mid.variable, 50.0), Result.success);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment