Advertisement
chertila

Untitled

Mar 25th, 2023
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. class MealyError(Exception):
  2.     def __init__(self, method_name):
  3.         self.method_name = method_name
  4.  
  5.  
  6. class Mealy:
  7.     def __init__(self):
  8.         self.state = 'A'
  9.  
  10.     def herd(self):
  11.         if self.state == 'A':
  12.             self.state = 'B'
  13.             return 0
  14.         elif self.state == 'B':
  15.             self.state = 'C'
  16.             return 2
  17.         elif self.state == 'D':
  18.             self.state = 'E'
  19.             return 5
  20.         elif self.state == 'E':
  21.             self.state = 'F'
  22.             return 7
  23.         elif self.state == 'F':
  24.             self.state = 'G'
  25.             return 8
  26.         else:
  27.             raise MealyError('herd')
  28.  
  29.     def stash(self):
  30.         if self.state == 'A':
  31.             self.state = 'C'
  32.             return 1
  33.         elif self.state == 'B':
  34.             self.state = 'G'
  35.             return 3
  36.         elif self.state == 'C':
  37.             self.state = 'D'
  38.             return 4
  39.         elif self.state == 'D':
  40.             self.state = 'G'
  41.             return 6
  42.         elif self.state == 'G':
  43.             self.state = 'C'
  44.             return 9
  45.         else:
  46.             raise MealyError('stash')
  47.  
  48.  
  49. def error(func, name):
  50.     try:
  51.         func()
  52.     except MealyError as error:
  53.         assert str(error) == name
  54.  
  55.  
  56. def test():
  57.     o = main()
  58.     o.state = "A"
  59.     assert o.herd() == 0
  60.     o.state = "A"
  61.     assert o.stash() == 1
  62.     o.state = "B"
  63.     assert o.herd() == 2
  64.     o.state = "B"
  65.     assert o.stash() == 3
  66.     o.state = "C"
  67.     assert o.stash() == 4
  68.     o.state = "C"
  69.     error(o.herd, "herd")
  70.     o.state = "D"
  71.     assert o.herd() == 5
  72.     o.state = "D"
  73.     assert o.stash() == 6
  74.     o.state = "E"
  75.     assert o.herd() == 7
  76.     o.state = "E"
  77.     error(o.stash, "stash")
  78.     o.state = "F"
  79.     assert o.herd() == 8
  80.     o.state = "F"
  81.     error(o.stash, "stash")
  82.     o.state = "G"
  83.     assert o.stash() == 9
  84.     o.state = "G"
  85.     error(o.stash, "heard")
  86.  
  87.  
  88. def main():
  89.     return Mealy()
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement