-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSparseVectorTest.php
More file actions
67 lines (55 loc) · 2 KB
/
SparseVectorTest.php
File metadata and controls
67 lines (55 loc) · 2 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
<?php
use PHPUnit\Framework\TestCase;
use Pgvector\SparseVector;
final class SparseVectorTest extends TestCase
{
public function testFromDense()
{
$embedding = new SparseVector([1, 0, 2, 0, 3, 0]);
$this->assertEquals(6, $embedding->dimensions());
$this->assertEquals([0, 2, 4], $embedding->indices());
$this->assertEquals([1, 2, 3], $embedding->values());
}
public function testFromDenseSplFixedArray()
{
$embedding = new SparseVector(SplFixedArray::fromArray([1, 0, 2, 0, 3, 0]));
$this->assertEquals('{1:1,3:2,5:3}/6', (string) $embedding);
}
public function testFromMap()
{
$map = [2 => 2, 4 => 3, 0 => 1, 3 => 0];
$embedding = new SparseVector($map, 6);
$this->assertEquals([1, 0, 2, 0, 3, 0], $embedding->toArray());
$this->assertEquals([0, 2, 4], $embedding->indices());
$this->assertEquals([2, 4, 0, 3], array_keys($map));
}
public function testFromString()
{
$embedding = new SparseVector('{1:1,3:2,5:3}/6');
$this->assertEquals(6, $embedding->dimensions());
$this->assertEquals([0, 2, 4], $embedding->indices());
$this->assertEquals([1, 2, 3], $embedding->values());
}
public function testFromStringDimensions()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Extra argument');
new SparseVector('{1:1,3:2,5:3}/6', 6);
}
public function testInvalidInteger()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Expected array');
new SparseVector(1);
}
public function testToString()
{
$embedding = new SparseVector([1, 0, 2, 0, 3, 0]);
$this->assertEquals('{1:1,3:2,5:3}/6', (string) $embedding);
}
public function testToArray()
{
$embedding = new SparseVector([1, 0, 2, 0, 3, 0]);
$this->assertEquals([1, 0, 2, 0, 3, 0], $embedding->toArray());
}
}