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-2015 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      @Test
25      public void solverTest()
26      {
27          // Linear equations
28          double[] result = Solver.solve(0, 0);
29          assertEquals("length of result should be 0", 0, result.length);
30          result = Solver.solve(0, 0, 0);
31          assertEquals("length of result should be 0", 0, result.length);
32          result = Solver.solve(0, 1);
33          assertEquals("length of result should be 0", 0, result.length);
34          result = Solver.solve(0, 0, 1);
35          assertEquals("length of result should be 0", 0, result.length);
36          try
37          {
38              Solver.firstSolutionAfter(0, 0, 0, 0);
39              fail("Unsolvable linear equation should have thrown an exception");
40          }
41          catch (ValueException ve)
42          {
43              // Ignore expected exception
44          }
45          result = Solver.solve(3, 1);
46          assertEquals("length of result should be 1", 1, result.length);
47          assertEquals("solution should match", -0.3333333333, result[0], 0.000001);
48          try
49          {
50              Solver.firstSolutionAfter(0, 0, 3, 1);
51              fail("No solution after boundary should have thrown an exception");
52          }
53          catch (ValueException exception)
54          {
55              // Ignore expected exception
56          }
57          try
58          {
59              assertEquals("solution should match", -0.3333333333, Solver.firstSolutionAfter(-0.5, 0, 3, 1), 0.000001);
60          }
61          catch (ValueException exception)
62          {
63              fail("Caught unexpected exception");
64          }
65          result = Solver.solve(0, 3, 1);
66          assertEquals("length of result should be 1", 1, result.length);
67          assertEquals("solution should match", -0.3333333333, result[0], 0.000001);
68          // Quadratic equations
69          result = Solver.solve(1, 0, 1);
70          assertEquals("length of result should be 0", 0, result.length);
71          try
72          {
73              Solver.firstSolutionAfter(0, 1, 0, 1);
74              fail("Equation with no solutions should have thrown an exception");
75          }
76          catch (ValueException exception)
77          {
78              // Ignore expected exception
79          }
80          result = Solver.solve(1, 0, 0);
81          assertEquals("length of result should be 1", 1, result.length);
82          assertEquals("solution should match", 0, result[0], 0.000001);
83          try
84          {
85              assertEquals("solution should match", 0, Solver.firstSolutionAfter(-0.00001, 1, 0, 0), 0.000001);
86          }
87          catch (ValueException exception)
88          {
89              fail("Caught unexpected exception");
90          }
91          result = Solver.solve(1, 0, -1);
92          assertEquals("length of result should be 2", 2, result.length);
93          assertEquals("Smallest solution should match", -1, Math.min(result[0], result[1]), 0.000001);
94          assertEquals("Largest solution should match", 1, Math.max(result[0], result[1]), 0.000001);
95          try
96          {
97              assertEquals("First solution after -10 should be -1", -1, Solver.firstSolutionAfter(-10, 1, 0, -1), 0.000001);
98          }
99          catch (ValueException exception)
100         {
101             fail("Caught unexpected exception");
102         }
103         try
104         {
105             assertEquals("First solution after 0 should be 1", 1, Solver.firstSolutionAfter(0, 1, 0, -1), 0.000001);
106         }
107         catch (ValueException exception)
108         {
109             fail("Caught unexpected exception");
110         }
111         try
112         {
113             Solver.firstSolutionAfter(2, 1, 0, -1);
114             fail("First solution after 2 should have thrown an exception");
115         }
116         catch (ValueException exception)
117         {
118             // Ignore expected exception
119         }
120         result = Solver.solve(1, 2, -3);
121         // System.out.println("x0=" + result[0] + " x1=" + result[1]);
122         assertEquals("length of result should be 2", 2, result.length);
123         assertEquals("Smallest solution should match", -3, Math.min(result[0], result[1]), 0.000001);
124         assertEquals("Largest solution should match", 1, Math.max(result[0], result[1]), 0.000001);
125         // System.out.println("solutions are " + Solver.solve(1, 2, -3)[0] + ", " + Solver.solve(1, 2, -3)[1]);
126         try
127         {
128             assertEquals("First solution after -10 should be -3", -3, Solver.firstSolutionAfter(-10, 1, 2, -3), 0.000001);
129         }
130         catch (ValueException exception)
131         {
132             fail("Caught unexpected exception");
133         }
134         try
135         {
136             assertEquals("First solution after 0 should be 1", 1, Solver.firstSolutionAfter(0, 1, 2, -3), 0.000001);
137         }
138         catch (ValueException exception)
139         {
140             fail("Caught unexpected exception");
141         }
142         try
143         {
144             Solver.firstSolutionAfter(2, 1, 2, -3);
145             fail("First solution after 2 should have thrown an exception");
146         }
147         catch (ValueException exception)
148         {
149             // Ignore expected exception
150         }
151         // Invert a (so the other branch of the if statement gets tested)
152         // System.out.println("solutions are " + Solver.solve(-1, -2, 3)[0] + ", " + Solver.solve(-1, -2, 3)[1]);
153         try
154         {
155             assertEquals("First solution after -10 should be -3", -3, Solver.firstSolutionAfter(-10, -1, -2, 3), 0.000001);
156         }
157         catch (ValueException exception)
158         {
159             fail("Caught unexpected exception");
160         }
161         try
162         {
163             assertEquals("First solution after 0 should be 1", 1, Solver.firstSolutionAfter(0, -1, -2, 3), 0.000001);
164         }
165         catch (ValueException exception)
166         {
167             fail("Caught unexpected exception");
168         }
169         try
170         {
171             Solver.firstSolutionAfter(2, -1, -2, 3);
172             fail("First solution after 2 should have thrown an exception");
173         }
174         catch (ValueException exception)
175         {
176             // Ignore expected exception
177         }
178 
179     }
180 }