View Javadoc
1   package org.opentrafficsim.core.compatibility;
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.assertNull;
7   import static org.junit.Assert.assertTrue;
8   
9   import org.junit.Test;
10  import org.opentrafficsim.core.dsol.OTSSimulator;
11  import org.opentrafficsim.core.gtu.GTUDirectionality;
12  import org.opentrafficsim.core.gtu.GTUType;
13  import org.opentrafficsim.core.network.LinkType;
14  import org.opentrafficsim.core.network.LongitudinalDirectionality;
15  import org.opentrafficsim.core.network.OTSNetwork;
16  
17  /**
18   * Test the classes and interfaces in the compatibility package.
19   * <p>
20   * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
21   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
22   * <p>
23   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Apr 13, 2016 <br>
24   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
25   */
26  public class CompatibilityTest
27  {
28  
29      /**
30       * Test the interface.
31       */
32      @Test
33      public void testInteface()
34      {
35          OTSNetwork network =
36                  new OTSNetwork("CompatibilityTestNetwork", true, new OTSSimulator("Simulator for CompatibilityTest"));
37          assertTrue("EVERYTHING returns true for any GTU type",
38                  Compatible.EVERYTHING.isCompatible(network.getGtuType(GTUType.DEFAULTS.CAR), GTUDirectionality.DIR_PLUS));
39          assertTrue("EVERYTHING returns true for any GTU type",
40                  Compatible.EVERYTHING.isCompatible(network.getGtuType(GTUType.DEFAULTS.CAR), GTUDirectionality.DIR_MINUS));
41          assertTrue("EVERYTHING returns true for any GTU type",
42                  Compatible.EVERYTHING.isCompatible(network.getGtuType(GTUType.DEFAULTS.SHIP), GTUDirectionality.DIR_PLUS));
43          assertTrue("EVERYTHING returns true for any GTU type",
44                  Compatible.EVERYTHING.isCompatible(network.getGtuType(GTUType.DEFAULTS.SHIP), GTUDirectionality.DIR_MINUS));
45          assertTrue("PLUS returns true for any GTU type in DIR_PLUS",
46                  Compatible.PLUS.isCompatible(network.getGtuType(GTUType.DEFAULTS.CAR), GTUDirectionality.DIR_PLUS));
47          assertFalse("PLUS returns false for any GTU type in DIR_MINUS",
48                  Compatible.PLUS.isCompatible(network.getGtuType(GTUType.DEFAULTS.CAR), GTUDirectionality.DIR_MINUS));
49          assertTrue("PLUS returns true for any GTU type in DIR_PLUS",
50                  Compatible.PLUS.isCompatible(network.getGtuType(GTUType.DEFAULTS.SHIP), GTUDirectionality.DIR_PLUS));
51          assertFalse("PLUS returns false for any GTU type in DIR_MINUS",
52                  Compatible.PLUS.isCompatible(network.getGtuType(GTUType.DEFAULTS.SHIP), GTUDirectionality.DIR_MINUS));
53          assertFalse("MINUS returns false for any GTU type in DIR_PLUS",
54                  Compatible.MINUS.isCompatible(network.getGtuType(GTUType.DEFAULTS.CAR), GTUDirectionality.DIR_PLUS));
55          assertTrue("MINUS returns true for any GTU type in DIR_MINUS",
56                  Compatible.MINUS.isCompatible(network.getGtuType(GTUType.DEFAULTS.CAR), GTUDirectionality.DIR_MINUS));
57          assertFalse("MINUS returns false for any GTU type in DIR_PLUS",
58                  Compatible.MINUS.isCompatible(network.getGtuType(GTUType.DEFAULTS.SHIP), GTUDirectionality.DIR_PLUS));
59          assertTrue("MINUS returns true for any GTU type in DIR_MINUS",
60                  Compatible.MINUS.isCompatible(network.getGtuType(GTUType.DEFAULTS.SHIP), GTUDirectionality.DIR_MINUS));
61      }
62  
63      /**
64       * Test the class.
65       */
66      @SuppressWarnings({ "unlikely-arg-type" })
67      @Test
68      public void testClass()
69      {
70          OTSNetwork network =
71                  new OTSNetwork("CompatibilityTestNetwork", true, new OTSSimulator("Simulator for CompatibilityTest"));
72          LinkType linkType = network.getLinkType(LinkType.DEFAULTS.FREEWAY);
73          GTUCompatibility<LinkType> compatibility = new GTUCompatibility<>(linkType);
74          assertNull("Freshly initialized compatibility does not know about any GTUType",
75                  compatibility.isCompatible(network.getGtuType(GTUType.DEFAULTS.CAR), GTUDirectionality.DIR_PLUS));
76          assertNull("Freshly initialized compatibility does not know about any GTUType",
77                  compatibility.isCompatible(network.getGtuType(GTUType.DEFAULTS.CAR), GTUDirectionality.DIR_MINUS));
78          compatibility.addAllowedGTUType(network.getGtuType(GTUType.DEFAULTS.CAR), LongitudinalDirectionality.DIR_PLUS);
79          assertTrue("now compatible with CAR in DIR_PLUS",
80                  compatibility.isCompatible(network.getGtuType(GTUType.DEFAULTS.CAR), GTUDirectionality.DIR_PLUS));
81          assertFalse("now incompatible with CAR in DIR_MINUS",
82                  compatibility.isCompatible(network.getGtuType(GTUType.DEFAULTS.CAR), GTUDirectionality.DIR_MINUS));
83          compatibility.removeAllowedGTUType(network.getGtuType(GTUType.DEFAULTS.CAR), LongitudinalDirectionality.DIR_PLUS);
84          assertNull("After remove, compatibility does not know about any GTUType",
85                  compatibility.isCompatible(network.getGtuType(GTUType.DEFAULTS.CAR), GTUDirectionality.DIR_PLUS));
86          assertNull("After remove, compatibility does not know about any GTUType",
87                  compatibility.isCompatible(network.getGtuType(GTUType.DEFAULTS.CAR), GTUDirectionality.DIR_MINUS));
88          compatibility.addAllowedGTUType(network.getGtuType(GTUType.DEFAULTS.SHIP), LongitudinalDirectionality.DIR_PLUS);
89          assertNull("Compatibility does not know about CAR",
90                  compatibility.isCompatible(network.getGtuType(GTUType.DEFAULTS.CAR), GTUDirectionality.DIR_PLUS));
91          assertNull("Compatibility does not know about CAR",
92                  compatibility.isCompatible(network.getGtuType(GTUType.DEFAULTS.CAR), GTUDirectionality.DIR_MINUS));
93          compatibility.addAllowedGTUType(network.getGtuType(GTUType.DEFAULTS.CAR), LongitudinalDirectionality.DIR_MINUS);
94          assertFalse("now incompatible with CAR in DIR_PLUS",
95                  compatibility.isCompatible(network.getGtuType(GTUType.DEFAULTS.CAR), GTUDirectionality.DIR_PLUS));
96          assertTrue("now compatible with CAR in DIR_MINUS",
97                  compatibility.isCompatible(network.getGtuType(GTUType.DEFAULTS.CAR), GTUDirectionality.DIR_MINUS));
98          GTUCompatibility<LinkType> copy = new GTUCompatibility<>(compatibility);
99          assertTrue("copy equals original", copy.equals(compatibility));
100         // Remove CAr from original
101         assertEquals("hashCode of copy is equal to hashcode of original", copy.hashCode(), compatibility.hashCode());
102         compatibility.removeAllowedGTUType(network.getGtuType(GTUType.DEFAULTS.CAR), LongitudinalDirectionality.DIR_MINUS);
103         // It is EXTREMELY unlikely that hash codes match after removal; so we'll test that as well
104         assertFalse("copy no longer equal to original", copy.equals(compatibility));
105         assertNotEquals("Hash codes should be different", copy.hashCode(), compatibility.hashCode());
106         // Check that it is still there in the copy
107         assertFalse("now incompatible with CAR in DIR_PLUS",
108                 copy.isCompatible(network.getGtuType(GTUType.DEFAULTS.CAR), GTUDirectionality.DIR_PLUS));
109         assertTrue("now compatible with CAR in DIR_MINUS",
110                 copy.isCompatible(network.getGtuType(GTUType.DEFAULTS.CAR), GTUDirectionality.DIR_MINUS));
111         assertNull("After remove, compatibility does not know about CAR",
112                 compatibility.isCompatible(network.getGtuType(GTUType.DEFAULTS.CAR), GTUDirectionality.DIR_PLUS));
113         assertNull("After remove, compatibility does not know about CAR",
114                 compatibility.isCompatible(network.getGtuType(GTUType.DEFAULTS.CAR), GTUDirectionality.DIR_MINUS));
115         compatibility.addAllowedGTUType(network.getGtuType(GTUType.DEFAULTS.CAR), LongitudinalDirectionality.DIR_BOTH);
116         assertTrue("now compatible with CAR in DIR_PLUS",
117                 compatibility.isCompatible(network.getGtuType(GTUType.DEFAULTS.CAR), GTUDirectionality.DIR_PLUS));
118         assertTrue("now compatible with CAR in DIR_MINUS",
119                 compatibility.isCompatible(network.getGtuType(GTUType.DEFAULTS.CAR), GTUDirectionality.DIR_MINUS));
120         compatibility.removeAllowedGTUType(network.getGtuType(GTUType.DEFAULTS.CAR), LongitudinalDirectionality.DIR_BOTH);
121         compatibility.addAllowedGTUType(network.getGtuType(GTUType.DEFAULTS.CAR), LongitudinalDirectionality.DIR_NONE);
122         assertFalse("now incompatible with CAR in DIR_PLUS",
123                 compatibility.isCompatible(network.getGtuType(GTUType.DEFAULTS.CAR), GTUDirectionality.DIR_PLUS));
124         assertFalse("now incompatible with CAR in DIR_MINUS",
125                 compatibility.isCompatible(network.getGtuType(GTUType.DEFAULTS.CAR), GTUDirectionality.DIR_MINUS));
126         // Additional tests for equals
127         assertFalse("not equal to null", compatibility.equals(null));
128         assertFalse("not equal to some other object", compatibility.equals("bla"));
129         assertTrue("equal to itself", compatibility.equals(compatibility));
130         // toString returns something descriptive
131         assertTrue("The toString methods returns something descriptive",
132                 compatibility.toString().startsWith("GTUCompatibility"));
133         // Hierarchical on GTUType
134         compatibility = new GTUCompatibility<>(linkType);
135         compatibility.addAllowedGTUType(network.getGtuType(GTUType.DEFAULTS.BUS), LongitudinalDirectionality.DIR_PLUS);
136         assertNull("Not directly compatible with scheduled bus",
137                 compatibility.isCompatible(network.getGtuType(GTUType.DEFAULTS.SCHEDULED_BUS), GTUDirectionality.DIR_PLUS));
138         assertNull("Not directly knowledgeable about scheduled bus",
139                 compatibility.getDirectionality(network.getGtuType(GTUType.DEFAULTS.SCHEDULED_BUS), false));
140         assertEquals("Indirectly compatible with scheduled bus",
141                 compatibility.getDirectionality(network.getGtuType(GTUType.DEFAULTS.SCHEDULED_BUS), true),
142                 LongitudinalDirectionality.DIR_PLUS);
143         assertEquals("Not directly or indirectly passable for SHIP",
144                 compatibility.getDirectionality(network.getGtuType(GTUType.DEFAULTS.SHIP), true),
145                 LongitudinalDirectionality.DIR_NONE);
146         // System.out.println(linkType);
147         // System.out.println(linkType.getParent());
148         assertEquals("Indirectly compatible with PEDESTRIAN",
149                 compatibility.getDirectionality(network.getGtuType(GTUType.DEFAULTS.PEDESTRIAN), true),
150                 LongitudinalDirectionality.DIR_BOTH);
151         assertNull("Not directly compatible with PEDESTRIAN",
152                 compatibility.getDirectionality(network.getGtuType(GTUType.DEFAULTS.PEDESTRIAN), false));
153         
154         // TODO Test isCompatibleWith method (OR remove that method from the class; it is never used).
155     }
156 
157 }