@@ -75,7 +75,7 @@ def test_immutable(self):
75
75
data .Field (1 , 0 ).offset = 1
76
76
77
77
78
- class StructLayoutTestCase (TestCase ):
78
+ class StructLayoutTestCase (FHDLTestCase ):
79
79
def test_construct (self ):
80
80
sl = data .StructLayout ({
81
81
"a" : unsigned (1 ),
@@ -126,8 +126,21 @@ def test_member_value_wrong(self):
126
126
r"^Struct layout member shape must be a shape-castable object, not 1\.0$" ):
127
127
data .StructLayout ({"a" : 1.0 })
128
128
129
+ def test_format (self ):
130
+ sl = data .StructLayout ({
131
+ "a" : unsigned (1 ),
132
+ "b" : signed (2 ),
133
+ })
134
+ sig = Signal (sl )
135
+ self .assertRepr (sl .format (sig , "" ), """
136
+ (format-struct (sig sig)
137
+ ('a' (format '{}' (slice (sig sig) 0:1)))
138
+ ('b' (format '{}' (s (slice (sig sig) 1:3))))
139
+ )
140
+ """ )
141
+
129
142
130
- class UnionLayoutTestCase (TestCase ):
143
+ class UnionLayoutTestCase (FHDLTestCase ):
131
144
def test_construct (self ):
132
145
ul = data .UnionLayout ({
133
146
"a" : unsigned (1 ),
@@ -184,8 +197,21 @@ def test_const_two_members_wrong(self):
184
197
r"\(specified: a, b\)$" ):
185
198
data .UnionLayout ({"a" : 1 , "b" : 2 }).const (dict (a = 1 , b = 2 ))
186
199
200
+ def test_format (self ):
201
+ ul = data .UnionLayout ({
202
+ "a" : unsigned (1 ),
203
+ "b" : 2
204
+ })
205
+ sig = Signal (ul )
206
+ self .assertRepr (ul .format (sig , "" ), """
207
+ (format-struct (sig sig)
208
+ ('a' (format '{}' (slice (sig sig) 0:1)))
209
+ ('b' (format '{}' (slice (sig sig) 0:2)))
210
+ )
211
+ """ )
187
212
188
- class ArrayLayoutTestCase (TestCase ):
213
+
214
+ class ArrayLayoutTestCase (FHDLTestCase ):
189
215
def test_construct (self ):
190
216
al = data .ArrayLayout (unsigned (2 ), 3 )
191
217
self .assertEqual (al .elem_shape , unsigned (2 ))
@@ -243,6 +269,48 @@ def test_key_wrong_type(self):
243
269
r"^Cannot index array layout with 'a'$" ):
244
270
al ["a" ]
245
271
272
+ def test_format (self ):
273
+ al = data .ArrayLayout (unsigned (2 ), 3 )
274
+ sig = Signal (al )
275
+ self .assertRepr (al .format (sig , "" ), """
276
+ (format-array (sig sig)
277
+ (format '{}' (slice (sig sig) 0:2))
278
+ (format '{}' (slice (sig sig) 2:4))
279
+ (format '{}' (slice (sig sig) 4:6))
280
+ )
281
+ """ )
282
+
283
+ def test_format_signed (self ):
284
+ al = data .ArrayLayout (signed (2 ), 3 )
285
+ sig = Signal (al )
286
+ self .assertRepr (al .format (sig , "" ), """
287
+ (format-array (sig sig)
288
+ (format '{}' (s (slice (sig sig) 0:2)))
289
+ (format '{}' (s (slice (sig sig) 2:4)))
290
+ (format '{}' (s (slice (sig sig) 4:6)))
291
+ )
292
+ """ )
293
+
294
+ def test_format_nested (self ):
295
+ al = data .ArrayLayout (data .ArrayLayout (unsigned (2 ), 2 ), 3 )
296
+ sig = Signal (al )
297
+ self .assertRepr (al .format (sig , "" ), """
298
+ (format-array (sig sig)
299
+ (format-array (slice (sig sig) 0:4)
300
+ (format '{}' (slice (slice (sig sig) 0:4) 0:2))
301
+ (format '{}' (slice (slice (sig sig) 0:4) 2:4))
302
+ )
303
+ (format-array (slice (sig sig) 4:8)
304
+ (format '{}' (slice (slice (sig sig) 4:8) 0:2))
305
+ (format '{}' (slice (slice (sig sig) 4:8) 2:4))
306
+ )
307
+ (format-array (slice (sig sig) 8:12)
308
+ (format '{}' (slice (slice (sig sig) 8:12) 0:2))
309
+ (format '{}' (slice (slice (sig sig) 8:12) 2:4))
310
+ )
311
+ )
312
+ """ )
313
+
246
314
247
315
class FlexibleLayoutTestCase (TestCase ):
248
316
def test_construct (self ):
@@ -1012,6 +1080,18 @@ class S(data.Struct):
1012
1080
self .assertRepr (v .b .q .as_value (), "(slice (slice (sig v) 1:9) 4:8)" )
1013
1081
self .assertRepr (v .b .q .r , "(s (slice (slice (slice (sig v) 1:9) 4:8) 0:2))" )
1014
1082
self .assertRepr (v .b .q .s , "(s (slice (slice (slice (sig v) 1:9) 4:8) 2:4))" )
1083
+ self .assertRepr (S .format (v , "" ), """
1084
+ (format-struct (sig v)
1085
+ ('a' (format '{}' (slice (sig v) 0:1)))
1086
+ ('b' (format-struct (slice (sig v) 1:9)
1087
+ ('p' (format '{}' (slice (slice (sig v) 1:9) 0:4)))
1088
+ ('q' (format-struct (slice (slice (sig v) 1:9) 4:8)
1089
+ ('r' (format '{}' (s (slice (slice (slice (sig v) 1:9) 4:8) 0:2))))
1090
+ ('s' (format '{}' (s (slice (slice (slice (sig v) 1:9) 4:8) 2:4))))
1091
+ ))
1092
+ ))
1093
+ )
1094
+ """ )
1015
1095
1016
1096
def test_construct_init (self ):
1017
1097
class S (data .Struct ):
0 commit comments