1 package org.opentrafficsim.road.gtu.perception;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.junit.jupiter.api.Assertions.assertFalse;
5 import static org.junit.jupiter.api.Assertions.assertNotEquals;
6 import static org.junit.jupiter.api.Assertions.assertTrue;
7 import static org.junit.jupiter.api.Assertions.fail;
8
9 import org.junit.jupiter.api.Test;
10 import org.opentrafficsim.core.network.LateralDirectionality;
11 import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
12
13
14
15
16
17
18
19
20
21 public class RelativeLaneTest
22 {
23
24
25
26
27 @Test
28 @SuppressWarnings({"unlikely-arg-type"})
29 public void testRelativeLane()
30 {
31 try
32 {
33 new RelativeLane(null, 1);
34 fail("Should have thrown a NullPointerException");
35 }
36 catch (NullPointerException npe)
37 {
38
39 }
40
41 try
42 {
43 new RelativeLane(LateralDirectionality.LEFT, -1);
44 fail("negative number of lanes should have thrown an IllegalArgumentException");
45 }
46 catch (IllegalArgumentException iae)
47 {
48
49 }
50
51 try
52 {
53 new RelativeLane(LateralDirectionality.NONE, 1);
54 fail("lateral directionality NONE with non-zero number of lanes should have thrown an IllegalArgumentException");
55 }
56 catch (IllegalArgumentException iae)
57 {
58
59 }
60
61 try
62 {
63 new RelativeLane(LateralDirectionality.RIGHT, -1);
64 fail("negative number of lanes should have thrown an IllegalArgumentException");
65 }
66 catch (IllegalArgumentException iae)
67 {
68
69 }
70
71
72 for (LateralDirectionality ld : new LateralDirectionality[] {LateralDirectionality.LEFT, LateralDirectionality.NONE,
73 LateralDirectionality.RIGHT})
74 {
75
76 int startAt = ld == LateralDirectionality.NONE ? 0 : 1;
77 int endAt = ld == LateralDirectionality.NONE ? 1 : 4;
78 for (int numLanes = startAt; numLanes < endAt; numLanes++)
79 {
80
81 RelativeLane rl = new RelativeLane(ld, numLanes);
82 assertTrue(rl.toString().startsWith("RelativeLane"), "toString returns something descriptive");
83 assertEquals(ld, rl.getLateralDirectionality(), "lateral directionality is returned");
84 assertEquals(numLanes, rl.getNumLanes(), "numLanes is returned");
85 if (numLanes == 0)
86 {
87 assertEquals(RelativeLane.CURRENT, rl, "ld should be CURRENT");
88 assertTrue(rl.isCurrent(), "ld is CURRENT");
89 assertTrue(rl.toString().contains("CURRENT"), "toString contains the word CURRENT");
90 }
91 else
92 {
93 assertNotEquals(RelativeLane.CURRENT, rl, "ld is not CURRENT");
94 assertFalse(rl.isCurrent(), "ld is not CURRENT");
95 }
96 if (numLanes == 1)
97 {
98 if (ld == LateralDirectionality.LEFT)
99 {
100 assertEquals(RelativeLane.LEFT, rl, "ld is LEFT");
101 assertTrue(rl.isLeft(), "ld is LEFT");
102 }
103 else
104 {
105 assertEquals(RelativeLane.RIGHT, rl, "ld is RIGHT");
106 assertTrue(rl.isRight(), "ld is RIGHT");
107 }
108 }
109 if (numLanes == 2)
110 {
111 if (ld == LateralDirectionality.LEFT)
112 {
113 assertEquals(RelativeLane.SECOND_LEFT, rl, "ld is SECOND_LEFT");
114 assertTrue(rl.isSecondLeft(), "ld is SECOND_LEFT");
115 }
116 else
117 {
118 assertEquals(RelativeLane.SECOND_RIGHT, rl, "ld is SECOND_RIGHT");
119 assertTrue(rl.isSecondRight(), "ld is SECOND_RIGHT");
120 }
121 }
122 if (ld == LateralDirectionality.LEFT)
123 {
124 assertTrue(rl.toString().contains("LEFT"), "toString contains LEFT");
125 }
126 if (ld == LateralDirectionality.RIGHT)
127 {
128 assertTrue(rl.toString().contains("RIGHT"), "toString contains RIGHT");
129 }
130 if (ld != LateralDirectionality.LEFT || numLanes != 1)
131 {
132 assertNotEquals(RelativeLane.LEFT, rl, "ld is not LEFT");
133 assertFalse(rl.isLeft(), "ld is not LEFT");
134 }
135 if (ld != LateralDirectionality.LEFT || numLanes != 2)
136 {
137 assertNotEquals(RelativeLane.SECOND_LEFT, rl, "ld is not SECOND_LEFT");
138 assertFalse(rl.isSecondLeft(), "ld is not SECOND_LEFT");
139 }
140 if (ld != LateralDirectionality.RIGHT || numLanes != 1)
141 {
142 assertNotEquals(RelativeLane.RIGHT, rl, "ld is not RIGHT");
143 assertFalse(rl.isRight(), "ld is not RIGHT");
144 }
145 if (ld != LateralDirectionality.RIGHT || numLanes != 2)
146 {
147 assertNotEquals(RelativeLane.SECOND_RIGHT, rl, "ld is not SECOND_RIGHT");
148 assertFalse(rl.isSecondRight(), "ld is not SECOND_RIGHT");
149 }
150 RelativeLane leftNeighbor = rl.getLeft();
151 if (numLanes == 0)
152 {
153 assertEquals(RelativeLane.LEFT, leftNeighbor, "left of CURRENT is LEFT");
154 }
155 if (numLanes == 1 && ld == LateralDirectionality.RIGHT)
156 {
157 assertEquals(RelativeLane.CURRENT, leftNeighbor, "left of RIGHT is CURRENT");
158 }
159 if (numLanes > 1 && ld == LateralDirectionality.RIGHT)
160 {
161 assertEquals(new RelativeLane(LateralDirectionality.RIGHT, numLanes - 1),
162 leftNeighbor, "left of right > 1 is right minus 1");
163 }
164 assertNotEquals(leftNeighbor.hashCode(), rl.hashCode(),
165 "hashCodes should be different for adjacent relative lanes");
166 RelativeLane rightNeighbor = rl.getRight();
167 if (numLanes == 0)
168 {
169 assertEquals(RelativeLane.RIGHT, rightNeighbor, "right of CURRENT is RIGHT");
170 }
171 if (numLanes == 1 && ld == LateralDirectionality.LEFT)
172 {
173 assertEquals(RelativeLane.CURRENT, rightNeighbor, "right of LEFT is CURRENT");
174 }
175 if (numLanes > 1 && ld == LateralDirectionality.LEFT)
176 {
177 assertEquals(new RelativeLane(LateralDirectionality.LEFT, numLanes - 1),
178 rightNeighbor, "right of LEFT > 1 is left minus 1");
179 }
180 assertNotEquals(rightNeighbor.hashCode(), rl.hashCode(),
181 "hashCodes should be different for adjacent relative lanes");
182 for (int delta = -5; delta <= +5; delta++)
183 {
184 RelativeLane other = new RelativeLane(delta < 0 ? LateralDirectionality.LEFT
185 : delta > 0 ? LateralDirectionality.RIGHT : LateralDirectionality.NONE, Math.abs(delta));
186 int rank = ld == LateralDirectionality.LEFT ? -numLanes : numLanes;
187 int diff = rank - delta;
188 if (diff > 0)
189 {
190 assertTrue(rl.compareTo(other) > 0, "compareTo returns > 0");
191 }
192 if (diff == 0)
193 {
194 assertTrue(rl.compareTo(other) == 0, "compareTo returns 0");
195 }
196 if (diff < 0)
197 {
198 assertTrue(rl.compareTo(other) < 0, "compareTo returns < 0");
199 }
200 }
201 assertFalse(rl.equals(null), "Not equal to null");
202 assertFalse(rl.equals("NO WAY"), "Not equal to some unrelated object");
203 assertEquals(rl, rl, "Equal to itself");
204 }
205 }
206
207 }
208
209 }