# Path: pyipmi/utils.py
# class ByteBuffer(object):
#     def __init__(self, data=None):
# 
#         if data is not None:
#             self.array = array('B', data)
#         else:
#             self.array = array('B')
# 
#     def push_unsigned_int(self, value, length):
#         for i in range(length):
#             self.array.append((value >> (8*i) & 0xff))
# 
#     def pop_unsigned_int(self, length):
#         value = 0
#         for i in range(length):
#             try:
#                 value |= self.array.pop(0) << (8*i)
#             except IndexError:
#                 raise DecodingError('Data too short for message')
#         return value
# 
#     def push_string(self, value):
#         if _PY3 and isinstance(value, str):
#             # Encode Unicode to UTF-8
#             value = value.encode()
#         py3_array_frombytes(self.array, value)
# 
#     def pop_string(self, length):
#         string = self.array[0:length]
#         del self.array[0:length]
#         return py3_array_tobytes(string)
#         # return py3dec_unic_bytes_fix(string.tostring())
# 
#     def pop_slice(self, length):
#         if len(self.array) < length:
#             raise DecodingError('Data too short for message')
# 
#         c = ByteBuffer(self.array[0:length])
#         self.__delslice__(0, length)
#         return c
# 
#     def tobytes(self):
#         return self.array.tobytes()
# 
#     def tostring(self):
#         return py3_array_tobytes(self.array)
# 
#     def extend(self, data):
#         self.array.extend(data)
# 
#     def append_array(self, a):
#         self.array.extend(a)
# 
#     def __getslice__(self, a, b):
#         return self.array[a:b]
# 
#     def __delslice__(self, a, b):
#         del self.array[a:b]
# 
#     def __len__(self):
#         return len(self.array)
# 
#     def __getitem__(self, idx):
#         return self.array[idx]
# 
# def chunks(data, count):
#     for i in range(0, len(data), count):
#         yield data[i:i+count]
# 
# Path: pyipmi/errors.py
# class DecodingError(Exception):
#     """Error on message decoding."""
#     pass


from array import array
from nose.tools import eq_, raises
from pyipmi.utils import ByteBuffer, chunks
from pyipmi.errors import DecodingError
    eq_(buf.tostring(), b'23')


def test_bytebuffer_tostring():
    buf = ByteBuffer(b'\x30\x31\x32\x33')
    eq_(buf.tostring(), b'0123')


def test_bytebuffer_pop_slice():
    buf = ByteBuffer(b'\x30\x31\x32\x33')
    cut = buf.pop_slice(1)
    eq_(buf.tostring(), b'123')
    eq_(cut.tostring(), b'0')

    buf = ByteBuffer(b'\x30\x31\x32\x33')
    cut = buf.pop_slice(2)
    eq_(buf.tostring(), b'23')
    eq_(cut.tostring(), b'01')

    buf = ByteBuffer(b'\x30\x31\x32\x33')
    cut = buf.pop_slice(3)
    eq_(buf.tostring(), b'3')
    eq_(cut.tostring(), b'012')

    buf = ByteBuffer(b'\x30\x31\x32\x33')
    cut = buf.pop_slice(4)
    eq_(buf.tostring(), b'')
    eq_(cut.tostring(), b'0123')



# Path: pyipmi/utils.py
# class ByteBuffer(object):
#     def __init__(self, data=None):
# 
#         if data is not None:
#             self.array = array('B', data)
#         else:
#             self.array = array('B')
# 
#     def push_unsigned_int(self, value, length):
#         for i in range(length):
#             self.array.append((value >> (8*i) & 0xff))
# 
#     def pop_unsigned_int(self, length):
#         value = 0
#         for i in range(length):
#             try:
#                 value |= self.array.pop(0) << (8*i)
#             except IndexError:
#                 raise DecodingError('Data too short for message')
#         return value
# 
#     def push_string(self, value):
#         if _PY3 and isinstance(value, str):
#             # Encode Unicode to UTF-8
#             value = value.encode()
#         py3_array_frombytes(self.array, value)
# 
#     def pop_string(self, length):
#         string = self.array[0:length]
#         del self.array[0:length]
#         return py3_array_tobytes(string)
#         # return py3dec_unic_bytes_fix(string.tostring())
# 
#     def pop_slice(self, length):
#         if len(self.array) < length:
#             raise DecodingError('Data too short for message')
# 
#         c = ByteBuffer(self.array[0:length])
#         self.__delslice__(0, length)
#         return c
# 
#     def tobytes(self):
#         return self.array.tobytes()
# 
#     def tostring(self):
#         return py3_array_tobytes(self.array)
# 
#     def extend(self, data):
#         self.array.extend(data)
# 
#     def append_array(self, a):
#         self.array.extend(a)
# 
#     def __getslice__(self, a, b):
#         return self.array[a:b]
# 
#     def __delslice__(self, a, b):
#         del self.array[a:b]
# 
#     def __len__(self):
#         return len(self.array)
# 
#     def __getitem__(self, idx):
#         return self.array[idx]
# 
# def chunks(data, count):
#     for i in range(0, len(data), count):
#         yield data[i:i+count]
# 
# Path: pyipmi/errors.py
# class DecodingError(Exception):
#     """Error on message decoding."""
#     pass


# Path: tests/test_utils.py
from array import array
from nose.tools import eq_, raises
from pyipmi.utils import ByteBuffer, chunks
from pyipmi.errors import DecodingError

    eq_(buf.tostring(), b'23')


def test_bytebuffer_tostring():
    buf = ByteBuffer(b'\x30\x31\x32\x33')
    eq_(buf.tostring(), b'0123')


def test_bytebuffer_pop_slice():
    buf = ByteBuffer(b'\x30\x31\x32\x33')
    cut = buf.pop_slice(1)
    eq_(buf.tostring(), b'123')
    eq_(cut.tostring(), b'0')

    buf = ByteBuffer(b'\x30\x31\x32\x33')
    cut = buf.pop_slice(2)
    eq_(buf.tostring(), b'23')
    eq_(cut.tostring(), b'01')

    buf = ByteBuffer(b'\x30\x31\x32\x33')
    cut = buf.pop_slice(3)
    eq_(buf.tostring(), b'3')
    eq_(cut.tostring(), b'012')

    buf = ByteBuffer(b'\x30\x31\x32\x33')
    cut = buf.pop_slice(4)
    eq_(buf.tostring(), b'')
    eq_(cut.tostring(), b'0123')



@raises(DecodingError)