Fix test failure with zero-length vector (#12493)

This adds assertions around the random test vector dimension count and continues to generate random vectors until it has a `squareSum > 0`
This commit is contained in:
Benjamin Trent 2023-08-08 08:38:46 -04:00 committed by GitHub
parent a65cf8960a
commit dd4e66dad6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 2 deletions

View File

@ -1222,15 +1222,23 @@ public abstract class BaseKnnVectorsFormatTestCase extends BaseIndexFileFormatTe
} }
private float[] randomVector(int dim) { private float[] randomVector(int dim) {
assert dim > 0;
float[] v = new float[dim]; float[] v = new float[dim];
for (int i = 0; i < dim; i++) { double squareSum = 0.0;
v[i] = random().nextFloat(); // keep generating until we don't get a zero-length vector
while (squareSum == 0.0) {
squareSum = 0.0;
for (int i = 0; i < dim; i++) {
v[i] = random().nextFloat();
squareSum += v[i] * v[i];
}
} }
VectorUtil.l2normalize(v); VectorUtil.l2normalize(v);
return v; return v;
} }
private byte[] randomVector8(int dim) { private byte[] randomVector8(int dim) {
assert dim > 0;
float[] v = randomVector(dim); float[] v = randomVector(dim);
byte[] b = new byte[dim]; byte[] b = new byte[dim];
for (int i = 0; i < dim; i++) { for (int i = 0; i < dim; i++) {