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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Tests du Système de Mémoire Barouia-Cortex Ultimate
|
| 4 |
+
Tests complets pour la mémoire quantique et classique
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import asyncio
|
| 8 |
+
import pytest
|
| 9 |
+
import numpy as np
|
| 10 |
+
from cortex.memory.quantum_memory import QuantumMemory, QuantumMemoryCell
|
| 11 |
+
from cortex.memory.hierarchical import HierarchicalMemory
|
| 12 |
+
from cortex.memory.associative import AssociativeMemory
|
| 13 |
+
|
| 14 |
+
class TestQuantumMemory:
|
| 15 |
+
"""Tests pour la mémoire quantique"""
|
| 16 |
+
|
| 17 |
+
@pytest.fixture
|
| 18 |
+
async def quantum_memory(self):
|
| 19 |
+
"""Fixture pour initialiser la mémoire quantique"""
|
| 20 |
+
memory = QuantumMemory(capacity=100)
|
| 21 |
+
await memory.initialize()
|
| 22 |
+
return memory
|
| 23 |
+
|
| 24 |
+
@pytest.mark.asyncio
|
| 25 |
+
async def test_memory_initialization(self, quantum_memory):
|
| 26 |
+
"""Test l'initialisation de la mémoire quantique"""
|
| 27 |
+
assert quantum_memory is not None
|
| 28 |
+
assert quantum_memory.capacity == 100
|
| 29 |
+
assert len(quantum_memory.memory_cells) > 0
|
| 30 |
+
assert quantum_memory.access_latency > 0
|
| 31 |
+
|
| 32 |
+
@pytest.mark.asyncio
|
| 33 |
+
async def test_data_integrity(self, quantum_memory):
|
| 34 |
+
"""Test l'intégrité des données stockées"""
|
| 35 |
+
test_data = {
|
| 36 |
+
"string": "Hello Quantum World",
|
| 37 |
+
"number": 42,
|
| 38 |
+
"list": [1, 2, 3, 4, 5],
|
| 39 |
+
"dict": {"key": "value", "nested": {"a": 1, "b": 2}}
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
for key, value in test_data.items():
|
| 43 |
+
address = await quantum_memory.store_quantum_data(value, f"test_{key}")
|
| 44 |
+
retrieved = await quantum_memory.retrieve_quantum_data(address)
|
| 45 |
+
assert retrieved == value, f"Data integrity failed for {key}"
|
| 46 |
+
|
| 47 |
+
@pytest.mark.asyncio
|
| 48 |
+
async def test_quantum_coherence(self, quantum_memory):
|
| 49 |
+
"""Test la préservation de la cohérence quantique"""
|
| 50 |
+
address = await quantum_memory.store_quantum_data("coherence_test")
|
| 51 |
+
cell = quantum_memory.memory_cells[address]
|
| 52 |
+
|
| 53 |
+
# Vérifie la cohérence initiale
|
| 54 |
+
assert cell.coherence > 0.9
|
| 55 |
+
|
| 56 |
+
# Simule le temps qui passe
|
| 57 |
+
await asyncio.sleep(0.1)
|
| 58 |
+
await quantum_memory._maintain_coherence(address)
|
| 59 |
+
|
| 60 |
+
# Vérifie que la cohérence a diminué mais pas trop
|
| 61 |
+
assert 0.8 < cell.coherence < 1.0
|
| 62 |
+
|
| 63 |
+
@pytest.mark.asyncio
|
| 64 |
+
async def test_entanglement_persistence(self, quantum_memory):
|
| 65 |
+
"""Test la persistance de l'intrication"""
|
| 66 |
+
addr1 = await quantum_memory.store_quantum_data("entangled_data_1")
|
| 67 |
+
addr2 = await quantum_memory.store_quantum_data("entangled_data_2")
|
| 68 |
+
|
| 69 |
+
# Crée l'intrication
|
| 70 |
+
await quantum_memory.entangle_memory_cells(addr1, addr2)
|
| 71 |
+
|
| 72 |
+
# Vérifie que l'intrication est persistée
|
| 73 |
+
cell1 = quantum_memory.memory_cells[addr1]
|
| 74 |
+
cell2 = quantum_memory.memory_cells[addr2]
|
| 75 |
+
|
| 76 |
+
assert addr2 in cell1.entangled_with
|
| 77 |
+
assert addr1 in cell2.entangled_with
|
| 78 |
+
|
| 79 |
+
@pytest.mark.asyncio
|
| 80 |
+
async def test_superposition_retrieval(self, quantum_memory):
|
| 81 |
+
"""Test la récupération depuis la superposition"""
|
| 82 |
+
data_states = ["state_alpha", "state_beta", "state_gamma"]
|
| 83 |
+
address = await quantum_memory.quantum_superposition_store(data_states)
|
| 84 |
+
|
| 85 |
+
# Récupération sans mesure (devrait retourner toutes les données)
|
| 86 |
+
retrieved = await quantum_memory.retrieve_quantum_data(address, measure=False)
|
| 87 |
+
assert retrieved == data_states
|
| 88 |
+
|
| 89 |
+
# Récupération avec mesure (devrait retourner un état spécifique)
|
| 90 |
+
measured = await quantum_memory.retrieve_quantum_data(address, measure=True)
|
| 91 |
+
assert measured in data_states
|
| 92 |
+
|
| 93 |
+
@pytest.mark.asyncio
|
| 94 |
+
async def test_pattern_recognition(self, quantum_memory):
|
| 95 |
+
"""Test la reconnaissance de pattern quantique"""
|
| 96 |
+
# Stocke différents patterns
|
| 97 |
+
patterns = [
|
| 98 |
+
"quantum computing is amazing",
|
| 99 |
+
"artificial intelligence revolution",
|
| 100 |
+
"quantum artificial intelligence",
|
| 101 |
+
"machine learning with quantum"
|
| 102 |
+
]
|
| 103 |
+
|
| 104 |
+
addresses = []
|
| 105 |
+
for pattern in patterns:
|
| 106 |
+
addr = await quantum_memory.store_quantum_data(pattern)
|
| 107 |
+
addresses.append(addr)
|
| 108 |
+
|
| 109 |
+
# Test la reconnaissance
|
| 110 |
+
results = await quantum_memory.quantum_pattern_recognition("quantum")
|
| 111 |
+
assert len(results) >= 2 # Au moins 2 patterns contiennent "quantum"
|
| 112 |
+
|
| 113 |
+
results = await quantum_memory.quantum_pattern_recognition("artificial intelligence")
|
| 114 |
+
assert len(results) >= 2
|
| 115 |
+
|
| 116 |
+
@pytest.mark.asyncio
|
| 117 |
+
async def test_memory_statistics(self, quantum_memory):
|
| 118 |
+
"""Test les statistiques de mémoire"""
|
| 119 |
+
# Remplit partiellement la mémoire
|
| 120 |
+
for i in range(25):
|
| 121 |
+
await quantum_memory.store_quantum_data(f"test_data_{i}")
|
| 122 |
+
|
| 123 |
+
stats = quantum_memory.get_memory_statistics()
|
| 124 |
+
|
| 125 |
+
assert stats["total_capacity"] == 100
|
| 126 |
+
assert stats["used_cells"] == 25
|
| 127 |
+
assert stats["available_cells"] == 75
|
| 128 |
+
assert 0 <= stats["memory_usage"] <= 1
|
| 129 |
+
|
| 130 |
+
class TestHierarchicalMemory:
|
| 131 |
+
"""Tests pour la mémoire hiérarchique"""
|
| 132 |
+
|
| 133 |
+
@pytest.fixture
|
| 134 |
+
async def hierarchical_memory(self):
|
| 135 |
+
"""Fixture pour initialiser la mémoire hiérarchique"""
|
| 136 |
+
memory = HierarchicalMemory()
|
| 137 |
+
await memory.initialize()
|
| 138 |
+
return memory
|
| 139 |
+
|
| 140 |
+
@pytest.mark.asyncio
|
| 141 |
+
async def test_memory_hierarchy(self, hierarchical_memory):
|
| 142 |
+
"""Test la hiérarchie de mémoire"""
|
| 143 |
+
# Test des différents niveaux
|
| 144 |
+
data_immediate = "immediate data"
|
| 145 |
+
data_short = "short term data"
|
| 146 |
+
data_long = "long term data"
|
| 147 |
+
|
| 148 |
+
# Stockage à différents niveaux
|
| 149 |
+
await hierarchical_memory.store(data_immediate, "immediate", priority=10)
|
| 150 |
+
await hierarchical_memory.store(data_short, "short_term", priority=5)
|
| 151 |
+
await hierarchical_memory.store(data_long, "long_term", priority=1)
|
| 152 |
+
|
| 153 |
+
# Vérification de l'accès
|
| 154 |
+
immediate = await hierarchical_memory.retrieve("immediate")
|
| 155 |
+
assert immediate == data_immediate
|
| 156 |
+
|
| 157 |
+
# Test de la promotion dans la hiérarchie
|
| 158 |
+
await hierarchical_memory.promote("long_term")
|
| 159 |
+
|
| 160 |
+
stats = hierarchical_memory.get_stats()
|
| 161 |
+
assert stats["total_items"] == 3
|
| 162 |
+
|
| 163 |
+
class TestAssociativeMemory:
|
| 164 |
+
"""Tests pour la mémoire associative"""
|
| 165 |
+
|
| 166 |
+
@pytest.fixture
|
| 167 |
+
async def associative_memory(self):
|
| 168 |
+
"""Fixture pour initialiser la mémoire associative"""
|
| 169 |
+
memory = AssociativeMemory()
|
| 170 |
+
await memory.initialize()
|
| 171 |
+
return memory
|
| 172 |
+
|
| 173 |
+
@pytest.mark.asyncio
|
| 174 |
+
async def test_associative_links(self, associative_memory):
|
| 175 |
+
"""Test les liens associatifs"""
|
| 176 |
+
# Crée des associations
|
| 177 |
+
await associative_memory.create_association("quantum", "computing", strength=0.9)
|
| 178 |
+
await associative_memory.create_association("quantum", "physics", strength=0.8)
|
| 179 |
+
await associative_memory.create_association("ai", "machine learning", strength=0.95)
|
| 180 |
+
|
| 181 |
+
# Test la récupération associative
|
| 182 |
+
quantum_associations = await associative_memory.get_associations("quantum")
|
| 183 |
+
assert len(quantum_associations) == 2
|
| 184 |
+
assert any("computing" in assoc for assoc in quantum_associations)
|
| 185 |
+
|
| 186 |
+
ai_associations = await associative_memory.get_associations("ai")
|
| 187 |
+
assert len(ai_associations) == 1
|
| 188 |
+
|
| 189 |
+
@pytest.mark.asyncio
|
| 190 |
+
async def test_pattern_completion(self, associative_memory):
|
| 191 |
+
"""Test la complétion de pattern"""
|
| 192 |
+
# Entraîne la mémoire avec des patterns
|
| 193 |
+
patterns = [
|
| 194 |
+
("cat", "animal", "feline", "pet"),
|
| 195 |
+
("dog", "animal", "canine", "pet"),
|
| 196 |
+
("quantum", "physics", "computing", "mechanics")
|
| 197 |
+
]
|
| 198 |
+
|
| 199 |
+
for pattern in patterns:
|
| 200 |
+
for i, concept in enumerate(pattern):
|
| 201 |
+
if i < len(pattern) - 1:
|
| 202 |
+
await associative_memory.create_association(
|
| 203 |
+
concept, pattern[i + 1], strength=0.8
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
# Test la complétion
|
| 207 |
+
completion = await associative_memory.pattern_completion("cat", "animal")
|
| 208 |
+
assert "feline" in completion or "pet" in completion
|
| 209 |
+
|
| 210 |
+
@pytest.mark.asyncio
|
| 211 |
+
async def test_memory_system_integration():
|
| 212 |
+
"""Test d'intégration du système de mémoire complet"""
|
| 213 |
+
# Initialisation de toutes les mémoires
|
| 214 |
+
quantum_mem = QuantumMemory(50)
|
| 215 |
+
hierarchical_mem = HierarchicalMemory()
|
| 216 |
+
associative_mem = AssociativeMemory()
|
| 217 |
+
|
| 218 |
+
await quantum_mem.initialize()
|
| 219 |
+
await hierarchical_mem.initialize()
|
| 220 |
+
await associative_mem.initialize()
|
| 221 |
+
|
| 222 |
+
# Workflow de test intégré
|
| 223 |
+
# 1. Stockage quantique
|
| 224 |
+
quantum_data = "Quantum memory test data"
|
| 225 |
+
q_address = await quantum_mem.store_quantum_data(quantum_data)
|
| 226 |
+
|
| 227 |
+
# 2. Stockage hiérarchique
|
| 228 |
+
await hierarchical_mem.store(quantum_data, "quantum_backup", priority=7)
|
| 229 |
+
|
| 230 |
+
# 3. Création d'associations
|
| 231 |
+
await associative_mem.create_association("quantum", "memory", strength=0.9)
|
| 232 |
+
await associative_mem.create_association("memory", "storage", strength=0.8)
|
| 233 |
+
|
| 234 |
+
# Vérifications
|
| 235 |
+
assert q_address is not None
|
| 236 |
+
hierarchical_data = await hierarchical_mem.retrieve("quantum_backup")
|
| 237 |
+
assert hierarchical_data == quantum_data
|
| 238 |
+
|
| 239 |
+
associations = await associative_mem.get_associations("quantum")
|
| 240 |
+
assert len(associations) > 0
|
| 241 |
+
|
| 242 |
+
print("✅ Tests d'intégration mémoire réussis!")
|
| 243 |
+
|
| 244 |
+
if __name__ == "__main__":
|
| 245 |
+
# Exécution des tests
|
| 246 |
+
asyncio.run(test_memory_system_integration())
|