示例#1
0
def test_check_constraints():
    a = Strings()
    a.empty_string = 'test'
    assert(a.empty_string == 'test')
    assert_raises(AssertionError, setattr, a, 'empty_string', 1234)
    a.ub_string = 'a' * 22
    assert(a.ub_string == 'a' * 22)
    assert_raises(AssertionError, setattr, a, 'ub_string', 'a' * 23)

    b = Nested()
    primitives = Primitives()
    b.primitives = primitives
    assert(b.primitives == primitives)
    assert_raises(AssertionError, setattr, b, 'primitives', 'foo')

    list_of_primitives = [primitives, primitives]
    tuple_of_primitives = (primitives, primitives)
    b.two_primitives = list_of_primitives
    assert(b.two_primitives == list_of_primitives)
    assert(type(b.two_primitives) == list)
    b.two_primitives = tuple_of_primitives
    assert(b.two_primitives == tuple_of_primitives)
    assert(type(b.two_primitives) == tuple)
    assert_raises(AssertionError, setattr, b, 'two_primitives', Primitives())
    assert_raises(AssertionError, setattr, b, 'two_primitives', [Primitives()])
    assert_raises(AssertionError, setattr, b, 'two_primitives',
                  [primitives, primitives, primitives])

    # TODO(wjwwood): reenable when bounded arrays are working C type support.
    # b.up_to_three_primitives = []
    # assert(b.up_to_three_primitives == [])
    # b.up_to_three_primitives = [primitives]
    # assert(b.up_to_three_primitives == [primitives])
    # b.up_to_three_primitives = [primitives, primitives]
    # assert(b.up_to_three_primitives == [primitives, primitives])
    # b.up_to_three_primitives = [primitives, primitives, primitives]
    # assert(b.up_to_three_primitives == [primitives, primitives, primitives])
    # assert_raises(AssertionError, setattr, b, 'up_to_three_primitives',
    #               [primitives, primitives, primitives, primitives])

    b.unbounded_primitives = [primitives, primitives]
    assert(b.unbounded_primitives == [primitives, primitives])

    c = Various()
    c.byte_value = b'a'
    assert(c.byte_value == b'a')
    assert(c.byte_value != 'a')
    assert_raises(AssertionError, setattr, c, 'byte_value', 'a')
    assert_raises(AssertionError, setattr, c, 'byte_value', b'abc')
    assert_raises(AssertionError, setattr, c, 'byte_value', 'abc')

    c.char_value = 'a'
    assert(c.char_value == 'a')
    assert(c.char_value != b'a')
    assert_raises(AssertionError, setattr, c, 'char_value', b'a')
    assert_raises(AssertionError, setattr, c, 'char_value', 'abc')
    assert_raises(AssertionError, setattr, c, 'char_value', b'abc')
def test_out_of_range():
    a = Primitives()
    with pytest.raises(AssertionError):
        setattr(a, 'char_value', '\x80')
    for i in [8, 16, 32, 64]:
        with pytest.raises(AssertionError):
            setattr(a, 'int%d_value' % i, 2**(i - 1))
        with pytest.raises(AssertionError):
            setattr(a, 'int%d_value' % i, -2**(i - 1) - 1)
        with pytest.raises(AssertionError):
            setattr(a, 'uint%d_value' % i, -1)
        with pytest.raises(AssertionError):
            setattr(a, 'int%d_value' % i, 2**i)

    b = Various()
    with pytest.raises(AssertionError):
        setattr(b, 'two_uint16_value', [2**16])
    with pytest.raises(AssertionError):
        setattr(b, 'two_uint16_value', [-1])

    with pytest.raises(AssertionError):
        setattr(b, 'up_to_three_int32_values', [2**31])
    with pytest.raises(AssertionError):
        setattr(b, 'up_to_three_int32_values', [-2**31 - 1])

    with pytest.raises(AssertionError):
        setattr(b, 'unbounded_uint64_values', [2**64])
    with pytest.raises(AssertionError):
        setattr(b, 'unbounded_uint64_values', [-1])
示例#3
0
def test_default_values():
    a = Strings()

    assert '' == a.empty_string
    assert 'Hello world!' == a.def_string
    a.def_string = 'Bye world'
    assert 'Bye world' == a.def_string
    assert 'Hello world!' == Strings.DEF_STRING__DEFAULT
    assert 'Hello world!' == a.DEF_STRING__DEFAULT

    assert 'Hello\'world!' == a.DEF_STRING2__DEFAULT
    assert 'Hello"world!' == a.DEF_STRING3__DEFAULT
    assert 'Hello\'world!' == a.DEF_STRING4__DEFAULT
    assert 'Hello"world!' == a.DEF_STRING5__DEFAULT
    with pytest.raises(AttributeError):
        setattr(Strings, 'DEF_STRING__DEFAULT', 'bar')

    b = StringArrays()
    assert ['What', 'a', 'wonderful', 'world',
            '!'] == b.DEF_STRING_DYNAMIC_ARRAY_VALUE__DEFAULT
    assert ['Hello', 'World', '!'] == b.DEF_STRING_STATIC_ARRAY_VALUE__DEFAULT
    assert ['Hello', 'World', '!'] == b.DEF_STRING_BOUNDED_ARRAY_VALUE__DEFAULT

    assert ['H"el\'lo', 'Wo\'r"ld'] == b.DEF_VARIOUS_QUOTES__DEFAULT
    assert ['Hel,lo', ',World', 'abcd', '!,'] == b.DEF_VARIOUS_COMMAS__DEFAULT

    c = Various()
    assert [5, 23] == c.TWO_UINT16_VALUE__DEFAULT

    assert [5, 23] == c.UP_TO_THREE_INT32_VALUES_WITH_DEFAULT_VALUES__DEFAULT

    assert '\x01' == c.CHAR_VALUE__DEFAULT
    assert '1' != c.CHAR_VALUE__DEFAULT
    assert b'\x01' == c.BYTE_VALUE__DEFAULT
    assert b'1' != c.BYTE_VALUE__DEFAULT
def test_default_values():
    a = Strings()

    assert '' == a.empty_string
    assert 'Hello world!' == a.def_string
    a.def_string = 'Bye world'
    assert 'Bye world' == a.def_string
    assert 'Hello world!' == Strings.DEF_STRING__DEFAULT
    assert 'Hello world!' == a.DEF_STRING__DEFAULT

    assert "Hello'world!" == a.DEF_STRING2__DEFAULT
    assert 'Hello"world!' == a.DEF_STRING3__DEFAULT
    assert "Hello'world!" == a.DEF_STRING4__DEFAULT
    assert 'Hello"world!' == a.DEF_STRING5__DEFAULT
    with pytest.raises(AttributeError):
        setattr(Strings, 'DEF_STRING__DEFAULT', 'bar')

    a = WStrings()
    assert '' == a.empty_wstring
    assert 'Hello world!' == a.def_wstring
    a.def_wstring = 'Bye world'
    assert 'Bye world' == a.def_wstring
    assert 'Hello world!' == WStrings.DEF_WSTRING__DEFAULT
    assert 'Hello world!' == a.DEF_WSTRING__DEFAULT

    assert "Hello'world!" == a.DEF_WSTRING2__DEFAULT
    assert 'Hello"world!' == a.DEF_WSTRING3__DEFAULT
    assert "Hello'world!" == a.DEF_WSTRING4__DEFAULT
    assert 'Hello"world!' == a.DEF_WSTRING5__DEFAULT
    with pytest.raises(AttributeError):
        setattr(WStrings, 'DEF_WSTRING__DEFAULT', 'bar')

    b = StringArrays()
    assert ['What', 'a', 'wonderful', 'world',
            '!'] == b.DEF_STRING_DYNAMIC_ARRAY_VALUE__DEFAULT
    assert ['Hello', 'World', '!'] == b.DEF_STRING_STATIC_ARRAY_VALUE__DEFAULT
    assert ['Hello', 'World', '!'] == b.DEF_STRING_BOUNDED_ARRAY_VALUE__DEFAULT

    assert ['H"el\'lo', 'Wo\'r"ld'] == b.DEF_VARIOUS_QUOTES__DEFAULT
    assert ['Hel,lo', ',World', 'abcd', '!,'] == b.DEF_VARIOUS_COMMAS__DEFAULT

    c = Various()
    assert isinstance(c.TWO_UINT16_VALUE__DEFAULT, numpy.ndarray)
    assert (2, ) == c.TWO_UINT16_VALUE__DEFAULT.shape
    assert numpy.uint16 == c.TWO_UINT16_VALUE__DEFAULT.dtype
    assert [5, 23] == c.TWO_UINT16_VALUE__DEFAULT.tolist()

    assert isinstance(c.UP_TO_THREE_INT32_VALUES_WITH_DEFAULT_VALUES__DEFAULT,
                      array.array)
    assert 'i' == \
        c.UP_TO_THREE_INT32_VALUES_WITH_DEFAULT_VALUES__DEFAULT.typecode
    assert [5, 23] == \
        c.UP_TO_THREE_INT32_VALUES_WITH_DEFAULT_VALUES__DEFAULT.tolist()

    assert 1 == c.CHAR_VALUE__DEFAULT
    assert '1' != c.CHAR_VALUE__DEFAULT
    assert b'\x01' == c.BYTE_VALUE__DEFAULT
    assert b'1' != c.BYTE_VALUE__DEFAULT
示例#5
0
def test_default_values():
    a = Strings()

    assert a.empty_string is ''
    assert 'Hello world!' == a.def_string
    a.def_string = 'Bye world'
    assert 'Bye world' == a.def_string
    assert 'Hello world!' == Strings.DEF_STRING__DEFAULT
    assert 'Hello world!' == a.DEF_STRING__DEFAULT
    assert 'Hello"world!' == a.DEF_STRING_DELIMITER__DEFAULT
    assert "Hello'world!" == a.DEF_STRING_DELIMITER2__DEFAULT
    assert_raises(AttributeError, setattr, Strings, 'DEF_STRING__DEFAULT',
                  'bar')

    b = Various()
    assert [5, 23] == b.TWO_UINT16_VALUE__DEFAULT

    assert [5, 23] == b.UP_TO_THREE_INT32_VALUES_WITH_DEFAULT_VALUES__DEFAULT

    assert '\x01' == b.CHAR_VALUE__DEFAULT
    assert '1' != b.CHAR_VALUE__DEFAULT
    assert b'\x01' == b.BYTE_VALUE__DEFAULT
    assert b'1' != b.BYTE_VALUE__DEFAULT
def test_check_constraints():
    a = Strings()
    a.empty_string = 'test'
    assert 'test' == a.empty_string
    with pytest.raises(AssertionError):
        setattr(a, 'empty_string', 1234)
    a.ub_string = 'a' * 22
    assert 'a' * 22 == a.ub_string
    with pytest.raises(AssertionError):
        setattr(a, 'ub_string', 'a' * 23)

    b = Nested()
    primitives = Primitives()
    b.primitives = primitives
    assert b.primitives == primitives
    with pytest.raises(AssertionError):
        setattr(b, 'primitives', 'foo')

    list_of_primitives = [primitives, primitives]
    tuple_of_primitives = (primitives, primitives)
    b.two_primitives = list_of_primitives
    assert b.two_primitives == list_of_primitives
    assert type(b.two_primitives) == list
    b.two_primitives = tuple_of_primitives
    assert b.two_primitives == tuple_of_primitives
    assert type(b.two_primitives) == tuple
    with pytest.raises(AssertionError):
        setattr(b, 'two_primitives', Primitives())
    with pytest.raises(AssertionError):
        setattr(b, 'two_primitives', [Primitives()])
    with pytest.raises(AssertionError):
        setattr(b, 'two_primitives', [primitives, primitives, primitives])

    b.up_to_three_primitives = []
    assert [] == b.up_to_three_primitives
    b.up_to_three_primitives = [primitives]
    assert [primitives] == b.up_to_three_primitives
    b.up_to_three_primitives = [primitives, primitives]
    assert [primitives, primitives] == b.up_to_three_primitives
    b.up_to_three_primitives = [primitives, primitives, primitives]
    assert [primitives, primitives, primitives] == b.up_to_three_primitives
    with pytest.raises(AssertionError):
        setattr(b, 'up_to_three_primitives',
                [primitives, primitives, primitives, primitives])

    b.unbounded_primitives = [primitives, primitives]
    assert [primitives, primitives] == b.unbounded_primitives

    c = Various()
    c.byte_value = b'a'
    assert b'a' == c.byte_value
    assert 'a' != c.byte_value
    with pytest.raises(AssertionError):
        setattr(c, 'byte_value', 'a')
    with pytest.raises(AssertionError):
        setattr(c, 'byte_value', b'abc')
    with pytest.raises(AssertionError):
        setattr(c, 'byte_value', 'abc')

    c.char_value = ord('a')
    assert ord('a') == c.char_value
    assert b'a' != c.char_value
    with pytest.raises(AssertionError):
        setattr(c, 'char_value', b'a')
    with pytest.raises(AssertionError):
        setattr(c, 'char_value', 'abc')
    with pytest.raises(AssertionError):
        setattr(c, 'char_value', b'abc')

    c.up_to_three_int32_values = []
    assert [] == c.up_to_three_int32_values.tolist()
    c.up_to_three_int32_values = [12345, -12345]
    assert [12345, -12345] == c.up_to_three_int32_values.tolist()
    c.up_to_three_int32_values = [12345, -12345, 6789]
    assert [12345, -12345, 6789] == c.up_to_three_int32_values.tolist()
    c.up_to_three_int32_values = [12345, -12345, 6789]
    with pytest.raises(AssertionError):
        setattr(c, 'up_to_three_int32_values', [12345, -12345, 6789, -6789])

    c.up_to_three_string_values = []
    assert [] == c.up_to_three_string_values
    c.up_to_three_string_values = ['foo', 'bar']
    assert ['foo', 'bar'] == c.up_to_three_string_values
    c.up_to_three_string_values = ['foo', 'bar', 'baz']
    assert ['foo', 'bar', 'baz'] == c.up_to_three_string_values
    with pytest.raises(AssertionError):
        setattr(c, 'up_to_three_string_values', ['foo', 'bar', 'baz', 'hello'])
示例#7
0
def test_check_constraints():
    a = Strings()
    a.empty_string = 'test'
    assert 'test' == a.empty_string
    assert_raises(AssertionError, setattr, a, 'empty_string', 1234)
    a.ub_string = 'a' * 22
    assert 'a' * 22 == a.ub_string
    assert_raises(AssertionError, setattr, a, 'ub_string', 'a' * 23)

    b = Nested()
    primitives = Primitives()
    b.primitives = primitives
    assert b.primitives == primitives
    assert_raises(AssertionError, setattr, b, 'primitives', 'foo')

    list_of_primitives = [primitives, primitives]
    tuple_of_primitives = (primitives, primitives)
    b.two_primitives = list_of_primitives
    assert b.two_primitives == list_of_primitives
    assert type(b.two_primitives) == list
    b.two_primitives = tuple_of_primitives
    assert b.two_primitives == tuple_of_primitives
    assert type(b.two_primitives) == tuple
    assert_raises(AssertionError, setattr, b, 'two_primitives', Primitives())
    assert_raises(AssertionError, setattr, b, 'two_primitives', [Primitives()])
    assert_raises(AssertionError, setattr, b, 'two_primitives',
                  [primitives, primitives, primitives])

    b.up_to_three_primitives = []
    assert [] == b.up_to_three_primitives
    b.up_to_three_primitives = [primitives]
    assert [primitives] == b.up_to_three_primitives
    b.up_to_three_primitives = [primitives, primitives]
    assert [primitives, primitives] == b.up_to_three_primitives
    b.up_to_three_primitives = [primitives, primitives, primitives]
    assert [primitives, primitives, primitives] == b.up_to_three_primitives
    assert_raises(AssertionError, setattr, b, 'up_to_three_primitives',
                  [primitives, primitives, primitives, primitives])

    b.unbounded_primitives = [primitives, primitives]
    assert [primitives, primitives] == b.unbounded_primitives

    c = Various()
    c.byte_value = b'a'
    assert b'a' == c.byte_value
    assert 'a' != c.byte_value
    assert_raises(AssertionError, setattr, c, 'byte_value', 'a')
    assert_raises(AssertionError, setattr, c, 'byte_value', b'abc')
    assert_raises(AssertionError, setattr, c, 'byte_value', 'abc')

    c.char_value = 'a'
    assert 'a' == c.char_value
    assert b'a' != c.char_value
    assert_raises(AssertionError, setattr, c, 'char_value', b'a')
    assert_raises(AssertionError, setattr, c, 'char_value', 'abc')
    assert_raises(AssertionError, setattr, c, 'char_value', b'abc')

    c.up_to_three_int32_values = []
    assert [] == c.up_to_three_int32_values
    c.up_to_three_int32_values = [12345, -12345]
    assert [12345, -12345] == c.up_to_three_int32_values
    c.up_to_three_int32_values = [12345, -12345, 6789]
    assert [12345, -12345, 6789] == c.up_to_three_int32_values
    c.up_to_three_int32_values = [12345, -12345, 6789]
    assert_raises(
        AssertionError, setattr, c, 'up_to_three_int32_values', [12345, -12345, 6789, -6789])

    c.up_to_three_string_values = []
    assert [] == c.up_to_three_string_values
    c.up_to_three_string_values = ['foo', 'bar']
    assert ['foo', 'bar'] == c.up_to_three_string_values
    c.up_to_three_string_values = ['foo', 'bar', 'baz']
    assert ['foo', 'bar', 'baz'] == c.up_to_three_string_values
    assert_raises(
        AssertionError, setattr, c, 'up_to_three_string_values', ['foo', 'bar', 'baz', 'hello'])