{#- ↑ Doesn't apply here, this is the template! +#}
{%- let ctx = self.template +%}
{%- let ctest_extern = self.extern_keyword +%}
mod generated_tests {
#![allow(non_snake_case)]
#![allow(improper_ctypes)]
#![deny(improper_ctypes_definitions)]
#[allow(unused_imports)]
use std::ffi::{CStr, c_int, c_char, c_uint};
use std::fmt::{Debug, Write};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
#[allow(unused_imports)]
use std::{mem, ptr, slice};
#[allow(unused_imports)]
use std::mem::{MaybeUninit, offset_of};
use super::*;
pub static FAILED: AtomicBool = AtomicBool::new(false);
pub static NTESTS: AtomicUsize = AtomicUsize::new(0);
fn check_same<T: PartialEq + Debug>(rust: T, c: T, attr: &str) {
if rust != c {
eprintln!("bad {attr}: rust: {rust:?} != c {c:?}");
FAILED.store(true, Ordering::Relaxed);
} else {
NTESTS.fetch_add(1, Ordering::Relaxed);
}
}
fn check_same_bytes(rust: &[u8], c: &[u8], attr: &str) {
if rust == c {
NTESTS.fetch_add(1, Ordering::Relaxed);
return;
}
FAILED.store(true, Ordering::Relaxed);
let mut s = String::new();
if rust.len() == c.len() {
for (i, (&rb, &cb)) in rust.iter().zip(c.iter()).enumerate() {
if rb != cb {
writeln!(
s, "bad {attr} at byte {i}: rust: {rb:?} ({rb:#x}) != c {cb:?} ({cb:#x})"
).unwrap();
break;
}
}
} else {
writeln!(s, "bad {attr}: rust len {} != c len {}", rust.len(), c.len()).unwrap();
}
write!(s, " rust bytes:").unwrap();
for b in rust {
write!(s, " {b:02x}").unwrap();
}
write!(s, "\n c bytes: ").unwrap();
for b in c {
write!(s, " {b:02x}").unwrap();
}
eprintln!("{s}");
}
{%- for const_cstr in ctx.const_cstr_tests +%}
pub fn {{ const_cstr.test_name }}() {
{{ ctest_extern }} "C" {
fn ctest_const_cstr__{{ const_cstr.id }}() -> *const c_char;
}
{# #}
let r_val = unsafe {
let r_ptr: *const c_char = {{ const_cstr.rust_val }};
assert!(!r_ptr.is_null(), "const `{{ const_cstr.rust_val }}` is null");
CStr::from_ptr(r_ptr)
};
{# #}
let c_val = unsafe {
let c_ptr: *const c_char = ctest_const_cstr__{{ const_cstr.id }}();
CStr::from_ptr(c_ptr)
};
check_same(r_val, c_val, "const `{{ const_cstr.rust_val }}` string");
}
{%- endfor +%}
{%- for constant in ctx.const_tests +%}
pub fn {{ constant.test_name }}() {
type T = {{ constant.rust_ty }};
{{ ctest_extern }} "C" {
fn ctest_const__{{ constant.id }}() -> *const T;
}
{#
#}
let r_val: T = {{ constant.rust_val }};
let r_bytes = unsafe {
slice::from_raw_parts(ptr::from_ref(&r_val).cast::<u8>(), size_of::<T>())
};
let c_bytes = unsafe {
let c_ptr: *const T = ctest_const__{{ constant.id }}();
slice::from_raw_parts(c_ptr.cast::<u8>(), size_of::<T>())
};
check_same_bytes(r_bytes, c_bytes, "`{{ constant.rust_val }}` value");
}
{%- endfor +%}
{%- for item in ctx.size_align_tests +%}
pub fn {{ item.test_name }}() {
{{ ctest_extern }} "C" {
fn ctest_size_of__{{ item.id }}() -> u64;
fn ctest_align_of__{{ item.id }}() -> u64;
}
let rust_size = size_of::<{{ item.rust_ty }}>() as u64;
let c_size = unsafe { ctest_size_of__{{ item.id }}() };
let rust_align = align_of::<{{ item.rust_ty }}>() as u64;
let c_align = unsafe { ctest_align_of__{{ item.id }}() };
check_same(rust_size, c_size, "`{{ item.id }}` size");
check_same(rust_align, c_align, "`{{ item.id }}` align");
}
{%- endfor +%}
{%- for alias in ctx.signededness_tests +%}
pub fn {{ alias.test_name }}() {
{{ ctest_extern }} "C" {
fn ctest_signededness_of__{{ alias.id }}() -> u32;
}
let all_ones = !(0 as {{ alias.id }});
let all_zeros = 0 as {{ alias.id }};
let c_is_signed = unsafe { ctest_signededness_of__{{ alias.id }}() };
check_same((all_ones < all_zeros) as u32, c_is_signed, "`{{ alias.id }}` signed");
}
{%- endfor +%}
{%- for item in ctx.field_size_offset_tests +%}
pub fn {{ item.test_name }}() {
{{ ctest_extern }} "C" {
fn ctest_offset_of__{{ item.id }}__{{ item.field.ident() }}() -> u64;
fn ctest_size_of__{{ item.id }}__{{ item.field.ident() }}() -> u64;
}
let uninit_ty = MaybeUninit::<{{ item.id }}>::zeroed();
let uninit_ty = uninit_ty.as_ptr();
{# #}
let ty_ptr = unsafe { &raw const (*uninit_ty).{{ item.field.rust_ident() }} };
{# #}
let val = unsafe { ty_ptr.read_unaligned() };
{# #}
let ctest_field_offset = unsafe { ctest_offset_of__{{ item.id }}__{{ item.field.ident() }}() };
check_same(offset_of!({{ item.id }}, {{ item.field.rust_ident() }}) as u64, ctest_field_offset,
"field offset `{{ item.field.rust_ident() }}` of `{{ item.id }}`");
{# #}
let ctest_field_size = unsafe { ctest_size_of__{{ item.id }}__{{ item.field.ident() }}() };
check_same(size_of_val(&val) as u64, ctest_field_size,
"field size `{{ item.field.rust_ident() }}` of `{{ item.id }}`");
}
{%- endfor +%}
{%- for item in ctx.field_ptr_tests +%}
pub fn {{ item.test_name }}() {
{{ ctest_extern }} "C" {
fn ctest_field_ptr__{{ item.id }}__{{ item.field.ident() }}(a: *const {{ item.id }}) -> *mut u8;
}
let uninit_ty = MaybeUninit::<{{ item.id }}>::zeroed();
let ty_ptr = uninit_ty.as_ptr();
let field_ptr = unsafe { &raw const ((*ty_ptr).{{ item.field.rust_ident() }}) };
let ctest_field_ptr = unsafe { ctest_field_ptr__{{ item.id }}__{{ item.field.ident() }}(ty_ptr) };
check_same(field_ptr.cast(), ctest_field_ptr,
"field pointer access `{{ item.field.rust_ident() }}` of `{{ item.id }}`");
}
{%- endfor +%}
{%- for item in ctx.roundtrip_tests +%}
fn roundtrip_padding__{{ item.id }}() -> Vec<bool> {
if {{ item.fields.len() }} == 0 {
{# #}
return vec![!{{ item.is_alias }}; size_of::<{{ item.id }}>()]
}
{# #}
#[allow(unused_mut)]
let mut v = Vec::<(usize, usize)>::new();
#[allow(unused_variables)]
let bar = MaybeUninit::<{{ item.id }}>::zeroed();
#[allow(unused_variables)]
let bar = bar.as_ptr();
{%- for field in item.fields +%}
let ty_ptr = unsafe { &raw const ((*bar).{{ field.rust_ident() }}) };
let val = unsafe { ty_ptr.read_unaligned() };
let size = size_of_val(&val);
let off = offset_of!({{ item.id }}, {{ field.rust_ident() }});
v.push((off, size));
{%- endfor +%}
{# #}
let mut is_padding_byte = vec![true; size_of::<{{ item.id }}>()];
for (off, size) in &v {
for i in 0..*size {
is_padding_byte[off + i] = false;
}
}
is_padding_byte
}
{# #}
pub fn {{ item.test_name }}() {
type U = {{ item.id }};
{{ ctest_extern }} "C" {
fn ctest_size_of__{{ item.id }}() -> u64;
fn ctest_roundtrip__{{ item.id }}(
input: MaybeUninit<U>, is_padding_byte: *const bool, value_bytes: *mut u8
) -> U;
}
const SIZE: usize = size_of::<U>();
let is_padding_byte = roundtrip_padding__{{ item.id }}();
let mut expected = vec![0u8; SIZE];
let mut input = MaybeUninit::<U>::zeroed();
let input_ptr = input.as_mut_ptr().cast::<u8>();
{# #}
for i in 0..SIZE {
let c: u8 = (i % 256) as u8;
let c = if c == 0 { 42 } else { c };
let d: u8 = 255_u8 - (i % 256) as u8;
let d = if d == 0 { 42 } else { d };
unsafe {
input_ptr.add(i).write_volatile(c);
expected[i] = d;
}
}
let c_size = unsafe { ctest_size_of__{{ item.id }}() } as usize;
if SIZE != c_size {
FAILED.store(true, Ordering::Relaxed);
eprintln!(
"size of `{{ item.c_ty }}` is {c_size} in C and {SIZE} in Rust\n",
);
return;
}
let mut c_value_bytes = vec![0; size_of::<{{ item.id }}>()];
let r: U = unsafe {
ctest_roundtrip__{{ item.id }}(input, is_padding_byte.as_ptr(), c_value_bytes.as_mut_ptr())
};
{# #}
for (i, is_padding_byte) in is_padding_byte.iter().enumerate() {
if *is_padding_byte { continue; }
let rust = unsafe { *input_ptr.add(i) };
let c = c_value_bytes[i];
if rust != c {
eprintln!("rust[{}] = {} != {} (C): Rust `{{ item.id }}` -> C", i, rust, c);
FAILED.store(true, Ordering::Relaxed);
}
}
{# #}
for (i, is_padding_byte) in is_padding_byte.iter().enumerate() {
if *is_padding_byte { continue; }
let rust = expected[i] as usize;
let c = unsafe { (&raw const r).cast::<u8>().add(i).read_volatile() as usize };
if rust != c {
eprintln!(
"rust [{i}] = {rust} != {c} (C): C `{{ item.id }}` -> Rust",
);
FAILED.store(true, Ordering::Relaxed);
}
}
}
{%- endfor +%}
{%- for item in ctx.foreign_fn_tests +%}
pub fn {{ item.test_name }}() {
{{ ctest_extern }} "C" {
fn ctest_foreign_fn__{{ item.id }}() -> unsafe extern "C" fn();
}
let actual = unsafe { ctest_foreign_fn__{{ item.id }}() } as u64;
let expected = {{ item.id }} as *const () as u64;
check_same(actual, expected, "`{{ item.id }}` function pointer");
}
{%- endfor +%}
{%- for static_ in ctx.foreign_static_tests +%}
pub fn {{ static_.test_name }}() {
{{ ctest_extern }} "C" {
fn ctest_static__{{ static_.id }}() -> *const {{ static_.rust_ty }};
}
let actual = (&raw const {{ static_.id }}).addr();
let expected = unsafe {
ctest_static__{{ static_.id }}().addr()
};
check_same(actual, expected, "`{{ static_.id }}` static");
}
{%- endfor +%}
}
use generated_tests::*;
fn main() {
println!("RUNNING ALL TESTS");
run_all();
if FAILED.load(std::sync::atomic::Ordering::Relaxed) {
panic!("some tests failed");
} else {
println!(
"PASSED {} tests",
NTESTS.load(std::sync::atomic::Ordering::Relaxed)
);
}
}
fn run_all() {
{%- for test in ctx.test_idents +%}
{{ test }}();
{%- endfor +%}
}