1# test ustruct with a count specified before the type
2
3try:
4    import ustruct as struct
5except:
6    try:
7        import struct
8    except ImportError:
9        print("SKIP")
10        raise SystemExit
11
12print(struct.calcsize('0s'))
13print(struct.unpack('0s', b''))
14print(struct.pack('0s', b'123'))
15
16print(struct.calcsize('2s'))
17print(struct.unpack('2s', b'12'))
18print(struct.pack('2s', b'123'))
19
20print(struct.calcsize('2H'))
21print(struct.unpack('<2H', b'1234'))
22print(struct.pack('<2H', 258, 515))
23
24print(struct.calcsize('0s1s0H2H'))
25print(struct.unpack('<0s1s0H2H', b'01234'))
26print(struct.pack('<0s1s0H2H', b'abc', b'abc', 258, 515))
27
28# check that we get an error if the buffer is too small
29try:
30    struct.unpack('2H', b'\x00\x00')
31except:
32    print('Exception')
33try:
34    struct.pack_into('2I', bytearray(4), 0, 0)
35except:
36    print('Exception')
37
38# check that unknown types raise an exception
39try:
40    struct.unpack('z', b'1')
41except:
42    print('Exception')
43
44try:
45    struct.pack('z', (b'1',))
46except:
47    print('Exception')
48
49try:
50    struct.calcsize('0z')
51except:
52    print('Exception')
53
54# check that a count without a type specifier raises an exception
55
56try:
57    struct.calcsize('1')
58except:
59    print('Exception')
60
61try:
62    struct.pack('1')
63except:
64    print('Exception')
65
66try:
67    struct.pack_into('1', bytearray(4), 0, 'xx')
68except:
69    print('Exception')
70
71try:
72    struct.unpack('1', 'xx')
73except:
74    print('Exception')
75
76try:
77    struct.unpack_from('1', 'xx')
78except:
79    print('Exception')
80