File size: 7,934 Bytes
7ccd32c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import torch
import numpy as np
import pytest
from singularity_core import SingularityCore

class TestSingularityCore:
    """Тесты сингулярного ядра самозаворачивания."""
    
    def setup_method(self):
        """Инициализация для каждого теста."""
        self.singularity = SingularityCore(
            singularity_radius=0.01,
            wormhole_depth=2.1,
            rotation_speed=0.0,
            embedding_dim=256
        )
        
        # Создаем тестовое состояние
        self.test_state = torch.randn(1, 10, 256)
    
    def test_singularity_initialization(self):
        """Тест инициализации сингулярного ядра."""
        assert self.singularity.singularity_radius == 0.01
        assert self.singularity.wormhole_depth == 2.1
        assert self.singularity.rotation_speed == 0.0
        assert self.singularity.embedding_dim == 256
        assert self.singularity.singularity_cycles == 0
    
    def test_singularity_transform(self):
        """Тест сингулярной трансформации."""
        original_state = self.test_state.clone()
        transformed_state = self.singularity.compute_singularity_transform(original_state, cycle=1)
        
        # Проверяем, что состояние действительно изменилось
        state_diff = torch.norm(transformed_state - original_state)
        assert state_diff > 0, "Сингулярная трансформация должна изменять состояние"
        
        # Проверяем размерности
        assert transformed_state.shape == original_state.shape
    
    def test_self_wrapping_cycle(self):
        """Тест полного цикла самозаворачивания."""
        result = self.singularity.self_wrapping_cycle(self.test_state, "Тестовый ввод")
        
        # Проверяем успешность операции
        assert result["success"] == True
        assert result["cycle"] == 1
        
        # Проверяем метрики
        metrics = result["metrics"]
        assert "state_transformation" in metrics
        assert "curvature" in metrics
        assert "entropy" in metrics
        assert "coherence" in metrics
        
        # Проверяем, что трансформация произошла
        assert metrics["state_transformation"] > 0
    
    def test_parameter_update(self):
        """Тест обновления параметров сингулярности."""
        original_radius = self.singularity.singularity_radius
        original_depth = self.singularity.wormhole_depth
        
        self.singularity.update_parameters(
            singularity_radius=0.05,
            wormhole_depth=2.5
        )
        
        assert self.singularity.singularity_radius == 0.05
        assert self.singularity.wormhole_depth == 2.5
        assert self.singularity.singularity_radius != original_radius
        assert self.singularity.wormhole_depth != original_depth
    
    def test_singularity_state(self):
        """Тест получения состояния сингулярности."""
        state = self.singularity.get_singularity_state()
        
        assert "singularity_radius" in state
        assert "wormhole_depth" in state
        assert "rotation_speed" in state
        assert "cycles" in state
        assert "current_state_shape" in state
        assert "transformation_history_length" in state
    
    def test_multiple_cycles(self):
        """Тест множественных циклов самозаворачивания."""
        # Первый цикл
        result1 = self.singularity.self_wrapping_cycle(self.test_state, "Цикл 1")
        cycle1 = result1["cycle"]
        
        # Второй цикл
        result2 = self.singularity.self_wrapping_cycle(self.test_state, "Цикл 2")
        cycle2 = result2["cycle"]
        
        # Проверяем, что циклы увеличиваются
        assert cycle2 > cycle1
        assert cycle2 == cycle1 + 1
        
        # Проверяем историю трансформаций
        assert len(self.singularity.transformation_history) == 2
    
    def test_wormhole_effect(self):
        """Тест wormhole эффекта."""
        # Создаем состояние с известными значениями
        test_state = torch.ones(1, 10, 256)
        
        # Применяем трансформацию с разными циклами
        transformed1 = self.singularity.compute_singularity_transform(test_state, cycle=1)
        transformed2 = self.singularity.compute_singularity_transform(test_state, cycle=2)
        
        # Проверяем, что результаты разные для разных циклов
        diff = torch.norm(transformed1 - transformed2)
        assert diff > 0, "Wormhole эффект должен давать разные результаты для разных циклов"
    
    def test_curvature_calculation(self):
        """Тест вычисления кривизны."""
        original_state = self.test_state.clone()
        transformed_state = self.singularity.compute_singularity_transform(original_state)
        
        metrics = self.singularity._compute_transformation_metrics(original_state, transformed_state)
        
        # Проверяем, что кривизна вычисляется корректно
        assert metrics["curvature"] >= 0
        assert not np.isnan(metrics["curvature"])
        assert not np.isinf(metrics["curvature"])
    
    def test_entropy_calculation(self):
        """Тест вычисления энтропии."""
        original_state = self.test_state.clone()
        transformed_state = self.singularity.compute_singularity_transform(original_state)
        
        metrics = self.singularity._compute_transformation_metrics(original_state, transformed_state)
        
        # Проверяем, что энтропия вычисляется корректно
        assert metrics["entropy"] >= 0
        assert not np.isnan(metrics["entropy"])
        assert not np.isinf(metrics["entropy"])
    
    def test_coherence_calculation(self):
        """Тест вычисления когерентности."""
        original_state = self.test_state.clone()
        transformed_state = self.singularity.compute_singularity_transform(original_state)
        
        metrics = self.singularity._compute_transformation_metrics(original_state, transformed_state)
        
        # Проверяем, что когерентность в разумных пределах
        assert 0 <= metrics["coherence"] <= 1
        assert not np.isnan(metrics["coherence"])
    
    def test_singularity_response_generation(self):
        """Тест генерации ответа сингулярности."""
        metrics = {
            "state_transformation": 0.15,
            "curvature": 0.6,
            "entropy": 2.5,
            "coherence": 0.8
        }
        
        response = self.singularity._generate_singularity_response(metrics, "Тестовый ввод")
        
        # Проверяем, что ответ содержит ожидаемые элементы
        assert "Сингулярность активирована" in response
        assert "Высокая кривизна" in response
        assert "Информационная энтропия" in response
        assert "Когерентность сингулярности" in response
        assert "Тестовый ввод" in response

if __name__ == "__main__":
    # Запуск тестов
    pytest.main([__file__, "-v"])