Example #1
0
 def test_hex(self):
     """Test hex representation."""
     assert ByteMatrix(2, 8, 0).hex(8) == b'0000'
     assert ByteMatrix(1, 8,
                       [[0, 1, 2, 4, 8, 16, 32, 64]]).hex(4) == b'1800'
     # zero fill-out
     assert ByteMatrix(1, 7, 1).hex(8) == b'fe'
Example #2
0
 def test_pack(self):
     """Test packed representation."""
     assert ByteMatrix(2, 8, 0).packed(8) == b'\0\0'
     assert ByteMatrix(
         1, 8,
         [[0, 1, 2, 4, 8, 16, 32, 64]]).packed(4) == bytearray(b'\x18\x00')
     # zero fill-out
     assert ByteMatrix(1, 7, 1).packed(8) == bytearray(b'\xfe')
Example #3
0
 def test_elementwise(self):
     """Test elementwise operations."""
     bm = ByteMatrix(2, 3, 1)
     rhs = ByteMatrix(2, 3, b'\x00\x01\x02\x03\x04\x05')
     assert (bm | rhs).to_bytes() == b'\x01\x01\x03\x03\x05\x05'
     assert (bm & rhs).to_bytes() == b'\x00\x01\x00\x01\x00\x01'
     assert (bm ^ rhs).to_bytes() == b'\x01\x00\x03\x02\x05\x04'
     assert (bm >> rhs).to_bytes() == b'\x01\0\0\0\0\0'
     assert (bm << rhs).to_bytes() == b'\x01\x02\x04\x08\x10\x20'
Example #4
0
 def test_setitem_int(self):
     """Test slice assignment to same int."""
     bm = ByteMatrix(2, 3, [b'123', b'456'])
     bm[0:1, 0:2] = 0
     assert bm.to_bytes() == b'\x00\x003456'
     bm[1, 0:2] = 1
     assert bm.to_bytes() == b'\x00\x003\x01\x016'
     bm[0:2, 0] = 2
     assert bm.to_bytes() == b'\x02\x003\x02\x016'
Example #5
0
 def test_getitem(self):
     """Test int and slice indexing."""
     bm = ByteMatrix(2, 3, [b'123', b'456'])
     assert bm[0, 0] == ord(b'1')
     assert isinstance(bm[0:1, 0], ByteMatrix)
     assert bm[:, :] == bm
     assert bm[0:1, 0:2] == ByteMatrix(1, 2, [b'12'])
     assert bm[0, 0:2] == ByteMatrix(1, 2, [b'12'])
     assert bm[0:2, 0] == ByteMatrix(2, 1, [b'1', b'4'])
     assert bm[0:0, :] == ByteMatrix()
Example #6
0
 def test_setitem(self):
     """Test int and slice assignment."""
     bm = ByteMatrix(2, 3, [b'123', b'456'])
     bm[1, 2] = ord(b'Z')
     assert bm.to_bytes() == b'12345Z'
     bm[0:1, 0:2] = ByteMatrix(1, 2, [[1, 2]])
     assert bm.to_bytes() == b'\x01\x02345Z'
     bm[1, 0:2] = ByteMatrix(1, 2, [[4, 5]])
     assert bm.to_bytes() == b'\x01\x023\x04\x05Z'
     bm[0:2, 0] = ByteMatrix(2, 1, [[65], [66]])
     assert bm.to_bytes() == b'A\x023B\x05Z'
Example #7
0
 def test_setitem_bad(self):
     """Test slice assignment to bad type."""
     bm = ByteMatrix(2, 3, [b'123', b'456'])
     with self.assertRaises(TypeError):
         bm[0:1, 0:2] = 1.5
     with self.assertRaises(ValueError):
         bm[0:1, 0:2] = -1
Example #8
0
 def test_elementwise_int(self):
     """Test elementwise operations with scalar."""
     bm = ByteMatrix(2, 3, b'\x00\x01\x02\x03\x04\x05')
     assert (bm | 1).to_bytes() == b'\x01\x01\x03\x03\x05\x05'
     assert (bm & 1).to_bytes() == b'\x00\x01\x00\x01\x00\x01'
     assert (bm ^ 1).to_bytes() == b'\x01\x00\x03\x02\x05\x04'
     assert (bm >> 1).to_bytes() == b'\x00\x00\x01\x01\x02\x02'
     assert (bm << 1).to_bytes() == b'\x00\x02\x04\x06\x08\x0a'
     # lsh out of bounds
     assert (bm << 8).to_bytes() == b'\0\0\0\0\0\0'
Example #9
0
 def test_unpack(self):
     """Test unpacking packed representation."""
     assert ByteMatrix.frompacked(b'\x18\x00', 1, 4) == ByteMatrix(
         1, 8, [[0, 1, 2, 0, 0, 0, 0, 0]])
     assert ByteMatrix.frompacked(b'\xfe', 1, 8) == ByteMatrix(
         1, 8, [[1, 1, 1, 1, 1, 1, 1, 0]])
     # empty
     assert ByteMatrix.frompacked(b'', 0, 8) == ByteMatrix()
Example #10
0
 def test_elementwise_inplace_int(self):
     """Test in-place elementwise operations with scalar."""
     bm = ByteMatrix(2, 3, 0)
     bm |= 1
     assert bm.to_bytes() == b'\x01' * 6
     bm &= 255
     assert bm.to_bytes() == b'\x01' * 6
     bm ^= 2
     assert bm.to_bytes() == b'\x03' * 6
     bm >>= 1
     assert bm.to_bytes() == b'\x01' * 6
     bm <<= 2
     assert bm.to_bytes() == b'\x04' * 6
Example #11
0
 def test_fromhex(self):
     """Test unpacking packed representation."""
     assert ByteMatrix.fromhex(b'1800', 1,
                               4) == ByteMatrix(1, 8,
                                                [[0, 1, 2, 0, 0, 0, 0, 0]])
     assert ByteMatrix.fromhex(b'fe', 1,
                               8) == ByteMatrix(1, 8,
                                                [[1, 1, 1, 1, 1, 1, 1, 0]])
     # empty
     assert ByteMatrix.fromhex(b'', 0, 8) == ByteMatrix()
Example #12
0
 def test_to_rows(self):
     """Test to_rows."""
     assert ByteMatrix(2, 3, b'123456').to_rows() == ((0x31, 0x32, 0x33),
                                                      (0x34, 0x35, 0x36))
Example #13
0
 def test_copy(self):
     """Test copying."""
     bm = ByteMatrix(2, 3, b'123456')
     copy = bm.copy()
     bm[:, :] = 0
     assert copy == ByteMatrix(2, 3, b'123456')
Example #14
0
 def test_move(self):
     """Test moving submatrix."""
     bm = ByteMatrix(2, 3, b'123456')
     bm.move(1, 2, 0, 2, 0, 0)
     assert bm == ByteMatrix(2, 3, b'453\x00\x006')
Example #15
0
 def test_to_bytes(self):
     """Test to_bytes."""
     assert ByteMatrix(2, 3, b'123456').to_bytes() == b'123456'
Example #16
0
 def test_htile(self):
     """Test horizontal tiling."""
     bm = ByteMatrix(2, 3, b'123456')
     assert bm.htile(2) == ByteMatrix(2, 6, b'123123456456')
Example #17
0
 def test_vtile(self):
     """Test vertical tiling."""
     bm = ByteMatrix(2, 3, b'123456')
     assert bm.vtile(2) == ByteMatrix(4, 3, b'123456123456')
Example #18
0
 def test_hrepeat(self):
     """Test horizontal repeating."""
     bm = ByteMatrix(2, 3, b'123456')
     assert bm.hrepeat(2) == ByteMatrix(2, 6, b'112233445566')
Example #19
0
 def test_vrepeat(self):
     """Test vertical reapeating."""
     bm = ByteMatrix(2, 3, b'123456')
     assert bm.vrepeat(2) == ByteMatrix(4, 3, b'123123456456')
Example #20
0
 def test_hextend(self):
     """Test horizontal extending."""
     bm = ByteMatrix(2, 3, 0)
     assert bm.hextend(2,
                       1) == ByteMatrix(2, 5,
                                        [[0, 0, 0, 1, 1], [0, 0, 0, 1, 1]])
Example #21
0
 def test_vextend(self):
     """Test vertical extending."""
     bm = ByteMatrix(2, 3, 0)
     assert bm.vextend(2, 1) == ByteMatrix(
         4, 3, [[0, 0, 0], [0, 0, 0], [1, 1, 1], [1, 1, 1]])
Example #22
0
 def test_repr(self):
     """Debugging repr."""
     bm = ByteMatrix(2, 3, [b'123', b'456'])
     assert isinstance(repr(bm), str)
Example #23
0
 def test_hstack(self):
     """Test horizontal stacking."""
     bm = ByteMatrix(2, 3, b'123456')
     bm2 = ByteMatrix(2, 1, b'ab')
     assert hstack((bm, bm2)) == ByteMatrix(2, 4, b'123a456b')
Example #24
0
 def test_view(self):
     """Test viewing."""
     bm = ByteMatrix(2, 3, b'123456')
     copy = bm.view
     bm[:, :] = 0
     assert copy == ByteMatrix(2, 3, 0)
Example #25
0
 def test_eq(self):
     """Test equality."""
     bm = ByteMatrix(2, 3, [b'123', b'456'])
     assert bm == bm
     assert bm == ByteMatrix(2, 3, [b'123', b'456'])
     assert not (bm == ByteMatrix(2, 3, [b'123', b'457']))
Example #26
0
 def test_ne(self):
     """Test nonequality."""
     bm = ByteMatrix(2, 3, [b'123', b'456'])
     assert not (bm != bm)
     assert not (bm != ByteMatrix(2, 3, [b'123', b'456']))
     assert bm != ByteMatrix(2, 3, [b'123', b'457'])
Example #27
0
 def test_view_from_buffer(self):
     """Test view over buffer with pitch."""
     buf = bytearray(b'1230000045600000')
     bm = ByteMatrix.view_from_buffer(2, 3, 8, buf)
     bm[:, :] = 0
     assert buf == bytearray(b'\0\0\x0000000\0\0\x0000000')
Example #28
0
 def test_render(self):
     """Test rendering."""
     bm = ByteMatrix(2, 3, 0)
     bm[1, :] = 1
     assert bm.render(10, 42) == ByteMatrix(2, 3,
                                            [[10, 10, 10], [42, 42, 42]])
Example #29
0
 def test_vstack(self):
     """Test vertical stacking."""
     bm = ByteMatrix(2, 3, b'123456')
     bm2 = ByteMatrix(1, 3, b'abc')
     assert vstack((bm, bm2)) == ByteMatrix(3, 3, b'123456abc')
Example #30
0
 def test_list_of_bytes(self):
     """Create matrix from list of bytes."""
     bm = ByteMatrix(2, 3, [b'123', b'456'])
     assert bm.width == 3
     assert bm.height == 2
     assert bm.to_bytes() == b'123456'