forked from pgvector/pgvector-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_bit.py
More file actions
63 lines (48 loc) · 2.27 KB
/
test_bit.py
File metadata and controls
63 lines (48 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import numpy as np
from pgvector import Bit
import pytest
class TestBit:
def test_list(self):
assert Bit([True, False, True]).to_list() == [True, False, True]
def test_list_none(self):
with pytest.warns(UserWarning, match='expected elements to be boolean'):
assert Bit([True, None, True]).to_text() == '101'
def test_list_int(self):
with pytest.warns(UserWarning, match='expected elements to be boolean'):
assert Bit([254, 7, 0]).to_text() == '110'
def test_tuple(self):
assert Bit((True, False, True)).to_list() == [True, False, True]
def test_str(self):
assert Bit('101').to_list() == [True, False, True]
def test_bytes(self):
assert Bit(b'\xff\x00\xf0').to_text() == '111111110000000011110000'
assert Bit(b'\xfe\x07\x00').to_text() == '111111100000011100000000'
def test_ndarray(self):
arr = np.array([True, False, True])
assert Bit(arr).to_list() == [True, False, True]
assert np.array_equal(Bit(arr).to_numpy(), arr)
def test_ndarray_unpackbits(self):
arr = np.unpackbits(np.array([254, 7, 0], dtype=np.uint8))
assert Bit(arr).to_text() == '111111100000011100000000'
def test_ndarray_uint8(self):
arr = np.array([254, 7, 0], dtype=np.uint8)
with pytest.warns(UserWarning, match='expected elements to be boolean'):
assert Bit(arr).to_text() == '110'
def test_ndarray_uint16(self):
arr = np.array([254, 7, 0], dtype=np.uint16)
with pytest.warns(UserWarning, match='expected elements to be boolean'):
assert Bit(arr).to_text() == '110'
def test_ndim_two(self):
with pytest.raises(ValueError) as error:
Bit([[True, False], [True, False]])
assert str(error.value) == 'expected ndim to be 1'
def test_ndim_zero(self):
with pytest.raises(ValueError) as error:
Bit(True)
assert str(error.value) == 'expected ndim to be 1'
def test_repr(self):
assert repr(Bit([True, False, True])) == 'Bit(101)'
assert str(Bit([True, False, True])) == 'Bit(101)'
def test_equality(self):
assert Bit([True, False, True]) == Bit([True, False, True])
assert Bit([True, False, True]) != Bit([True, False, False])