Wiki

root/cobra/trunk/Tests/100-basics/920-hex-lit.cobra

Revision 1581, 2.9 KB (checked in by Chuck.Esterbrook, 4 years ago)

Fixed: Some bitwise operations with integer literals of 16 or 8 bits produce invalid compilation errors.
reported-by:hopscc

  • Property svn:eol-style set to native
Line 
1namespace Test
2    class HexLit
3        """
4            r'HEX_LIT_UNSIGN        0x[\dA-Fa-f][\dA-Fa-f]*(u|_u)(8|16|32|64)?'
5            r'HEX_LIT_EXPLICIT      0x[\dA-Fa-f][\dA-Fa-f]*_(8|16|32|64)?'
6            r'HEX_LIT               0x[\dA-Fa-f][\dA-Fa-f]*'
7        """
8   
9        def lit is shared
10            """
11            r'HEX_LIT               0x[\dA-Fa-f][\dA-Fa-f]*',
12            """
13
14            h = 0x1
15            assert h == 1u
16            assert h.getType is UInt32
17       
18            h1 = 0xa
19            assert h1 == 10u
20            assert h1.getType is UInt32
21       
22            h2 = 0xF
23            assert h2 == 15u
24            assert h2.getType is UInt32
25       
26            h3 = 0x02
27            assert h3 == 2u
28            assert h3.getType is UInt32
29       
30            h4 = 0x0A
31            assert h4 == 10u
32            assert h4.getType is UInt32
33       
34            h5 = 0x0F
35            assert h5 == 15u
36            assert h5.getType is UInt32
37   
38            h6 = 0x03
39            assert h6 == 3u
40            assert h6.getType is UInt32
41       
42            h7 = 0x0fe
43            assert h7 == 254u
44            assert h.getType is UInt32
45       
46            h8 = 0x0FF
47            assert h8 == 255u
48            assert h8.getType is UInt32
49       
50            h9 = 0x0FFf
51            assert h9 == 4095u
52            assert h9.getType is UInt32
53   
54        def unsign is shared
55            """
56            r'HEX_LIT_UNSIGN        0x[\dA-Fa-f][\dA-Fa-f]*(u|_u)(8|16|32|64)?'
57            """
58           
59            h2a = 0xfu
60            assert h2a == 15u
61            assert h2a.getType is UInt32
62   
63            h2b = 0xf_u
64            assert h2b == 15u
65            assert h2b.getType is UInt32
66           
67            h1 = 0xffu
68            assert h1 == 255u
69            assert h1.getType is UInt32
70           
71            h2 = 0xff_u
72            assert h2 == 255u
73            assert h2.getType is UInt32
74       
75            h3 = 0xf0u16
76            assert h3 == 240u
77            assert h3.getType is UInt16
78       
79            h4 = 0xf0_u16
80            assert h4 == 240u
81            assert h4.getType is UInt16
82           
83            h5 = 0xFF_u64
84            assert h5 == 255u
85            assert h5.getType is UInt64
86           
87            h6 = 0xFF1_u64
88            assert h6 == 4081u
89            assert h6.getType is UInt64
90           
91            h7 = 0xFF_u8
92            assert h7 == 255u
93            assert h7.getType is Byte
94       
95            h8 = 0xFFu8
96            assert h8 == 255u
97            assert h8.getType is Byte
98           
99        def explicit is shared
100            """
101            r'HEX_LIT_EXPLICIT      0x[\dA-Fa-f][\dA-Fa-f]*_(8|16|32|64)?
102            """
103            h1 = 0xe_8
104            assert h1 == 14u
105            assert h1.getType is Byte
106               
107            h2 = 0xa_
108            assert h2 == 10u
109            assert h2.getType is UInt32
110       
111            h3 = 0xe_16
112            assert h3 == 14u
113            assert h3.getType is UInt16
114               
115            h4 = 0x1f_32
116            assert h4 == 31u
117            assert h4.getType is UInt32
118               
119            h5 = 0xFffF_64
120            assert h5 == 65535u
121            assert h5.getType is UInt64
122               
123            h6 = 0xFeed_16
124            assert h6 == 65261u
125            assert h6.getType is UInt16
126                   
127        def random is shared
128            b = 255u8
129            b = 255u8 & 31u8
130            assert b == 31_u8
131            assert b.getType is Byte
132            assert b == 0x1fu8
133               
134            b = 255u8
135            b &= 0x1fu8
136            assert b == 0x1F_u8
137            assert b.getType is Byte
138               
139            b = 255u8
140            b = b & 0x1fu8
141            assert b == 0x1fu8
142
143            b = 255u8
144            b = b & 31u8
145            assert b == 0x1fu8
146
147            b = 255u8
148            b1 = 0x1fu8
149            b = b & b1
150            assert b == 0x1fu8
151   
152            c = 0x0_u8
153            c |= 0x1e_u8
154            assert c == 0x1e_u8
155               
156            c = 0x0_u8
157            c = c | 0x1e_u8
158            assert c == 0x1e_u8
159
160            c = 0x0_u8
161            c1 = 0x1e_u8
162            c = c | c1
163            assert c == 0x1e_u8
164   
165        def main is shared
166            .lit
167            .unsign
168            .explicit
169            .random
170                   
171                       
172                       
Note: See TracBrowser for help on using the browser.