from xctype import * 

def test():
    a = U32()
    print(a,a.get_size())
    a.set_val(32)
    print(a,a.get_size())
    b = U16()
    print(b,b.get_size())
    b.set_val(16)
    print(b,b.get_size())

    s = make_struct('only_primitive_struct_auto', a=U32(), b=U16())()
    print(s,s.get_size())
    ba = bytearray()
    ba += a.conv_to_bytes()
    ba += b.conv_to_bytes()
    s.conv_from_bytes(ba)
    print(s,s.get_size())
    s.a = 0xAA
    print(s)
    s.a = 0x10203040
    print(s)

    #exit()

    array = Array(elem_class=U16,num_elem=3)
    print(array,array.get_size())
    array.conv_from_bytes(ba)
    print(array, array.get_size())

    s = make_struct('struct_with_array_auto', a=U16(), b=Array(elem_class = U16,num_elem=2))()
    print(s,s.get_size())
    print(s.conv_to_str(deep=True))
    s.conv_from_bytes(ba)
    print(s,s.get_size())
    print(s.conv_to_str(deep=True))
    asize = symbols('asize')
    array = Array(elem_class=U16,num_elem=asize)
    print(array,array.get_size())
    
    struct_with_var_len_array_auto_class = make_struct('struct_with_var_len_array_auto', size=U16(), data=Array(elem_class = U16,num_elem=symbols('size')/2))
    s = struct_with_var_len_array_auto_class()
    ssize = s.get_size()
    print(s,s.get_size())
    print(s.conv_to_str(deep=True))
    ba = bytearray()
    size = U16(8)
    ba += size.conv_to_bytes()
    for i in range(0,size.get_val()//2):
        e = U16()
        e.set_val(i)
        ba += e.conv_to_bytes()
    s.conv_from_bytes(ba)
    print(s,s.get_size())
    print(s.conv_to_str(deep=True))
    print(s.size)
    print(s.data)

