@@ -46,12 +46,15 @@ impl CTestTemplate {
4646/// Stores all information necessary for generation of tests for all items.
4747#[ derive( Clone , Debug , Default ) ]
4848pub ( crate ) struct TestTemplate {
49+ pub signededness_tests : Vec < TestSignededness > ,
50+ pub size_align_tests : Vec < TestSizeAlign > ,
4951 pub const_cstr_tests : Vec < TestCStr > ,
5052 pub const_tests : Vec < TestConst > ,
5153 pub test_idents : Vec < BoxStr > ,
5254}
5355
5456impl TestTemplate {
57+ /// Populate all tests for all items depending on the configuration provided.
5558 pub ( crate ) fn new (
5659 ffi_items : & FfiItems ,
5760 generator : & TestGenerator ,
@@ -62,15 +65,20 @@ impl TestTemplate {
6265 translator : Translator :: new ( ) ,
6366 } ;
6467
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) ?;
7072
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 ( ) {
7482 if let syn:: Type :: Ptr ( ptr) = & constant. ty
7583 && let syn:: Type :: Path ( path) = & * ptr. elem
7684 && path. path . segments . last ( ) . unwrap ( ) . ident == "c_char"
@@ -82,29 +90,94 @@ impl TestTemplate {
8290 rust_val : constant. ident ( ) . into ( ) ,
8391 c_val : helper. c_ident ( constant) . into ( ) ,
8492 } ;
85- const_cstr_tests. push ( item)
93+ self . const_cstr_tests . push ( item. clone ( ) ) ;
94+ self . test_idents . push ( item. test_name ) ;
8695 } else {
8796 let item = TestConst {
8897 id : constant. ident ( ) . into ( ) ,
8998 test_name : const_test_ident ( constant. ident ( ) ) ,
90- rust_val : constant. ident . clone ( ) ,
99+ rust_val : constant. ident ( ) . into ( ) ,
91100 rust_ty : constant. ty . to_token_stream ( ) . to_string ( ) . into_boxed_str ( ) ,
92101 c_val : helper. c_ident ( constant) . into ( ) ,
93102 c_ty : helper. c_type ( constant) ?. into ( ) ,
94103 } ;
95- const_tests. push ( item)
104+ self . const_tests . push ( item. clone ( ) ) ;
105+ self . test_idents . push ( item. test_name ) ;
96106 }
97107 }
98108
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+ }
102111
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 ( ( ) )
108181 }
109182}
110183
@@ -119,6 +192,21 @@ impl TestTemplate {
119192 * - `c_ty`: The C type of the constant, qualified with `struct` or `union` if needed.
120193 */
121194
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+
122210/// Information required to test a constant CStr.
123211#[ derive( Clone , Debug ) ]
124212pub ( crate ) struct TestCStr {
@@ -139,16 +227,18 @@ pub(crate) struct TestConst {
139227 pub c_ty : BoxStr ,
140228}
141229
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+
145238fn cstr_test_ident ( ident : & str ) -> BoxStr {
146239 format ! ( "ctest_const_cstr_{ident}" ) . into ( )
147240}
148241
149- /// The Rust name of the const test.
150- ///
151- /// The C name of this test is the same with `__` prepended.
152242fn const_test_ident ( ident : & str ) -> BoxStr {
153243 format ! ( "ctest_const_{ident}" ) . into ( )
154244}
0 commit comments