@@ -46,12 +46,15 @@ impl CTestTemplate {
46
46
/// Stores all information necessary for generation of tests for all items.
47
47
#[ derive( Clone , Debug , Default ) ]
48
48
pub ( crate ) struct TestTemplate {
49
+ pub signededness_tests : Vec < TestSignededness > ,
50
+ pub size_align_tests : Vec < TestSizeAlign > ,
49
51
pub const_cstr_tests : Vec < TestCStr > ,
50
52
pub const_tests : Vec < TestConst > ,
51
53
pub test_idents : Vec < BoxStr > ,
52
54
}
53
55
54
56
impl TestTemplate {
57
+ /// Populate all tests for all items depending on the configuration provided.
55
58
pub ( crate ) fn new (
56
59
ffi_items : & FfiItems ,
57
60
generator : & TestGenerator ,
@@ -62,15 +65,20 @@ impl TestTemplate {
62
65
translator : Translator :: new ( ) ,
63
66
} ;
64
67
65
- /* Figure out which tests are to be generated. */
66
- // FIXME(ctest): Populate more test information, maybe extract into separate methods.
67
- // The workflow would be to create a struct that stores information for the new test,
68
- // and populating that struct here, so that the also things that have to be added to
69
- // the test templates are the new tests parameterized by that struct.
68
+ let mut template = Self :: default ( ) ;
69
+ template. populate_const_and_cstr_tests ( & helper) ?;
70
+ template. populate_size_align_tests ( & helper) ?;
71
+ template. populate_signededness_tests ( & helper) ?;
70
72
71
- let mut const_tests = vec ! [ ] ;
72
- let mut const_cstr_tests = vec ! [ ] ;
73
- for constant in ffi_items. constants ( ) {
73
+ Ok ( template)
74
+ }
75
+
76
+ /// Populates tests for constants and C-str constants, keeping track of the names of each test.
77
+ fn populate_const_and_cstr_tests (
78
+ & mut self ,
79
+ helper : & TranslateHelper ,
80
+ ) -> Result < ( ) , TranslationError > {
81
+ for constant in helper. ffi_items . constants ( ) {
74
82
if let syn:: Type :: Ptr ( ptr) = & constant. ty
75
83
&& let syn:: Type :: Path ( path) = & * ptr. elem
76
84
&& path. path . segments . last ( ) . unwrap ( ) . ident == "c_char"
@@ -82,29 +90,95 @@ impl TestTemplate {
82
90
rust_val : constant. ident ( ) . into ( ) ,
83
91
c_val : helper. c_ident ( constant) . into ( ) ,
84
92
} ;
85
- const_cstr_tests. push ( item)
93
+ self . const_cstr_tests . push ( item. clone ( ) ) ;
94
+ self . test_idents . push ( item. test_name ) ;
86
95
} else {
87
96
let item = TestConst {
88
97
id : constant. ident ( ) . into ( ) ,
89
98
test_name : const_test_ident ( constant. ident ( ) ) ,
90
- rust_val : constant. ident . clone ( ) ,
99
+ rust_val : constant. ident ( ) . into ( ) ,
91
100
rust_ty : constant. ty . to_token_stream ( ) . to_string ( ) . into_boxed_str ( ) ,
92
101
c_val : helper. c_ident ( constant) . into ( ) ,
93
102
c_ty : helper. c_type ( constant) ?. into ( ) ,
94
103
} ;
95
- const_tests. push ( item)
104
+ self . const_tests . push ( item. clone ( ) ) ;
105
+ self . test_idents . push ( item. test_name ) ;
96
106
}
97
107
}
98
108
99
- let mut test_idents = vec ! [ ] ;
100
- test_idents. extend ( const_cstr_tests. iter ( ) . map ( |test| test. test_name . clone ( ) ) ) ;
101
- test_idents. extend ( const_tests. iter ( ) . map ( |test| test. test_name . clone ( ) ) ) ;
109
+ Ok ( ( ) )
110
+ }
102
111
103
- Ok ( Self {
104
- const_cstr_tests,
105
- const_tests,
106
- test_idents,
107
- } )
112
+ /// Populates size and alignment tests for aliases, structs, and unions.
113
+ ///
114
+ /// It also keeps track of the names of each test.
115
+ fn populate_size_align_tests (
116
+ & mut self ,
117
+ helper : & TranslateHelper ,
118
+ ) -> Result < ( ) , TranslationError > {
119
+ for alias in helper. ffi_items . aliases ( ) {
120
+ let item = TestSizeAlign {
121
+ test_name : size_align_test_ident ( alias. ident ( ) ) ,
122
+ id : alias. ident ( ) . into ( ) ,
123
+ rust_ty : alias. ident ( ) . into ( ) ,
124
+ c_ty : helper. c_type ( alias) ?. into ( ) ,
125
+ } ;
126
+ self . size_align_tests . push ( item. clone ( ) ) ;
127
+ self . test_idents . push ( item. test_name ) ;
128
+ }
129
+ for struct_ in helper. ffi_items . structs ( ) {
130
+ let item = TestSizeAlign {
131
+ test_name : size_align_test_ident ( struct_. ident ( ) ) ,
132
+ id : struct_. ident ( ) . into ( ) ,
133
+ rust_ty : struct_. ident ( ) . into ( ) ,
134
+ c_ty : helper. c_type ( struct_) ?. into ( ) ,
135
+ } ;
136
+ self . size_align_tests . push ( item. clone ( ) ) ;
137
+ self . test_idents . push ( item. test_name ) ;
138
+ }
139
+ for union_ in helper. ffi_items . unions ( ) {
140
+ let item = TestSizeAlign {
141
+ test_name : size_align_test_ident ( union_. ident ( ) ) ,
142
+ id : union_. ident ( ) . into ( ) ,
143
+ rust_ty : union_. ident ( ) . into ( ) ,
144
+ c_ty : helper. c_type ( union_) ?. into ( ) ,
145
+ } ;
146
+ self . size_align_tests . push ( item. clone ( ) ) ;
147
+ self . test_idents . push ( item. test_name ) ;
148
+ }
149
+
150
+ Ok ( ( ) )
151
+ }
152
+
153
+ /// Populates signededness tests for aliases.
154
+ ///
155
+ /// It also keeps track of the names of each test.
156
+ fn populate_signededness_tests (
157
+ & mut self ,
158
+ helper : & TranslateHelper ,
159
+ ) -> Result < ( ) , TranslationError > {
160
+ for alias in helper. ffi_items . aliases ( ) {
161
+ let should_skip_signededness_test = helper
162
+ . generator
163
+ . skip_signededness
164
+ . as_ref ( )
165
+ . is_some_and ( |skip| skip ( alias. ident ( ) ) ) ;
166
+
167
+ if !helper. translator . is_signed ( helper. ffi_items , & alias. ty )
168
+ || should_skip_signededness_test
169
+ {
170
+ continue ;
171
+ }
172
+ let item = TestSignededness {
173
+ test_name : signededness_test_ident ( alias. ident ( ) ) ,
174
+ id : alias. ident ( ) . into ( ) ,
175
+ c_ty : helper. c_type ( alias) ?. into ( ) ,
176
+ } ;
177
+ self . signededness_tests . push ( item. clone ( ) ) ;
178
+ self . test_idents . push ( item. test_name ) ;
179
+ }
180
+
181
+ Ok ( ( ) )
108
182
}
109
183
}
110
184
@@ -119,6 +193,21 @@ impl TestTemplate {
119
193
* - `c_ty`: The C type of the constant, qualified with `struct` or `union` if needed.
120
194
*/
121
195
196
+ #[ derive( Clone , Debug ) ]
197
+ pub ( crate ) struct TestSignededness {
198
+ pub test_name : BoxStr ,
199
+ pub id : BoxStr ,
200
+ pub c_ty : BoxStr ,
201
+ }
202
+
203
+ #[ derive( Clone , Debug ) ]
204
+ pub ( crate ) struct TestSizeAlign {
205
+ pub test_name : BoxStr ,
206
+ pub id : BoxStr ,
207
+ pub rust_ty : BoxStr ,
208
+ pub c_ty : BoxStr ,
209
+ }
210
+
122
211
/// Information required to test a constant CStr.
123
212
#[ derive( Clone , Debug ) ]
124
213
pub ( crate ) struct TestCStr {
@@ -139,16 +228,18 @@ pub(crate) struct TestConst {
139
228
pub c_ty : BoxStr ,
140
229
}
141
230
142
- /// The Rust name of the cstr test.
143
- ///
144
- /// The C name of this same test is the same with `__` prepended.
231
+ fn signededness_test_ident ( ident : & str ) -> BoxStr {
232
+ format ! ( "ctest_signededness_{ident}" ) . into ( )
233
+ }
234
+
235
+ fn size_align_test_ident ( ident : & str ) -> BoxStr {
236
+ format ! ( "ctest_size_align_{ident}" ) . into ( )
237
+ }
238
+
145
239
fn cstr_test_ident ( ident : & str ) -> BoxStr {
146
240
format ! ( "ctest_const_cstr_{ident}" ) . into ( )
147
241
}
148
242
149
- /// The Rust name of the const test.
150
- ///
151
- /// The C name of this test is the same with `__` prepended.
152
243
fn const_test_ident ( ident : & str ) -> BoxStr {
153
244
format ! ( "ctest_const_{ident}" ) . into ( )
154
245
}
0 commit comments