Advertisement
selebry

sdf

May 27th, 2023
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. class MealyError(Exception):
  2. pass
  3.  
  4.  
  5. class StateMachine:
  6. def __init__(self):
  7. self.state = 'A'
  8.  
  9. def wreck(self):
  10. if self.state == 'A':
  11. self.state = 'B'
  12. return 0
  13. if self.state == 'C':
  14. self.state = 'D'
  15. return 3
  16. if self.state == 'E':
  17. self.state = 'B'
  18. return 7
  19. if self.state == 'G':
  20. self.state = 'G'
  21. return 9
  22. raise MealyError('wreck')
  23.  
  24. def peep(self):
  25. if self.state == 'A':
  26. self.state = 'C'
  27. return 1
  28. if self.state == 'B':
  29. self.state = 'C'
  30. return 2
  31. if self.state == 'C':
  32. self.state = 'F'
  33. return 4
  34. if self.state == 'D':
  35. self.state = 'E'
  36. return 5
  37. if self.state == 'E':
  38. self.state = 'F'
  39. return 6
  40. if self.state == 'F':
  41. self.state = 'G'
  42. return 8
  43. raise MealyError('peep')
  44.  
  45.  
  46. def main():
  47. return StateMachine()
  48.  
  49.  
  50. def raises(func, error):
  51. output = None
  52. try:
  53. output = func()
  54. except Exception as e:
  55. assert type(e) == error
  56. assert output is None
  57.  
  58.  
  59. def test():
  60. o = main()
  61. assert o.wreck() == 0
  62. assert o.peep() == 2
  63. assert o.wreck() == 3
  64. assert o.peep() == 5
  65. assert o.wreck() == 7
  66. assert o.peep() == 2
  67. assert o.peep() == 4
  68. assert o.peep() == 8
  69. assert o.wreck() == 9
  70.  
  71. o = main()
  72. assert o.peep() == 1
  73. assert o.wreck() == 3
  74. raises(lambda: o.wreck(), MealyError)
  75. assert o.peep() == 5
  76. assert o.peep() == 6
  77. assert o.peep() == 8
  78. raises(lambda: o.peep(), MealyError)
  79.  
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement