-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathtest_bit.py
More file actions
103 lines (81 loc) · 3.61 KB
/
Copy pathtest_bit.py
File metadata and controls
103 lines (81 loc) · 3.61 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from pgvector import Bit
import pytest
import random
from struct import pack
from .conftest import numpy as np
class TestBit:
def test_list(self) -> None:
assert Bit([True, False, True]).to_list() == [True, False, True]
def test_list_none(self) -> None:
with pytest.raises(ValueError) as error:
Bit([True, None, True]) # type: ignore
assert str(error.value) == 'expected list[bool]'
def test_list_int(self) -> None:
with pytest.raises(ValueError) as error:
Bit([254, 7, 0]) # type: ignore
assert str(error.value) == 'expected list[bool]'
def test_list_list(self) -> None:
with pytest.raises(ValueError) as error:
Bit([[True, False], [True, False]]) # type: ignore
assert str(error.value) == 'expected list[bool]'
def test_str(self) -> None:
assert Bit('101').to_list() == [True, False, True]
def test_str_two(self) -> None:
with pytest.raises(ValueError) as error:
Bit('201')
assert str(error.value) == 'expected bit string'
def test_bytes(self) -> None:
assert Bit(b'\xff\x00\xf0').to_text() == '111111110000000011110000'
assert Bit(b'\xfe\x07\x00').to_text() == '111111100000011100000000'
def test_ndarray(self) -> None:
if np is None:
pytest.skip('NumPy required')
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) -> None:
if np is None:
pytest.skip('NumPy required')
arr = np.unpackbits(np.array([254, 7, 0], dtype=np.uint8))
assert Bit(arr).to_text() == '111111100000011100000000'
def test_ndarray_uint8(self) -> None:
if np is None:
pytest.skip('NumPy required')
arr = np.array([254, 7, 0], dtype=np.uint8)
with pytest.raises(ValueError) as error:
Bit(arr)
assert str(error.value) == 'expected elements to be boolean'
def test_ndarray_uint16(self) -> None:
if np is None:
pytest.skip('NumPy required')
arr = np.array([254, 7, 0], dtype=np.uint16)
with pytest.raises(ValueError) as error:
Bit(arr) # type: ignore
assert str(error.value) == 'expected elements to be boolean'
def test_bool(self) -> None:
with pytest.raises(ValueError) as error:
Bit(True) # type: ignore
assert str(error.value) == 'expected bytes, str, list, or ndarray'
def test_random(self) -> None:
value = ''.join(random.choices(['0', '1'], k=random.randint(1024, 2048)))
assert Bit(value).to_text() == value
def test_repr(self) -> None:
assert repr(Bit([True, False, True])) == 'Bit(101)'
assert str(Bit([True, False, True])) == 'Bit(101)'
def test_equality(self) -> None:
assert Bit([True, False, True]) == Bit([True, False, True])
assert Bit([True, False, True]) != Bit([True, False, False])
assert Bit([True, False, True]) != 1
def test_from_text(self) -> None:
vec = Bit.from_text('101')
assert vec.to_list() == [True, False, True]
if np is not None:
assert np.array_equal(vec.to_numpy(), [True, False, True])
assert vec.to_text() == '101'
def test_from_binary(self) -> None:
data = pack('>iB', 3, 5 << 5)
vec = Bit.from_binary(data)
assert vec.to_list() == [True, False, True]
if np is not None:
assert np.array_equal(vec.to_numpy(), [True, False, True])
assert vec.to_binary() == data