-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for Faiss HNSW Byte Vector
Signed-off-by: Naveen Tatikonda <[email protected]>
- Loading branch information
1 parent
24a3d4c
commit 5d532b0
Showing
4 changed files
with
207 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 1 addition & 7 deletions
8
src/main/java/org/opensearch/knn/index/codec/transfer/VectorTransferByteToFloat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/test/java/org/opensearch/knn/index/codec/transfer/VectorTransferByteToFloatTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.knn.index.codec.transfer; | ||
|
||
import junit.framework.TestCase; | ||
import lombok.SneakyThrows; | ||
import org.opensearch.knn.index.codec.util.SerializationMode; | ||
import org.opensearch.knn.jni.JNICommons; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.stream.IntStream; | ||
|
||
import static org.junit.Assert.assertNotEquals; | ||
|
||
public class VectorTransferByteToFloatTests extends TestCase { | ||
@SneakyThrows | ||
public void testTransfer_whenCalled_thenAdded() { | ||
final ByteArrayInputStream bais1 = getByteArrayOfVectors(20); | ||
final ByteArrayInputStream bais2 = getByteArrayOfVectors(20); | ||
VectorTransferByteToFloat vectorTransfer = new VectorTransferByteToFloat(1000); | ||
try { | ||
vectorTransfer.init(2); | ||
|
||
vectorTransfer.transfer(bais1); | ||
// flush is not called | ||
assertEquals(0, vectorTransfer.getVectorAddress()); | ||
|
||
vectorTransfer.transfer(bais2); | ||
// flush should be called | ||
assertNotEquals(0, vectorTransfer.getVectorAddress()); | ||
} finally { | ||
if (vectorTransfer.getVectorAddress() != 0) { | ||
JNICommons.freeVectorData(vectorTransfer.getVectorAddress()); | ||
} | ||
} | ||
} | ||
|
||
@SneakyThrows | ||
public void testSerializationMode_whenCalled_thenReturn() { | ||
final ByteArrayInputStream bais = getByteArrayOfVectors(20); | ||
VectorTransferByteToFloat vectorTransfer = new VectorTransferByteToFloat(1000); | ||
|
||
// Verify | ||
assertEquals(SerializationMode.COLLECTION_OF_FLOATS, vectorTransfer.getSerializationMode(bais)); | ||
} | ||
|
||
private ByteArrayInputStream getByteArrayOfVectors(int vectorLength) { | ||
byte[] vector = new byte[vectorLength]; | ||
IntStream.range(0, vectorLength).forEach(index -> vector[index] = (byte) ThreadLocalRandom.current().nextInt(-128, 127)); | ||
return new ByteArrayInputStream(vector); | ||
} | ||
} |