View Javadoc
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   * Test the RelativeLane class.
15   * <p>
16   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
18   * </p>
19   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
20   */
21  public final class RelativeLaneTest
22  {
23  
24      /** */
25      private RelativeLaneTest()
26      {
27          // do not instantiate test class
28      }
29  
30      /**
31       * Test the RelativeLane class.
32       */
33      @Test
34      @SuppressWarnings({"unlikely-arg-type"})
35      public void testRelativeLane()
36      {
37          try
38          {
39              new RelativeLane(null, 1);
40              fail("Should have thrown a NullPointerException");
41          }
42          catch (NullPointerException npe)
43          {
44              // Ignore expected exception
45          }
46  
47          try
48          {
49              new RelativeLane(LateralDirectionality.LEFT, -1);
50              fail("negative number of lanes should have thrown an IllegalArgumentException");
51          }
52          catch (IllegalArgumentException iae)
53          {
54              // Ignore expected exception
55          }
56  
57          try
58          {
59              new RelativeLane(LateralDirectionality.NONE, 1);
60              fail("lateral directionality NONE with non-zero number of lanes should have thrown an IllegalArgumentException");
61          }
62          catch (IllegalArgumentException iae)
63          {
64              // Ignore expected exception
65          }
66  
67          try
68          {
69              new RelativeLane(LateralDirectionality.RIGHT, -1);
70              fail("negative number of lanes should have thrown an IllegalArgumentException");
71          }
72          catch (IllegalArgumentException iae)
73          {
74              // Ignore expected exception
75          }
76  
77          // TODO Wait for Wouter to indicate whether numLanes == 0 is really permitted when lat != LateralDirectionality.NONE
78          for (LateralDirectionality ld : new LateralDirectionality[] {LateralDirectionality.LEFT, LateralDirectionality.NONE,
79                  LateralDirectionality.RIGHT})
80          {
81              // TODO fix if 0 is only permitted for LateralDirectionality.NONE
82              int startAt = ld == LateralDirectionality.NONE ? 0 : 1;
83              int endAt = ld == LateralDirectionality.NONE ? 1 : 4;
84              for (int numLanes = startAt; numLanes < endAt; numLanes++)
85              {
86                  // System.out.println("Testing " + ld + ", with numLanes " + numLanes);
87                  RelativeLane rl = new RelativeLane(ld, numLanes);
88                  assertTrue(rl.toString().startsWith("RelativeLane"), "toString returns something descriptive");
89                  assertEquals(ld, rl.getLateralDirectionality(), "lateral directionality is returned");
90                  assertEquals(numLanes, rl.getNumLanes(), "numLanes is returned");
91                  if (numLanes == 0)
92                  {
93                      assertEquals(RelativeLane.CURRENT, rl, "ld should be CURRENT");
94                      assertTrue(rl.isCurrent(), "ld is CURRENT");
95                      assertTrue(rl.toString().contains("CURRENT"), "toString contains the word CURRENT");
96                  }
97                  else
98                  {
99                      assertNotEquals(RelativeLane.CURRENT, rl, "ld is not CURRENT");
100                     assertFalse(rl.isCurrent(), "ld is not CURRENT");
101                 }
102                 if (numLanes == 1)
103                 {
104                     if (ld == LateralDirectionality.LEFT)
105                     {
106                         assertEquals(RelativeLane.LEFT, rl, "ld is LEFT");
107                         assertTrue(rl.isLeft(), "ld is LEFT");
108                     }
109                     else
110                     {
111                         assertEquals(RelativeLane.RIGHT, rl, "ld is RIGHT");
112                         assertTrue(rl.isRight(), "ld is RIGHT");
113                     }
114                 }
115                 if (numLanes == 2)
116                 {
117                     if (ld == LateralDirectionality.LEFT)
118                     {
119                         assertEquals(RelativeLane.SECOND_LEFT, rl, "ld is SECOND_LEFT");
120                         assertTrue(rl.isSecondLeft(), "ld is SECOND_LEFT");
121                     }
122                     else
123                     {
124                         assertEquals(RelativeLane.SECOND_RIGHT, rl, "ld is SECOND_RIGHT");
125                         assertTrue(rl.isSecondRight(), "ld is SECOND_RIGHT");
126                     }
127                 }
128                 if (ld == LateralDirectionality.LEFT)
129                 {
130                     assertTrue(rl.toString().contains("LEFT"), "toString contains LEFT");
131                 }
132                 if (ld == LateralDirectionality.RIGHT)
133                 {
134                     assertTrue(rl.toString().contains("RIGHT"), "toString contains RIGHT");
135                 }
136                 if (ld != LateralDirectionality.LEFT || numLanes != 1)
137                 {
138                     assertNotEquals(RelativeLane.LEFT, rl, "ld is not LEFT");
139                     assertFalse(rl.isLeft(), "ld is not LEFT");
140                 }
141                 if (ld != LateralDirectionality.LEFT || numLanes != 2)
142                 {
143                     assertNotEquals(RelativeLane.SECOND_LEFT, rl, "ld is not SECOND_LEFT");
144                     assertFalse(rl.isSecondLeft(), "ld is not SECOND_LEFT");
145                 }
146                 if (ld != LateralDirectionality.RIGHT || numLanes != 1)
147                 {
148                     assertNotEquals(RelativeLane.RIGHT, rl, "ld is not RIGHT");
149                     assertFalse(rl.isRight(), "ld is not RIGHT");
150                 }
151                 if (ld != LateralDirectionality.RIGHT || numLanes != 2)
152                 {
153                     assertNotEquals(RelativeLane.SECOND_RIGHT, rl, "ld is not SECOND_RIGHT");
154                     assertFalse(rl.isSecondRight(), "ld is not SECOND_RIGHT");
155                 }
156                 RelativeLane leftNeighbor = rl.getLeft();
157                 if (numLanes == 0)
158                 {
159                     assertEquals(RelativeLane.LEFT, leftNeighbor, "left of CURRENT is LEFT");
160                 }
161                 if (numLanes == 1 && ld == LateralDirectionality.RIGHT)
162                 {
163                     assertEquals(RelativeLane.CURRENT, leftNeighbor, "left of RIGHT is CURRENT");
164                 }
165                 if (numLanes > 1 && ld == LateralDirectionality.RIGHT)
166                 {
167                     assertEquals(new RelativeLane(LateralDirectionality.RIGHT, numLanes - 1), leftNeighbor,
168                             "left of right > 1 is right minus 1");
169                 }
170                 assertNotEquals(leftNeighbor.hashCode(), rl.hashCode(),
171                         "hashCodes should be different for adjacent relative lanes");
172                 RelativeLane rightNeighbor = rl.getRight();
173                 if (numLanes == 0)
174                 {
175                     assertEquals(RelativeLane.RIGHT, rightNeighbor, "right of CURRENT is RIGHT");
176                 }
177                 if (numLanes == 1 && ld == LateralDirectionality.LEFT)
178                 {
179                     assertEquals(RelativeLane.CURRENT, rightNeighbor, "right of LEFT is CURRENT");
180                 }
181                 if (numLanes > 1 && ld == LateralDirectionality.LEFT)
182                 {
183                     assertEquals(new RelativeLane(LateralDirectionality.LEFT, numLanes - 1), rightNeighbor,
184                             "right of LEFT > 1 is left minus 1");
185                 }
186                 assertNotEquals(rightNeighbor.hashCode(), rl.hashCode(),
187                         "hashCodes should be different for adjacent relative lanes");
188                 for (int delta = -5; delta <= +5; delta++)
189                 {
190                     RelativeLane other = new RelativeLane(delta < 0 ? LateralDirectionality.LEFT
191                             : delta > 0 ? LateralDirectionality.RIGHT : LateralDirectionality.NONE, Math.abs(delta));
192                     int rank = ld == LateralDirectionality.LEFT ? -numLanes : numLanes;
193                     int diff = rank - delta;
194                     if (diff > 0)
195                     {
196                         assertTrue(rl.compareTo(other) > 0, "compareTo returns > 0");
197                     }
198                     if (diff == 0)
199                     {
200                         assertTrue(rl.compareTo(other) == 0, "compareTo returns 0");
201                     }
202                     if (diff < 0)
203                     {
204                         assertTrue(rl.compareTo(other) < 0, "compareTo returns < 0");
205                     }
206                 }
207                 assertFalse(rl.equals(null), "Not equal to null");
208                 assertFalse(rl.equals("NO WAY"), "Not equal to some unrelated object");
209                 assertEquals(rl, rl, "Equal to itself");
210             }
211         }
212 
213     }
214 
215 }