Skip to content

Commit

Permalink
Add test for new LinearExpr.SolutionValue() method
Browse files Browse the repository at this point in the history
  • Loading branch information
koen-lee committed Jun 28, 2023
1 parent a4c7921 commit aa894f0
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions ortools/linear_solver/csharp/LinearSolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -442,5 +442,28 @@ static void testSetHintAndSolverGetters()

solver.SetHint(new Variable[] { x, y }, new double[] { 2.0, 3.0 });
}

[Fact]
static void Given_a_LinearExpr_and_a_solution_When_SolutionValue_is_called_then_the_result_is_correct()
{
Console.WriteLine(nameof(Given_a_LinearExpr_and_a_solution_When_SolutionValue_is_called_then_the_result_is_correct));
Solver solver = Solver.CreateSolver("glop");
// x, y and z are fixed; we don't want to test the solver here.
Variable x = solver.MakeIntVar(3, 3, "x");
Variable y = solver.MakeIntVar(4, 4, "y");
Variable z = solver.MakeIntVar(5, 5, "z");

LinearExpr part1 = x * 2; // 6
LinearExpr part2 = y * 3 + z + 4; // 21
LinearExpr objective = part1 + part2; // 27
LinearExpr anew = new();
solver.Maximize(objective);
solver.Solve();
Assert.Equal(27, objective.SolutionValue(), precision: 9);
Assert.Equal(6, part1.SolutionValue(), precision: 9);
Assert.Equal(21, part2.SolutionValue(), precision: 9);
Assert.Equal(0, anew.SolutionValue(), precision: 9);
Assert.Equal(27, (objective + anew).SolutionValue(), precision: 9);
}
}
} // namespace Google.OrTools.Tests

0 comments on commit aa894f0

Please sign in to comment.