View Javadoc
1   package org.opentrafficsim.core.math;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.fail;
5   
6   import org.djunits.value.ValueException;
7   import org.junit.Test;
8   
9   /**
10   * Test the Solver class.
11   * <p>
12   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
14   * <p>
15   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Dec 10, 2015 <br>
16   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
17   */
18  public class SolverTest
19  {
20  
21      /**
22       * Linear solver.
23       */
24      @SuppressWarnings("checkstyle:methodlength")
25      @Test
26      public final void solverTest()
27      {
28          // Linear equations
29          double[] result = Solver.solve(0, 0);
30          assertEquals("length of result should be 0", 0, result.length);
31          result = Solver.solve(0, 0, 0);
32          assertEquals("length of result should be 0", 0, result.length);
33          result = Solver.solve(0, 1);
34          assertEquals("length of result should be 0", 0, result.length);
35          result = Solver.solve(0, 0, 1);
36          assertEquals("length of result should be 0", 0, result.length);
37          try
38          {
39              Solver.firstSolutionAfter(0, 0, 0, 0);
40              fail("Unsolvable linear equation should have thrown an exception");
41          }
42          catch (ValueException ve)
43          {
44              // Ignore expected exception
45          }
46          result = Solver.solve(3, 1);
47          assertEquals("length of result should be 1", 1, result.length);
48          assertEquals("solution should match", -0.3333333333, result[0], 0.000001);
49          try
50          {
51              Solver.firstSolutionAfter(0, 0, 3, 1);
52              fail("No solution after boundary should have thrown an exception");
53          }
54          catch (ValueException exception)
55          {
56              // Ignore expected exception
57          }
58          try
59          {
60              assertEquals("solution should match", -0.3333333333, Solver.firstSolutionAfter(-0.5, 0, 3, 1), 0.000001);
61          }
62          catch (ValueException exception)
63          {
64              fail("Caught unexpected exception");
65          }
66          result = Solver.solve(0, 3, 1);
67          assertEquals("length of result should be 1", 1, result.length);
68          assertEquals("solution should match", -0.3333333333, result[0], 0.000001);
69          // Quadratic equations
70          result = Solver.solve(1, 0, 1);
71          assertEquals("length of result should be 0", 0, result.length);
72          try
73          {
74              Solver.firstSolutionAfter(0, 1, 0, 1);
75              fail("Equation with no solutions should have thrown an exception");
76          }
77          catch (ValueException exception)
78          {
79              // Ignore expected exception
80          }
81          result = Solver.solve(1, 0, 0);
82          assertEquals("length of result should be 1", 1, result.length);
83          assertEquals("solution should match", 0, result[0], 0.000001);
84          try
85          {
86              assertEquals("solution should match", 0, Solver.firstSolutionAfter(-0.00001, 1, 0, 0), 0.000001);
87          }
88          catch (ValueException exception)
89          {
90              fail("Caught unexpected exception");
91          }
92          result = Solver.solve(1, 0, -1);
93          assertEquals("length of result should be 2", 2, result.length);
94          assertEquals("Smallest solution should match", -1, Math.min(result[0], result[1]), 0.000001);
95          assertEquals("Largest solution should match", 1, Math.max(result[0], result[1]), 0.000001);
96          try
97          {
98              assertEquals("First solution after -10 should be -1", -1, Solver.firstSolutionAfter(-10, 1, 0, -1), 0.000001);
99          }
100         catch (ValueException exception)
101         {
102             fail("Caught unexpected exception");
103         }
104         try
105         {
106             assertEquals("First solution after 0 should be 1", 1, Solver.firstSolutionAfter(0, 1, 0, -1), 0.000001);
107         }
108         catch (ValueException exception)
109         {
110             fail("Caught unexpected exception");
111         }
112         try
113         {
114             Solver.firstSolutionAfter(2, 1, 0, -1);
115             fail("First solution after 2 should have thrown an exception");
116         }
117         catch (ValueException exception)
118         {
119             // Ignore expected exception
120         }
121         result = Solver.solve(1, 2, -3);
122         // System.out.println("x0=" + result[0] + " x1=" + result[1]);
123         assertEquals("length of result should be 2", 2, result.length);
124         assertEquals("Smallest solution should match", -3, Math.min(result[0], result[1]), 0.000001);
125         assertEquals("Largest solution should match", 1, Math.max(result[0], result[1]), 0.000001);
126         // System.out.println("solutions are " + Solver.solve(1, 2, -3)[0] + ", " + Solver.solve(1, 2, -3)[1]);
127         try
128         {
129             assertEquals("First solution after -10 should be -3", -3, Solver.firstSolutionAfter(-10, 1, 2, -3), 0.000001);
130         }
131         catch (ValueException exception)
132         {
133             fail("Caught unexpected exception");
134         }
135         try
136         {
137             assertEquals("First solution after 0 should be 1", 1, Solver.firstSolutionAfter(0, 1, 2, -3), 0.000001);
138         }
139         catch (ValueException exception)
140         {
141             fail("Caught unexpected exception");
142         }
143         try
144         {
145             Solver.firstSolutionAfter(2, 1, 2, -3);
146             fail("First solution after 2 should have thrown an exception");
147         }
148         catch (ValueException exception)
149         {
150             // Ignore expected exception
151         }
152         // Invert a (so the other branch of the if statement gets tested)
153         // System.out.println("solutions are " + Solver.solve(-1, -2, 3)[0] + ", " + Solver.solve(-1, -2, 3)[1]);
154         try
155         {
156             assertEquals("First solution after -10 should be -3", -3, Solver.firstSolutionAfter(-10, -1, -2, 3), 0.000001);
157         }
158         catch (ValueException exception)
159         {
160             fail("Caught unexpected exception");
161         }
162         try
163         {
164             assertEquals("First solution after 0 should be 1", 1, Solver.firstSolutionAfter(0, -1, -2, 3), 0.000001);
165         }
166         catch (ValueException exception)
167         {
168             fail("Caught unexpected exception");
169         }
170         try
171         {
172             Solver.firstSolutionAfter(2, -1, -2, 3);
173             fail("First solution after 2 should have thrown an exception");
174         }
175         catch (ValueException exception)
176         {
177             // Ignore expected exception
178         }
179 
180     }
181 }