Skip to content

Commit 0dcb4b8

Browse files
committed
wip
1 parent 17e1e35 commit 0dcb4b8

File tree

11 files changed

+362
-0
lines changed

11 files changed

+362
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../tests/utils/basic.inc';
4+
5+
$expectedFailures = [];
6+
7+
$outputPath = realpath(__DIR__ . '/../tests') . '/bson-binary-vector/';
8+
9+
if ( ! is_dir($outputPath) && ! mkdir($outputPath, 0755, true)) {
10+
printf("Error creating output path: %s\n", $outputPath);
11+
}
12+
13+
foreach (array_slice($argv, 1) as $inputFile) {
14+
if ( ! is_readable($inputFile) || ! is_file($inputFile)) {
15+
printf("Error reading %s\n", $inputFile);
16+
continue;
17+
}
18+
19+
$test = json_decode(file_get_contents($inputFile), true);
20+
21+
if (json_last_error() !== JSON_ERROR_NONE) {
22+
printf("Error decoding %s: %s\n", $inputFile, json_last_error_msg());
23+
continue;
24+
}
25+
26+
if ( ! isset($test['description'])) {
27+
printf("Skipping test file without \"description\" field: %s\n", $inputFile);
28+
continue;
29+
}
30+
31+
if ( ! isset($test['test_key'])) {
32+
printf("Skipping test file without \"test_key\" field: %s\n", $inputFile);
33+
continue;
34+
}
35+
36+
if ( ! empty($test['tests'])) {
37+
foreach ($test['tests'] as $i => $case) {
38+
$outputFile = sprintf('%s-%03d.phpt', pathinfo($inputFile, PATHINFO_FILENAME), $i + 1);
39+
try {
40+
$output = renderPhpt(getParamsForTestCase($test, $case), $expectedFailures);
41+
} catch (Exception $e) {
42+
printf("Error processing tests[%d] in %s: %s\n", $i, $inputFile, $e->getMessage());
43+
continue;
44+
}
45+
46+
if (false === file_put_contents($outputPath . '/' . $outputFile, $output)) {
47+
printf("Error writing tests[%d] in %s\n", $i, $inputFile);
48+
continue;
49+
}
50+
}
51+
}
52+
}
53+
54+
function getParamsForTestCase(array $test, array $case)
55+
{
56+
foreach (['description', 'valid'] as $field) {
57+
if (!isset($case[$field])) {
58+
throw new InvalidArgumentException(sprintf('Missing "%s" field', $field));
59+
}
60+
}
61+
62+
$code = '';
63+
$expect = '';
64+
65+
if (!$case['valid']) {
66+
throw new InvalidArgumentException(sprintf('valid:false is not supported'));
67+
}
68+
69+
$enum = match ($case['dtype_hex']) {
70+
'0x27' => MongoDB\BSON\VectorType::Float32,
71+
'0x03' => MongoDB\BSON\VectorType::Int8,
72+
'0x10' => MongoDB\BSON\VectorType::PackedBit,
73+
};
74+
75+
$code = sprintf('$vector = MongoDB\BSON\Binary::fromVector(%s, %s);', var_export($case['vector'], true), var_export($enum, true)) . "\n";
76+
77+
$canonicalBson = $case['canonical_bson'];
78+
$expectedCanonicalBson = strtolower($canonicalBson);
79+
$code .= sprintf('$canonicalBson = hex2bin(%s);', var_export($canonicalBson, true)) . "\n";
80+
81+
$code .= sprintf('echo bin2hex((string) MongoDB\BSON\Document::fromPHP([%s => $vector])), "\n";', var_export($test['test_key'], true));
82+
83+
return [
84+
'%NAME%' => sprintf('%s: %s', trim($test['description']), trim($case['description'])),
85+
'%CODE%' => trim($code),
86+
'%EXPECT%' => trim($expectedCanonicalBson),
87+
];
88+
}
89+
90+
function renderPhpt(array $params, array $expectedFailures)
91+
{
92+
$params['%XFAIL%'] = isset($expectedFailures[$params['%NAME%']])
93+
? "--XFAIL--\n" . $expectedFailures[$params['%NAME%']] . "\n"
94+
: '';
95+
96+
$params['%SKIPIF%'] = '';
97+
98+
$template = <<< 'TEMPLATE'
99+
--TEST--
100+
%NAME%
101+
%XFAIL%%SKIPIF%--DESCRIPTION--
102+
Generated by scripts/convert-bson-corpus-tests.php
103+
104+
DO NOT EDIT THIS FILE
105+
--FILE--
106+
<?php
107+
108+
require_once __DIR__ . '/../utils/basic.inc';
109+
110+
%CODE%
111+
112+
?>
113+
===DONE===
114+
<?php exit(0); ?>
115+
--EXPECT--
116+
%EXPECT%
117+
===DONE===
118+
TEMPLATE;
119+
120+
return str_replace(array_keys($params), array_values($params), $template);
121+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Tests of Binary subtype 9, Vectors, with dtype FLOAT32: Simple Vector FLOAT32
3+
--DESCRIPTION--
4+
Generated by scripts/convert-bson-corpus-tests.php
5+
6+
DO NOT EDIT THIS FILE
7+
--FILE--
8+
<?php
9+
10+
require_once __DIR__ . '/../utils/basic.inc';
11+
12+
$vector = MongoDB\BSON\Binary::fromVector(array (
13+
0 => 127.0,
14+
1 => 7.0,
15+
), \MongoDB\BSON\VectorType::Float32);
16+
$canonicalBson = hex2bin('1C00000005766563746F72000A0000000927000000FE420000E04000');
17+
echo bin2hex((string) MongoDB\BSON\Document::fromPHP(['vector' => $vector])), "\n";
18+
19+
?>
20+
===DONE===
21+
<?php exit(0); ?>
22+
--EXPECT--
23+
1c00000005766563746f72000a0000000927000000fe420000e04000
24+
===DONE===
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Tests of Binary subtype 9, Vectors, with dtype FLOAT32: Vector with decimals and negative value FLOAT32
3+
--DESCRIPTION--
4+
Generated by scripts/convert-bson-corpus-tests.php
5+
6+
DO NOT EDIT THIS FILE
7+
--FILE--
8+
<?php
9+
10+
require_once __DIR__ . '/../utils/basic.inc';
11+
12+
$vector = MongoDB\BSON\Binary::fromVector(array (
13+
0 => 127.7,
14+
1 => -7.7,
15+
), \MongoDB\BSON\VectorType::Float32);
16+
$canonicalBson = hex2bin('1C00000005766563746F72000A0000000927006666FF426666F6C000');
17+
echo bin2hex((string) MongoDB\BSON\Document::fromPHP(['vector' => $vector])), "\n";
18+
19+
?>
20+
===DONE===
21+
<?php exit(0); ?>
22+
--EXPECT--
23+
1c00000005766563746f72000a0000000927006666ff426666f6c000
24+
===DONE===
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Tests of Binary subtype 9, Vectors, with dtype FLOAT32: Empty Vector FLOAT32
3+
--DESCRIPTION--
4+
Generated by scripts/convert-bson-corpus-tests.php
5+
6+
DO NOT EDIT THIS FILE
7+
--FILE--
8+
<?php
9+
10+
require_once __DIR__ . '/../utils/basic.inc';
11+
12+
$vector = MongoDB\BSON\Binary::fromVector(array (
13+
), \MongoDB\BSON\VectorType::Float32);
14+
$canonicalBson = hex2bin('1400000005766563746F72000200000009270000');
15+
echo bin2hex((string) MongoDB\BSON\Document::fromPHP(['vector' => $vector])), "\n";
16+
17+
?>
18+
===DONE===
19+
<?php exit(0); ?>
20+
--EXPECT--
21+
1400000005766563746f72000200000009270000
22+
===DONE===
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Tests of Binary subtype 9, Vectors, with dtype FLOAT32: Infinity Vector FLOAT32
3+
--DESCRIPTION--
4+
Generated by scripts/convert-bson-corpus-tests.php
5+
6+
DO NOT EDIT THIS FILE
7+
--FILE--
8+
<?php
9+
10+
require_once __DIR__ . '/../utils/basic.inc';
11+
12+
$vector = MongoDB\BSON\Binary::fromVector(array (
13+
0 =>
14+
array (
15+
'$numberDouble' => '-Infinity',
16+
),
17+
1 => 0.0,
18+
2 =>
19+
array (
20+
'$numberDouble' => 'Infinity',
21+
),
22+
), \MongoDB\BSON\VectorType::Float32);
23+
$canonicalBson = hex2bin('2000000005766563746F72000E000000092700000080FF000000000000807F00');
24+
echo bin2hex((string) MongoDB\BSON\Document::fromPHP(['vector' => $vector])), "\n";
25+
26+
?>
27+
===DONE===
28+
<?php exit(0); ?>
29+
--EXPECT--
30+
2000000005766563746f72000e000000092700000080ff000000000000807f00
31+
===DONE===
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Tests of Binary subtype 9, Vectors, with dtype INT8: Simple Vector INT8
3+
--DESCRIPTION--
4+
Generated by scripts/convert-bson-corpus-tests.php
5+
6+
DO NOT EDIT THIS FILE
7+
--FILE--
8+
<?php
9+
10+
require_once __DIR__ . '/../utils/basic.inc';
11+
12+
$vector = MongoDB\BSON\Binary::fromVector(array (
13+
0 => 127,
14+
1 => 7,
15+
), \MongoDB\BSON\VectorType::Int8);
16+
$canonicalBson = hex2bin('1600000005766563746F7200040000000903007F0700');
17+
echo bin2hex((string) MongoDB\BSON\Document::fromPHP(['vector' => $vector])), "\n";
18+
19+
?>
20+
===DONE===
21+
<?php exit(0); ?>
22+
--EXPECT--
23+
1600000005766563746f7200040000000903007f0700
24+
===DONE===
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Tests of Binary subtype 9, Vectors, with dtype INT8: Empty Vector INT8
3+
--DESCRIPTION--
4+
Generated by scripts/convert-bson-corpus-tests.php
5+
6+
DO NOT EDIT THIS FILE
7+
--FILE--
8+
<?php
9+
10+
require_once __DIR__ . '/../utils/basic.inc';
11+
12+
$vector = MongoDB\BSON\Binary::fromVector(array (
13+
), \MongoDB\BSON\VectorType::Int8);
14+
$canonicalBson = hex2bin('1400000005766563746F72000200000009030000');
15+
echo bin2hex((string) MongoDB\BSON\Document::fromPHP(['vector' => $vector])), "\n";
16+
17+
?>
18+
===DONE===
19+
<?php exit(0); ?>
20+
--EXPECT--
21+
1400000005766563746f72000200000009030000
22+
===DONE===
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Tests of Binary subtype 9, Vectors, with dtype PACKED_BIT: Simple Vector PACKED_BIT
3+
--DESCRIPTION--
4+
Generated by scripts/convert-bson-corpus-tests.php
5+
6+
DO NOT EDIT THIS FILE
7+
--FILE--
8+
<?php
9+
10+
require_once __DIR__ . '/../utils/basic.inc';
11+
12+
$vector = MongoDB\BSON\Binary::fromVector(array (
13+
0 => 127,
14+
1 => 7,
15+
), \MongoDB\BSON\VectorType::PackedBit);
16+
$canonicalBson = hex2bin('1600000005766563746F7200040000000910007F0700');
17+
echo bin2hex((string) MongoDB\BSON\Document::fromPHP(['vector' => $vector])), "\n";
18+
19+
?>
20+
===DONE===
21+
<?php exit(0); ?>
22+
--EXPECT--
23+
1600000005766563746f7200040000000910007f0700
24+
===DONE===
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Tests of Binary subtype 9, Vectors, with dtype PACKED_BIT: PACKED_BIT with padding
3+
--DESCRIPTION--
4+
Generated by scripts/convert-bson-corpus-tests.php
5+
6+
DO NOT EDIT THIS FILE
7+
--FILE--
8+
<?php
9+
10+
require_once __DIR__ . '/../utils/basic.inc';
11+
12+
$vector = MongoDB\BSON\Binary::fromVector(array (
13+
0 => 127,
14+
1 => 8,
15+
), \MongoDB\BSON\VectorType::PackedBit);
16+
$canonicalBson = hex2bin('1600000005766563746F7200040000000910037F0800');
17+
echo bin2hex((string) MongoDB\BSON\Document::fromPHP(['vector' => $vector])), "\n";
18+
19+
?>
20+
===DONE===
21+
<?php exit(0); ?>
22+
--EXPECT--
23+
1600000005766563746f7200040000000910037f0800
24+
===DONE===
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Tests of Binary subtype 9, Vectors, with dtype PACKED_BIT: Empty Vector PACKED_BIT
3+
--DESCRIPTION--
4+
Generated by scripts/convert-bson-corpus-tests.php
5+
6+
DO NOT EDIT THIS FILE
7+
--FILE--
8+
<?php
9+
10+
require_once __DIR__ . '/../utils/basic.inc';
11+
12+
$vector = MongoDB\BSON\Binary::fromVector(array (
13+
), \MongoDB\BSON\VectorType::PackedBit);
14+
$canonicalBson = hex2bin('1400000005766563746F72000200000009100000');
15+
echo bin2hex((string) MongoDB\BSON\Document::fromPHP(['vector' => $vector])), "\n";
16+
17+
?>
18+
===DONE===
19+
<?php exit(0); ?>
20+
--EXPECT--
21+
1400000005766563746f72000200000009100000
22+
===DONE===

0 commit comments

Comments
 (0)