#[cfg(test)]
#[repr(C)]
#[derive(Debug)]
pub(crate) struct ClipsAbiLayout {
magic: usize,
self_size: usize,
size_of_long: usize,
size_of_pointer: usize,
size_of_size_t: usize,
type_header_size: usize,
type_header_align: usize,
type_header_type: usize,
clips_value_size: usize,
clips_value_align: usize,
lexeme_size: usize,
lexeme_align: usize,
lexeme_header: usize,
lexeme_next: usize,
lexeme_count: usize,
lexeme_contents: usize,
float_size: usize,
float_align: usize,
float_header: usize,
float_next: usize,
float_count: usize,
float_contents: usize,
integer_size: usize,
integer_align: usize,
integer_header: usize,
integer_next: usize,
integer_count: usize,
integer_contents: usize,
multifield_size: usize,
multifield_align: usize,
multifield_header: usize,
multifield_busy_count: usize,
multifield_length: usize,
multifield_next: usize,
multifield_contents: usize,
udf_value_size: usize,
udf_value_align: usize,
udf_value_supplemental_info: usize,
udf_value_value: usize,
udf_value_begin: usize,
udf_value_range: usize,
udf_value_next: usize,
udf_context_size: usize,
udf_context_align: usize,
udf_context_environment: usize,
udf_context_context: usize,
udf_context_the_function: usize,
udf_context_last_position: usize,
udf_context_last_arg: usize,
udf_context_return_value: usize,
string_builder_size: usize,
string_builder_align: usize,
string_builder_environment: usize,
string_builder_contents: usize,
string_builder_buffer_reset: usize,
string_builder_length: usize,
string_builder_buffer_maximum: usize,
sentinel: usize,
}
#[cfg(test)]
unsafe extern "C" {
#[link_name = "ClipsAbiLayoutTable"]
pub(crate) static CLIPS_ABI_LAYOUT: ClipsAbiLayout;
}
#[cfg(test)]
mod tests {
use std::ffi::{c_long, c_void};
use std::mem::{align_of, offset_of, size_of};
use super::{CLIPS_ABI_LAYOUT, ClipsAbiLayout};
use crate::ffi;
const MAGIC: usize = 0x4341_4249;
fn layout() -> &'static ClipsAbiLayout {
unsafe { &CLIPS_ABI_LAYOUT }
}
#[test]
fn the_layout_table_is_populated_and_the_same_length_on_both_sides() {
let c = layout();
assert_eq!(c.magic, MAGIC, "ABI layout table is missing or mislinked");
assert_eq!(c.sentinel, MAGIC, "ABI layout table is truncated");
assert_eq!(
c.self_size,
size_of::<ClipsAbiLayout>(),
"the Rust mirror of struct clipsAbiLayout has a different field count"
);
}
#[test]
fn rust_and_c_agree_on_the_primitive_widths() {
let c = layout();
assert_eq!(size_of::<c_long>(), c.size_of_long);
assert_eq!(size_of::<*const c_void>(), c.size_of_pointer);
assert_eq!(size_of::<usize>(), c.size_of_size_t);
}
#[test]
fn type_header_matches_the_c_layout() {
let c = layout();
assert_eq!(size_of::<ffi::TypeHeader>(), c.type_header_size);
assert_eq!(align_of::<ffi::TypeHeader>(), c.type_header_align);
assert_eq!(offset_of!(ffi::TypeHeader, type_), c.type_header_type);
}
#[test]
fn clips_value_matches_the_c_layout() {
let c = layout();
assert_eq!(size_of::<ffi::CLIPSValue>(), c.clips_value_size);
assert_eq!(align_of::<ffi::CLIPSValue>(), c.clips_value_align);
}
#[test]
fn atom_prefixes_match_the_c_layout() {
let c = layout();
assert_eq!(size_of::<ffi::CLIPSLexeme>(), c.lexeme_size);
assert_eq!(align_of::<ffi::CLIPSLexeme>(), c.lexeme_align);
assert_eq!(offset_of!(ffi::CLIPSLexeme, header), c.lexeme_header);
assert_eq!(offset_of!(ffi::CLIPSLexeme, next), c.lexeme_next);
assert_eq!(offset_of!(ffi::CLIPSLexeme, count), c.lexeme_count);
assert_eq!(offset_of!(ffi::CLIPSLexeme, contents), c.lexeme_contents);
assert_eq!(size_of::<ffi::CLIPSFloat>(), c.float_size);
assert_eq!(align_of::<ffi::CLIPSFloat>(), c.float_align);
assert_eq!(offset_of!(ffi::CLIPSFloat, header), c.float_header);
assert_eq!(offset_of!(ffi::CLIPSFloat, next), c.float_next);
assert_eq!(offset_of!(ffi::CLIPSFloat, count), c.float_count);
assert_eq!(offset_of!(ffi::CLIPSFloat, contents), c.float_contents);
assert_eq!(size_of::<ffi::CLIPSInteger>(), c.integer_size);
assert_eq!(align_of::<ffi::CLIPSInteger>(), c.integer_align);
assert_eq!(offset_of!(ffi::CLIPSInteger, header), c.integer_header);
assert_eq!(offset_of!(ffi::CLIPSInteger, next), c.integer_next);
assert_eq!(offset_of!(ffi::CLIPSInteger, count), c.integer_count);
assert_eq!(offset_of!(ffi::CLIPSInteger, contents), c.integer_contents);
}
#[test]
fn multifield_matches_the_c_layout() {
let c = layout();
assert_eq!(size_of::<ffi::Multifield>(), c.multifield_size);
assert_eq!(align_of::<ffi::Multifield>(), c.multifield_align);
assert_eq!(offset_of!(ffi::Multifield, header), c.multifield_header);
assert_eq!(
offset_of!(ffi::Multifield, busy_count),
c.multifield_busy_count
);
assert_eq!(offset_of!(ffi::Multifield, length), c.multifield_length);
assert_eq!(offset_of!(ffi::Multifield, next), c.multifield_next);
assert_eq!(offset_of!(ffi::Multifield, contents), c.multifield_contents);
}
#[test]
fn udf_value_matches_the_c_layout() {
let c = layout();
assert_eq!(size_of::<ffi::UDFValue>(), c.udf_value_size);
assert_eq!(align_of::<ffi::UDFValue>(), c.udf_value_align);
assert_eq!(
offset_of!(ffi::UDFValue, supplemental_info),
c.udf_value_supplemental_info
);
assert_eq!(offset_of!(ffi::UDFValue, value), c.udf_value_value);
assert_eq!(offset_of!(ffi::UDFValue, begin), c.udf_value_begin);
assert_eq!(offset_of!(ffi::UDFValue, range), c.udf_value_range);
assert_eq!(offset_of!(ffi::UDFValue, next), c.udf_value_next);
}
#[test]
fn udf_context_matches_the_c_layout() {
let c = layout();
assert_eq!(size_of::<ffi::UDFContext>(), c.udf_context_size);
assert_eq!(align_of::<ffi::UDFContext>(), c.udf_context_align);
assert_eq!(
offset_of!(ffi::UDFContext, environment),
c.udf_context_environment
);
assert_eq!(offset_of!(ffi::UDFContext, context), c.udf_context_context);
assert_eq!(
offset_of!(ffi::UDFContext, the_function),
c.udf_context_the_function
);
assert_eq!(
offset_of!(ffi::UDFContext, last_position),
c.udf_context_last_position
);
assert_eq!(
offset_of!(ffi::UDFContext, last_arg),
c.udf_context_last_arg
);
assert_eq!(
offset_of!(ffi::UDFContext, return_value),
c.udf_context_return_value
);
}
#[test]
fn string_builder_matches_the_c_layout() {
let c = layout();
assert_eq!(size_of::<ffi::StringBuilder>(), c.string_builder_size);
assert_eq!(align_of::<ffi::StringBuilder>(), c.string_builder_align);
assert_eq!(
offset_of!(ffi::StringBuilder, environment),
c.string_builder_environment
);
assert_eq!(
offset_of!(ffi::StringBuilder, contents),
c.string_builder_contents
);
assert_eq!(
offset_of!(ffi::StringBuilder, buffer_reset),
c.string_builder_buffer_reset
);
assert_eq!(
offset_of!(ffi::StringBuilder, length),
c.string_builder_length
);
assert_eq!(
offset_of!(ffi::StringBuilder, buffer_maximum),
c.string_builder_buffer_maximum
);
}
}