Skip to content

Commit c0b5f23

Browse files
mbyxtgross35
authored andcommitted
ctest: add foreign static test
1 parent 9e26bd8 commit c0b5f23

File tree

11 files changed

+86
-7
lines changed

11 files changed

+86
-7
lines changed

ctest-next/src/ffi_items.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ impl FfiItems {
6363
}
6464

6565
/// Return a list of all foreign statics found mapped by their ABI.
66-
#[cfg_attr(not(test), expect(unused))]
6766
pub(crate) fn foreign_statics(&self) -> &Vec<Static> {
6867
&self.foreign_statics
6968
}

ctest-next/src/template.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ impl CTestTemplate {
5050
/// Stores all information necessary for generation of tests for all items.
5151
#[derive(Clone, Debug, Default)]
5252
pub(crate) struct TestTemplate {
53+
pub foreign_static_tests: Vec<TestForeignStatic>,
5354
pub field_ptr_tests: Vec<TestFieldPtr>,
5455
pub field_size_offset_tests: Vec<TestFieldSizeOffset>,
5556
pub roundtrip_tests: Vec<TestRoundtrip>,
@@ -75,6 +76,7 @@ impl TestTemplate {
7576
template.populate_field_size_offset_tests(&helper)?;
7677
template.populate_field_ptr_tests(&helper)?;
7778
template.populate_roundtrip_tests(&helper)?;
79+
template.populate_foreign_static_tests(&helper)?;
7880

7981
Ok(template)
8082
}
@@ -380,6 +382,28 @@ impl TestTemplate {
380382

381383
Ok(())
382384
}
385+
386+
/// Populates tests for foreign statics, keeping track of the names of each test.
387+
fn populate_foreign_static_tests(
388+
&mut self,
389+
helper: &TranslateHelper,
390+
) -> Result<(), TranslationError> {
391+
for static_ in helper.ffi_items.foreign_statics() {
392+
let rust_ty = static_.ty.to_token_stream().to_string().into_boxed_str();
393+
394+
let item = TestForeignStatic {
395+
test_name: static_test_ident(static_.ident()),
396+
id: static_.ident().into(),
397+
c_val: helper.c_ident(static_).into_boxed_str(),
398+
rust_ty,
399+
};
400+
401+
self.foreign_static_tests.push(item.clone());
402+
self.test_idents.push(item.test_name);
403+
}
404+
405+
Ok(())
406+
}
383407
}
384408

385409
/* Many test structures have the following fields:
@@ -457,6 +481,14 @@ pub(crate) struct TestRoundtrip {
457481
pub is_alias: bool,
458482
}
459483

484+
#[derive(Clone, Debug)]
485+
pub(crate) struct TestForeignStatic {
486+
pub test_name: BoxStr,
487+
pub id: BoxStr,
488+
pub c_val: BoxStr,
489+
pub rust_ty: BoxStr,
490+
}
491+
460492
fn signededness_test_ident(ident: &str) -> BoxStr {
461493
format!("ctest_signededness_{ident}").into()
462494
}
@@ -485,6 +517,10 @@ fn roundtrip_test_ident(ident: &str) -> BoxStr {
485517
format!("ctest_roundtrip_{ident}").into()
486518
}
487519

520+
fn static_test_ident(ident: &str) -> BoxStr {
521+
format!("ctest_static_{ident}").into()
522+
}
523+
488524
/// Wrap methods that depend on both ffi items and the generator.
489525
pub(crate) struct TranslateHelper<'a> {
490526
ffi_items: &'a FfiItems,

ctest-next/templates/test.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,12 @@ ctest_field_ptr__{{ item.id }}__{{ item.field.ident() }}({{ item.c_ty }} *b) {
118118
#ifdef _MSC_VER
119119
# pragma warning(default:4365)
120120
#endif
121+
122+
{%- for static_ in ctx.foreign_static_tests +%}
123+
124+
// Return a pointer to the static variable content.
125+
void *ctest_static__{{ static_.id }}(void) {
126+
// FIXME(ctest): Not correct due to casting the function to a data pointer.
127+
return (void *)&{{ static_.c_val }};
128+
}
129+
{%- endfor +%}

ctest-next/templates/test.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod generated_tests {
1010
#![allow(non_snake_case)]
1111
#![deny(improper_ctypes_definitions)]
1212
#[allow(unused_imports)]
13-
use std::ffi::{CStr, c_int, c_char};
13+
use std::ffi::{CStr, c_int, c_char, c_uint};
1414
use std::fmt::{Debug, LowerHex};
1515
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
1616
#[allow(unused_imports)]
@@ -314,6 +314,21 @@ mod generated_tests {
314314
}
315315
}
316316
{%- endfor +%}
317+
318+
{%- for static_ in ctx.foreign_static_tests +%}
319+
320+
// Tests if the pointer to the static variable matches in both Rust and C.
321+
pub fn {{ static_.test_name }}() {
322+
extern "C" {
323+
fn ctest_static__{{ static_.id }}() -> *const {{ static_.rust_ty }};
324+
}
325+
let actual = (&raw const {{ static_.id }}).addr();
326+
let expected = unsafe {
327+
ctest_static__{{ static_.id }}().addr()
328+
};
329+
check_same(actual, expected, "{{ static_.id }} static");
330+
}
331+
{%- endfor +%}
317332
}
318333

319334
use generated_tests::*;

ctest-next/tests/input/hierarchy.out.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,9 @@ in6_addr ctest_roundtrip__in6_addr(
6464
#ifdef _MSC_VER
6565
# pragma warning(default:4365)
6666
#endif
67+
68+
// Return a pointer to the static variable content.
69+
void *ctest_static__in6addr_any(void) {
70+
// FIXME(ctest): Not correct due to casting the function to a data pointer.
71+
return (void *)&in6addr_any;
72+
}

ctest-next/tests/input/hierarchy.out.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod generated_tests {
77
#![allow(non_snake_case)]
88
#![deny(improper_ctypes_definitions)]
99
#[allow(unused_imports)]
10-
use std::ffi::{CStr, c_int, c_char};
10+
use std::ffi::{CStr, c_int, c_char, c_uint};
1111
use std::fmt::{Debug, LowerHex};
1212
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
1313
#[allow(unused_imports)]
@@ -213,6 +213,18 @@ mod generated_tests {
213213
}
214214
}
215215
}
216+
217+
// Tests if the pointer to the static variable matches in both Rust and C.
218+
pub fn ctest_static_in6addr_any() {
219+
extern "C" {
220+
fn ctest_static__in6addr_any() -> *const in6_addr;
221+
}
222+
let actual = (&raw const in6addr_any).addr();
223+
let expected = unsafe {
224+
ctest_static__in6addr_any().addr()
225+
};
226+
check_same(actual, expected, "in6addr_any static");
227+
}
216228
}
217229

218230
use generated_tests::*;
@@ -236,4 +248,5 @@ fn run_all() {
236248
ctest_size_align_in6_addr();
237249
ctest_signededness_in6_addr();
238250
ctest_roundtrip_in6_addr();
251+
ctest_static_in6addr_any();
239252
}

ctest-next/tests/input/hierarchy/foo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ pub const ON: bool = true;
66
unsafe extern "C" {
77
fn malloc(size: usize) -> *mut c_void;
88

9-
static in6addr_any: in6_addr;
9+
pub static in6addr_any: in6_addr;
1010
}

ctest-next/tests/input/macro.out.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod generated_tests {
77
#![allow(non_snake_case)]
88
#![deny(improper_ctypes_definitions)]
99
#[allow(unused_imports)]
10-
use std::ffi::{CStr, c_int, c_char};
10+
use std::ffi::{CStr, c_int, c_char, c_uint};
1111
use std::fmt::{Debug, LowerHex};
1212
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
1313
#[allow(unused_imports)]

ctest-next/tests/input/simple.out.with-renames.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod generated_tests {
77
#![allow(non_snake_case)]
88
#![deny(improper_ctypes_definitions)]
99
#[allow(unused_imports)]
10-
use std::ffi::{CStr, c_int, c_char};
10+
use std::ffi::{CStr, c_int, c_char, c_uint};
1111
use std::fmt::{Debug, LowerHex};
1212
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
1313
#[allow(unused_imports)]

ctest-next/tests/input/simple.out.with-skips.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod generated_tests {
77
#![allow(non_snake_case)]
88
#![deny(improper_ctypes_definitions)]
99
#[allow(unused_imports)]
10-
use std::ffi::{CStr, c_int, c_char};
10+
use std::ffi::{CStr, c_int, c_char, c_uint};
1111
use std::fmt::{Debug, LowerHex};
1212
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
1313
#[allow(unused_imports)]

0 commit comments

Comments
 (0)