View Javadoc
1   package org.opentrafficsim.road.gtu.perception;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertFalse;
5   import static org.junit.Assert.assertNotEquals;
6   import static org.junit.Assert.assertTrue;
7   import static org.junit.Assert.fail;
8   
9   import org.junit.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-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
18   * <p>
19   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
20   */
21  public class RelativeLaneTest
22  {
23  
24      /**
25       * ] Test the RelativeLane class.
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              // Ignore expected exception
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              // Ignore expected exception
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              // Ignore expected exception
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              // Ignore expected exception
69          }
70  
71          // TODO Wait for Wouter to indicate whether numLanes == 0 is really permitted when lat != LateralDirectionality.NONE
72          for (LateralDirectionality ld : new LateralDirectionality[] { LateralDirectionality.LEFT, LateralDirectionality.NONE,
73                  LateralDirectionality.RIGHT })
74          {
75              // TODO fix if 0 is only permitted for LateralDirectionality.NONE
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                  // System.out.println("Testing " + ld + ", with numLanes " + numLanes);
81                  RelativeLane rl = new RelativeLane(ld, numLanes);
82                  assertTrue("toString returns something descriptive", rl.toString().startsWith("RelativeLane"));
83                  assertEquals("lateral directionality is returned", ld, rl.getLateralDirectionality());
84                  assertEquals("numLanes is returned", numLanes, rl.getNumLanes());
85                  if (numLanes == 0)
86                  {
87                      assertEquals("ld should be CURRENT", RelativeLane.CURRENT, rl);
88                      assertTrue("ld is CURRENT", rl.isCurrent());
89                      assertTrue("toString contains the word CURRENT", rl.toString().contains("CURRENT"));
90                  }
91                  else
92                  {
93                      assertNotEquals("ld is not CURRENT", RelativeLane.CURRENT, rl);
94                      assertFalse("ld is not CURRENT", rl.isCurrent());
95                  }
96                  if (numLanes == 1)
97                  {
98                      if (ld == LateralDirectionality.LEFT)
99                      {
100                         assertEquals("ld is LEFT", RelativeLane.LEFT, rl);
101                         assertTrue("ld is LEFT", rl.isLeft());
102                     }
103                     else
104                     {
105                         assertEquals("ld is RIGHT", RelativeLane.RIGHT, rl);
106                         assertTrue("ld is RIGHT", rl.isRight());
107                     }
108                 }
109                 if (numLanes == 2)
110                 {
111                     if (ld == LateralDirectionality.LEFT)
112                     {
113                         assertEquals("ld is SECOND_LEFT", RelativeLane.SECOND_LEFT, rl);
114                         assertTrue("ld is SECOND_LEFT", rl.isSecondLeft());
115                     }
116                     else
117                     {
118                         assertEquals("ld is SECOND_RIGHT", RelativeLane.SECOND_RIGHT, rl);
119                         assertTrue("ld is SECOND_RIGHT", rl.isSecondRight());
120                     }
121                 }
122                 if (ld == LateralDirectionality.LEFT)
123                 {
124                     assertTrue("toString contains LEFT", rl.toString().contains("LEFT"));
125                 }
126                 if (ld == LateralDirectionality.RIGHT)
127                 {
128                     assertTrue("toString contains RIGHT", rl.toString().contains("RIGHT"));
129                 }
130                 if (ld != LateralDirectionality.LEFT || numLanes != 1)
131                 {
132                     assertNotEquals("ld is not LEFT", RelativeLane.LEFT, rl);
133                     assertFalse("ld is not LEFT", rl.isLeft());
134                 }
135                 if (ld != LateralDirectionality.LEFT || numLanes != 2)
136                 {
137                     assertNotEquals("ld is not SECOND_LEFT", RelativeLane.SECOND_LEFT, rl);
138                     assertFalse("ld is not SECOND_LEFT", rl.isSecondLeft());
139                 }
140                 if (ld != LateralDirectionality.RIGHT || numLanes != 1)
141                 {
142                     assertNotEquals("ld is not RIGHT", RelativeLane.RIGHT, rl);
143                     assertFalse("ld is not RIGHT", rl.isRight());
144                 }
145                 if (ld != LateralDirectionality.RIGHT || numLanes != 2)
146                 {
147                     assertNotEquals("ld is not SECOND_RIGHT", RelativeLane.SECOND_RIGHT, rl);
148                     assertFalse("ld is not SECOND_RIGHT", rl.isSecondRight());
149                 }
150                 RelativeLane leftNeighbor = rl.getLeft();
151                 if (numLanes == 0)
152                 {
153                     assertEquals("left of CURRENT is LEFT", RelativeLane.LEFT, leftNeighbor);
154                 }
155                 if (numLanes == 1 && ld == LateralDirectionality.RIGHT)
156                 {
157                     assertEquals("left of RIGHT is CURRENT", RelativeLane.CURRENT, leftNeighbor);
158                 }
159                 if (numLanes > 1 && ld == LateralDirectionality.RIGHT)
160                 {
161                     assertEquals("left of right > 1 is right minus 1",
162                             new RelativeLane(LateralDirectionality.RIGHT, numLanes - 1), leftNeighbor);
163                 }
164                 assertNotEquals("hashCodes should be different for adjacent relative lanes", leftNeighbor.hashCode(),
165                         rl.hashCode());
166                 RelativeLane rightNeighbor = rl.getRight();
167                 if (numLanes == 0)
168                 {
169                     assertEquals("right of CURRENT is RIGHT", RelativeLane.RIGHT, rightNeighbor);
170                 }
171                 if (numLanes == 1 && ld == LateralDirectionality.LEFT)
172                 {
173                     assertEquals("right of LEFT is CURRENT", RelativeLane.CURRENT, rightNeighbor);
174                 }
175                 if (numLanes > 1 && ld == LateralDirectionality.LEFT)
176                 {
177                     assertEquals("right of LEFT > 1 is left minus 1",
178                             new RelativeLane(LateralDirectionality.LEFT, numLanes - 1), rightNeighbor);
179                 }
180                 assertNotEquals("hashCodes should be different for adjacent relative lanes", rightNeighbor.hashCode(),
181                         rl.hashCode());
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("compareTo returns > 0", rl.compareTo(other) > 0);
191                     }
192                     if (diff == 0)
193                     {
194                         assertTrue("compareTo returns 0", rl.compareTo(other) == 0);
195                     }
196                     if (diff < 0)
197                     {
198                         assertTrue("compareTo returns < 0", rl.compareTo(other) < 0);
199                     }
200                 }
201                 assertFalse("Not equal to null", rl.equals(null));
202                 assertFalse("Not equal to some unrelated object", rl.equals("NO WAY"));
203                 assertEquals("Equal to itself", rl, rl);
204             }
205         }
206 
207     }
208 
209 }