mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-07 13:10:56 +08:00
Added basic fuzz testing for input functions
This commit is contained in:
62
test/t/037_inputs.pl
Normal file
62
test/t/037_inputs.pl
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
use strict;
|
||||||
|
use warnings;
|
||||||
|
use PostgresNode;
|
||||||
|
use TestLib;
|
||||||
|
use Test::More;
|
||||||
|
|
||||||
|
# Initialize node
|
||||||
|
my $node = get_new_node('node');
|
||||||
|
$node->init;
|
||||||
|
$node->start;
|
||||||
|
|
||||||
|
# Create extension
|
||||||
|
$node->safe_psql("postgres", "CREATE EXTENSION vector;");
|
||||||
|
|
||||||
|
my @types = ("vector", "halfvec", "sparsevec");
|
||||||
|
my @inputs = ("[1.23,4.56,7.89]", "[1.23,4.56,7.89]", "{1:1.23,2:4.56,3:7.89}/3");
|
||||||
|
my @subs = (" ", " ", ",", ":", "-", "1", "9", "\0", "2147483648", "-2147483649");
|
||||||
|
|
||||||
|
for my $i (0 .. $#types)
|
||||||
|
{
|
||||||
|
my $type = $types[$i];
|
||||||
|
|
||||||
|
for (1 .. 100)
|
||||||
|
{
|
||||||
|
my $input = $inputs[$i] . "";
|
||||||
|
|
||||||
|
for (1 .. 1 + int(rand() * 2))
|
||||||
|
{
|
||||||
|
my $r = int(rand() * length($input));
|
||||||
|
my $sub = $subs[int(rand() * scalar(@subs))];
|
||||||
|
if ($sub eq "\0")
|
||||||
|
{
|
||||||
|
# Truncate
|
||||||
|
$input = substr($input, 0, $r);
|
||||||
|
}
|
||||||
|
elsif (rand() > 0.5)
|
||||||
|
{
|
||||||
|
# Insert
|
||||||
|
substr($input, $r, 0) = $sub;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
# Replace
|
||||||
|
substr($input, $r, length($sub), $sub);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
my ($ret, $stdout, $stderr) = $node->psql("postgres", "SELECT '$input'::$type;");
|
||||||
|
if ($ret != 0)
|
||||||
|
{
|
||||||
|
# Test for type in error message
|
||||||
|
like($stderr, qr/$type/);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
# Count test
|
||||||
|
is($ret, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
done_testing();
|
||||||
Reference in New Issue
Block a user