From 45b79c64b49f9540720bf5f8b0aa15d28aaa6f48 Mon Sep 17 00:00:00 2001 From: Elton Cardoso do Nascimento <43186596+EltonCN@users.noreply.github.com> Date: Fri, 11 Oct 2024 14:59:43 -0300 Subject: [PATCH] Core tests --- src/cst_python/core/entities/__init__.py | 13 +- src/cst_python/core/entities/coalition.py | 4 + src/cst_python/core/entities/codelet.py | 10 +- .../core/entities/codelet_container.py | 7 + tests/core/entities/CoalitionTest.py | 15 - tests/core/entities/CodeRackTest.py | 15 - tests/core/entities/CodeletContainerTest.py | 15 - .../core/entities/DisconnectedCodeletTest.py | 15 - tests/core/entities/MemoryBufferTest.py | 15 - tests/core/entities/MemoryContainerTest.py | 15 - tests/core/entities/test_CoalitionTest.py | 53 ++ tests/core/entities/test_CodeRackTest.py | 62 ++ .../entities/test_CodeletContainerTest.py | 823 ++++++++++++++++++ .../{CodeletTest.py => test_CodeletTest.py} | 0 .../entities/test_DisconnectedCodeletTest.py | 27 + tests/core/entities/test_MemoryBufferTest.py | 109 +++ .../core/entities/test_MemoryContainerTest.py | 511 +++++++++++ ...ObjectTest.py => test_MemoryObjectTest.py} | 0 .../{MemoryTest.py => test_MemoryTest.py} | 0 ...RawMemoryTest.py => test_RawMemoryTest.py} | 0 .../entities/{MindTest.py => test_mind.py} | 2 +- 21 files changed, 1610 insertions(+), 101 deletions(-) create mode 100644 src/cst_python/core/entities/coalition.py create mode 100644 src/cst_python/core/entities/codelet_container.py delete mode 100644 tests/core/entities/CoalitionTest.py delete mode 100644 tests/core/entities/CodeRackTest.py delete mode 100644 tests/core/entities/CodeletContainerTest.py delete mode 100644 tests/core/entities/DisconnectedCodeletTest.py delete mode 100644 tests/core/entities/MemoryBufferTest.py delete mode 100644 tests/core/entities/MemoryContainerTest.py create mode 100644 tests/core/entities/test_CoalitionTest.py create mode 100644 tests/core/entities/test_CodeRackTest.py create mode 100644 tests/core/entities/test_CodeletContainerTest.py rename tests/core/entities/{CodeletTest.py => test_CodeletTest.py} (100%) create mode 100644 tests/core/entities/test_DisconnectedCodeletTest.py create mode 100644 tests/core/entities/test_MemoryBufferTest.py create mode 100644 tests/core/entities/test_MemoryContainerTest.py rename tests/core/entities/{MemoryObjectTest.py => test_MemoryObjectTest.py} (100%) rename tests/core/entities/{MemoryTest.py => test_MemoryTest.py} (100%) rename tests/core/entities/{RawMemoryTest.py => test_RawMemoryTest.py} (100%) rename tests/core/entities/{MindTest.py => test_mind.py} (98%) diff --git a/src/cst_python/core/entities/__init__.py b/src/cst_python/core/entities/__init__.py index 0a004b8..ed3a2ac 100644 --- a/src/cst_python/core/entities/__init__.py +++ b/src/cst_python/core/entities/__init__.py @@ -1,11 +1,14 @@ +from .coalition import Coalition +from .code_rack import CodeRack from .codelet import Codelet +from .codelet_container import CodeletContainer +from .memory import Memory from .memory import Memory from .memory_buffer import MemoryBuffer +from .memory_container import MemoryContainer from .memory_object import MemoryObject -from .memory import Memory from .memory_observer import MemoryObserver -from .memory_container import MemoryContainer -from .rest_memory_container import RESTMemoryContainer -from .rest_memory_object import RESTMemoryObject from .mind import Mind -from .raw_memory import RawMemory \ No newline at end of file +from .raw_memory import RawMemory +from .rest_memory_container import RESTMemoryContainer +from .rest_memory_object import RESTMemoryObject \ No newline at end of file diff --git a/src/cst_python/core/entities/coalition.py b/src/cst_python/core/entities/coalition.py new file mode 100644 index 0000000..f6b9733 --- /dev/null +++ b/src/cst_python/core/entities/coalition.py @@ -0,0 +1,4 @@ +class Coalition: + + def __init__(self) -> None: + raise NotImplementedError() \ 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 749097e..bead8ff 100644 --- a/src/cst_python/core/entities/codelet.py +++ b/src/cst_python/core/entities/codelet.py @@ -32,7 +32,7 @@ def __init__(self) -> None: self._activation = 0.0 #self._timer = self._is_profiling = False - self._thread : threading.Thread = None + self._thread : threading.Thread = threading.Thread(target=self.run, daemon=True) self._codelet_profiler = None self._additional_wait = 0.0 @@ -199,17 +199,17 @@ def run(self) -> None: traceback.print_exception(e) def start(self) -> None: - thread = threading.Thread(target=self.run, daemon=True) - self._thread = thread - thread.start() + self._thread.start() #thread.join(0.0) def stop(self): self.loop = False - self._thread.join(0.0) + + if self._thread.is_alive(): + self._thread.join(0.0) #@alias.alias("impendingAccess") def impending_acess(self, accessing:Codelet) -> bool: diff --git a/src/cst_python/core/entities/codelet_container.py b/src/cst_python/core/entities/codelet_container.py new file mode 100644 index 0000000..bcf29c4 --- /dev/null +++ b/src/cst_python/core/entities/codelet_container.py @@ -0,0 +1,7 @@ +from .memory import Memory + +class CodeletContainer(Memory): + + def __init__(self) -> None: + super().__init__() + raise NotImplementedError() \ No newline at end of file diff --git a/tests/core/entities/CoalitionTest.py b/tests/core/entities/CoalitionTest.py deleted file mode 100644 index 64459e5..0000000 --- a/tests/core/entities/CoalitionTest.py +++ /dev/null @@ -1,15 +0,0 @@ -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 deleted file mode 100644 index 64459e5..0000000 --- a/tests/core/entities/CodeRackTest.py +++ /dev/null @@ -1,15 +0,0 @@ -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 deleted file mode 100644 index 64459e5..0000000 --- a/tests/core/entities/CodeletContainerTest.py +++ /dev/null @@ -1,15 +0,0 @@ -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/DisconnectedCodeletTest.py b/tests/core/entities/DisconnectedCodeletTest.py deleted file mode 100644 index 64459e5..0000000 --- a/tests/core/entities/DisconnectedCodeletTest.py +++ /dev/null @@ -1,15 +0,0 @@ -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 deleted file mode 100644 index 64459e5..0000000 --- a/tests/core/entities/MemoryBufferTest.py +++ /dev/null @@ -1,15 +0,0 @@ -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 deleted file mode 100644 index 64459e5..0000000 --- a/tests/core/entities/MemoryContainerTest.py +++ /dev/null @@ -1,15 +0,0 @@ -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/test_CoalitionTest.py b/tests/core/entities/test_CoalitionTest.py new file mode 100644 index 0000000..e1b842b --- /dev/null +++ b/tests/core/entities/test_CoalitionTest.py @@ -0,0 +1,53 @@ +import unittest + +from cst_python.core.entities import Coalition +from .utils import CodeletMock + +@unittest.skip("Coalition not implemented") +class Test(unittest.TestCase): + def setUp(self) -> None: + self.test_codelet = CodeletMock() + self.other_codelet = CodeletMock() + + + def test_calculateActivationTest(self) -> None: + coalition = Coalition([self.test_codelet, self.other_codelet]) + try: + coalition.getCodeletsList().get(0).setActivation(1.0) + except CodeletActivationBoundsException as e: + e.printStackTrace() + + + self.assertEqual(0.5, coalition.calculateActivation(), 0) + + + + def test_setCodeletListTest(self) -> None: + coalition = Coalition([self.test_codelet]) + + list_test = [self.test_codelet, self.other_codelet] + coalition.setCodeletsList(list_test) + + self.assertEqual(list_test, coalition.getCodeletsList()) + + + + def test_activation_test(self) -> None: + coalition = Coalition([self.test_codelet]) + + activation_test = 0.8 + coalition.setActivation(activation_test) + + self.assertEqual(0.8, coalition.getActivation(), 0) + + + + def test_toStringTest(self) -> None: + list_test = [self.test_codelet, self.other_codelet] + coalition = Coalition([self.test_codelet, self.other_codelet]) + coalition.setActivation(0.8) + + expect_message = f"Coalition [activation={0.8}, codeletsList={list_test}]" + + self.assertIn(str(coalition), expect_message) + \ No newline at end of file diff --git a/tests/core/entities/test_CodeRackTest.py b/tests/core/entities/test_CodeRackTest.py new file mode 100644 index 0000000..ca8969b --- /dev/null +++ b/tests/core/entities/test_CodeRackTest.py @@ -0,0 +1,62 @@ +import unittest + +from cst_python.core.entities import CodeRack, MemoryObject, Codelet +from .utils import CodeletMock + +class Test(unittest.TestCase): + def setUp(self) -> None: + self.test_codelet = CodeletMock() + self.other_codelet = CodeletMock() + + + def test_setAllCodeletTest(self) -> None: + code_rack = CodeRack() + test_list : list[Codelet] = [self.test_codelet, self.other_codelet] + + code_rack.all_codelets = test_list + self.assertEqual(test_list, code_rack.all_codelets) + + + + def test_insertCodeletTest(self) -> None: + code_rack = CodeRack() + test_list : list[Codelet] = [self.test_codelet] + + code_rack.insert_codelet(self.test_codelet) + + self.assertEqual(test_list, code_rack.all_codelets) + + + + def test_createCodeletTest(self) -> None: + code_rack = CodeRack() + mem_input_test = [MemoryObject(), MemoryObject()] + mem_output_test = [MemoryObject()] + + code_rack.create_codelet(0.5, None, mem_input_test, mem_output_test, self.test_codelet) + + self.assertEqual(self.test_codelet, code_rack.all_codelets[0]) + + def test_destroyCodeletTest(self) -> None: + code_rack = CodeRack() + mem_input_test = [MemoryObject(), MemoryObject()] + mem_output_test = [MemoryObject()] + + code_rack.create_codelet(0.5, None, mem_input_test, mem_output_test, self.test_codelet) + + code_rack.destroy_codelet(self.test_codelet) + + self.assertEqual(0, len(code_rack.all_codelets)) + + + def test_startStopTest(self) -> None: + code_rack = CodeRack() + test_list : list[Codelet] = [self.test_codelet, self.other_codelet] + + code_rack.all_codelets = test_list + code_rack.start() + self.assertTrue(code_rack.all_codelets[0].loop) + + code_rack.stop() + self.assertFalse(code_rack.all_codelets[0].loop) + \ No newline at end of file diff --git a/tests/core/entities/test_CodeletContainerTest.py b/tests/core/entities/test_CodeletContainerTest.py new file mode 100644 index 0000000..076efec --- /dev/null +++ b/tests/core/entities/test_CodeletContainerTest.py @@ -0,0 +1,823 @@ +import unittest +import time + +from cst_python.core.entities import Codelet, CodeletContainer, Mind, MemoryObject, Memory + +class CodeletToTestOne(Codelet): + def __init__(self, name:str): + self.counter = 0 + + self.name = name + + def access_memory_objects(self): #NOSONAR + pass + + def calculate_activation(self): + self.activation = self.counter + + def proc(self): + self.counter += 1 + if (self.outputs is not None and len(self.outputs) != 0): + self.outputs[0].set_info("CODELET 1 OUTPUT") + +class CodeletToTestTwo(Codelet): + def __init__(self, name:str): + self.counter = 0 + + self.name = name + + def access_memory_objects(self): #NOSONAR + pass + + def calculate_activation(self): #NOSONAR + pass + + def proc(self): + self.counter += 2 + +class CodeletToTestThree(Codelet): + def __init__(self, name:str): + self.counter = 0 + + self.name = name + + def access_memory_objects(self): #NOSONAR + pass + + def calculate_activation(self): + self.activation = self.counter + + def proc(self): + self.counter += 3 + if (self.outputs is not None and len(self.outputs) != 0): + self.outputs[0].set_info("CODELET 3 OUTPUT") + +@unittest.skip("CodeletContainer not implemented") +class CodeletContainerTest (unittest.TestCase): + + def sleep(self, timestep:int) -> None: + time.sleep(timestep/1000) + + + def test_noMemoryChangeTest(self) -> None: + # no codelet runs + codelet_one = CodeletToTestOne("Codelet 1") + codelet_two = CodeletToTestTwo("Codelet 2") + codelet_three = CodeletToTestThree("Codelet 3") + + mind = Mind() + memory1 = mind.create_memory_object("MEMORY1", 0.12) + memory2 = mind.create_memory_object("MEMORY2", 0.32) + memory3 = mind.create_memory_object("MEMORY3", 0.32) + memory4 = mind.create_memory_object("MEMORY4", 0.32) + + codelet_one.add_input(memory1) + codelet_one.add_broadcast(memory2) + + codelet_two.add_broadcast(memory3) + + codelet_three.add_input(memory4) + + codelet_container_array : list[Codelet] = [] + codelet_container_array.append(codelet_one) + codelet_container_array.append(codelet_two) + codelet_container_array.append(codelet_three) + + codelet_container = CodeletContainer(codelet_container_array, False) + + mind.insert_codelet(codelet_one) + mind.insert_codelet(codelet_two) + mind.insert_codelet(codelet_three) + mind.start() + self.sleep(2000) + mind.shutdown() + + self.assertEqual(0, codelet_container.getOutputs().size()) + self.assertEqual( [], codelet_container.getOutputs()) + self.assertEqual( [], codelet_container.getOutputs()) + self.assertEqual( [], codelet_container.getOutputs()) + self.assertEqual(0, codelet_container.getEvaluation(), 0) + + + + + def test_noMemoryChangeButCodeletAddedIsStartedTest(self) -> None: + # no codelet runs + codelet_one = CodeletToTestOne("Codelet 1") + codelet_two = CodeletToTestTwo("Codelet 2") + codelet_three = CodeletToTestThree("Codelet 3") + + mind = Mind() + memory1 = mind.create_memory_object("MEMORY1", 0.12) + memory2 = mind.create_memory_object("MEMORY2", 0.32) + memory3 = mind.create_memory_object("MEMORY3", 0.32) + memory4 = mind.create_memory_object("MEMORY4", 0.32) + + codelet_one.add_input(memory1) + codelet_one.add_broadcast(memory2) + + codelet_two.add_broadcast(memory3) + + codelet_three.add_input(memory4) + + codelet_container_array : list[Codelet] = [] + codelet_container_array.append(codelet_one) + codelet_container_array.append(codelet_two) + codelet_container_array.append(codelet_three) + + codelet_container = CodeletContainer(codelet_container_array, True) + + mind.insert_codelet(codelet_one) + mind.insert_codelet(codelet_two) + mind.insert_codelet(codelet_three) + mind.start() + self.sleep(2000) + mind.shutdown() + + self.assertEqual(0, len(codelet_container.getOutputs())) + self.assertEqual( [], codelet_container.getOutputs()) + self.assertEqual( [], codelet_container.getOutputs()) + self.assertEqual( [], codelet_container.getOutputs()) + self.assertEqual(0, codelet_container.getEvaluation(), 0) + + + + + def test_runningCodeletChangingInputTest(self) -> None: + # changes the codelet container input + codelet_one = CodeletToTestOne("Codelet 1") + codelet_two = CodeletToTestTwo("Codelet 2") + codelet_three = CodeletToTestThree("Codelet 3") + + mind = Mind() + memory_input1 = mind.create_memory_object("MEMORY_INPUT_1", 0.12) + memory_input2 = mind.create_memory_object("MEMORY_INPUT_2", 0.32) + memory_input3 = mind.create_memory_object("MEMORY_INPUT_3", 0.32) + memory_input4 = mind.create_memory_object("MEMORY_INPUT_4", 0.32) + memory_output1 = mind.create_memory_object("MEMORY_OUTPUT_1", 0.22) + memory_output2 = mind.create_memory_object("MEMORY_OUTPUT_2", 0.22) + memory_output3 = mind.create_memory_object("MEMORY_OUTPUT_3", 0.22) + + codelet_one.add_input(memory_input1) + codelet_one.add_broadcast(memory_input2) + codelet_one.add_output(memory_output1) + + codelet_two.add_broadcast(memory_input3) + codelet_two.add_output(memory_output2) + + codelet_three.add_input(memory_input4) + codelet_three.add_output(memory_output3) + + codelet_container_array : list[Codelet] = [] + codelet_container_array.append(codelet_one) + codelet_container_array.append(codelet_two) + codelet_container_array.append(codelet_three) + + codelet_container = CodeletContainer(codelet_container_array, False) + + mind.insert_codelet(codelet_one) + mind.insert_codelet(codelet_two) + mind.insert_codelet(codelet_three) + codelet_container.set_info(10) + mind.start() + self.sleep(2000) + mind.shutdown() + + for codelet in codelet_container.getAll(): + for mem in codelet.inputs: + self.assertEqual(10, mem.get_info()) + + + + for codelet in codelet_container.getAll(): + for mem in codelet.broadcast: + self.assertEqual(0.32, mem.get_info()) + + + + self.assertEqual(3, codelet_container.getOutputs().size()) + expected_outputs = [] + expected_outputs.append(memory_output1) + expected_outputs.append(memory_output2) + expected_outputs.append(memory_output3) + assert expected_outputs == list(codelet_container.getOutputs()) + self.assertEqual(0.22, codelet_container.getOutputs()[1].get_info()) + self.assertEqual("MEMORY_OUTPUT_3", codelet_container.getOutputs()[2].name) + self.assertEqual(0, codelet_container.getEvaluation(), 0) + + + + + def test_runningCodeletChangingInputCodeletStartedWhenAddedTest(self) -> None: + # changes the codelet container input + codelet_one = CodeletToTestOne("Codelet 1") + codelet_two = CodeletToTestTwo("Codelet 2") + codelet_three = CodeletToTestThree("Codelet 3") + + mind = Mind() + memory_input1 = mind.create_memory_object("MEMORY_INPUT_1", 0.12) + memory_input2 = mind.create_memory_object("MEMORY_INPUT_2", 0.32) + memory_input3 = mind.create_memory_object("MEMORY_INPUT_3", 0.32) + memory_input4 = mind.create_memory_object("MEMORY_INPUT_4", 0.32) + memory_output1 = mind.create_memory_object("MEMORY_OUTPUT_1", 0.22) + memory_output2 = mind.create_memory_object("MEMORY_OUTPUT_2", 0.22) + memory_output3 = mind.create_memory_object("MEMORY_OUTPUT_3", 0.22) + + codelet_one.add_input(memory_input1) + codelet_one.add_broadcast(memory_input2) + codelet_one.add_output(memory_output1) + + codelet_two.add_broadcast(memory_input3) + codelet_two.add_output(memory_output2) + + codelet_three.add_input(memory_input4) + codelet_three.add_output(memory_output3) + + codelet_container_array : list[Codelet] = [] + codelet_container_array.append(codelet_one) + codelet_container_array.append(codelet_two) + codelet_container_array.append(codelet_three) + + codelet_container = CodeletContainer(codelet_container_array, True) + + codelet_container.set_info(10) + self.sleep(2000) + + for codelet in codelet_container.getAll(): + for mem in codelet.inputs: + self.assertEqual(10, mem.get_info()) + + + + for codelet in codelet_container.getAll(): + for mem in codelet.broadcast: + self.assertEqual(0.32, mem.get_info()) + + + + codelet_to_test_one : CodeletToTestOne = codelet_container.getCodelet("Codelet 1") + self.assertEqual(7, codelet_to_test_one.counter) + self.assertEqual(3, codelet_container.getOutputs().size()) + expected_outputs : list[Memory] = [] + expected_outputs.append(memory_output1) + expected_outputs.append(memory_output2) + expected_outputs.append(memory_output3) + assert expected_outputs == list(codelet_container.getOutputs()) + self.assertEqual(0.22, codelet_container.getOutputs()[1].get_info()) + self.assertEqual("MEMORY_OUTPUT_3", codelet_container.getOutputs()[2].name) + self.assertEqual(0, codelet_container.getEvaluation(), 0) + + + + + def test_addCodeletsToCodeletContainerTest(self) -> None: + # changes the codelet container input + codelet_one = CodeletToTestOne("Codelet 1") + codelet_two = CodeletToTestTwo("Codelet 2") + codelet_three = CodeletToTestThree("Codelet 3") + + mind = Mind() + memory_input1 = mind.create_memory_object("MEMORY_INPUT_1", 0.12) + memory_input2 = mind.create_memory_object("MEMORY_INPUT_2", 0.32) + memory_input3 = mind.create_memory_object("MEMORY_INPUT_3", 0.32) + memory_input4 = mind.create_memory_object("MEMORY_INPUT_4", 0.32) + memory_output1 = mind.create_memory_object("MEMORY_OUTPUT_1", 0.22) + memory_output2 = mind.create_memory_object("MEMORY_OUTPUT_2", 0.22) + memory_output3 = mind.create_memory_object("MEMORY_OUTPUT_3", 0.22) + + codelet_one.add_input(memory_input1) + codelet_one.add_broadcast(memory_input2) + codelet_one.add_output(memory_output1) + + codelet_two.add_broadcast(memory_input3) + codelet_two.add_output(memory_output2) + + codelet_three.add_input(memory_input4) + codelet_three.add_output(memory_output3) + + codelet_container_array : list[Codelet] = [] + codelet_container_array.append(codelet_one) + codelet_container_array.append(codelet_two) + codelet_container_array.append(codelet_three) + + codelet_container = CodeletContainer() + codelet_container.addCodelet(codelet_one, False) + codelet_container.addCodelet(codelet_two, False) + codelet_container.addCodelet(codelet_three, False) + + + self.assertEqual(3, codelet_container.getOutputs().size()) + expected_outputs : list[Memory] = [] + expected_outputs.append(memory_output1) + expected_outputs.append(memory_output2) + expected_outputs.append(memory_output3) + assert expected_outputs == list(codelet_container.getOutputs()) + + self.assertEqual("MEMORY_OUTPUT_1", codelet_container.getOutputs()[0].name) + self.assertEqual("MEMORY_OUTPUT_2", codelet_container.getOutputs()[1].name) + self.assertEqual("MEMORY_OUTPUT_3", codelet_container.getOutputs()[2].name) + self.assertEqual(3, codelet_container.getCodelet(codelet_one.name).outputs.size()) + self.assertEqual(3, codelet_container.getCodelet(codelet_two.name).outputs.size()) + self.assertEqual(3, codelet_container.getCodelet(codelet_three.name).outputs.size()) + + self.assertEqual(2, codelet_container.getInputs().size()) + expected_inputs : list[Memory] = [] + expected_inputs.append(memory_input1) + expected_inputs.append(memory_input4) + assert expected_inputs == list(codelet_container.getInputs()) + self.assertEqual("MEMORY_INPUT_1", codelet_container.getInputs()[0].name) + self.assertEqual("MEMORY_INPUT_4", codelet_container.getInputs()[1].name) + self.assertEqual(2, codelet_container.getCodelet(codelet_one.name).inputs.size()) + self.assertEqual(2, codelet_container.getCodelet(codelet_three.name).inputs.size()) + + self.assertEqual(2, codelet_container.getBroadcast().size()) + expected_broadcast : list[Memory] = [] + expected_broadcast.append(memory_input2) + expected_broadcast.append(memory_input3) + assert expected_broadcast == list(codelet_container.getBroadcast()) + self.assertEqual("MEMORY_INPUT_2", codelet_container.getBroadcast()[0].name) + self.assertEqual("MEMORY_INPUT_3", codelet_container.getBroadcast()[1].name) + self.assertEqual(2, codelet_container.getCodelet(codelet_one.name).broadcast.size()) + self.assertEqual(2, codelet_container.getCodelet(codelet_two.name).broadcast.size()) + + + + def test_addCodeletsToCodeletContainerWhichHasInputsAndOuputsTest(self) -> None: + # changes the codelet container input + codelet_one = CodeletToTestOne("Codelet 1") + codelet_two = CodeletToTestTwo("Codelet 2") + codelet_three = CodeletToTestThree("Codelet 3") + + mind = Mind() + memory_input1 = mind.create_memory_object("MEMORY_INPUT_1", 0.12) + # 2 and 3 is not used + memory_input4 = mind.create_memory_object("MEMORY_INPUT_4", 0.32) + memory_output1 = mind.create_memory_object("MEMORY_OUTPUT_1", 0.22) + memory_output2 = mind.create_memory_object("MEMORY_OUTPUT_2", 0.22) + memory_output3 = mind.create_memory_object("MEMORY_OUTPUT_3", 0.22) + + codelet_container = CodeletContainer() + + new_inputs : list[Memory] = [] + new_inputs.append(memory_input1) + new_inputs.append(memory_input4) + codelet_container.set_infonputs(new_inputs) + + new_outputs: list[Memory] = [] + new_outputs.append(memory_output1) + new_outputs.append(memory_output2) + new_outputs.append(memory_output3) + codelet_container.setOutputs(new_outputs) + + codelet_container.addCodelet(codelet_one, False) + codelet_container.addCodelet(codelet_two, False) + codelet_container.addCodelet(codelet_three, False) + + + self.assertEqual(3, codelet_container.getOutputs().size()) + expected_outputs : list[Memory] = [] + expected_outputs.append(memory_output1) + expected_outputs.append(memory_output2) + expected_outputs.append(memory_output3) + assert expected_outputs == list(codelet_container.getOutputs()) + self.assertEqual("MEMORY_OUTPUT_1", codelet_container.getOutputs()[0].name) + self.assertEqual("MEMORY_OUTPUT_2", codelet_container.getOutputs()[1].name) + self.assertEqual("MEMORY_OUTPUT_3", codelet_container.getOutputs()[2].name) + self.assertEqual(3, codelet_container.getCodelet(codelet_one.name).outputs.size()) + self.assertEqual(3, codelet_container.getCodelet(codelet_two.name).outputs.size()) + self.assertEqual(3, codelet_container.getCodelet(codelet_three.name).outputs.size()) + + self.assertEqual(2, codelet_container.getInputs().size()) + expected_inputs : list[Memory] = [] + expected_inputs.append(memory_input1) + expected_inputs.append(memory_input4) + assert expected_inputs == list(codelet_container.getInputs()) + self.assertEqual("MEMORY_INPUT_1", codelet_container.getInputs()[0].name) + self.assertEqual("MEMORY_INPUT_4", codelet_container.getInputs()[1].name) + self.assertEqual(2, codelet_container.getCodelet(codelet_one.name).inputs.size()) + self.assertEqual(2, codelet_container.getCodelet(codelet_three.name).inputs.size()) + + + + + def test_removeCodeletsFromCodeletContainerTest(self) -> None: + # changes the codelet container input + codelet_one = CodeletToTestOne("Codelet 1") + codelet_two = CodeletToTestTwo("Codelet 2") + codelet_three = CodeletToTestThree("Codelet 3") + + mind = Mind() + memory_input1 = mind.create_memory_object("MEMORY_INPUT_1", 0.12) + memory_input2 = mind.create_memory_object("MEMORY_INPUT_2", 0.32) + memory_input3 = mind.create_memory_object("MEMORY_INPUT_3", 0.32) + memory_input4 = mind.create_memory_object("MEMORY_INPUT_4", 0.32) + memory_output1 = mind.create_memory_object("MEMORY_OUTPUT_1", 0.22) + memory_output2 = mind.create_memory_object("MEMORY_OUTPUT_2", 0.22) + memory_output3 = mind.create_memory_object("MEMORY_OUTPUT_3", 0.22) + + codelet_one.add_input(memory_input1) + codelet_one.add_broadcast(memory_input2) + codelet_one.add_output(memory_output1) + + codelet_two.add_broadcast(memory_input3) + codelet_two.add_output(memory_output2) + + codelet_three.add_input(memory_input4) + codelet_three.add_output(memory_output3) + + codelet_container_array : list[Codelet] = [] + codelet_container_array.append(codelet_one) + codelet_container_array.append(codelet_two) + codelet_container_array.append(codelet_three) + + codelet_container = CodeletContainer(codelet_container_array, False) + + + self.assertEqual(3, codelet_container.getOutputs().size()) + expected_outputs : list[Memory] = [] + expected_outputs.append(memory_output1) + expected_outputs.append(memory_output2) + expected_outputs.append(memory_output3) + assert expected_outputs == list(codelet_container.getOutputs()) + self.assertEqual("MEMORY_OUTPUT_1", codelet_container.getOutputs()[0].name) + self.assertEqual("MEMORY_OUTPUT_2", codelet_container.getOutputs()[1].name) + self.assertEqual("MEMORY_OUTPUT_3", codelet_container.getOutputs()[2].name) + self.assertEqual(3, codelet_container.getCodelet(codelet_one.name).outputs.size()) + self.assertEqual(3, codelet_container.getCodelet(codelet_two.name).outputs.size()) + self.assertEqual(3, codelet_container.getCodelet(codelet_three.name).outputs.size()) + + self.assertEqual(2, codelet_container.getInputs().size()) + expected_inputs : list[Memory] = [] + expected_inputs.append(memory_input1) + expected_inputs.append(memory_input4) + assert expected_inputs == list(codelet_container.getInputs()) + self.assertEqual("MEMORY_INPUT_1", codelet_container.getInputs()[0].name) + self.assertEqual("MEMORY_INPUT_4", codelet_container.getInputs()[1].name) + self.assertEqual(2, codelet_container.getCodelet(codelet_one.name).inputs.size()) + self.assertEqual(2, codelet_container.getCodelet(codelet_three.name).inputs.size()) + + self.assertEqual(2, codelet_container.getBroadcast().size()) + expected_broadcast : list[Memory] = [] + expected_broadcast.append(memory_input2) + expected_broadcast.append(memory_input3) + assert expected_broadcast == list(codelet_container.getBroadcast()) + self.assertEqual("MEMORY_INPUT_2", codelet_container.getBroadcast()[0].name) + self.assertEqual("MEMORY_INPUT_3", codelet_container.getBroadcast()[1].name) + self.assertEqual(2, codelet_container.getCodelet(codelet_one.name).broadcast.size()) + self.assertEqual(2, codelet_container.getCodelet(codelet_two.name).broadcast.size()) + + codelet_container.removeCodelet(codelet_one) + + self.assertEqual(2, codelet_container.getOutputs().size()) + expected_outputs = [] + expected_outputs.append(memory_output2) + expected_outputs.append(memory_output3) + assert expected_outputs == list(codelet_container.getOutputs()) + self.assertEqual(2, codelet_container.getCodelet(codelet_two.name).outputs.size()) + self.assertEqual(2, codelet_container.getCodelet(codelet_three.name).outputs.size()) + + self.assertEqual(1, codelet_container.getInputs().size()) + expected_inputs = [] + expected_inputs.append(memory_input4) + assert expected_inputs == list(codelet_container.getInputs()) + self.assertEqual("MEMORY_INPUT_4", codelet_container.getInputs()[0].name) + self.assertEqual(1, codelet_container.getCodelet(codelet_three.name).inputs.size()) + + self.assertEqual(1, codelet_container.getBroadcast().size()) + expected_broadcast = [] + expected_broadcast.append(memory_input3) + assert expected_broadcast == list(codelet_container.getBroadcast()) + self.assertEqual("MEMORY_INPUT_3", codelet_container.getBroadcast()[0].name) + self.assertEqual(1, codelet_container.getCodelet(codelet_two.name).broadcast.size()) + + codelet_container.removeCodelet(codelet_two) + + self.assertEqual(1, codelet_container.getOutputs().size()) + expected_outputs = [] + expected_outputs.append(memory_output3) + assert expected_outputs == list(codelet_container.getOutputs()) + self.assertEqual(1, codelet_container.getCodelet(codelet_three.name).outputs.size()) + + self.assertEqual(1, codelet_container.getInputs().size()) + expected_inputs = [] + expected_inputs.append(memory_input4) + assert expected_inputs == list(codelet_container.getInputs()) + self.assertEqual("MEMORY_INPUT_4", codelet_container.getInputs()[0].name) + self.assertEqual(1, codelet_container.getCodelet(codelet_three.name).inputs.size()) + + self.assertEqual(0, codelet_container.getBroadcast().size()) + self.assertEqual(0, codelet_container.getCodelet(codelet_three.name).broadcast.size()) + + + + + + def test_getEvaluationTest(self) -> None: + codelet_one = CodeletToTestOne("Codelet 1") + codelet_two = CodeletToTestTwo("Codelet 2") + codelet_three = CodeletToTestThree("Codelet 3") + + mind = Mind() + memory1 = mind.create_memory_object("MEMORY1", 0.12) + memory2 = mind.create_memory_object("MEMORY2", 0.32) + memory3 = mind.create_memory_object("MEMORY3", 0.32) + memory4 = mind.create_memory_object("MEMORY4", 0.32) + + codelet_one.add_input(memory1) + codelet_one.add_broadcast(memory2) + + codelet_two.add_broadcast(memory3) + + codelet_three.add_input(memory4) + + codelet_container_array : list[Codelet] = [] + codelet_container_array.append(codelet_one) + codelet_container_array.append(codelet_two) + codelet_container_array.append(codelet_three) + + codelet_container = CodeletContainer(codelet_container_array, False) + test_value = 100.0 + + mind.insert_codelet(codelet_one) + mind.insert_codelet(codelet_two) + mind.insert_codelet(codelet_three) + memory1.setEvaluation(test_value) + codelet_container.set_info(10) + mind.start() + self.sleep(2000) + mind.shutdown() + + + + self.assertEqual(test_value, codelet_container.getEvaluation()) + + + + + + def test_getActivationTest(self) -> None: + codelet_one = CodeletToTestOne("Codelet 1") + codelet_two = CodeletToTestTwo("Codelet 2") + codelet_three = CodeletToTestThree("Codelet 3") + + mind = Mind() + memory1 = mind.create_memory_object("MEMORY1", 0.12) + memory2 = mind.create_memory_object("MEMORY2", 0.32) + memory3 = mind.create_memory_object("MEMORY3", 0.32) + memory4 = mind.create_memory_object("MEMORY4", 0.32) + + codelet_one.add_input(memory1) + codelet_one.add_broadcast(memory2) + + codelet_two.add_broadcast(memory3) + + codelet_three.add_input(memory4) + + codelet_container_array : list[Codelet] = [] + codelet_container_array.append(codelet_one) + codelet_container_array.append(codelet_two) + codelet_container_array.append(codelet_three) + + codelet_container = CodeletContainer(codelet_container_array, False) + test_value = 6.0 + + mind.insert_codelet(codelet_one) + mind.insert_codelet(codelet_two) + mind.insert_codelet(codelet_three) + memory1.setEvaluation(test_value) + codelet_container.set_info(10) + mind.start() + self.sleep(2000) + mind.shutdown() + + + + self.assertEqual(test_value, codelet_container.getActivation(), 0) + + + + + def test_set_infonputsTest(self) -> None: + codelet_one = CodeletToTestOne("Codelet 1") + codelet_two = CodeletToTestTwo("Codelet 2") + codelet_three = CodeletToTestThree("Codelet 3") + + mind = Mind() + memory1 = mind.create_memory_object("MEMORY1", 0.12) + memory2 = mind.create_memory_object("MEMORY2", 0.32) + memory3 = mind.create_memory_object("MEMORY3", 0.32) + memory4 = mind.create_memory_object("MEMORY4", 0.32) + + codelet_one.add_input(memory1) + codelet_one.add_broadcast(memory2) + + codelet_two.add_broadcast(memory3) + + codelet_three.add_input(memory4) + + codelet_container_array : list[Codelet] = [] + codelet_container_array.append(codelet_one) + codelet_container_array.append(codelet_two) + codelet_container_array.append(codelet_three) + + codelet_container = CodeletContainer(codelet_container_array, False) + + mind.insert_codelet(codelet_one) + mind.insert_codelet(codelet_two) + mind.insert_codelet(codelet_three) + + new_inputs : list[Memory] = [] + new_inputs.append(memory1) + codelet_container.set_infonputs(new_inputs) + + + + self.assertEqual(new_inputs, codelet_container.getInputs()) + + + + + def test_setOutputsTest(self) -> None: + codelet_one = CodeletToTestOne("Codelet 1") + codelet_two = CodeletToTestTwo("Codelet 2") + codelet_three = CodeletToTestThree("Codelet 3") + + mind = Mind() + memory1 = mind.create_memory_object("MEMORY1", 0.12) + memory2 = mind.create_memory_object("MEMORY2", 0.32) + memory3 = mind.create_memory_object("MEMORY3", 0.32) + memory4 = mind.create_memory_object("MEMORY4", 0.32) + + codelet_one.add_input(memory1) + codelet_one.add_broadcast(memory2) + + codelet_two.add_broadcast(memory3) + + codelet_three.add_input(memory4) + + codelet_container_array : list[Codelet] = [] + codelet_container_array.append(codelet_one) + codelet_container_array.append(codelet_two) + codelet_container_array.append(codelet_three) + + codelet_container = CodeletContainer(codelet_container_array, False) + + mind.insert_codelet(codelet_one) + mind.insert_codelet(codelet_two) + mind.insert_codelet(codelet_three) + + new_outputs: list[Memory] = [] + new_outputs.append(memory1) + codelet_container.setOutputs(new_outputs) + + + + self.assertEqual(new_outputs, codelet_container.getOutputs()) + + + + + def test_setBroadcastTest(self) -> None: + codelet_one = CodeletToTestOne("Codelet 1") + codelet_two = CodeletToTestTwo("Codelet 2") + codelet_three = CodeletToTestThree("Codelet 3") + + mind = Mind() + memory1 = mind.create_memory_object("MEMORY1", 0.12) + memory2 = mind.create_memory_object("MEMORY2", 0.32) + memory3 = mind.create_memory_object("MEMORY3", 0.32) + memory4 = mind.create_memory_object("MEMORY4", 0.32) + + codelet_one.add_input(memory1) + codelet_one.add_broadcast(memory2) + + codelet_two.add_broadcast(memory3) + + codelet_three.add_input(memory4) + + codelet_container_array : list[Codelet] = [] + codelet_container_array.append(codelet_one) + codelet_container_array.append(codelet_two) + codelet_container_array.append(codelet_three) + + codelet_container = CodeletContainer(codelet_container_array, False) + + mind.insert_codelet(codelet_one) + mind.insert_codelet(codelet_two) + mind.insert_codelet(codelet_three) + + new_broadcast : list[Memory] = [] + new_broadcast.append(memory1) + codelet_container.setBroadcast(new_broadcast) + + + + self.assertEqual(new_broadcast, codelet_container.getBroadcast()) + + + + + def test_setNameTest(self) -> None: + codelet_container = CodeletContainer() + codelet_container.setName("Container") + self.assertEqual("Container", codelet_container.name) + + + + def test_setTypeTest(self) -> None: + codelet_container = CodeletContainer() + codelet_container.setType("Container") + self.assertEqual("Container", codelet_container.name) + + + + def test_setEvaluationTest(self) -> None: + codelet_one = CodeletToTestOne("Codelet 1") + mind = Mind() + memory1 = mind.create_memory_object("MEMORY1", 0.12) + codelet_one.add_input(memory1) + codelet_container_array : list[Codelet] = [] + codelet_container_array.append(codelet_one) + codelet_container = CodeletContainer(codelet_container_array, False) + codelet_container.setEvaluation(5.0) + self.assertEqual(5.0, codelet_container.getCodelet("Codelet 1").getInputs()[0].getEvaluation(),0) + + + + def test_addMemoryObserverTest(self) -> None: + codelet_one = CodeletToTestOne("Codelet 1") + codelet_two = CodeletToTestTwo("Codelet 2") + codelet_three = CodeletToTestThree("Codelet 3") + + mind = Mind() + memory1 = mind.create_memory_object("MEMORY1", 0.12) + memory2 = mind.create_memory_object("MEMORY2", 0.32) + memory3 = mind.create_memory_object("MEMORY3", 0.32) + memory4 = mind.create_memory_object("MEMORY4", 0.32) + + codelet_one.set_infosMemoryObserver(True) + codelet_one.add_input(memory1) + codelet_one.add_broadcast(memory2) + + codelet_two.add_broadcast(memory3) + + codelet_three.add_input(memory4) + + codelet_container_array : list[Codelet] = [] + codelet_container_array.append(codelet_one) + codelet_container_array.append(codelet_two) + codelet_container_array.append(codelet_three) + + codelet_container = CodeletContainer(codelet_container_array, False) + codelet_container.addMemoryObserver(codelet_one) + + mind.insert_codelet(codelet_one) + mind.insert_codelet(codelet_two) + mind.insert_codelet(codelet_three) + codelet_container.set_info(10) + mind.start() + self.sleep(2000) + mind.shutdown() + + + codelet_to_test_one : CodeletToTestOne = codelet_container.getCodelet("Codelet 1") + self.assertEqual(6, codelet_to_test_one.counter) + + + + def test_getTimestampTest(self) -> None: + codelet_one = CodeletToTestOne("Codelet 1") + codelet_two = CodeletToTestTwo("Codelet 2") + codelet_three = CodeletToTestThree("Codelet 3") + + mind = Mind() + memory1 = mind.create_memory_object("MEMORY1", 0.12) + memory2 = mind.create_memory_object("MEMORY2", 0.32) + memory3 = mind.create_memory_object("MEMORY3", 0.32) + memory4 = mind.create_memory_object("MEMORY4", 0.32) + + codelet_one.add_input(memory1) + codelet_one.add_broadcast(memory2) + + codelet_two.add_broadcast(memory3) + + codelet_three.add_input(memory4) + + codelet_container_array : list[Codelet] = [] + codelet_container_array.append(codelet_one) + codelet_container_array.append(codelet_two) + codelet_container_array.append(codelet_three) + + codelet_container = CodeletContainer(codelet_container_array, False) + + mind.insert_codelet(codelet_one) + mind.insert_codelet(codelet_two) + mind.insert_codelet(codelet_three) + codelet_container.set_info(10) + mind.start() + self.sleep(2000) + mind.shutdown() + + self.assertGreater(codelet_container.getTimestamp().doubleValue(), 1) + + \ No newline at end of file diff --git a/tests/core/entities/CodeletTest.py b/tests/core/entities/test_CodeletTest.py similarity index 100% rename from tests/core/entities/CodeletTest.py rename to tests/core/entities/test_CodeletTest.py diff --git a/tests/core/entities/test_DisconnectedCodeletTest.py b/tests/core/entities/test_DisconnectedCodeletTest.py new file mode 100644 index 0000000..5896a6c --- /dev/null +++ b/tests/core/entities/test_DisconnectedCodeletTest.py @@ -0,0 +1,27 @@ +import unittest + +from .utils import CodeletMock + +class disconnected_codeletTest (unittest.TestCase): + def setUp(self) -> None: + self.message = "" + + def tearDown(self) -> None: + super().tearDown() + + + def test_disconnected_codelet(self) -> None: + + disconnected_codelet = CodeletMock() + + disconnected_codelet.name = "Disconnected Codelet" + try: + disconnected_codelet.start() + disconnected_codelet.getInput("TYPE", 0) + disconnected_codelet.stop() + except Exception as e: + message = repr(e) + #print("Testing disconnected_codelet:"+e.getMessage()) + + disconnected_codelet.stop() + #print("Codelet stopped !") \ No newline at end of file diff --git a/tests/core/entities/test_MemoryBufferTest.py b/tests/core/entities/test_MemoryBufferTest.py new file mode 100644 index 0000000..f5d913a --- /dev/null +++ b/tests/core/entities/test_MemoryBufferTest.py @@ -0,0 +1,109 @@ +from contextlib import redirect_stdout +import io +import unittest + +from cst_python.core.entities import RawMemory, MemoryBuffer, MemoryObject + +@unittest.skip("MemoryBuffer not implemented") +class MemoryBufferTest(unittest.TestCase): + + @unittest.skip("'setType' is not implemented in Python as is deprecated") + def test_basic_call(self) -> None: + rawMemory = RawMemory() + memoryBuffer = MemoryBuffer(3, rawMemory) + + testList : list[MemoryObject] = [MemoryObject(), MemoryObject(), MemoryObject()] + testList[0].setType("memory_0") + testList[1].setType("memory_1") + testList[2].setType("memory_2") + + memoryBuffer.putList(testList) + + self.assertEqual(3, len(memoryBuffer)) + self.assertEqual(memoryBuffer.get(), memoryBuffer.getAll()) + self.assertEqual(testList[2], memoryBuffer.getMostRecent()) + self.assertEqual(testList[0], memoryBuffer.getOldest()) + + + @unittest.skip("'setType' is not implemented in Python as is deprecated") + def test_puts_more_than_max(self) -> None: + rawMemory = RawMemory() + memoryBuffer = MemoryBuffer(3, rawMemory) + + testList : list[MemoryObject] = [MemoryObject(), MemoryObject(), MemoryObject(), MemoryObject()] + testList[0].setType("memory_0") + testList[1].setType("memory_1") + testList[2].setType("memory_2") + testList[3].setType("memory_3") + memoryBuffer.putList(testList) + + self.assertEqual(3, len(memoryBuffer)) + self.assertEqual(memoryBuffer.get(), memoryBuffer.getAll()) + self.assertEqual(testList[1], memoryBuffer.get()[0]) + + memoryBuffer.put(MemoryObject()) + self.assertEqual(testList[2], memoryBuffer.get()[0]) + + + @unittest.skip("'setType' is not implemented in Python as is deprecated") + def test_put_pop(self) -> None: + rawMemory = RawMemory() + memoryBuffer = MemoryBuffer(3, rawMemory) + + testMemory = MemoryObject() + testMemory.setType("memory_0") + memoryBuffer.put(testMemory) + + self.assertEqual(testMemory, memoryBuffer.pop()) + self.assertEqual(0, len(memoryBuffer)) + + + + def test_null_oldest_and_newest(self) -> None: + rawMemory = RawMemory() + memoryBuffer = MemoryBuffer(3, rawMemory) + + self.assertIsNone(memoryBuffer.getOldest()) + self.assertIsNone(memoryBuffer.getMostRecent()) + + + @unittest.skip("'setType' is not implemented in Python as is deprecated") + def test_remove_and_clear(self) -> None: + rawMemory = RawMemory() + memoryBuffer = MemoryBuffer(3, rawMemory) + + testList : list[MemoryObject] = [MemoryObject(), MemoryObject(), MemoryObject()] + testList[0].setType("memory_0") + testList[1].setType("memory_1") + testList[2].setType("memory_2") + + memoryBuffer.putList(testList) + memoryBuffer.remove(testList[1]) + + self.assertEqual(2, len(memoryBuffer)) + self.assertEqual(testList[2], memoryBuffer.get()[1]) + + memoryBuffer.clear() + self.assertEqual(0, len(memoryBuffer)) + + + @unittest.skip("'setType' is not implemented in Python as is deprecated") + def test_pint_status(self) -> None: + rawMemory = RawMemory() + memoryBuffer = MemoryBuffer(3, rawMemory) + + testList : list[MemoryObject] = [MemoryObject()] + testList[0].setType("memory_0") + memoryBuffer.putList(testList) + + expectedMessage ='''"###### Memory Buffer ########\n# Content: [MemoryObject [idmemoryobject=null, timestamp=null, evaluation=0.0, I=null, name=memory_0]]" + + "\n# Size: 1\n###############################''' + + with redirect_stdout(io.StringIO()) as f: + memoryBuffer.printStatus() + + printed = f.getvalue().replace("\r\n", "\n") + expectedMessage = expectedMessage.replace("\r\n", "\n") + + self.assertTrue(expectedMessage in printed) + \ No newline at end of file diff --git a/tests/core/entities/test_MemoryContainerTest.py b/tests/core/entities/test_MemoryContainerTest.py new file mode 100644 index 0000000..8128013 --- /dev/null +++ b/tests/core/entities/test_MemoryContainerTest.py @@ -0,0 +1,511 @@ +import unittest +from typing import Callable + +from cst_python.core.entities import MemoryContainer, Memory, MemoryObject + +@unittest.skip("Memory Container not implemented") +class MemoryContainerTest (unittest.TestCase): + + def test_memory_container_content(self) -> None: + memoryContainer = MemoryContainer("TYPE") + + memoryContainer.set_info(71, 0.1, "TYPE") + memoryContainer.set_info(75, 0.2, "TYPE") + + self.assertEqual(75, memoryContainer.get_info()) + + def test_memory_container_size(self) -> None: + + memoryContainer = MemoryContainer("TYPE") + + memoryContainer.set_info(71, 0.1, "TYPE2") + memoryContainer.set_info(75, 0.2, "TYPE2") + memoryContainer.set_info(75, 0.2, "TYPE2") + memoryContainer.set_info(75, 0.2, "TYPE2") + memoryContainer.set_info(75, 0.2, "TYPE2") + memoryContainer.set_info(75, 0.2, "TYPE2") + memoryContainer.set_info(75, 0.2, "TYPE2") + memoryContainer.set_info(75, 0.2, "TYPE3") + + self.assertEqual(2, memoryContainer.getAllMemories().size()) + + def test_set_type(self) -> None: + # memoryContainer = MemoryContainer() + # memoryContainer.setType("TYPE") + # self.assertEqual("TYPE", memoryContainer.get_name()) + + memoryContainer = MemoryContainer() + memoryContainer.set_name("TYPE2") + self.assertEqual("TYPE2", memoryContainer.get_name()) + + memoryContainer = MemoryContainer("TYPE3") + self.assertEqual("TYPE3", memoryContainer.get_name()) + + def test_get_type(self) -> None: + memoryContainer = MemoryContainer("TYPE-Container") + memoryContainer.set_info("value", 1.0, "TYPE") + self.assertEqual(memoryContainer.get_info("TYPE"), "value") + print("-- This test will raise a warning ...") + self.assertIsNone(memoryContainer.get_info("TYPE2")) + + def test_get_i(self) -> None: + + memoryContainer = MemoryContainer("TYPE") + + memoryContainer.set_info(71, 0.1, "TYPE2") + memoryContainer.set_info(75, 0.2, "TYPE2") + memoryContainer.set_info(70, 0.3, "TYPE3") + + self.assertEqual(70, memoryContainer.get_info()) + self.assertEqual(75, memoryContainer.get_info(0)) + print("-- This test will raise a warning ...") + # This test will raise a warning for index greater than the number of stored memories + self.assertIsNone(memoryContainer.get_info(2)) + self.assertEqual(70, memoryContainer.get_info("TYPE3")) + + def test_get_i_predicate(self) -> None: + + memoryContainer = MemoryContainer("TYPE") + + memoryContainer.set_info(71, 0.1, "TYPE2") + memoryContainer.set_info(75, 0.2, "TYPE2") + memoryContainer.set_info(70, 0.3, "TYPE3") + memoryContainer.set_info(70, 0.25) + + pred: Callable[[Memory], bool] = lambda m: m.get_name() == "TYPE2" + + self.assertEqual(75, memoryContainer.get_info(pred)) + + def test_get_i_accumulator(self) -> None: + + memoryContainer = MemoryContainer("TYPE") + + memoryContainer.set_info(75, 0.2, "TYPE2") + memoryContainer.set_info(70, 0.3, "TYPE3") + memoryContainer.set_info(80) + + binaryOperator: Callable[[Memory, Memory], Memory] = lambda mem1, mem2: mem1 if mem1.get_evaluation( + ) <= mem2.get_evaluation() else mem2 + + self.assertEqual(80, memoryContainer.get_info(binaryOperator)) + + def test_set_i_specific(self) -> None: + + memoryContainer = MemoryContainer("TYPE") + + memoryContainer.set_info(75, 0.2, "TYPE2") + memoryContainer.set_info(70, 0.3, "TYPE3") + memoryContainer.set_info(80) + + memoryContainer.set_info(60, 1) + memoryContainer.set_info(90, 0.5, 2) + + self.assertEqual(60, memoryContainer.get_info(1)) + self.assertEqual(90, memoryContainer.get_info()) + self.assertEqual(0.5, memoryContainer.get_evaluation(), 0) + + def test_set_evaluation(self) -> None: + + memoryContainer = MemoryContainer("TYPE") + + memoryContainer.set_info(75, 0.2, "TYPE2") + memoryContainer.set_info(70, 0.3, "TYPE3") + + memoryContainer.set_info(90, 0.5, 2) + + self.assertEqual(70, memoryContainer.get_info()) + memoryContainer.set_evaluation(0.5, 0) + self.assertEqual(75, memoryContainer.get_info()) + + def test_set_evaluation_last(self) -> None: + memoryContainer = MemoryContainer("TYPE") + print("-- This test will raise a warning ...") + memoryContainer.set_evaluation(2.0) + self.assertEqual(memoryContainer.get_evaluation(), None) + memoryContainer.set_info("message") + memoryContainer.set_evaluation(2.0) + self.assertEqual(memoryContainer.get_evaluation(), 2.0) + + def test_get_timestamp_not_valid(self) -> None: + memoryContainer = MemoryContainer("TYPE") + print("-- This test will raise a warning ...") + ts: int = memoryContainer.get_timestamp() + self.assertEqual(ts, None) + memoryContainer.set_info("message") + ts = memoryContainer.get_timestamp() + self.assertTrue(ts != None) + + def test_add(self) -> None: + + memoryContainer = MemoryContainer("TYPE") + + memoryContainer.set_info(75, 0.2, "TYPE2") + memoryContainer.set_info(70, 0.3, "TYPE3") + memoryContainer.add(MemoryObject()) + + self.assertEqual(3, memoryContainer.getAllMemories().size()) + + def test_get_internal(self) -> None: + + memoryContainer = MemoryContainer("TYPE") + + memoryContainer.set_info(75, 0.2, "TYPE2") + memoryContainer.set_info(70, 0.3, "TYPE3") + + self.assertEqual( + 75, memoryContainer.getInternalMemory("TYPE2").get_info()) + self.assertIsNone(memoryContainer.getInternalMemory("TYPE4")) + + def test_get_timestamp(self) -> None: + + memoryContainer = MemoryContainer("TYPE") + + memoryContainer.set_info(75, 0.2, "TYPE2") + memoryContainer.set_info(70, 0.3, "TYPE3") + + self.assertEqual(memoryContainer.getInternalMemory("TYPE3").get_timestamp(), + memoryContainer.get_timestamp()) + + def test_max_policy(self) -> None: + memoryContainer = MemoryContainer("MAX") + memoryContainer.setPolicy(Policy.MAX) + m1 = memoryContainer.set_info(1, 0.2) + m2 = memoryContainer.set_info(2, 0.4) + m3 = memoryContainer.set_info(3, 0.8) + i: int = memoryContainer.get_info() + self.assertEqual(i, 3) + memoryContainer.set_evaluation(0.1) + i: int = memoryContainer.get_info() + self.assertEqual(i, 2) + memoryContainer.set_evaluation(0.1) + i: int = memoryContainer.get_info() + self.assertEqual(i, 1) + memoryContainer.set_evaluation(0.1, m1) + memoryContainer.set_evaluation(0.1, m2) + memoryContainer.set_evaluation(0.1, m3) + + for j in range(20): + m: int = memoryContainer.get_info() + ver: bool = (m == 1 or m == 2 or m == 3) + self.assertEqual(ver, True) + # print("max: "+m) + + memoryContainer.set_evaluation(0.05, m1) + for j in range(20): + m: int = memoryContainer.get_info() + ver: bool = (m == 2 or m == 3) + self.assertEqual(ver, True) + # print("max2: "+m) + + def test_max_policy_with_same_eval(self) -> None: + memoryContainer = MemoryContainer("MAX") + memoryContainer.setPolicy(Policy.MAX) + m1 = memoryContainer.set_info(1, 0.2) + m2 = memoryContainer.set_info(2, 0.2) + m3 = memoryContainer.set_info(3, 0.2) + i = -1 + oldi = 0 + for j in range(10): + oldi = i + # Despite the choice is random, if no chance in I happens, it stays the same + i: int = memoryContainer.get_info() + if (j > 0): + self.assertEqual(oldi, i) + + i2: int = 0 + k = 0 + + while True: + memoryContainer.set_info(1, m1) + memoryContainer.set_info(2, m2) + memoryContainer.set_info(3, m3) + i2: int = memoryContainer.get_info() + + k += 1 + + if not (i2 == i and k < 100): + break + + self.assertTrue(k != 100) + + def test_min_policy_with_same_eval(self) -> None: + memoryContainer = MemoryContainer("MIN") + memoryContainer.setPolicy(Policy.MIN) + m1: int = memoryContainer.set_info(1, 0.2) + m2: int = memoryContainer.set_info(2, 0.2) + m3: int = memoryContainer.set_info(3, 0.2) + i = -1 + oldi = 0 + for j in range(10): + oldi = i + # Despite the choice is random, if no chance in I happens, it stays the same + i: int = memoryContainer.get_info() + if (j > 0): + self.assertEqual(oldi, i) + + i2 = 0 + k = 0 + while True: + # Changing I will trigger a different choice, though ! + memoryContainer.set_info(1, m1) + memoryContainer.set_info(2, m2) + memoryContainer.set_info(3, m3) + i2: int = memoryContainer.get_info() + k += 1 + if not (i2 == i and k < 100): + break + self.assertTrue(k != 100) + + def test_max_unique_policy(self) -> None: + memoryContainer = MemoryContainer("MAX") + memoryContainer.setPolicy(Policy.MAX) + memoryContainer.set_info(1) + i: int = memoryContainer.get_info() + self.assertEqual(i, 1) + + def test_min_policy(self) -> None: + memoryContainer = MemoryContainer("MIN") + memoryContainer.setPolicy(Policy.MIN) + m1: int = memoryContainer.set_info(1, 0.2) + m2: int = memoryContainer.set_info(2, 0.4) + m3: int = memoryContainer.set_info(3, 0.8) + i: int = memoryContainer.get_info() + self.assertEqual(i, 1) + memoryContainer.set_evaluation(0.9) + i: int = memoryContainer.get_info() + self.assertEqual(i, 2) + memoryContainer.set_evaluation(0.9) + i: int = memoryContainer.get_info() + self.assertEqual(i, 3) + memoryContainer.set_evaluation(0.1, m1) + memoryContainer.set_evaluation(0.1, m2) + memoryContainer.set_evaluation(0.1, m3) + for k in range(20): + m: int = memoryContainer.get_info() + ver: bool = (m == 1 or m == 2 or m == 3) + self.assertEqual(ver, True) + # print("min: "+m) + + memoryContainer.set_evaluation(0.2, m1) + for k in range(20): + m: int = memoryContainer.get_info() + ver: bool = (m == 2 or m == 3) + self.assertEqual(ver, True) + # print("min2: "+m) + + def test_random_proportional_policy(self) -> None: + memoryContainer = MemoryContainer("RANDOMPROPORTIONAL") + memoryContainer.setPolicy(Policy.RANDOM_PROPORTIONA) + memoryContainer.set_info(1, 0.2) # 14 % + memoryContainer.set_info(2, 0.4) # 28 % + memoryContainer.set_info(3, 0.8) # 57 % + count = [0, 0, 0] + for i in range(1000): + j: int = memoryContainer.get_info() + count[j-1] += 1 + + # print("[0]: "+count[0]+" [1]: "+count[1]+" [2]: "+count[2]) + self.assertEqual(count[0] < count[1], True) + self.assertEqual(count[1] < count[2], True) + memoryContainer.set_evaluation(0.8, 0) + memoryContainer.set_evaluation(0.4, 1) + memoryContainer.set_evaluation(0.2, 2) + count = int[3] + for i in range(1000): + j: int = memoryContainer.get_info() + count[j-1] += 1 + + # print("[0]: "+count[0]+" [1]: "+count[1]+" [2]: "+count[2]) + self.assertEqual(count[0] > count[1], True) + self.assertEqual(count[1] > count[2], True) + memoryContainer.set_info(1, 0.5, 0) + memoryContainer.set_info(2, 0.0, 1) + memoryContainer.set_info(3, 0.0, 2) + for i in range(5): + j: int = memoryContainer.get_info() + self.assertEqual(j, 1) + + memoryContainer.set_info(1, 0.0, 0) + memoryContainer.set_info(2, 0.5, 1) + memoryContainer.set_info(3, 0.0, 2) + for i in range(5): + j: int = memoryContainer.get_info() + self.assertEqual(j, 2) + + memoryContainer.set_info(1, 0.0, 0) + memoryContainer.set_info(2, 0.0, 1) + memoryContainer.set_info(3, 0.5, 2) + for i in range(5): + j: int = memoryContainer.get_info() + self.assertEqual(j, 3) + + memoryContainer.set_info(1, 0.0, 0) + memoryContainer.set_info(2, 0.0, 1) + memoryContainer.set_info(3, 0.0, 2) + count = int[3] + for i in range(30): + j: int = memoryContainer.get_info() + count[j-1] += 1 + + # print("[0]: "+count[0]+" [1]: "+count[1]+" [2]: "+count[2]) + self.assertEqual(count[0] > 0, True) + self.assertEqual(count[1] > 0, True) + self.assertEqual(count[2] > 0, True) + + def test_random_proportional_stable_policy(self) -> None: + memoryContainer = MemoryContainer("RANDOMPROPORTIONALSTABLE") + memoryContainer.setPolicy(Policy.RANDOM_PROPORTIONAL_STABLE) + n = memoryContainer.set_info(1, 0.2) # 14 % + memoryContainer.set_info(2, 0.4) # 28 % + memoryContainer.set_info(3, 0.8) # 57 % + count = [0, 0, 0] + first = 0 + for i in range(1000): + j: int = memoryContainer.get_info() + if (i == 0): + first = j-1 + count[j-1] += 1 + + # print("[0]: "+count[0]+" [1]: "+count[1]+" [2]: "+count[2]+" first:"+first) + for i in range(3): + if (i == first): + self.assertEqual(count[i] > 0, True) + else: + self.assertEqual(count[i] > 0, False) + + count[0] = 0 + count[1] = 0 + count[2] = 0 + for i in range(1000): + memoryContainer.set_info(1, 0.2, n) + j: int = memoryContainer.get_info() + count[j-1] += 1 + + # print("[0]: "+count[0]+" [1]: "+count[1]+" [2]: "+count[2]) + self.assertEqual(count[0] < count[1], True) + self.assertEqual(count[1] < count[2], True) + + def test_random_flat(self) -> None: + memoryContainer = MemoryContainer("RANDOMFLAT") + memoryContainer.setPolicy(Policy.RANDOM_FLAT) + memoryContainer.set_info(1, 0.2) # 14 % + memoryContainer.set_info(2, 0.4) # 28 % + memoryContainer.set_info(3, 0.8) # 57 % + count = [0, 0, 0] + for i in range(1000): + j: int = memoryContainer.get_info() + count[j-1] += 1 + + self.assertEqual(count[0] > 0, True) + self.assertEqual(count[1] > 0, True) + self.assertEqual(count[2] > 0, True) + + def test_random_flat_stable(self) -> None: + memoryContainer = MemoryContainer("RANDOMFLATSTABLE") + memoryContainer.setPolicy(Policy.RANDOM_FLAT_STABLE) + n1 = memoryContainer.set_info(1, 0.2) # 14 % + memoryContainer.set_info(2, 0.4) # 28 % + memoryContainer.set_info(3, 0.8) # 57 % + count = [0, 0, 0] + first = 0 + for i in range(1000): + j: int = memoryContainer.get_info() + if (i == 0): + first = j-1 + count[j-1] += 1 + + for i in range(3): + if (i == first): + self.assertEqual(count[i] > 0, True) + else: + self.assertEqual(count[i] > 0, False) + + count[0] = 0 + count[1] = 0 + count[2] = 0 + for i in range(1000): + memoryContainer.set_info(1, 0.2, n1) + j: int = memoryContainer.get_info() + if (i == 0): + first = j-1 + count[j-1] += 1 + + self.assertEqual(count[0] > 0, True) + self.assertEqual(count[1] > 0, True) + self.assertEqual(count[2] > 0, True) + + def test_iterate_policy(self) -> None: + memoryContainer = MemoryContainer("ITERATE") + memoryContainer.setPolicy(Policy.ITERATE) + print("-- This test will raise a warning ...") + k: int = memoryContainer.get_info() + self.assertIsNone(k) + memoryContainer.set_info(1) + memoryContainer.set_info(2) + memoryContainer.set_info(3) + for i in range(9): + j: int = memoryContainer.get_info() + self.assertEqual(j, i % 3+1) + + def test_get_evaluation(self) -> None: + memoryContainer = MemoryContainer("TEST") + self.assertEqual(memoryContainer.get(-1), None) + self.assertEqual(memoryContainer.get(0), None) + self.assertEqual(memoryContainer.get(10), None) + self.assertEqual(memoryContainer.get_name(), "TEST") + memoryContainer.set_name("TEST-NEW") + self.assertEqual(memoryContainer.get_name(), "TEST-NEW") + memoryContainer.setType("TEST-NEW") + self.assertEqual(memoryContainer.get_name(), "TEST-NEW") + # Testing the getEvaluation without any included MemoryObject + self.assertEqual(memoryContainer.get_evaluation(), None) + self.assertEqual(memoryContainer.get_evaluation(0), None) + self.assertEqual(memoryContainer.get_evaluation(1), None) + self.assertEqual(memoryContainer.getPolicy(), Policy.MAX) + res: float = memoryContainer.get_evaluation() + self.assertEqual(res, None) + memoryContainer.set_info(1) + memoryContainer.set_evaluation(0.5) + self.assertEqual(memoryContainer.get_evaluation(), 0.5) + self.assertEqual(memoryContainer.get_evaluation(0), 0.5) + memoryContainer.setPolicy(Policy.ITERATE) + self.assertEqual(memoryContainer.getPolicy(), Policy.ITERATE) + i: int = memoryContainer.get_info() + self.assertEqual(i, 1) + i: int = memoryContainer.getLastI() + self.assertEqual(i, 1) + mo: MemoryObject = memoryContainer.getLast() + i: int = mo.get_info() + self.assertEqual(i, 1) + memoryContainer.set_evaluation(0.6, 0) + self.assertEqual(memoryContainer.get_evaluation(), 0.6) + self.assertEqual(memoryContainer.get_evaluation(0), 0.6) + + def test_get_timestamp(self) -> None: + memoryContainer = MemoryContainer("TEST") + # Without any initialization, the timestamp must be None + self.assertEqual(memoryContainer.get_timestamp(), None) + print("This test will raise a warning...") + self.assertEqual(memoryContainer.get_timestamp(0), None) + print("This test will raise a warning...") + self.assertEqual(memoryContainer.get_timestamp(1), None) + # after we initialize the container, the timestamp must be something different from None + memoryContainer.set_info(1) + self.assertEqual(memoryContainer.get_timestamp() != None, True) + self.assertEqual(memoryContainer.get_timestamp(0) != None, True) + # nevertheless, if we go further, it should remain None + print("This test will raise a warning...") + self.assertEqual(memoryContainer.get_timestamp(1), None) + self.assertEqual(memoryContainer.get( + 0).get_info(), memoryContainer.get_info()) + + def test_double_indirection(self) -> None: + mc1: MemoryContainer = MemoryContainer("TEST1") + mc2: MemoryContainer = MemoryContainer("TEST2") + mc2.set_info(0) + mc1.add(mc2) + self.assertEqual(mc1.get_info(), 0) + mc1.set_info(1, 0.5, 0) + self.assertEqual(mc1.get_info(), 1) + mc1.set_evaluation(0.6, 0) + self.assertEqual(mc1.get_evaluation(), 0.6) diff --git a/tests/core/entities/MemoryObjectTest.py b/tests/core/entities/test_MemoryObjectTest.py similarity index 100% rename from tests/core/entities/MemoryObjectTest.py rename to tests/core/entities/test_MemoryObjectTest.py diff --git a/tests/core/entities/MemoryTest.py b/tests/core/entities/test_MemoryTest.py similarity index 100% rename from tests/core/entities/MemoryTest.py rename to tests/core/entities/test_MemoryTest.py diff --git a/tests/core/entities/RawMemoryTest.py b/tests/core/entities/test_RawMemoryTest.py similarity index 100% rename from tests/core/entities/RawMemoryTest.py rename to tests/core/entities/test_RawMemoryTest.py diff --git a/tests/core/entities/MindTest.py b/tests/core/entities/test_mind.py similarity index 98% rename from tests/core/entities/MindTest.py rename to tests/core/entities/test_mind.py index d23628d..3a44d81 100644 --- a/tests/core/entities/MindTest.py +++ b/tests/core/entities/test_mind.py @@ -4,7 +4,7 @@ from .utils import CodeletMock -class MindTest(unittest.TestCase): +class TestMind(unittest.TestCase): def setUp(self) -> None: self.test_codelet = CodeletMock() self.mind = Mind()