Skip to content

Commit

Permalink
Added length and toByteArray methods to PGbit
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed May 23, 2024
1 parent 44637d9 commit 8240cd4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
18 changes: 18 additions & 0 deletions src/main/java/com/pgvector/PGbit.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ public void toBytes(byte[] bytes, int offset) {
}
}

/**
* Returns the length
*
* @return an array
*/
public int length() {
return length;
}

/**
* Returns a byte array
*
* @return an array
*/
public byte[] toByteArray() {
return data;
}

/**
* Returns an array
*
Expand Down
16 changes: 10 additions & 6 deletions src/test/java/com/pgvector/PGbitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@
public class PGbitTest {
@Test
void testArrayConstructor() {
PGbit vec = new PGbit(new boolean[] {true, false, true});
assertArrayEquals(new boolean[] {true, false, true}, vec.toArray());
PGbit vec = new PGbit(new boolean[] {false, true, false, true, false, false, false, false, true});
assertEquals(9, vec.length());
assertArrayEquals(new byte[] {(byte) 0b01010000, (byte) 0b10000000}, vec.toByteArray());
assertArrayEquals(new boolean[] {false, true, false, true, false, false, false, false, true}, vec.toArray());
}

@Test
void testStringConstructor() throws SQLException {
PGbit vec = new PGbit("101");
assertArrayEquals(new boolean[] {true, false, true}, vec.toArray());
PGbit vec = new PGbit("010100001");
assertEquals(9, vec.length());
assertArrayEquals(new byte[] {(byte) 0b01010000, (byte) 0b10000000}, vec.toByteArray());
assertArrayEquals(new boolean[] {false, true, false, true, false, false, false, false, true}, vec.toArray());
}

@Test
void testGetValue() {
PGbit vec = new PGbit(new boolean[] {true, false, true});
assertEquals("101", vec.getValue());
PGbit vec = new PGbit(new boolean[] {false, true, false, true, false, false, false, false, true});
assertEquals("010100001", vec.getValue());
}
}

0 comments on commit 8240cd4

Please sign in to comment.