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()); } }