use super::*;
use nsi_ffi_wrap as nsi;
use nsi_trait::Type;
use std::num::NonZeroUsize;
#[test]
fn owns_a_single_f32() {
let arg = nsi::f32!("roughness", 0.3);
let owned = OwnedArgument::from_param(&arg);
assert_eq!(owned.name, "roughness");
assert_eq!(owned.type_tag, Type::F32);
assert_eq!(owned.data, OwnedData::F32(vec![0.3]));
}
#[test]
fn owns_a_point_slice_with_all_floats() {
let points = [[0.0f32, 0.0, 0.0], [1.0, 0.0, 0.0]];
let arg = nsi::point_slice!("P", &points);
let owned = OwnedArgument::from_param(&arg);
assert_eq!(owned.type_tag, Type::Point);
assert_eq!(
owned.data,
OwnedData::F32(vec![0.0, 0.0, 0.0, 1.0, 0.0, 0.0])
);
}
#[test]
fn owns_every_scalar_of_an_array_len_argument() {
use NonZeroUsize;
let resolution = [1280i32, 720];
let arg = nsi::i32_slice!("resolution", &resolution)
.array_len(const { NonZeroUsize::new(2).unwrap() });
let owned = OwnedArgument::from_param(&arg);
assert_eq!(owned.array_length, 2);
assert_eq!(owned.data, OwnedData::I32(vec![1280, 720]));
}
#[test]
fn records_a_reference_as_the_address_not_its_contents() {
let payload = Box::new(0xdead_beef_cafe_f00d_u64);
let expected = &*payload as *const u64 as usize;
let arg = nsi::reference!("outputdriver", &payload);
let owned = OwnedArgument::from_param(&arg);
assert_eq!(owned.type_tag, Type::Reference);
match &owned.data {
OwnedData::Reference(pointers) => {
assert_eq!(pointers.len(), 1);
assert_eq!(
pointers[0].0 as usize, expected,
"recorder must store the payload's address"
);
}
other => panic!("expected Reference, got {other:?}"),
}
}
#[test]
fn owns_a_string() {
let arg = nsi::string!("shaderfilename", "dlPrincipled");
let owned = OwnedArgument::from_param(&arg);
assert_eq!(owned.type_tag, Type::String);
assert_eq!(
owned.data,
OwnedData::String(vec![b"dlPrincipled".to_vec()])
);
}
#[test]
fn an_array_len_run_is_rounded_down_as_the_c_call_does() {
use NonZeroUsize;
let arg = nsi::f32_slice!("x", &[1.0f32, 2.0, 3.0])
.array_len(const { NonZeroUsize::new(2).unwrap() });
let owned = OwnedArgument::from_param(&arg);
assert_eq!(
owned.data,
OwnedData::F32(vec![1.0, 2.0]),
"the renderer reads one element of float[2]; the third is not sent"
);
}
#[test]
fn a_tuple_array_len_run_is_rounded_down_too() {
use NonZeroUsize;
let points = [[0.0f32, 0.0, 0.0], [1.0, 1.0, 1.0], [2.0, 2.0, 2.0]];
let arg = nsi::point_slice!("P", &points)
.array_len(const { NonZeroUsize::new(2).unwrap() });
let owned = OwnedArgument::from_param(&arg);
assert_eq!(
owned.data,
OwnedData::F32(vec![0.0, 0.0, 0.0, 1.0, 1.0, 1.0])
);
}
#[test]
fn recording_keeps_a_non_utf8_byte() {
let arg = nsi::string!("imagefilename", b"caf\xE9.exr".to_vec());
let owned = OwnedArgument::from_param(&arg);
assert_eq!(owned.type_tag, Type::String);
assert_eq!(
owned.data,
OwnedData::String(vec![b"caf\xE9.exr".to_vec()]),
"the byte survives; U+FFFD would be `ef bf bd`",
);
}
#[test]
fn typed_accessors_refuse_the_wrong_layout() {
let fov = OwnedArgument::from_param(&nsi::f32!("fov", 45.0));
assert_eq!(fov.as_f32(), Some(45.0));
assert_eq!(fov.as_f32s(), Some(&[45.0f32][..]));
assert!(fov.as_i32s().is_none());
assert!(fov.as_matrix().is_none());
let c = OwnedArgument::from_param(&nsi::color!("c", &[0.1, 0.2, 0.3]));
assert_eq!(c.as_f32s().map(<[f32]>::len), Some(3));
assert_eq!(c.as_f32(), None, "not the first component of a colour");
#[rustfmt::skip]
let m = [
1.0f64, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
7.0, 0.0, 0.0, 1.0,
];
let matrix = OwnedArgument::from_param(&nsi::matrix_f64!("m", &m));
assert_eq!(matrix.as_matrix().map(|v| v[12]), Some(7.0));
let sixteen = OwnedArgument {
name: "m".to_string(),
type_tag: Type::F64,
array_length: 1,
flags: 0,
data: OwnedData::F64(m.to_vec()),
};
assert_eq!(sixteen.as_matrix(), None);
assert_eq!(sixteen.as_f64s().map(<[f64]>::len), Some(16));
let s = OwnedArgument::from_param(&nsi::string!("f", b"caf\xE9".to_vec()));
assert_eq!(s.as_strings(), Some(&[b"caf\xE9".to_vec()][..]));
assert!(s.as_f32s().is_none());
}