use std::path::PathBuf;
use coremlit::{ComputeUnits, DataType, Features, Model, MultiArray};
#[test]
fn compile_produces_a_loadable_predictable_mlmodelc() {
let source = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/identity.mlmodel");
let compiled = Model::compile(&source).expect("CoreML compiles the identity model");
assert_eq!(
compiled.extension().and_then(|e| e.to_str()),
Some("mlmodelc"),
"compile returns the emitted bundle path, got {}",
compiled.display()
);
assert!(compiled.is_dir(), "compiled model is a bundle directory");
let model = Model::load(&compiled, ComputeUnits::CpuOnly).expect("compiled bundle loads");
let x = MultiArray::from_slice(&[2], &[1.5f32, -2.0]).unwrap();
let outputs = model.predict(&Features::new().with("x", x)).unwrap();
let y = outputs.get("y").expect("identity output present");
assert_eq!(y.shape(), vec![2]);
assert_eq!(y.data_type(), DataType::F32);
let values = y.as_slice::<f32>().unwrap();
assert!(
(values[0] - 1.5).abs() < 1e-6 && (values[1] + 2.0).abs() < 1e-6,
"y == x"
);
std::fs::remove_dir_all(&compiled).expect("temp bundle removable");
}