@@ -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,94 @@ 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
+ // && skip_signededness
162
+ let should_skip_signededness_test = helper
163
+ . generator
164
+ . skip_signededness
165
+ . as_ref ( )
166
+ . is_some_and ( |skip| skip ( alias. ident ( ) ) ) ;
167
+ if helper. translator . is_signed ( helper. ffi_items , & alias. ty )
168
+ && !should_skip_signededness_test
169
+ {
170
+ let item = TestSignededness {
171
+ test_name : signededness_test_ident ( alias. ident ( ) ) ,
172
+ id : alias. ident ( ) . into ( ) ,
173
+ c_ty : helper. c_type ( alias) ?. into ( ) ,
174
+ } ;
175
+ self . signededness_tests . push ( item. clone ( ) ) ;
176
+ self . test_idents . push ( item. test_name ) ;
177
+ }
178
+ }
179
+
180
+ Ok ( ( ) )
108
181
}
109
182
}
110
183
@@ -119,6 +192,21 @@ impl TestTemplate {
119
192
* - `c_ty`: The C type of the constant, qualified with `struct` or `union` if needed.
120
193
*/
121
194
195
+ #[ derive( Clone , Debug ) ]
196
+ pub ( crate ) struct TestSignededness {
197
+ pub test_name : BoxStr ,
198
+ pub id : BoxStr ,
199
+ pub c_ty : BoxStr ,
200
+ }
201
+
202
+ #[ derive( Clone , Debug ) ]
203
+ pub ( crate ) struct TestSizeAlign {
204
+ pub test_name : BoxStr ,
205
+ pub id : BoxStr ,
206
+ pub rust_ty : BoxStr ,
207
+ pub c_ty : BoxStr ,
208
+ }
209
+
122
210
/// Information required to test a constant CStr.
123
211
#[ derive( Clone , Debug ) ]
124
212
pub ( crate ) struct TestCStr {
@@ -139,16 +227,18 @@ pub(crate) struct TestConst {
139
227
pub c_ty : BoxStr ,
140
228
}
141
229
142
- /// The Rust name of the cstr test.
143
- ///
144
- /// The C name of this same test is the same with `__` prepended.
230
+ fn signededness_test_ident ( ident : & str ) -> BoxStr {
231
+ format ! ( "ctest_signededness_{ident}" ) . into ( )
232
+ }
233
+
234
+ fn size_align_test_ident ( ident : & str ) -> BoxStr {
235
+ format ! ( "ctest_size_align_{ident}" ) . into ( )
236
+ }
237
+
145
238
fn cstr_test_ident ( ident : & str ) -> BoxStr {
146
239
format ! ( "ctest_const_cstr_{ident}" ) . into ( )
147
240
}
148
241
149
- /// The Rust name of the const test.
150
- ///
151
- /// The C name of this test is the same with `__` prepended.
152
242
fn const_test_ident ( ident : & str ) -> BoxStr {
153
243
format ! ( "ctest_const_{ident}" ) . into ( )
154
244
}
0 commit comments