# Path: bdec/constraints.py
# class Equals(Constraint):
#     type = '!='
# 
#     def check(self, entry, value, context):
#         expected = self.limit.evaluate(context)
#         if isinstance(expected, int):
#             if int(value) !=  expected:
#                 raise ConstraintError(entry, int(value), '==', expected)
#         elif isinstance(expected, Data):
#             # We aren't checking the length here, only the value (for example,
#             # the expected value for a variable length binary field will have
#             # a different length than the actual). Only compare the bits that
#             # are valid.
#             length_diff = len(value) - len(expected)
#             if length_diff > 0:
#                 # The expected value is shorter than the actual, so grow it.
#                 expected = Data('\x00' * (length_diff / 8 + 1), 0, length_diff) + expected
#             elif length_diff < 0:
#                 # The expected value is longer than the actual, so shrink it.
#                 shorter = expected.copy()
#                 leading = shorter.pop(-length_diff)
#                 if not int(leading):
#                     # We can safely pop the leading bits.
#                     expected = shorter
#             if value !=  expected:
#                 raise ConstraintError(entry, value, '==', expected)
#         else:
#             if value !=  expected:
#                 raise ConstraintError(entry, value, '==', expected)
# 
#     def range(self):
#         limit = _limit(self.limit)
#         return Range(limit, limit)
# 
# class Minimum(Constraint):
#     type = '<'
# 
#     def check(self, entry, value, context):
#         expected = self.limit.evaluate(context)
#         if isinstance(value, basestring):
#             # It is useful to check the bounds of a text character...
#             value = ord(value)
#         if int(value) < expected:
#             raise ConstraintError(entry, int(value), '>=', expected)
# 
#     def range(self):
#         return Range(_limit(self.limit), None)
# 
# class Maximum(Constraint):
#     type = '>'
# 
#     def check(self, entry, value, context):
#         expected = self.limit.evaluate(context)
#         if isinstance(value, basestring):
#             # It is useful to check the bounds of a text character...
#             value = ord(value)
#         if int(value) > expected:
#             raise ConstraintError(entry, int(value), '<=', expected)
# 
#     def range(self):
#         return Range(None, _limit(self.limit))
# 
# Path: bdec/sequenceof.py
# class SequenceOf(bdec.entry.Entry):
#     """
#     A protocol entry representing a sequence of another protocol entry.
# 
#     The number of times the child entry will loop can be set in one of three
#     ways;
#      
#      * It can loop for a specified amount of times
#      * It can loop until a buffer is empty
#      * It can loop until a child entry decodes
#     """
#     STOPPED = "stopped"
#     ITERATING = "iterating"
#     STOPPING = "stopping"
# 
#     def __init__(self, name, child, count=None, length=None, end_entries=[]):
#         """
#         count -- The number of times the child will repeat. If this value is
#           None, the count will not be used.
#         length -- The size of the buffer in bits. When the buffer is empty, the
#           looping will stop. If None, the length will not be used.
#         end_entries -- A list of child entries whose successful decode
#           indicates the loop should stop.
# 
#         If neither count, length, or end_entries are used, the SequenceOf will
#         fail to decode after using all of the available buffer.
#         """
#         bdec.entry.Entry.__init__(self, name, length, [child])
#         if isinstance(count, int):
#             count = Constant(count)
#         assert count is None or isinstance(count, Expression)
#         self.count = count
#         self.end_entries = end_entries
# 
#     def _validate(self):
#         bdec.entry.Entry._validate(self)
#         for entry in self.end_entries:
#             assert isinstance(entry, bdec.entry.Entry), "%s isn't an entry instance!" % str(entry)


import unittest
import bdec.choice as chc
import bdec.data as dt
import bdec.expression as expr
import bdec.field as fld
import bdec.inspect.chooser as chsr
import bdec.sequence as seq
from bdec.constraints import Equals, Minimum, Maximum
from bdec.sequenceof import SequenceOf
        chooser = chsr.Chooser([b1, b2])
        self.assertEqual([b1, b2], chooser.choose(dt.Data('\x00\x00')))
        self.assertEqual([b2], chooser.choose(dt.Data('\x00')))

    def test_choice_with_empty_sequenceof(self):
        # When an option is a sequenceof (with a possible length of zero), it
        # should always be treated as a possible.
        end = seq.Sequence(None, [])
        a = SequenceOf('a',
                end,
                end_entries=[end])
        b = fld.Field('b', length=8, constraints=[Equals(dt.Data('\x00'))])

        chooser = chsr.Chooser([a, b])
        self.assertEqual([a, b], chooser.choose(dt.Data('\x00')))
        self.assertEqual([a], chooser.choose(dt.Data('')))

    def test_choice_with_trailing_sequenceof(self):
        # There was a bug with choosing between embedded choices and sequenceof
        # objects
        end = seq.Sequence(None, [])
        optional_ws = SequenceOf('optional whitespace',
                chc.Choice('optional', [
                    fld.Field('a1', length=16, constraints=[Equals(dt.Data(' '))]),
                    end]),
                end_entries=[end])

        a = chc.Choice('a', [
                seq.Sequence('if', [
                    fld.Field('a1', length=16, constraints=[Equals(dt.Data('if'))]),

# Path: bdec/constraints.py
# class Equals(Constraint):
#     type = '!='
# 
#     def check(self, entry, value, context):
#         expected = self.limit.evaluate(context)
#         if isinstance(expected, int):
#             if int(value) !=  expected:
#                 raise ConstraintError(entry, int(value), '==', expected)
#         elif isinstance(expected, Data):
#             # We aren't checking the length here, only the value (for example,
#             # the expected value for a variable length binary field will have
#             # a different length than the actual). Only compare the bits that
#             # are valid.
#             length_diff = len(value) - len(expected)
#             if length_diff > 0:
#                 # The expected value is shorter than the actual, so grow it.
#                 expected = Data('\x00' * (length_diff / 8 + 1), 0, length_diff) + expected
#             elif length_diff < 0:
#                 # The expected value is longer than the actual, so shrink it.
#                 shorter = expected.copy()
#                 leading = shorter.pop(-length_diff)
#                 if not int(leading):
#                     # We can safely pop the leading bits.
#                     expected = shorter
#             if value !=  expected:
#                 raise ConstraintError(entry, value, '==', expected)
#         else:
#             if value !=  expected:
#                 raise ConstraintError(entry, value, '==', expected)
# 
#     def range(self):
#         limit = _limit(self.limit)
#         return Range(limit, limit)
# 
# class Minimum(Constraint):
#     type = '<'
# 
#     def check(self, entry, value, context):
#         expected = self.limit.evaluate(context)
#         if isinstance(value, basestring):
#             # It is useful to check the bounds of a text character...
#             value = ord(value)
#         if int(value) < expected:
#             raise ConstraintError(entry, int(value), '>=', expected)
# 
#     def range(self):
#         return Range(_limit(self.limit), None)
# 
# class Maximum(Constraint):
#     type = '>'
# 
#     def check(self, entry, value, context):
#         expected = self.limit.evaluate(context)
#         if isinstance(value, basestring):
#             # It is useful to check the bounds of a text character...
#             value = ord(value)
#         if int(value) > expected:
#             raise ConstraintError(entry, int(value), '<=', expected)
# 
#     def range(self):
#         return Range(None, _limit(self.limit))
# 
# Path: bdec/sequenceof.py
# class SequenceOf(bdec.entry.Entry):
#     """
#     A protocol entry representing a sequence of another protocol entry.
# 
#     The number of times the child entry will loop can be set in one of three
#     ways;
#      
#      * It can loop for a specified amount of times
#      * It can loop until a buffer is empty
#      * It can loop until a child entry decodes
#     """
#     STOPPED = "stopped"
#     ITERATING = "iterating"
#     STOPPING = "stopping"
# 
#     def __init__(self, name, child, count=None, length=None, end_entries=[]):
#         """
#         count -- The number of times the child will repeat. If this value is
#           None, the count will not be used.
#         length -- The size of the buffer in bits. When the buffer is empty, the
#           looping will stop. If None, the length will not be used.
#         end_entries -- A list of child entries whose successful decode
#           indicates the loop should stop.
# 
#         If neither count, length, or end_entries are used, the SequenceOf will
#         fail to decode after using all of the available buffer.
#         """
#         bdec.entry.Entry.__init__(self, name, length, [child])
#         if isinstance(count, int):
#             count = Constant(count)
#         assert count is None or isinstance(count, Expression)
#         self.count = count
#         self.end_entries = end_entries
# 
#     def _validate(self):
#         bdec.entry.Entry._validate(self)
#         for entry in self.end_entries:
#             assert isinstance(entry, bdec.entry.Entry), "%s isn't an entry instance!" % str(entry)


# Path: bdec/inspect/test/testchooser.py
import unittest
import bdec.choice as chc
import bdec.data as dt
import bdec.expression as expr
import bdec.field as fld
import bdec.inspect.chooser as chsr
import bdec.sequence as seq
from bdec.constraints import Equals, Minimum, Maximum
from bdec.sequenceof import SequenceOf

        chooser = chsr.Chooser([b1, b2])
        self.assertEqual([b1, b2], chooser.choose(dt.Data('\x00\x00')))
        self.assertEqual([b2], chooser.choose(dt.Data('\x00')))

    def test_choice_with_empty_sequenceof(self):
        # When an option is a sequenceof (with a possible length of zero), it
        # should always be treated as a possible.
        end = seq.Sequence(None, [])
        a = SequenceOf('a',
                end,
                end_entries=[end])
        b = fld.Field('b', length=8, constraints=[Equals(dt.Data('\x00'))])

        chooser = chsr.Chooser([a, b])
        self.assertEqual([a, b], chooser.choose(dt.Data('\x00')))
        self.assertEqual([a], chooser.choose(dt.Data('')))

    def test_choice_with_trailing_sequenceof(self):
        # There was a bug with choosing between embedded choices and sequenceof
        # objects
        end = seq.Sequence(None, [])
        optional_ws = SequenceOf('optional whitespace',
                chc.Choice('optional', [
                    fld.Field('a1', length=16, constraints=[Equals(dt.Data(' '))]),
                    end]),
                end_entries=[end])

        a = chc.Choice('a', [
                seq.Sequence('if', [
                    fld.Field('a1', length=16, constraints=[Equals(dt.Data('if'))]),

                    SequenceOf('footer',