@@ -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,95 @@ 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+ 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 ( ( ) )
108182 }
109183}
110184
@@ -119,6 +193,21 @@ impl TestTemplate {
119193 * - `c_ty`: The C type of the constant, qualified with `struct` or `union` if needed.
120194 */
121195
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+
122211/// Information required to test a constant CStr.
123212#[ derive( Clone , Debug ) ]
124213pub ( crate ) struct TestCStr {
@@ -139,16 +228,18 @@ pub(crate) struct TestConst {
139228 pub c_ty : BoxStr ,
140229}
141230
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+
145239fn cstr_test_ident ( ident : & str ) -> BoxStr {
146240 format ! ( "ctest_const_cstr_{ident}" ) . into ( )
147241}
148242
149- /// The Rust name of the const test.
150- ///
151- /// The C name of this test is the same with `__` prepended.
152243fn const_test_ident ( ident : & str ) -> BoxStr {
153244 format ! ( "ctest_const_{ident}" ) . into ( )
154245}
0 commit comments