Skip to content

Commit 312ce70

Browse files
committed
Added more tests
1 parent bb91460 commit 312ce70

4 files changed

Lines changed: 67 additions & 3 deletions

File tree

‎tests/test_bit.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,23 @@ def test_eq(self) -> None:
9696
assert Bit([True, False, True]) != Bit([True, False, False])
9797
assert Bit([True, False, True]) != 1
9898

99+
def test_to_list(self) -> None:
100+
assert Bit('101').to_list() == [True, False, True]
101+
102+
def test_to_numpy(self) -> None:
103+
if np is None:
104+
pytest.skip('NumPy required')
105+
106+
arr = Bit('101').to_numpy()
107+
assert np.array_equal(arr, np.array([True, False, True]))
108+
assert arr.dtype == np.bool_
109+
110+
def test_to_text(self) -> None:
111+
assert Bit('101').to_text() == '101'
112+
113+
def test_to_binary(self) -> None:
114+
assert Bit('101').to_binary() == struct.pack('>iB', 3, 5 << 5)
115+
99116
def test_from_text(self) -> None:
100117
assert Bit.from_text('101') == Bit('101')
101118

‎tests/test_half_vector.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,23 @@ def test_eq(self) -> None:
6060
def test_dimensions(self) -> None:
6161
assert HalfVector([1, 2, 3]).dimensions() == 3
6262

63+
def test_to_list(self) -> None:
64+
assert HalfVector([1.5, 2, 3]).to_list() == [1.5, 2, 3]
65+
66+
def test_to_numpy(self) -> None:
67+
if np is None:
68+
pytest.skip('NumPy required')
69+
70+
arr = HalfVector([1.5, 2, 3]).to_numpy()
71+
assert np.array_equal(arr, np.array([1.5, 2, 3]))
72+
assert arr.dtype == np.float16
73+
74+
def test_to_text(self) -> None:
75+
assert HalfVector([1.5, 2, 3]).to_text() == '[1.5,2.0,3.0]'
76+
77+
def test_to_binary(self) -> None:
78+
assert HalfVector([1.5, 2, 3]).to_binary() == struct.pack('>hh3e', 3, 0, 1.5, 2, 3)
79+
6380
def test_from_text(self) -> None:
6481
assert HalfVector.from_text('[1.5,2,3]') == HalfVector([1.5, 2, 3])
6582

‎tests/test_sparse_vector.py‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,22 @@ def test_to_coo(self) -> None:
155155

156156
assert np.array_equal(SparseVector([1, 0, 2, 0, 3, 0]).to_coo().toarray(), [[1, 0, 2, 0, 3, 0]])
157157

158-
def test_zero_vector_text(self) -> None:
159-
vec = SparseVector({}, 3)
160-
assert vec.to_list() == SparseVector.from_text(vec.to_text()).to_list()
158+
def test_to_list(self) -> None:
159+
assert SparseVector({2: 2, 4: 3, 0: 1.5, 3: 0}, 6).to_list() == [1.5, 0, 2, 0, 3, 0]
160+
161+
def test_to_numpy(self) -> None:
162+
if np is None:
163+
pytest.skip('NumPy required')
164+
165+
arr = SparseVector({2: 2, 4: 3, 0: 1.5, 3: 0}, 6).to_numpy()
166+
assert np.array_equal(arr, np.array([1.5, 0, 2, 0, 3, 0]))
167+
assert arr.dtype == np.float32
168+
169+
def test_to_text(self) -> None:
170+
assert SparseVector({2: 2, 4: 3, 0: 1.5, 3: 0}, 6).to_text() == '{1:1.5,3:2.0,5:3.0}/6'
171+
172+
def test_to_binary(self) -> None:
173+
assert SparseVector({2: 2, 4: 3, 0: 1.5, 3: 0}, 6).to_binary() == struct.pack('>iii3i3f', 6, 3, 0, 0, 2, 4, 1.5, 2, 3)
161174

162175
def test_from_text(self) -> None:
163176
assert SparseVector.from_text('{1:1.5,3:2,5:3}/6') == SparseVector([1.5, 0, 2, 0, 3, 0])

‎tests/test_vector.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,23 @@ def test_eq(self) -> None:
6060
def test_dimensions(self) -> None:
6161
assert Vector([1, 2, 3]).dimensions() == 3
6262

63+
def test_to_list(self) -> None:
64+
assert Vector([1.5, 2, 3]).to_list() == [1.5, 2, 3]
65+
66+
def test_to_numpy(self) -> None:
67+
if np is None:
68+
pytest.skip('NumPy required')
69+
70+
arr = Vector([1.5, 2, 3]).to_numpy()
71+
assert np.array_equal(arr, np.array([1.5, 2, 3]))
72+
assert arr.dtype == np.float32
73+
74+
def test_to_text(self) -> None:
75+
assert Vector([1.5, 2, 3]).to_text() == '[1.5,2.0,3.0]'
76+
77+
def test_to_binary(self) -> None:
78+
assert Vector([1.5, 2, 3]).to_binary() == struct.pack('>hh3f', 3, 0, 1.5, 2, 3)
79+
6380
def test_from_text(self) -> None:
6481
assert Vector.from_text('[1.5,2,3]') == Vector([1.5, 2, 3])
6582

0 commit comments

Comments
 (0)