# Path: pif_ir/bir/objects/value_instance.py
# class ValueInstance(object):
#     def __init__(self, name, bit_width, value=0):
#         if bit_width <= 0:
#             raise BIRFieldWidthError(name, bit_width)
#         logging.debug("Adding value {0}".format(name))
#         self.name = name
#         self.bit_width = bit_width  
#         self.value = value 
# 
#     def __len__(self):
#         return self.bit_width
# 
#     def __int__(self):
#         return self.value
# 
#     def extract(self, buf, bit_offset=0):
#         self.value = bytearray_to_int(buf, self.bit_width, bit_offset)
#         return self.value
# 
#     def update(self, buf, bit_offset=0):
#         byte_offset = bit_offset / 8 
#         offset = bit_offset % 8
# 
#         vals, masks = int_to_bytearray(offset, self.value, self.bit_width)
#         for idx, (val, mask) in enumerate(zip(vals,masks)):
#             buf[byte_offset + idx] &= mask
#             buf[byte_offset + idx] += val


import logging
import struct
from pif_ir.bir.objects.value_instance import ValueInstance
#!/usr/bin/env python

logging.basicConfig(level=logging.INFO)
logging.info("RUNNING TEST: %s" % __file__)

data = struct.pack("BBBBBBBB", 0x81, 0, 0xa1, 0x64, 0x81, 0, 0, 0xc8)
pkt = bytearray(data)

# test cases: (size, offset, expected result)
tests = [(8, 0, 0x81), (16, 0, 0x8100), (3, 0, 4), (12, 0, 0x810),
         (8, 3, 0x08), (16, 3, 0x0805), (3, 3, 0), (12, 3, 0x080),
         (8, 7, 0x80), (16, 7, 0x8050), (3, 7, 4), (12, 7, 0x805),
         (3, 5, 1), (12, 4, 0x100), (13, 19, 0x164)]

for case, args in enumerate(tests):
    val = ValueInstance('val', args[0])
    val.extract(pkt, bit_offset=args[1])
    if int(val) != args[2]:
        logging.error("Test Case {0}: {1} != {2}".format(
            case, hex(int(val)), hex(args[2])))
        exit(1)

values = range(16)
values.extend([0xaaaaaaaa, 0x55555555, 0xffffffff])


# Path: pif_ir/bir/objects/value_instance.py
# class ValueInstance(object):
#     def __init__(self, name, bit_width, value=0):
#         if bit_width <= 0:
#             raise BIRFieldWidthError(name, bit_width)
#         logging.debug("Adding value {0}".format(name))
#         self.name = name
#         self.bit_width = bit_width  
#         self.value = value 
# 
#     def __len__(self):
#         return self.bit_width
# 
#     def __int__(self):
#         return self.value
# 
#     def extract(self, buf, bit_offset=0):
#         self.value = bytearray_to_int(buf, self.bit_width, bit_offset)
#         return self.value
# 
#     def update(self, buf, bit_offset=0):
#         byte_offset = bit_offset / 8 
#         offset = bit_offset % 8
# 
#         vals, masks = int_to_bytearray(offset, self.value, self.bit_width)
#         for idx, (val, mask) in enumerate(zip(vals,masks)):
#             buf[byte_offset + idx] &= mask
#             buf[byte_offset + idx] += val


# Path: pif_ir/bir/tests/test_value_instance.py
import logging
import struct
from pif_ir.bir.objects.value_instance import ValueInstance

#!/usr/bin/env python

logging.basicConfig(level=logging.INFO)
logging.info("RUNNING TEST: %s" % __file__)

data = struct.pack("BBBBBBBB", 0x81, 0, 0xa1, 0x64, 0x81, 0, 0, 0xc8)
pkt = bytearray(data)

# test cases: (size, offset, expected result)
tests = [(8, 0, 0x81), (16, 0, 0x8100), (3, 0, 4), (12, 0, 0x810),
         (8, 3, 0x08), (16, 3, 0x0805), (3, 3, 0), (12, 3, 0x080),
         (8, 7, 0x80), (16, 7, 0x8050), (3, 7, 4), (12, 7, 0x805),
         (3, 5, 1), (12, 4, 0x100), (13, 19, 0x164)]

for case, args in enumerate(tests):
    val = ValueInstance('val', args[0])
    val.extract(pkt, bit_offset=args[1])
    if int(val) != args[2]:
        logging.error("Test Case {0}: {1} != {2}".format(
            case, hex(int(val)), hex(args[2])))
        exit(1)

values = range(16)
values.extend([0xaaaaaaaa, 0x55555555, 0xffffffff])


for width in range(1, 33):