-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypePromotionTest.php
More file actions
245 lines (196 loc) · 8.12 KB
/
Copy pathTypePromotionTest.php
File metadata and controls
245 lines (196 loc) · 8.12 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
241
242
243
244
245
<?php
declare(strict_types=1);
namespace PhpMlKit\NDArray\Tests\Unit;
use PhpMlKit\NDArray\Complex;
use PhpMlKit\NDArray\DType;
use PhpMlKit\NDArray\NDArray;
use PHPUnit\Framework\TestCase;
/**
* @internal
*
* @coversNothing
*/
final class TypePromotionTest extends TestCase
{
// ==================== Arithmetic Operations ====================
public function testIntArrayPlusFloatScalarPromotesToFloat64(): void
{
$a = NDArray::array([1, 2, 3], DType::Int64);
$result = $a->add(1.5);
$this->assertSame(DType::Float64, $result->dtype());
$this->assertEqualsWithDelta([2.5, 3.5, 4.5], $result->toArray(), 0.0001);
}
public function testIntArrayMinusFloatScalarPromotesToFloat64(): void
{
$a = NDArray::array([10, 20, 30], DType::Int64);
$result = $a->subtract(2.5);
$this->assertSame(DType::Float64, $result->dtype());
$this->assertEqualsWithDelta([7.5, 17.5, 27.5], $result->toArray(), 0.0001);
}
public function testIntArrayTimesFloatScalarPromotesToFloat64(): void
{
$a = NDArray::array([2, 4, 6], DType::Int64);
$result = $a->multiply(1.5);
$this->assertSame(DType::Float64, $result->dtype());
$this->assertEqualsWithDelta([3.0, 6.0, 9.0], $result->toArray(), 0.0001);
}
public function testIntArrayDividedByFloatScalarPromotesToFloat64(): void
{
$a = NDArray::array([10, 20, 30], DType::Int64);
$result = $a->divide(2.5);
$this->assertSame(DType::Float64, $result->dtype());
$this->assertEqualsWithDelta([4.0, 8.0, 12.0], $result->toArray(), 0.0001);
}
public function testFloatArrayPlusIntScalarStaysFloat64(): void
{
$a = NDArray::array([1.5, 2.5, 3.5], DType::Float64);
$result = $a->add(10);
$this->assertSame(DType::Float64, $result->dtype());
$this->assertEqualsWithDelta([11.5, 12.5, 13.5], $result->toArray(), 0.0001);
}
public function testIntArrayPlusComplexScalarPromotesToComplex128(): void
{
$a = NDArray::array([1, 2, 3], DType::Int64);
$complex = new Complex(3.0, 4.0);
$result = $a->add($complex);
$this->assertSame(DType::Complex128, $result->dtype());
$values = $result->toArray();
$this->assertCount(3, $values);
$this->assertEqualsWithDelta(4.0, $values[0]->real, 0.0001);
$this->assertEqualsWithDelta(4.0, $values[0]->imag, 0.0001);
}
public function testFloatArrayPlusComplexScalarPromotesToComplex128(): void
{
$a = NDArray::array([1.5, 2.5], DType::Float64);
$complex = new Complex(1.0, 2.0);
$result = $a->add($complex);
$this->assertSame(DType::Complex128, $result->dtype());
$values = $result->toArray();
$this->assertEqualsWithDelta(2.5, $values[0]->real, 0.0001);
$this->assertEqualsWithDelta(2.0, $values[0]->imag, 0.0001);
}
public function testIntArrayRemFloatScalarPromotesToFloat64(): void
{
$a = NDArray::array([10, 20, 30], DType::Int64);
$result = $a->rem(3.5);
$this->assertSame(DType::Float64, $result->dtype());
$this->assertEqualsWithDelta([3.0, 2.5, 2.0], $result->toArray(), 0.0001);
}
// ==================== Comparison Operations ====================
public function testIntArrayEqFloatScalar(): void
{
$a = NDArray::array([1, 2, 3], DType::Int64);
$result = $a->eq(2.0);
$this->assertSame(DType::Bool, $result->dtype());
$this->assertSame([false, true, false], $result->toArray());
}
public function testIntArrayGtFloatScalar(): void
{
$a = NDArray::array([1, 2, 3], DType::Int64);
$result = $a->gt(1.5);
$this->assertSame(DType::Bool, $result->dtype());
$this->assertSame([false, true, true], $result->toArray());
}
public function testFloatArrayLtIntScalar(): void
{
$a = NDArray::array([1.5, 2.5, 3.5], DType::Float64);
$result = $a->lt(3);
$this->assertSame(DType::Bool, $result->dtype());
$this->assertSame([true, true, false], $result->toArray());
}
// ==================== Bitwise Operations ====================
public function testIntArrayBitandIntScalar(): void
{
$a = NDArray::array([7, 14, 21], DType::Int64);
$result = $a->bitand(3);
$this->assertSame(DType::Int64, $result->dtype());
$this->assertSame([7 & 3, 14 & 3, 21 & 3], $result->toArray());
}
public function testIntArrayBitorIntScalar(): void
{
$a = NDArray::array([1, 2, 4], DType::Int64);
$result = $a->bitor(8);
$this->assertSame(DType::Int64, $result->dtype());
$this->assertSame([1 | 8, 2 | 8, 4 | 8], $result->toArray());
}
public function testIntArrayLeftShiftScalar(): void
{
$a = NDArray::array([1, 2, 3], DType::Int64);
$result = $a->leftShift(2);
$this->assertSame(DType::Int64, $result->dtype());
$this->assertSame([4, 8, 12], $result->toArray());
}
public function testIntArrayRightShiftScalar(): void
{
$a = NDArray::array([8, 16, 32], DType::Int64);
$result = $a->rightShift(2);
$this->assertSame(DType::Int64, $result->dtype());
$this->assertSame([2, 4, 8], $result->toArray());
}
// ==================== Edge Cases ====================
public function testSameTypeNoPromotion(): void
{
$a = NDArray::array([1, 2, 3], DType::Int64);
$result = $a->add(10);
$this->assertSame(DType::Int64, $result->dtype());
$this->assertSame([11, 12, 13], $result->toArray());
}
public function testFloatArrayPlusFloatScalar(): void
{
$a = NDArray::array([1.0, 2.0, 3.0], DType::Float64);
$result = $a->add(1.5);
$this->assertSame(DType::Float64, $result->dtype());
$this->assertEqualsWithDelta([2.5, 3.5, 4.5], $result->toArray(), 0.0001);
}
public function testScalarBufferNotCorrupted(): void
{
// This test verifies that the scalar buffer is read in its native dtype
// If read as output dtype, int 1065353216 (0x3F800000 = 1.0f) would be corrupted
$a = NDArray::array([100], DType::Int64);
$result = $a->add(1.0);
$this->assertSame(DType::Float64, $result->dtype());
$this->assertEqualsWithDelta([101.0], $result->toArray(), 0.0001);
}
public function testFloat32PlusFloatScalarStaysFloat32(): void
{
$a = NDArray::array([1.5, 2.5, 3.5], DType::Float32);
$result = $a->add(5.0);
$this->assertSame(DType::Float32, $result->dtype());
$this->assertEqualsWithDelta([6.5, 7.5, 8.5], $result->toArray(), 0.0001);
}
public function testFloat32MultiplyFloatScalarStaysFloat32(): void
{
$a = NDArray::array([1.5, 2.5, 3.5], DType::Float32);
$result = $a->multiply(2.0);
$this->assertSame(DType::Float32, $result->dtype());
$this->assertEqualsWithDelta([3.0, 5.0, 7.0], $result->toArray(), 0.0001);
}
public function testUint8PlusIntScalarStaysUint8(): void
{
$a = NDArray::array([10, 20, 30], DType::UInt8);
$result = $a->add(5);
$this->assertSame(DType::UInt8, $result->dtype());
$this->assertEquals([15, 25, 35], $result->toArray());
}
public function testInt32PlusIntScalarStaysInt32(): void
{
$a = NDArray::array([1, 2, 3], DType::Int32);
$result = $a->add(10);
$this->assertSame(DType::Int32, $result->dtype());
$this->assertEquals([11, 12, 13], $result->toArray());
}
public function testImplicitIntPlusFloatScalarPromotesToFloat(): void
{
$a = NDArray::array([1, 2, 3]);
$result = $a->add(2.5);
$this->assertSame(DType::Float64, $result->dtype());
$this->assertEqualsWithDelta([3.5, 4.5, 5.5], $result->toArray(), 0.0001);
}
public function testUint64PlusSignedScalarPromotesToFloat64(): void
{
$a = NDArray::array([1, 2, 3], DType::UInt64);
$result = $a->add(-1);
$this->assertSame(DType::Float64, $result->dtype());
$this->assertEqualsWithDelta([0.0, 1.0, 2.0], $result->toArray(), 0.0001);
}
}