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
11
12
13
14
15
16
17
18 public class SolverTest
19 {
20
21
22
23
24 @SuppressWarnings("checkstyle:methodlength")
25 @Test
26 public final void solverTest()
27 {
28
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
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
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
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
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
120 }
121 result = Solver.solve(1, 2, -3);
122
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
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
151 }
152
153
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
178 }
179
180 }
181 }