aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/philo/philo.py13
-rw-r--r--src/philo/table.py18
-rw-r--r--src/suite.py23
3 files changed, 29 insertions, 25 deletions
diff --git a/src/philo/philo.py b/src/philo/philo.py
index 3319a0b..3565c41 100644
--- a/src/philo/philo.py
+++ b/src/philo/philo.py
@@ -6,7 +6,7 @@
# By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/10/01 10:52:56 by cacharle #+# #+# #
-# Updated: 2020/10/05 14:03:07 by cacharle ### ########.fr #
+# Updated: 2020/12/30 14:29:30 by charles ### ########.fr #
# #
# ############################################################################ #
@@ -25,7 +25,7 @@ class Philo:
timeout_sleep: int,
meal_num: int
):
- self.logs = []
+ self.logs = []
self.id = id_
self._timeout_die = timeout_die
self._timeout_eat = timeout_eat
@@ -56,10 +56,7 @@ class Philo:
# check log event number
grouped = [(e, list(g)) for e, g in itertools.groupby(self.logs, (lambda x: x.event))]
for e, g in grouped[:-1]:
- if e is Event.EAT:
- if len(g) != self._meal_num:
- self._raise("Should eat {} times".format(self._meal_num))
- elif e is Event.FORK:
+ if e is Event.FORK:
if len(g) != 2:
self._raise("Should take fork 2 times")
elif len(g) != 1:
@@ -127,3 +124,7 @@ class Philo:
else:
return 1
return 0
+
+ @property
+ def meal_num_finished(self):
+ return len([log for log in self.logs if log.event is Event.EAT]) >= self._meal_num
diff --git a/src/philo/table.py b/src/philo/table.py
index 66eaa9d..804608b 100644
--- a/src/philo/table.py
+++ b/src/philo/table.py
@@ -6,13 +6,10 @@
# By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/10/01 10:53:29 by cacharle #+# #+# #
-# Updated: 2020/10/05 14:03:35 by cacharle ### ########.fr #
+# Updated: 2020/12/30 14:26:37 by charles ### ########.fr #
# #
# ############################################################################ #
-
-import itertools
-
from philo import Philo
from philo import error
from philo.event import Event
@@ -31,20 +28,21 @@ class Table:
for id_ in range(1, philo_num + 1)]
self._logs = []
self._philo_num = philo_num
- self.dead = False
+ self.finished = False
+ self._meal_num = meal_num
def add_log(self, log):
""" Add a log to the correct philosopher
- Set the dead flag if it's a death log
+ Set the finished flag if it's a death log
"""
self._logs.append(log)
philo = next(p for p in self._philos if p.id == log.id)
philo.logs.append(log)
# move
- if self.dead:
+ if self.finished:
raise error.Log(self._logs, "Output after death")
- if log.event is Event.DIE:
- self.dead = True
+ if log.event is Event.DIE or all(p.meal_num_finished for p in self._philos):
+ self.finished = True
def check(self):
""" Check global logs and all philosophers logs for errors
@@ -54,7 +52,7 @@ class Table:
- Timestamps should be in increasing order
"""
- if self.dead:
+ if self.finished:
return
for p in self._philos:
p.check()
diff --git a/src/suite.py b/src/suite.py
index dd087e6..0491dd9 100644
--- a/src/suite.py
+++ b/src/suite.py
@@ -6,7 +6,7 @@
# By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/10/01 10:41:43 by cacharle #+# #+# #
-# Updated: 2020/10/05 13:49:30 by cacharle ### ########.fr #
+# Updated: 2020/12/30 14:15:55 by charles ### ########.fr #
# #
# ############################################################################ #
@@ -40,15 +40,20 @@ def suite():
Test.new_error(["10", "10", "10", str(-config.UINT_MAX)])
Test.new_error(["10", "10", "10", "10", str(-config.UINT_MAX)])
- Test(0, 100, 10, 10)
- Test(1, 100, 10, 10)
+ Test(0, 100, 100, 100)
+ Test(1, 100, 100, 100)
- Test(2, 100, 50, 50)
- Test(3, 100, 50, 50)
- Test(4, 100, 50, 50)
- Test(5, 100, 50, 50)
- Test(6, 100, 50, 50)
- Test(7, 100, 50, 50)
+ Test(2, 200, 100, 100)
+ Test(3, 200, 100, 100)
+ Test(4, 200, 100, 100)
+ Test(5, 200, 100, 100)
+ Test(6, 200, 100, 100)
+ Test(7, 200, 100, 100)
+
+ Test(5, 800, 200, 200)
+ Test(5, 800, 200, 200, 7)
+ Test(4, 410, 200, 200)
+ Test(4, 310, 200, 100)
# Test(100, 100, 50, 50)
#