1   /*
2    * @(#) BasicReflectionTest.java Aug 26, 2003
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.dsol.eventList;
11  
12  import junit.framework.Assert;
13  import junit.framework.TestCase;
14  import nl.tudelft.simulation.dsol.eventlists.EventListInterface;
15  import nl.tudelft.simulation.dsol.formalisms.devs.SimEvent;
16  import nl.tudelft.simulation.dsol.formalisms.devs.SimEventInterface;
17  
18  /***
19   * This class defines the JUnit test for the TreeMapEventListOld <br>
20   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
21   * University of Technology </a>, the Netherlands. <br>
22   * See for project information <a
23   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
24   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
25   * License (GPL) </a>, no warranty <br>
26   * 
27   * @version 2.0 21.09.2003 <br>
28   * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
29   *         Jacobs </a>, <a
30   *         href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
31   *         Verbraeck </a>
32   */
33  public class EventListTest extends TestCase
34  {
35  	/*** TEST_METHOD_NAME refers to the name of the test method */
36  	public static final String TEST_METHOD_NAME = "test";
37  
38  	/*** eventList is the eventList on which the test is fired */
39  	private EventListInterface eventList = null;
40  
41  	/***
42  	 * constructs a new BasicReflectionTest
43  	 * 
44  	 * @param eventList is the eventList on which the test is fired
45  	 */
46  	public EventListTest(final EventListInterface eventList)
47  	{
48  		this(EventListTest.TEST_METHOD_NAME, eventList);
49  	}
50  
51  	/***
52  	 * constructs a new BasicReflectionTest
53  	 * 
54  	 * @param arg0 the name of the test method
55  	 * @param eventList is the eventList on which the test is fired
56  	 */
57  	public EventListTest(final String arg0, final EventListInterface eventList)
58  	{
59  		super(arg0);
60  		this.eventList = eventList;
61  	}
62  
63  	/***
64  	 * tests the TreeMapEventListOld
65  	 */
66  	public void test()
67  	{
68  		Assert.assertNotNull(this.eventList);
69  		try
70  		{
71  			//We fill the eventList with 500 events with random times
72  			// between [0..200]
73  			for (int i = 0; i < 500; i++)
74  			{
75  				this.eventList.add(new SimEvent(200 * Math.random(), this,
76  						new String(), "trim", null));
77  			}
78  
79  			//Now we assert some getters on the eventList
80  			Assert.assertTrue(!this.eventList.isEmpty());
81  			Assert.assertTrue(this.eventList.size() == 500);
82  
83  			//Let's see if the eventList was properly ordered
84  			double time = 0;
85  			for (int i = 0; i < 500; i++)
86  			{
87  				SimEventInterface simEvent = this.eventList.first();
88  				this.eventList.remove(this.eventList.first());
89  				double executionTime = simEvent.getAbsoluteExecutionTime();
90  				Assert.assertTrue(executionTime >= 0.0);
91  				Assert.assertTrue(executionTime <= 200.0);
92  				Assert.assertTrue(executionTime >= time);
93  				time = executionTime;
94  			}
95  
96  			//Now we fill the eventList with a number of events with
97  			//different priorities on time=0.0
98  			for (int i = 1; i < 10; i++)
99  			{
100 				this.eventList.add(new SimEvent(0.0, (short) i, this,
101 						new String(), "trim", null));
102 			}
103 			short priority = SimEventInterface.MAX_PRIORITY;
104 
105 			//Let's empty the eventList and check the priorities
106 			while (!this.eventList.isEmpty())
107 			{
108 				SimEventInterface simEvent = this.eventList.first();
109 				this.eventList.remove(this.eventList.first());
110 				double executionTime = simEvent.getAbsoluteExecutionTime();
111 				short eventPriority = simEvent.getPriority();
112 
113 				Assert.assertTrue(executionTime == 0.0);
114 				Assert
115 						.assertTrue(eventPriority <= SimEventInterface.MAX_PRIORITY);
116 				Assert
117 						.assertTrue(eventPriority >= SimEventInterface.MIN_PRIORITY);
118 				Assert.assertTrue(eventPriority <= priority);
119 				priority = eventPriority;
120 			}
121 
122 			//Let's check the empty eventList
123 			Assert.assertTrue(this.eventList.isEmpty());
124 			Assert.assertNull(this.eventList.first());
125 			Assert.assertFalse(this.eventList.remove(null));
126 			Assert.assertFalse(this.eventList.remove(new SimEvent(200 * Math
127 					.random(), this, new String(), "trim", null)));
128 			this.eventList.clear();
129 
130 			//Let's cancel an event
131 			this.eventList.add(new SimEvent(100, this, this, "toString", null));
132 			SimEventInterface simEvent = new SimEvent(100, this, this,
133 					"toString", null);
134 			this.eventList.add(simEvent);
135 			assertTrue(this.eventList.remove(simEvent));
136 		} catch (Exception exception)
137 		{
138 			exception.printStackTrace();
139 			Assert.fail(exception.getMessage());
140 		}
141 	}
142 }