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 @Test
25 public void solverTest()
26 {
27
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
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
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
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
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
119 }
120 result = Solver.solve(1, 2, -3);
121
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
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
150 }
151
152
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
177 }
178
179 }
180 }