diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..fd17cc9 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,2 @@ +[pytest] +addopts = --ignore=examples --ignore=docs --doctest-modules \ No newline at end of file diff --git a/src/cst_python/__init__.py b/src/cst_python/__init__.py index 0fa67dc..756ee08 100644 --- a/src/cst_python/__init__.py +++ b/src/cst_python/__init__.py @@ -1,5 +1,4 @@ from . import python from . import core -from .core.entities import Codelet, Mind, MemoryObject -from .python.manager import * \ No newline at end of file +from .core.entities import Codelet, Mind, MemoryObject \ No newline at end of file diff --git a/src/cst_python/core/entities/__init__.py b/src/cst_python/core/entities/__init__.py index fb71558..0a004b8 100644 --- a/src/cst_python/core/entities/__init__.py +++ b/src/cst_python/core/entities/__init__.py @@ -7,4 +7,5 @@ from .memory_container import MemoryContainer from .rest_memory_container import RESTMemoryContainer from .rest_memory_object import RESTMemoryObject -from .mind import Mind \ No newline at end of file +from .mind import Mind +from .raw_memory import RawMemory \ No newline at end of file diff --git a/src/cst_python/core/entities/codelet.py b/src/cst_python/core/entities/codelet.py index 27ef6ea..749097e 100644 --- a/src/cst_python/core/entities/codelet.py +++ b/src/cst_python/core/entities/codelet.py @@ -26,7 +26,7 @@ def __init__(self) -> None: self._time_step = 300 self._enabled = True self._enable_count = 0 - self._name = threading.currentThread().name+"|"+type(self).__name__+str(Codelet._last_id) + self._name = threading.current_thread().name+"|"+type(self).__name__+str(Codelet._last_id) self._last_start_time = 0.0 self._lock = threading.RLock() self._activation = 0.0 @@ -82,8 +82,13 @@ def activation(self) -> float: @activation.setter def activation(self, value:float): if value > 1.0 or value < 0.0: + if value > 1.0: + self._activation = 1.0 + else: + self._activation = 0.0 + raise ValueError(f"Codelet activation must be in (0.0 , 1.0) \ - (value {value} is not allowed).") +(value {value} is not allowed).") self._activation = value @@ -115,9 +120,14 @@ def threshold(self) -> float: #@alias.alias("set_threshold", "setThreshold") @threshold.setter def threshold(self, value:float): - if value > 1.0 or value < 1.0: + if value > 1.0 or value < 0.0: + if value > 1.0: + self._threshold = 1.0 + else: + self._threshold = 0.0 + raise ValueError(f"Codelet threshold must be in (0.0 , 1.0) \ - (value {value} is not allowed).") +(value {value} is not allowed).") self._threshold = value @@ -145,12 +155,12 @@ def broadcast(self, value:List[Memory]) -> None: #@alias.alias("IsProfiling") @property - def is_profiling(self) -> bool: + def profiling(self) -> bool: return self._is_profiling #@alias.alias("set_profiling", "setProfiling") - @is_profiling.setter - def is_profiling(self, value:bool): + @profiling.setter + def profiling(self, value:bool): if value is True: raise NotImplementedError("Profiling is not implemented") @@ -299,7 +309,7 @@ def add_broadcasts(self, memories:List[Memory]) -> None: #@alias.alias("getThreadName") def get_thread_name(self) -> str: - return threading.currentThread().name + return threading.current_thread().name #@alias.alias("to_string", "toString") def __str__(self) -> str: @@ -308,16 +318,18 @@ def __str__(self) -> str: result = f"Codelet [activation={self._activation}, name={self._name}, " if self._broadcast is not None: - result += self._broadcast[min(len(self._broadcast), max_len)] + result += "broadcast=" + result += str(self._broadcast[:min(len(self._broadcast), max_len)]) result += ", " - + if self._inputs is not None: - result += self._inputs[min(len(self._outputs), max_len)] + result += "inputs=" + result += str(self._inputs[:min(len(self.inputs), max_len)]) result += ", " if self._outputs is not None: - result += self._outputs[min(len(self._outputs), max_len)] - result += ", " + result += "outputs=" + result += str(self._outputs[:min(len(self._outputs), max_len)]) result += "]" @@ -414,7 +426,7 @@ def notify_codelet(self) -> None: self._raise_exception() except Exception as e: - #Logging + #TODO Logging pass finally: diff --git a/src/cst_python/core/entities/memory.py b/src/cst_python/core/entities/memory.py index bbaa0a6..15da8a9 100644 --- a/src/cst_python/core/entities/memory.py +++ b/src/cst_python/core/entities/memory.py @@ -61,10 +61,6 @@ def get_id(self) -> int: def set_id(self, memory_id:int) -> None: ... - #@alias.alias("getTimestamp") - @abc.abstractmethod - def get_timestamp(self) -> int: - ... def compare_name(self, other_name:str) -> bool: diff --git a/tests/core/__init__.py b/tests/core/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/core/entities/CoalitionTest.py b/tests/core/entities/CoalitionTest.py new file mode 100644 index 0000000..64459e5 --- /dev/null +++ b/tests/core/entities/CoalitionTest.py @@ -0,0 +1,15 @@ +import unittest + +class Test(unittest.TestCase): + def setUp(self) -> None: + super().setUp() + + def tearDown(self) -> None: + super().tearDown() + + @classmethod + def tearDownClass(cls): + ... + + def test_(self) -> None: + ... \ No newline at end of file diff --git a/tests/core/entities/CodeRackTest.py b/tests/core/entities/CodeRackTest.py new file mode 100644 index 0000000..64459e5 --- /dev/null +++ b/tests/core/entities/CodeRackTest.py @@ -0,0 +1,15 @@ +import unittest + +class Test(unittest.TestCase): + def setUp(self) -> None: + super().setUp() + + def tearDown(self) -> None: + super().tearDown() + + @classmethod + def tearDownClass(cls): + ... + + def test_(self) -> None: + ... \ No newline at end of file diff --git a/tests/core/entities/CodeletContainerTest.py b/tests/core/entities/CodeletContainerTest.py new file mode 100644 index 0000000..64459e5 --- /dev/null +++ b/tests/core/entities/CodeletContainerTest.py @@ -0,0 +1,15 @@ +import unittest + +class Test(unittest.TestCase): + def setUp(self) -> None: + super().setUp() + + def tearDown(self) -> None: + super().tearDown() + + @classmethod + def tearDownClass(cls): + ... + + def test_(self) -> None: + ... \ No newline at end of file diff --git a/tests/core/entities/CodeletTest.py b/tests/core/entities/CodeletTest.py new file mode 100644 index 0000000..4e7a6ee --- /dev/null +++ b/tests/core/entities/CodeletTest.py @@ -0,0 +1,313 @@ +from contextlib import redirect_stdout +import math +import unittest +import time +import threading +import io + +from cst_python import MemoryObject, Mind +from .utils import CodeletMock + +class TestCodelet(unittest.TestCase): + def setUp(self) -> None: + self.test_codelet = CodeletMock() + + + def test_get_is_loop_test(self) -> None: + # Any instantiated Codelet, if not changed, should be looping + assert self.test_codelet.loop == True + + + def test_upper_activation_bound_exception(self) -> None: + + with self.assertRaises(ValueError) as ve: + self.test_codelet.activation = 2.0 + + assert str(ve.exception) == "Codelet activation must be in (0.0 , 1.0) (value 2.0 is not allowed)." + + assert math.isclose(1.0, self.test_codelet.activation) + + + def test_lowerActivationBoundException(self) -> None: + with self.assertRaises(ValueError) as ve: + self.test_codelet.activation = -0.8 + + assert str(ve.exception) == "Codelet activation must be in (0.0 , 1.0) (value -0.8 is not allowed)." + + assert math.isclose(0.0, self.test_codelet.activation) + + + + def test_setInputs(self) -> None: + dummy_inputs : list[MemoryObject] = [MemoryObject(), MemoryObject()] + self.test_codelet.inputs = dummy_inputs + self.assertEqual(2, len(self.test_codelet.inputs)) + + + + def test_getInput(self) -> None: + + dummy_inputs : list[MemoryObject] = [MemoryObject(), MemoryObject()] + dummy_inputs[0].set_name("testName1") + self.test_codelet.inputs = dummy_inputs + + + self.assertEqual(dummy_inputs[0], self.test_codelet.get_input(name="testName1")) + + + + def test_getInputNull(self) -> None: + + dummy_inputs : list[MemoryObject] = [MemoryObject(), MemoryObject()] + self.test_codelet.inputs = dummy_inputs + + self.assertIsNone(self.test_codelet.get_input(name="testName2")) + + + + def test_add_inputs(self) -> None: + + dummy_inputs : list[MemoryObject] = [MemoryObject(), MemoryObject()] + self.test_codelet.add_inputs(dummy_inputs) + self.assertEqual(2, len(self.test_codelet.inputs)) + + + + + def test_removes_input(self) -> None: + + to_remove = MemoryObject() + + self.test_codelet.add_input(to_remove) + self.assertEqual(1, len(self.test_codelet.inputs)) + + self.test_codelet.removes_input(to_remove) + self.assertEqual(0, len(self.test_codelet.inputs)) + + + + def test_remove_from_input(self) -> None: + + to_remove = [MemoryObject(), MemoryObject()] + + self.test_codelet.add_inputs(to_remove) + self.assertEqual(2, len(self.test_codelet.inputs)) + + self.test_codelet.remove_from_input(to_remove) + self.assertEqual(0, len(self.test_codelet.inputs)) + + + + def test_remove_from_output(self) -> None: + + to_remove = [MemoryObject(), MemoryObject()] + + self.test_codelet.add_outputs(to_remove) + self.assertEqual(2, len(self.test_codelet.outputs)) + + self.test_codelet.remove_from_output(to_remove) + self.assertEqual(0, len(self.test_codelet.outputs)) + + + + def test_add_outputs(self) -> None: + + dummy_outputs : list[MemoryObject] = [MemoryObject(), MemoryObject()] + self.test_codelet.add_outputs(dummy_outputs) + self.assertEqual(2, len(self.test_codelet.outputs)) + + + + def test_get_outputs(self) -> None: + + dummy_outputs : list[MemoryObject] = [MemoryObject(), MemoryObject()] + self.test_codelet.add_outputs(dummy_outputs) + self.assertEqual(dummy_outputs, self.test_codelet.outputs) + + + + def test_get_output(self) -> None: + + dummy_outputs : list[MemoryObject] = [MemoryObject(), MemoryObject()] + dummy_outputs[0].set_name("testName3") + self.test_codelet.add_outputs(dummy_outputs) + self.assertEqual(dummy_outputs[0], self.test_codelet.get_output(name="testName3")) + + + + def test_get_outputNullReturn(self) -> None: + + dummy_outputs : list[MemoryObject] = [MemoryObject(), MemoryObject()] + self.test_codelet.add_outputs(dummy_outputs) + self.assertIsNone(self.test_codelet.get_output("testName4")) + + + + def test_get_outputEnableFalse(self) -> None: + self.test_codelet.time_step = 100 + #with self.assertRaises(Exception): #TODO Fix test after Java correction + with redirect_stdout(io.StringIO()): + self.test_codelet.name = "thisCodeletWillFail" + dummy_outputs : list[MemoryObject] = [MemoryObject(), MemoryObject()] + self.test_codelet.outputs = dummy_outputs + + + self.test_codelet.get_output("testType", 3) # This line will raise an exception + + + mind = Mind() + mind.insert_codelet(self.test_codelet) + mind.start() + time.sleep(0.1) + + mind.shutdown() + + + self.assertFalse(self.test_codelet.enabled) + self.test_codelet.enabled = True + self.assertTrue(self.test_codelet.enabled) + + + + def test_set_outputs(self) -> None: + + dummy_outputs : list[MemoryObject] = [MemoryObject(), MemoryObject()] + self.test_codelet.outputs = dummy_outputs + self.assertEqual(2, len(self.test_codelet.outputs)) + + + + def test_getInputsOfType(self) -> None: + dummy_inputs : list[MemoryObject] = [MemoryObject(), MemoryObject(), MemoryObject(), MemoryObject()] + + dummy_inputs[0].set_name("toGet") + dummy_inputs[1].set_name("toGet") + + self.test_codelet.add_inputs(dummy_inputs) + self.assertEqual(2, len(self.test_codelet.get_inputs_of_type("toGet"))) + + + + def test_get_outputs_of_type(self) -> None: + + dummy_outputs : list[MemoryObject] = [MemoryObject(), MemoryObject(), MemoryObject(), MemoryObject()] + + dummy_outputs[0].set_name("toGet") + dummy_outputs[1].set_name("toGet") + + self.test_codelet.add_outputs(dummy_outputs) + self.assertEqual(2, len(self.test_codelet.get_outputs_of_type("toGet"))) + + + + def test_get_broadcastNull(self) -> None: + + self.assertIsNone(self.test_codelet.get_broadcast("testName5")) + + + + def test_get_broadcastType(self) -> None: + dummy_outputs : list[MemoryObject] = [MemoryObject(), MemoryObject()] + dummy_outputs[0].set_name("testName6") + self.test_codelet.add_broadcasts(dummy_outputs) + self.assertEqual(dummy_outputs[0], self.test_codelet.get_broadcast("testName6", 0)) + + + + def test_get_broadcastTypeIndex(self) -> None: + dummy_outputs : list[MemoryObject] = [MemoryObject(), MemoryObject()] + dummy_outputs[0].set_name("testName") + dummy_outputs[1].set_name("testName") + self.test_codelet.add_broadcasts(dummy_outputs) + self.assertEqual(dummy_outputs[1], self.test_codelet.get_broadcast("testName", 1)) + + + + def test_addBroadcasts(self) -> None: + dummy_outputs : list[MemoryObject] = [MemoryObject(), MemoryObject()] + self.test_codelet.add_broadcasts(dummy_outputs) + self.assertEqual(2, len(self.test_codelet.broadcast)) + + + + def test_get_thread_name(self) -> None: + threading.current_thread().name = "newThreadName" + self.assertEqual("newThreadName", self.test_codelet.get_thread_name()) + + + + def test_toString(self) -> None: + + dummy_inputs : list[MemoryObject] = [MemoryObject(), MemoryObject()] + dummy_broadcasts : list[MemoryObject] = [MemoryObject(), MemoryObject()] + + expected_string = ("Codelet [activation=" + str(0.5) + ", " + "name=" + "testName" + ", " + + ("broadcast=" + str(dummy_broadcasts[0:min(len(dummy_broadcasts), 10)]) + ", ") + + ("inputs=" + str(dummy_inputs[0:min(len(dummy_inputs), 10)])) + ", " + + ("outputs=" + "[]") + "]") + + self.test_codelet.name = "testName" + + self.test_codelet.activation = 0.5 + + self.test_codelet.inputs = dummy_inputs + self.test_codelet.broadcast = dummy_broadcasts + + self.assertEqual(expected_string, str(self.test_codelet)) + + + + def test_setThreshold(self) -> None: + self.test_codelet.threshold = 0.5 + + + assert math.isclose(0.5, self.test_codelet.threshold) + + + + def test_upperThresholdBound(self) -> None: + + with self.assertRaises(ValueError) as ve: + self.test_codelet.threshold = 2.0 + + assert str(ve.exception) == "Codelet threshold must be in (0.0 , 1.0) (value 2.0 is not allowed)." + + assert math.isclose(1.0, self.test_codelet.threshold) + + + + + def test_lowerThresholdBound(self) -> None: + + with self.assertRaises(ValueError) as ve: + self.test_codelet.threshold = -1.0 + + assert str(ve.exception) == "Codelet threshold must be in (0.0 , 1.0) (value -1.0 is not allowed)." + + assert math.isclose(0.0, self.test_codelet.threshold) + + + + def test_getTimeStep(self) -> None: + + self.test_codelet.time_step = 222 + self.assertEqual(222, self.test_codelet.time_step) + + + + @unittest.skip("Codelet profiling not implemented") + def test_runProfiling(self) -> None: + + self.test_codelet.profiling = True + self.test_codelet.time_step = 100 + + with redirect_stdout(io.StringIO()): + mind = Mind() + mind.insert_codelet(self.test_codelet) + mind.start() + + time.sleep(0.1) + + + self.assertTrue(self.test_codelet.profiling) + diff --git a/tests/core/entities/DisconnectedCodeletTest.py b/tests/core/entities/DisconnectedCodeletTest.py new file mode 100644 index 0000000..64459e5 --- /dev/null +++ b/tests/core/entities/DisconnectedCodeletTest.py @@ -0,0 +1,15 @@ +import unittest + +class Test(unittest.TestCase): + def setUp(self) -> None: + super().setUp() + + def tearDown(self) -> None: + super().tearDown() + + @classmethod + def tearDownClass(cls): + ... + + def test_(self) -> None: + ... \ No newline at end of file diff --git a/tests/core/entities/MemoryBufferTest.py b/tests/core/entities/MemoryBufferTest.py new file mode 100644 index 0000000..64459e5 --- /dev/null +++ b/tests/core/entities/MemoryBufferTest.py @@ -0,0 +1,15 @@ +import unittest + +class Test(unittest.TestCase): + def setUp(self) -> None: + super().setUp() + + def tearDown(self) -> None: + super().tearDown() + + @classmethod + def tearDownClass(cls): + ... + + def test_(self) -> None: + ... \ No newline at end of file diff --git a/tests/core/entities/MemoryContainerTest.py b/tests/core/entities/MemoryContainerTest.py new file mode 100644 index 0000000..64459e5 --- /dev/null +++ b/tests/core/entities/MemoryContainerTest.py @@ -0,0 +1,15 @@ +import unittest + +class Test(unittest.TestCase): + def setUp(self) -> None: + super().setUp() + + def tearDown(self) -> None: + super().tearDown() + + @classmethod + def tearDownClass(cls): + ... + + def test_(self) -> None: + ... \ No newline at end of file diff --git a/tests/core/entities/MemoryObjectTest.py b/tests/core/entities/MemoryObjectTest.py new file mode 100644 index 0000000..18052cc --- /dev/null +++ b/tests/core/entities/MemoryObjectTest.py @@ -0,0 +1,150 @@ +import unittest + +from cst_python import MemoryObject, Mind + +class MemoryObjectTest(unittest.TestCase): + def setUp(self) -> None: + self.mo = MemoryObject() + + def test_id(self) -> None: + self.mo.set_id(2000) + + assert 2000 == self.mo.get_id() + + def test_to_string(self) -> None: + I = object() + + self.mo.set_id(2000) + self.mo.set_evaluation(0.8) + self.mo.set_info(I) + self.mo.set_type("testName") + + expected_string = f'''MemoryObject [idmemoryobject={2000}, timestamp={self.mo.get_timestamp()}, evaluation={0.8}, I={I}, name={"testName"}]''' + + assert expected_string == str(self.mo) + + def test_hash_code(self): + I = object() + self.mo_eval = 0.8 + self.mo_id = 2000 + name = "test_name" + + self.mo.set_id(self.mo_id) + self.mo.set_evaluation(self.mo_eval) + self.mo.set_info(I) + self.mo.set_type(name) + + prime = 31 + excepted_value = 1 + excepted_value = prime * excepted_value + (hash(I)) + excepted_value = prime * excepted_value + (hash(self.mo_eval)) + excepted_value = prime * excepted_value + (hash(self.mo_id)) + excepted_value = prime * excepted_value + (hash(name)) + excepted_value = prime * excepted_value + (0 if self.mo.get_timestamp() is None else hash(self.mo.get_timestamp())) + + #Python truncates the __hash__ return if is too long hashing it, so here we need hash(expected_value) + assert hash(excepted_value) == hash(self.mo) + + def test_equals(self): + other_mo = MemoryObject() + third_mo = MemoryObject() + fourth_mo = MemoryObject() + + self.mo.set_info(0.0) + other_mo.set_info(0.0) + third_mo.set_info(1.0) + + assert self.mo != fourth_mo + assert self.mo != third_mo + + self.mo.set_evaluation(0.0) + other_mo.set_evaluation(0.0) + third_mo.set_evaluation(1.0) + + fourth_mo.set_info(0.0) + fourth_mo.set_evaluation(None) + + assert self.mo != fourth_mo + assert self.mo != third_mo + + self.mo.set_id(1000) + other_mo.set_id(2000) + third_mo.set_id(2000) + + + fourth_mo.set_evaluation(0.0) + fourth_mo.set_id(None) + + assert fourth_mo != self.mo + assert self.mo != other_mo + + other_mo.set_id(1000) + fourth_mo.set_id(1000) + + self.mo.set_type("firstName") + other_mo.set_type("firstName") + third_mo.set_type("secondName") + + assert fourth_mo != self.mo + assert self.mo != third_mo + + fourth_mo.set_type("firstName") + + self.mo.timestamp = 100 + other_mo.timestamp = 100 + third_mo.timestamp = 200 + fourth_mo.timestamp = None + + assert fourth_mo != self.mo + assert self.mo != third_mo + + fourth_mo.timestamp = 200 + assert fourth_mo != self.mo + + assert self.mo == other_mo + + def test_equals_false_None(self) -> None: + other_mo = MemoryObject() + third_mo = MemoryObject() + fourth_mo = MemoryObject() + + self.mo.set_info(0.0) + other_mo.set_info(0.0) + third_mo.set_info(1.0) + + assert fourth_mo != self.mo + assert self.mo != third_mo + + self.mo.set_evaluation(0.0) + other_mo.set_evaluation(0.0) + third_mo.set_evaluation(1.0) + + assert fourth_mo != self.mo + assert self.mo != third_mo + + + self.mo.set_id(100) + other_mo.set_id(100) + third_mo.set_id(200) + + assert fourth_mo != self.mo + assert self.mo != third_mo + + + self.mo.set_type("firstName") + other_mo.set_type("firstName") + third_mo.set_type("secondName") + + assert fourth_mo != self.mo + assert self.mo != third_mo + + + self.mo.timestamp = 10 + other_mo.timestamp = 10 + third_mo.timestamp = 20 + fourth_mo.timestamp = None + + assert fourth_mo != self.mo + assert self.mo != third_mo + + assert self.mo == other_mo \ No newline at end of file diff --git a/tests/core/entities/MemoryTest.py b/tests/core/entities/MemoryTest.py new file mode 100644 index 0000000..4e68f76 --- /dev/null +++ b/tests/core/entities/MemoryTest.py @@ -0,0 +1,82 @@ +import math +import unittest +from typing import Any + +from cst_python.core.entities import Memory, MemoryObject, MemoryObserver + +class MemorySubclass(Memory): + def __init__(self) -> None: + super().__init__() + + self.I = None + self.evaluation = 0.0 + self.name = "" + self.timestamp = 10 + self.id = None + + def get_id(self) -> int: + return self.id + + def set_id(self, memory_id: int) -> None: + self.id = memory_id + + def get_info(self) -> Any: + return self.I + + def set_info(self, value: Any) -> int: + self.I = value + + def get_evaluation(self) -> float: + return self.evaluation + + def get_name(self) -> str: + return self.name + + def set_name(self, name: str) -> None: + self.name = name + + def set_evaluation(self, evaluation: float) -> None: + self.evaluation = evaluation + + def get_timestamp(self) -> int: + return self.timestamp + + def add_memory_observer(self, observer: MemoryObserver) -> None: + # TODO copy from CST when implemented + pass + + def remove_memory_observer(self, observer: MemoryObserver) -> None: + # TODO copy from CST when implemented + pass + +class MemoryTest(unittest.TestCase): + def setUp(self) -> None: + self.test_memory = MemorySubclass() + super().setUp() + + def test_get_set_info(self) -> None: + assert self.test_memory.get_info() is None + + test_value = 100.0 + self.test_memory.set_info(test_value) + + assert math.isclose(test_value, self.test_memory.get_info()) + + test_list : list[Memory] = [MemoryObject(), MemoryObject()] + self.test_memory.set_info(test_list) + + assert test_list == self.test_memory.get_info() + + + + def test_get_set_eval(self) -> None: + + test_value = 100.0 + self.test_memory.set_evaluation(test_value) + + assert math.isclose(test_value, self.test_memory.get_evaluation()) + + + def test_get_timestamp(self) -> None: + assert 10 == self.test_memory.get_timestamp() + \ No newline at end of file diff --git a/tests/core/entities/MindTest.py b/tests/core/entities/MindTest.py new file mode 100644 index 0000000..d23628d --- /dev/null +++ b/tests/core/entities/MindTest.py @@ -0,0 +1,55 @@ +import unittest + +from cst_python import Mind, MemoryObject +from .utils import CodeletMock + + +class MindTest(unittest.TestCase): + def setUp(self) -> None: + self.test_codelet = CodeletMock() + self.mind = Mind() + + def test_create_codelet_group(self) -> None: + self.mind.create_codelet_group("testGroup") + + assert "testGroup" in self.mind.codelet_groups + + + def test_create_memory_group(self) -> None: + self.mind.create_memory_group("testGroup") + + assert "testGroup" in self.mind.memory_groups + + + def test_insert_codelet_group(self) -> None: + self.mind.create_codelet_group("testGroup") + self.mind.insert_codelet(self.test_codelet, "testGroup") + + + assert 1 == len(self.mind.code_rack.all_codelets) + assert "testGroup" in self.mind.codelet_groups + assert self.test_codelet == self.mind.get_codelet_group_list("testGroup")[0] + + def test_register_memory_group(self) -> None: + mo = MemoryObject() + + self.mind.create_memory_group("testGroup") + self.mind.register_memory(mo, "testGroup") + + assert "testGroup" in self.mind.memory_groups + assert mo == self.mind.memory_groups["testGroup"][0] + assert 1 == len(self.mind.get_memory_group_list("testGroup")) + + + def test_register_memory_by_name(self) -> None: + mo = MemoryObject() + mo.set_name("testName") + + self.mind.create_memory_group("testGroup") + self.mind.raw_memory.add_memory(mo) + self.mind.register_memory("testName", "testGroup") + + assert "testGroup" in self.mind.memory_groups + assert mo == self.mind.memory_groups.get("testGroup")[0] + assert 1 == len(self.mind.memory_groups.get("testGroup")) + \ No newline at end of file diff --git a/tests/core/entities/RawMemoryTest.py b/tests/core/entities/RawMemoryTest.py new file mode 100644 index 0000000..52f55af --- /dev/null +++ b/tests/core/entities/RawMemoryTest.py @@ -0,0 +1,55 @@ +import unittest +import io +from contextlib import redirect_stdout + +from cst_python.core.entities import RawMemory, MemoryObject, Memory + +class TestRawMemory(unittest.TestCase): + def setUp(self) -> None: + self.raw_memory = RawMemory() + + + def test_getAllOfType(self) -> None: + test_list : list[Memory] = [MemoryObject(), MemoryObject(), MemoryObject(), MemoryObject()] + test_list[0].set_name("TYPE") + test_list[1].set_name("TYPE") + self.raw_memory.all_memories = test_list + + assert 2 == len(self.raw_memory.get_all_of_type("TYPE")) + assert test_list[0:2] == self.raw_memory.get_all_of_type("TYPE") + + + def test_printContent(self) -> None: + mem = MemoryObject() + mem.set_name("TYPE") + self.raw_memory.add_memory(mem) + expected_message = f'''MemoryObject [idmemoryobject={mem.get_id()}, timestamp={mem.get_timestamp()}, evaluation={0.0}, I={None}, name={"TYPE"}]''' + + with redirect_stdout(io.StringIO()) as f: + self.raw_memory.print_content() + + printed = f.getvalue().splitlines()[0] + + assert printed == expected_message + + + + def test_createAndDestroyMemoryObject(self) -> None: + self.raw_memory.create_memory_object("TYPE") + + assert 1 == len(self.raw_memory) + self.raw_memory.destroy_memory(self.raw_memory.all_memories[0]) + + assert 0 == len(self.raw_memory) + + + + def test_shutdown(self) -> None: + test_list : list[Memory] = [MemoryObject(), MemoryObject(), MemoryObject(), MemoryObject()] + self.raw_memory.all_memories = test_list + + assert 4 == len(self.raw_memory) + + self.raw_memory.shutdown() + assert 0 == len(self.raw_memory) + \ No newline at end of file diff --git a/tests/core/entities/__init__.py b/tests/core/entities/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/core/entities/utils.py b/tests/core/entities/utils.py new file mode 100644 index 0000000..6b229bb --- /dev/null +++ b/tests/core/entities/utils.py @@ -0,0 +1,11 @@ +from cst_python import Codelet + +class CodeletMock(Codelet): + def access_memory_objects(self) -> None: #NOSONAR + pass + + def calculate_activation(self) -> None: #NOSONAR + pass + + def proc(self) -> None: #NOSONAR + pass \ No newline at end of file