#[cfg(any(test, feature = "jit"))]
use crate::type_tracking::NativeKind;
#[cfg(any(test, feature = "jit"))]
#[inline]
pub fn marshal_arg_to_jit(bits: u64, kind: NativeKind) -> u64 {
match kind {
NativeKind::Float64
| NativeKind::NullableFloat64
| NativeKind::Int8
| NativeKind::NullableInt8
| NativeKind::UInt8
| NativeKind::NullableUInt8
| NativeKind::Int16
| NativeKind::NullableInt16
| NativeKind::UInt16
| NativeKind::NullableUInt16
| NativeKind::Int32
| NativeKind::NullableInt32
| NativeKind::UInt32
| NativeKind::NullableUInt32
| NativeKind::Int64
| NativeKind::NullableInt64
| NativeKind::UInt64
| NativeKind::NullableUInt64
| NativeKind::IntSize
| NativeKind::NullableIntSize
| NativeKind::UIntSize
| NativeKind::NullableUIntSize
| NativeKind::Bool
| NativeKind::Float32
| NativeKind::Char
| NativeKind::StringV2
| NativeKind::DecimalV2
| NativeKind::String
| NativeKind::Ptr(_)
| NativeKind::Null => bits,
}
}
#[cfg(any(test, feature = "jit"))]
#[inline]
pub fn unmarshal_jit_result(bits: u64, kind: NativeKind) -> u64 {
match kind {
NativeKind::Float64
| NativeKind::NullableFloat64
| NativeKind::Int8
| NativeKind::NullableInt8
| NativeKind::UInt8
| NativeKind::NullableUInt8
| NativeKind::Int16
| NativeKind::NullableInt16
| NativeKind::UInt16
| NativeKind::NullableUInt16
| NativeKind::Int32
| NativeKind::NullableInt32
| NativeKind::UInt32
| NativeKind::NullableUInt32
| NativeKind::Int64
| NativeKind::NullableInt64
| NativeKind::UInt64
| NativeKind::NullableUInt64
| NativeKind::IntSize
| NativeKind::NullableIntSize
| NativeKind::UIntSize
| NativeKind::NullableUIntSize
| NativeKind::Bool
| NativeKind::Float32
| NativeKind::Char
| NativeKind::StringV2
| NativeKind::DecimalV2
| NativeKind::String
| NativeKind::Ptr(_)
| NativeKind::Null => bits,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_marshal_int64_identity() {
let bits = 42u64;
assert_eq!(marshal_arg_to_jit(bits, NativeKind::Int64), bits);
}
#[test]
fn test_marshal_int64_negative_identity() {
let bits = (-17i64) as u64;
assert_eq!(marshal_arg_to_jit(bits, NativeKind::Int64), bits);
}
#[test]
fn test_marshal_uint64_high_bits() {
let bits = (i64::MAX as u64) + 1;
assert_eq!(marshal_arg_to_jit(bits, NativeKind::UInt64), bits);
}
#[test]
fn test_marshal_float64_identity() {
let bits = 3.14f64.to_bits();
assert_eq!(marshal_arg_to_jit(bits, NativeKind::Float64), bits);
}
#[test]
fn test_marshal_bool_true() {
assert_eq!(marshal_arg_to_jit(1, NativeKind::Bool), 1);
}
#[test]
fn test_marshal_bool_false() {
assert_eq!(marshal_arg_to_jit(0, NativeKind::Bool), 0);
}
#[test]
fn test_marshal_string_identity() {
let bits = 0xDEAD_BEEF_CAFE_F00Du64;
assert_eq!(marshal_arg_to_jit(bits, NativeKind::String), bits);
}
#[test]
fn test_unmarshal_int64_result() {
assert_eq!(unmarshal_jit_result(42u64, NativeKind::Int64), 42u64);
}
#[test]
fn test_unmarshal_int64_negative_result() {
let bits = (-17i64) as u64;
assert_eq!(unmarshal_jit_result(bits, NativeKind::Int64), bits);
}
#[test]
fn test_unmarshal_float64_result() {
let bits = 2.718f64.to_bits();
assert_eq!(unmarshal_jit_result(bits, NativeKind::Float64), bits);
}
#[test]
fn test_unmarshal_bool_true_result() {
assert_eq!(unmarshal_jit_result(1, NativeKind::Bool), 1);
}
#[test]
fn test_unmarshal_bool_false_result() {
assert_eq!(unmarshal_jit_result(0, NativeKind::Bool), 0);
}
#[test]
fn test_roundtrip_int64() {
let bits = 42u64;
let marshaled = marshal_arg_to_jit(bits, NativeKind::Int64);
let unmarshaled = unmarshal_jit_result(marshaled, NativeKind::Int64);
assert_eq!(unmarshaled, bits);
}
#[test]
fn test_roundtrip_float64() {
let bits = 3.14159f64.to_bits();
let marshaled = marshal_arg_to_jit(bits, NativeKind::Float64);
let unmarshaled = unmarshal_jit_result(marshaled, NativeKind::Float64);
assert_eq!(unmarshaled, bits);
}
#[test]
fn test_roundtrip_bool_true() {
let marshaled = marshal_arg_to_jit(1, NativeKind::Bool);
let unmarshaled = unmarshal_jit_result(marshaled, NativeKind::Bool);
assert_eq!(unmarshaled, 1);
}
#[test]
fn test_roundtrip_bool_false() {
let marshaled = marshal_arg_to_jit(0, NativeKind::Bool);
let unmarshaled = unmarshal_jit_result(marshaled, NativeKind::Bool);
assert_eq!(unmarshaled, 0);
}
#[test]
fn test_mixed_type_args() {
let kinds = [NativeKind::Int64, NativeKind::Float64, NativeKind::Bool];
let arg_bits = [
10u64, 2.5f64.to_bits(), 1u64, ];
let marshaled: Vec<u64> = arg_bits
.iter()
.zip(kinds.iter())
.map(|(bits, kind)| marshal_arg_to_jit(*bits, *kind))
.collect();
assert_eq!(marshaled[0], 10);
assert_eq!(marshaled[1], 2.5f64.to_bits());
assert_eq!(marshaled[2], 1);
}
}