Package SimPy :: Module testSimPy_simident

[hide private]
[frames] | no frames]

# Source Code for Module SimPy.testSimPy_simident

      1  #!/usr / bin / env python
      2  # coding=utf-8
      3  from SimPy.Simulation  import *
      4  from SimPy.MonitorTest import *
      5  import unittest
      6  # $Revision: 414 $ $Date: 2010-03-26 07:29:21 +0100 (Fri, 26 Mar 2010) $
      7  """testSimPy_simident.py
      8  SimPy version 2.1
      9  Unit tests of checks of correct use of sim parameter.
     10  2.1 introduces checks that two entities involved in a yield (e.g.
     11  a Process and a Resource) belong to the same Simulation instance.
     12
     13  NOTE: This unit test set only works if __debug__ == True. If
     14  Python is called with the -O or -OO parameter, the checks are not
     15  being executed.
     16
     17  #'$Revision: 414 $ $Date: 2010-03-26 07:29:21 +0100 (Fri, 26 Mar 2010) $ kgm'
     18
     19  """
     20
     21  simulationVersion=version
     22  print "Under test: Simulation.py %s"%simulationVersion
     23  __version__ = '2.1 $Revision: 414 $ $Date: 2010-03-26 07:29:21 +0100 (Fri, 26 Mar 2010) $ '
     24  print 'testSimpy_simident.py %s'%__version__
     25  if not __debug__:
     26      print "Unit tests not executed -- run in __debug__ mode."
     27      sys.exit()
     28
     29 -class Activatetest(Process):
     30      """Used in testActivate.
     31      """
     32 -    def run(self):
     33          yield hold,self,0
     34
     35 -class Requesttest(Process):
     36      """Used in testRequest.
     37      """
     38 -    def run(self,res):
     39          yield request,self,res
     40
     41 -class PutStoretest(Process):
     42      """Used in testPutStore.
     43      """
     44 -    def run(self,store):
     45          yield put,self,store,[1]
     46
     47 -class GetStoretest(Process):
     48      """Used in testGetStore.
     49      """
     50 -    def run(self,store):
     51          yield get,self,store,1
     52
     53 -class PutLeveltest(Process):
     54      """Used in testPutLevel.
     55      """
     56 -    def run(self,level):
     57          yield put,self,level,1
     58
     59 -class GetLeveltest(Process):
     60      """Used in testGetLevel.
     61      """
     62 -    def run(self,level):
     63          yield get,self,level,1
     64
     65 -class Waiteventtest(Process):
     66      """Used in testWaitevent.
     67      """
     68 -    def run(self,event):
     69          yield waitevent,self,event
     70
     71 -class Queueeventtest(Process):
     72      """Used in testQueueevent.
     73      """
     74 -    def run(self,event):
     75          yield queueevent,self,event
     76
     77 -class makesimtestcase(unittest.TestCase):
     78      """Tests of checks for identical sim parameters.
     79      """
     80      ## -------------------------------------------------------------
     81      ## Test of "activate" call
     82      ## -------------------------------------------------------------
     83 -    def testActivate(self):
     84          s = Simulation()
     85          s.initialize()
     86          r = Activatetest(sim=s)
     87          try:
     88              activate(r,r.run()) ## missing s.
     89          except FatalSimerror:
     90              pass
     91          else:
     92              self.fail("expected FatalSimerror")
     93          s.simulate(until=1)
     94
     95      ## -------------------------------------------------------------
     96      ## Test of "yield request,self,res"
     97      ## -------------------------------------------------------------
     98 -    def testRequest(self):
     99          s = Simulation()
    100          s.initialize()
    101          res = Resource( ) # wrong sim
    102          r = Requesttest(sim=s)
    103          s.activate(r,r.run(res = res))
    104          try:
    105              s.simulate(until=1)
    106          except FatalSimerror:
    107              pass
    108          else:
    109              self.fail("expected FatalSimerror")
    110
    111      ## -------------------------------------------------------------
    112      ## Test of "yield put,self,store"
    113      ## -------------------------------------------------------------
    114 -    def testPutStore (self):
    115          s = Simulation()
    116          s.initialize()
    117          store = Store( ) # wrong sim
    118          r = PutStoretest(sim=s)
    119          s.activate(r,r.run(store = store))
    120          try:
    121              s.simulate(until=1)
    122          except FatalSimerror:
    123              pass
    124          else:
    125              self.fail("expected FatalSimerror")
    126      ## -------------------------------------------------------------
    127      ## Test of "yield put,self,level"
    128      ## -------------------------------------------------------------
    129 -    def testPutLevel (self):
    130          s = Simulation()
    131          s.initialize()
    132          levl = Level( ) # wrong sim
    133          r = PutLeveltest(sim=s)
    134          s.activate(r,r.run(level = levl))
    135          try:
    136              s.simulate(until=1)
    137          except FatalSimerror:
    138              pass
    139          else:
    140              self.fail("expected FatalSimerror")
    141
    142      ## -------------------------------------------------------------
    143      ## Test of "yield get,self,store"
    144      ## -------------------------------------------------------------
    145 -    def testGetStore(self):
    146          s = Simulation()
    147          s.initialize()
    148          store = Store( ) # wrong sim
    149          r = GetStoretest(sim=s)
    150          s.activate(r,r.run(store = store))
    151          try:
    152              s.simulate(until=1)
    153          except FatalSimerror:
    154              pass
    155          else:
    156              self.fail("expected FatalSimerror")
    157
    158      ## -------------------------------------------------------------
    159      ## Test of "yield get,self,level"
    160      ## -------------------------------------------------------------
    161 -    def testGetLevel(self):
    162          s = Simulation()
    163          s.initialize()
    164          levl = Level( ) # wrong sim
    165          r = GetLeveltest(sim=s)
    166          s.activate(r,r.run(level = levl))
    167          try:
    168              s.simulate(until=1)
    169          except FatalSimerror:
    170              pass
    171          else:
    172              self.fail("expected FatalSimerror")
    173
    174      ## -------------------------------------------------------------
    175      ## Test of "yield waitevent,self,evt"
    176      ## -------------------------------------------------------------
    177 -    def testWaitevent(self):
    178          s = Simulation()
    179          s.initialize()
    180          evt = SimEvent() ## wrong sim
    181          w = Waiteventtest(sim = s)
    182          s.activate(w,w.run(event = evt))
    183          try:
    184              s.simulate(until=1)
    185          except FatalSimerror:
    186              pass
    187          else:
    188              self.fail("expected FatalSimerror")
    189
    190      ## -------------------------------------------------------------
    191      ## Test of "yield queueevent,self,evt"
    192      ## -------------------------------------------------------------
    193 -    def testQueueevent(self):
    194          s = Simulation()
    195          s.initialize()
    196          evt = SimEvent() ## wrong sim
    197          w = Queueeventtest(sim = s)
    198          s.activate(w,w.run(event = evt))
    199          try:
    200              s.simulate(until=1)
    201          except FatalSimerror:
    202              pass
    203          else:
    204              self.fail("expected FatalSimerror")
    205
    206 -def makesimSuite():
    207      suite = unittest.TestSuite()
    208      testRequest =  makesimtestcase('testRequest')
    209      testActivate = makesimtestcase('testActivate')
    210      testPutStore = makesimtestcase('testPutStore')
    211      testPutLevel = makesimtestcase('testPutLevel')
    212      testGetStore = makesimtestcase('testGetStore')
    213      testGetLevel = makesimtestcase('testGetLevel')
    214      testWaitevent = makesimtestcase('testWaitevent')
    215      testQueueevent = makesimtestcase('testQueueevent')
    216
    217      suite.addTests([testRequest, testActivate, testPutStore, testPutLevel,
    218                      testGetStore, testGetLevel, testWaitevent, testQueueevent])
    219      return suite
    220
    221  if __name__ == '__main__':
    222      alltests = unittest.TestSuite((makesimSuite()
    223                                  ))
    224      runner = unittest.TextTestRunner()
    225      runner.run(alltests)
    226