-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPanicMappingTest.php
More file actions
240 lines (194 loc) · 7.3 KB
/
Copy pathPanicMappingTest.php
File metadata and controls
240 lines (194 loc) · 7.3 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
declare(strict_types=1);
namespace PhpMlKit\NDArray\Tests\Unit;
use PhpMlKit\NDArray\DType;
use PhpMlKit\NDArray\Exceptions\DTypeException;
use PhpMlKit\NDArray\Exceptions\IndexException;
use PhpMlKit\NDArray\Exceptions\NDArrayException;
use PhpMlKit\NDArray\Exceptions\ShapeException;
use PhpMlKit\NDArray\NDArray;
use PHPUnit\Framework\TestCase;
/**
* Comprehensive tests for panic-to-exception mapping.
*
* These tests intentionally trigger real error conditions to verify
* that Rust panics/errors are properly mapped to PHP exceptions.
*
* @covers \PhpMlKit\NDArray\FFI\Lib::checkStatus
*
* @internal
*/
final class PanicMappingTest extends TestCase
{
public function testConcatenateIncompatibleShapesThrowsShapeException(): void
{
$a = NDArray::array([[1, 2], [3, 4]], DType::Float64); // 2x2
$b = NDArray::array([[5, 6, 7]], DType::Float64); // 1x3
$this->expectException(ShapeException::class);
NDArray::concatenate([$a, $b], 0);
}
public function testConcatenateDifferentDimensionsThrowsShapeException(): void
{
$a = NDArray::array([1, 2, 3], DType::Float64); // 1D
$b = NDArray::array([[4, 5, 6]], DType::Float64); // 2D
$this->expectException(ShapeException::class);
NDArray::concatenate([$a, $b], 0);
}
public function testStackIncompatibleShapesThrowsShapeException(): void
{
$a = NDArray::array([1, 2], DType::Float64); // length 2
$b = NDArray::array([3, 4, 5], DType::Float64); // length 3
$this->expectException(ShapeException::class);
NDArray::stack([$a, $b], 0);
}
public function testInvalidAxisForSumThrowsShapeException(): void
{
$arr = NDArray::array([[1, 2], [3, 4]], DType::Float64);
$this->expectException(ShapeException::class);
$arr->sum(5); // Axis 5 doesn't exist (only 0 and 1)
}
public function testInvalidAxisForMeanThrowsShapeException(): void
{
$arr = NDArray::array([[1, 2], [3, 4]], DType::Float64);
$this->expectException(ShapeException::class);
$arr->mean(-3); // Axis -3 is out of bounds for 2D array
}
public function testInvalidAxisForMinThrowsShapeException(): void
{
$arr = NDArray::array([1, 2, 3], DType::Float64);
$this->expectException(ShapeException::class);
$arr->min(1); // Axis 1 doesn't exist for 1D array
}
public function testInvalidAxisForMaxThrowsShapeException(): void
{
$arr = NDArray::array([1, 2, 3], DType::Float64);
$this->expectException(ShapeException::class);
$arr->max(1); // Axis 1 doesn't exist for 1D array
}
public function testInvalidAxisForArgminThrowsShapeException(): void
{
$arr = NDArray::array([[1, 2], [3, 4]], DType::Float64);
$this->expectException(ShapeException::class);
$arr->argmin(5);
}
public function testInvalidAxisForArgmaxThrowsShapeException(): void
{
$arr = NDArray::array([[1, 2], [3, 4]], DType::Float64);
$this->expectException(ShapeException::class);
$arr->argmax(5);
}
public function testInvalidAxisForCumsumThrowsShapeException(): void
{
$arr = NDArray::array([[1, 2], [3, 4]], DType::Float64);
$this->expectException(ShapeException::class);
$arr->cumsum(5);
}
public function testInvalidAxisForCumprodThrowsShapeException(): void
{
$arr = NDArray::array([[1, 2], [3, 4]], DType::Float64);
$this->expectException(ShapeException::class);
$arr->cumprod(5);
}
public function testInvalidAxisForSortThrowsShapeException(): void
{
$arr = NDArray::array([[1, 2], [3, 4]], DType::Float64);
$this->expectException(ShapeException::class);
$arr->sort(5);
}
public function testInvalidAxisForArgsortThrowsShapeException(): void
{
$arr = NDArray::array([[1, 2], [3, 4]], DType::Float64);
$this->expectException(ShapeException::class);
$arr->argsort(5);
}
public function testEmptyConcatenateThrowsShapeException(): void
{
$this->expectException(ShapeException::class);
$this->expectExceptionMessage('concatenate requires at least one array');
NDArray::concatenate([], 0);
}
public function testEmptyStackThrowsShapeException(): void
{
$this->expectException(ShapeException::class);
NDArray::stack([], 0);
}
public function testTakeWithInvalidIndicesThrowsIndexException(): void
{
$arr = NDArray::array([1, 2, 3, 4, 5], DType::Float64);
$indices = NDArray::array([0, 1, 10], DType::Int64); // 10 is out of bounds
$this->expectException(IndexException::class);
$arr->take($indices);
}
public function testBincountOnFloatArrayThrowsDTypeException(): void
{
$arr = NDArray::array([1.5, 2.5, 3.5], DType::Float64);
$this->expectException(DTypeException::class);
$arr->bincount();
}
public function testStdOnSingleElementThrowsException(): void
{
$arr = NDArray::array([42], DType::Float64);
// std with ddof=1 on single element is undefined (division by zero)
$this->expectException(NDArrayException::class);
$this->expectExceptionMessage('ddof');
$arr->std(null, 1);
}
public function testVarOnSingleElementThrowsException(): void
{
$arr = NDArray::array([42], DType::Float64);
// var with ddof=1 on single element is undefined (division by zero)
$this->expectException(NDArrayException::class);
$this->expectExceptionMessage('ddof');
$arr->var(null, 1);
}
public function testShapeErrorMessageIsDescriptive(): void
{
$arr = NDArray::array([[1, 2], [3, 4]], DType::Float64);
try {
$arr->sum(5);
$this->fail('Expected ShapeException');
} catch (ShapeException $e) {
$message = $e->getMessage();
$this->assertNotEmpty($message);
// Should mention the invalid axis
$this->assertStringContainsString('5', $message);
// Should not be generic panic message
$this->assertStringNotContainsString('Rust panic occurred', $message);
}
}
public function testConcatenateErrorMessageIsDescriptive(): void
{
$a = NDArray::array([[1, 2]], DType::Float64);
$b = NDArray::array([[3, 4, 5]], DType::Float64);
try {
NDArray::concatenate([$a, $b], 0);
$this->fail('Expected ShapeException');
} catch (ShapeException $e) {
$message = $e->getMessage();
$this->assertNotEmpty($message);
$this->assertStringNotContainsString('Rust panic occurred', $message);
}
}
/**
* @dataProvider reductionOperationsProvider
*/
public function testAllReductionsThrowShapeExceptionOnInvalidAxis(string $method): void
{
$arr = NDArray::array([[1, 2], [3, 4]], DType::Float64);
$this->expectException(ShapeException::class);
$arr->{$method}(5);
}
/**
* @return array<array{string}>
*/
public static function reductionOperationsProvider(): array
{
return [
['sum'],
['mean'],
['min'],
['max'],
['product'],
];
}
}