aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/__pycache__/config.cpython-38.pycbin0 -> 462 bytes
-rw-r--r--src/__pycache__/test.cpython-38.pycbin0 -> 980 bytes
-rw-r--r--src/config.py40
-rwxr-xr-xsrc/main.py58
-rw-r--r--src/test/__init__.py13
-rw-r--r--src/test/__pycache__/__init__.cpython-38.pycbin0 -> 185 bytes
-rw-r--r--src/test/__pycache__/test.cpython-38.pycbin0 -> 1876 bytes
-rw-r--r--src/test/philo.py23
-rw-r--r--src/test/table.py23
-rw-r--r--src/test/test.py112
10 files changed, 269 insertions, 0 deletions
diff --git a/src/__pycache__/config.cpython-38.pyc b/src/__pycache__/config.cpython-38.pyc
new file mode 100644
index 0000000..0dd6446
--- /dev/null
+++ b/src/__pycache__/config.cpython-38.pyc
Binary files differ
diff --git a/src/__pycache__/test.cpython-38.pyc b/src/__pycache__/test.cpython-38.pyc
new file mode 100644
index 0000000..aec2223
--- /dev/null
+++ b/src/__pycache__/test.cpython-38.pyc
Binary files differ
diff --git a/src/config.py b/src/config.py
new file mode 100644
index 0000000..3da1285
--- /dev/null
+++ b/src/config.py
@@ -0,0 +1,40 @@
+# ############################################################################ #
+# #
+# ::: :::::::: #
+# config.py :+: :+: :+: #
+# +:+ +:+ +:+ #
+# By: charles <me@cacharle.xyz> +#+ +:+ +#+ #
+# +#+#+#+#+#+ +#+ #
+# Created: 2020/09/27 11:05:38 by charles #+# #+# #
+# Updated: 2020/09/27 11:51:41 by charles ### ########.fr #
+# #
+# ############################################################################ #
+
+import os
+
+
+# Location of your project directory
+PROJECT_PATH = "../philosophers"
+
+# Build your project before the test if set to True
+BUILD_BEFORE = True
+
+# Command to run before the test to build your project
+# `{path}` is replaced by the philosophers directory (e.g `../philo_one` `../philo_two`)
+BUILD_CMD = "make --no-print-directory -C {path}"
+
+################################################################################
+# Do not edit
+################################################################################
+
+PHILO_PATHS = [
+ os.path.join(PROJECT_PATH, "philo_one"),
+ os.path.join(PROJECT_PATH, "philo_two"),
+ os.path.join(PROJECT_PATH, "philo_three")
+]
+
+PHILO_EXEC_PATHS = [
+ os.path.join(PHILO_PATHS[0], "philo_one"),
+ os.path.join(PHILO_PATHS[1], "philo_two"),
+ os.path.join(PHILO_PATHS[2], "philo_three")
+]
diff --git a/src/main.py b/src/main.py
new file mode 100755
index 0000000..861bc8e
--- /dev/null
+++ b/src/main.py
@@ -0,0 +1,58 @@
+#!/bin/python3
+
+# ############################################################################ #
+# #
+# ::: :::::::: #
+# run :+: :+: :+: #
+# +:+ +:+ +:+ #
+# By: charles <me@cacharle.xyz> +#+ +:+ +#+ #
+# +#+#+#+#+#+ +#+ #
+# Created: 2020/09/27 11:36:34 by charles #+# #+# #
+# Updated: 2020/09/27 11:36:34 by charles ### ########.fr #
+# #
+# ############################################################################ #
+
+# invalid state switch
+# none existant fork
+# timestamp not in order
+# crash
+# should be infinity
+# print lines after died
+# bad output format
+# should be dead
+
+import os
+import sys
+import subprocess
+import argparse
+
+import config
+from test import Test
+
+
+def main():
+ parser = argparse.ArgumentParser(description="Philosophers test")
+ parser.add_argument("-p", "--philo", help="Id of the philosophers to test ",
+ required=True, type=int, choices=[1, 2, 3])
+ parser.add_argument("-b", "--build", help="Build and exit")
+ args = parser.parse_args()
+
+ if config.BUILD_BEFORE or args.build:
+ try:
+ print("=====================================BUILD======================================")
+ subprocess.run(config.BUILD_CMD.format(path=config.PHILO_PATHS[0]).split(' '), check=True)
+ print("================================================================================")
+ except subprocess.CalledProcessError:
+ sys.exit(1)
+ if args.build:
+ sys.exit(0)
+
+ if args.philo != 1:
+ sys.exit(1)
+
+ Test(10, 100, 100, 10)
+ Test.run_all(config.PHILO_EXEC_PATHS[0])
+ # print("yo")
+
+if __name__ == "__main__":
+ main()
diff --git a/src/test/__init__.py b/src/test/__init__.py
new file mode 100644
index 0000000..a6d7632
--- /dev/null
+++ b/src/test/__init__.py
@@ -0,0 +1,13 @@
+# ############################################################################ #
+# #
+# ::: :::::::: #
+# __init__.py :+: :+: :+: #
+# +:+ +:+ +:+ #
+# By: charles <me@cacharle.xyz> +#+ +:+ +#+ #
+# +#+#+#+#+#+ +#+ #
+# Created: 2020/09/27 13:00:31 by charles #+# #+# #
+# Updated: 2020/09/27 13:01:05 by charles ### ########.fr #
+# #
+# ############################################################################ #
+
+from test.test import Test
diff --git a/src/test/__pycache__/__init__.cpython-38.pyc b/src/test/__pycache__/__init__.cpython-38.pyc
new file mode 100644
index 0000000..37cb896
--- /dev/null
+++ b/src/test/__pycache__/__init__.cpython-38.pyc
Binary files differ
diff --git a/src/test/__pycache__/test.cpython-38.pyc b/src/test/__pycache__/test.cpython-38.pyc
new file mode 100644
index 0000000..ce4b3b7
--- /dev/null
+++ b/src/test/__pycache__/test.cpython-38.pyc
Binary files differ
diff --git a/src/test/philo.py b/src/test/philo.py
new file mode 100644
index 0000000..c103bec
--- /dev/null
+++ b/src/test/philo.py
@@ -0,0 +1,23 @@
+# ############################################################################ #
+# #
+# ::: :::::::: #
+# philo.py :+: :+: :+: #
+# +:+ +:+ +:+ #
+# By: charles <me@cacharle.xyz> +#+ +:+ +#+ #
+# +#+#+#+#+#+ +#+ #
+# Created: 2020/09/27 12:54:12 by charles #+# #+# #
+# Updated: 2020/09/27 12:59:50 by charles ### ########.fr #
+# #
+# ############################################################################ #
+
+class Philo:
+ def __init__(id_: int):
+ self.id = id_
+ self.last_event = None
+ self.last_eat_date = None
+
+ def add_log(self, match):
+ pass
+
+ def check(self):
+ return True
diff --git a/src/test/table.py b/src/test/table.py
new file mode 100644
index 0000000..6aa1b15
--- /dev/null
+++ b/src/test/table.py
@@ -0,0 +1,23 @@
+# ############################################################################ #
+# #
+# ::: :::::::: #
+# table.py :+: :+: :+: #
+# +:+ +:+ +:+ #
+# By: charles <me@cacharle.xyz> +#+ +:+ +#+ #
+# +#+#+#+#+#+ +#+ #
+# Created: 2020/09/27 12:44:48 by charles #+# #+# #
+# Updated: 2020/09/27 12:54:01 by charles ### ########.fr #
+# #
+# ############################################################################ #
+
+class Table:
+ def __init__(self, philo_num):
+ self._philos = [Philo(id_) for id_ in range(1, philo_num + 1)]
+
+ def update(self, match):
+ philo = itertools.first_true(self._philos, pred = lambda x: x.id == match.id)
+ philo.add_log(match)
+
+ def check(self):
+ return True
+
diff --git a/src/test/test.py b/src/test/test.py
new file mode 100644
index 0000000..7be1d18
--- /dev/null
+++ b/src/test/test.py
@@ -0,0 +1,112 @@
+# ############################################################################ #
+# #
+# ::: :::::::: #
+# test.py :+: :+: :+: #
+# +:+ +:+ +:+ #
+# By: charles <me@cacharle.xyz> +#+ +:+ +#+ #
+# +#+#+#+#+#+ +#+ #
+# Created: 2020/09/27 11:36:32 by charles #+# #+# #
+# Updated: 2020/09/27 13:28:15 by charles ### ########.fr #
+# #
+# ############################################################################ #
+
+import re
+import subprocess
+# import threading
+
+# from result import Result
+
+
+class Test: #(threading.Thread):
+ _tests = []
+ _exec_path = None
+ # _stdout_lock = threading.Lock()
+
+ @classmethod
+ def run_all(cls, exec_path: str):
+ cls._exec_path = exec_path
+ for t in cls._tests:
+ t.run()
+ # threads = [test.start() for test in cls._tests]
+ # for thread in threads:
+ # thread.join()
+
+ def __init__(
+ self,
+ philo_num: int,
+ timeout_die: int,
+ timeout_eat: int,
+ timeout_sleep: int,
+ meal_num: int = None,
+ should_fail: bool = False
+ ):
+ self._philo_num = philo_num
+ self._timeout_die = timeout_die
+ self._timeout_eat = timeout_eat
+ self._timeout_sleep = timeout_sleep
+ self._meal_num = meal_num
+ Test._tests.append(self)
+ # threading.Thread.__init__(self)
+
+ def run(self):
+ argv = [
+ Test._exec_path,
+ str(self._philo_num),
+ str(self._timeout_die),
+ str(self._timeout_eat),
+ str(self._timeout_sleep)
+ ]
+ if self._meal_num is not None:
+ argv.append(str(self._meal_num))
+ # try:
+ process_result = subprocess.run(
+ argv,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT
+ )
+ output = process_result.stdout.decode()
+ print(output)
+ # timeout=1,
+ # except subprocess.TimeoutExpered as err:
+ # return Result(err)
+ # except subprocess.CalledProcessError as err:
+ # return Result(err)
+
+ self._check_output(output)
+ # return Result()
+
+ def _check_output(self, stream):
+ # table = Table(self._philo_num)
+
+ for line in stream.split('\n'):
+ match = re.match(
+ "^(?P<timestamp>\d+) "
+ "(?P<id>\d+) "
+ "(?P<event>is thinking|is eating|is sleeping|died)$",
+ line
+ )
+ if match is None:
+ print("Bad line format |{}|".format(line))
+ return
+
+ print(match.group('event'))
+ try:
+ timestamp = int(match.group("timestamp"))
+ # if timestamp < 0
+ except ValueError:
+ print("Invalid timestamp value {}".format(match.group("timestamp")))
+
+ table.add_log(match)
+
+ # philo_states.append(Philo)
+
+ # def _print(self, *args):
+ # Test._stdout_lock.aquire()
+ # print(*args)
+ # Test._stdout_lock.release()
+
+# from enum import Enum
+# class PhiloEvent(Enum):
+# EATING = 1
+# SLEEPING = 2
+# THINKING = 3