//! .npz examples. use ndarray::{array, Array2}; use ndarray_npy::{NpzReader, NpzWriter}; use std::{error::Error, io::Cursor}; #[test] fn round_trip_npz() -> Result<(), Box> { let mut buf = Vec::::new(); let arr1 = array![[1i32, 3, 0], [4, 7, -1]]; let arr2 = array![[9i32, 6], [-5, 2], [3, -1]]; { let mut writer = NpzWriter::new(Cursor::new(&mut buf)); writer.add_array("arr1", &arr1)?; writer.add_array("arr2", &arr2)?; writer.finish()?; } { let mut reader = NpzReader::new(Cursor::new(&buf))?; assert!(!reader.is_empty()); assert_eq!(reader.len(), 2); assert_eq!( reader.names()?, vec!["arr1".to_string(), "arr2".to_string()], ); { let by_name: Array2 = reader.by_name("arr1")?; assert_eq!(by_name, arr1); } { let by_name: Array2 = reader.by_name("arr1.npy")?; assert_eq!(by_name, arr1); } { let by_name: Array2 = reader.by_name("arr2")?; assert_eq!(by_name, arr2); } { let by_name: Array2 = reader.by_name("arr2.npy")?; assert_eq!(by_name, arr2); } { let res: Result, _> = reader.by_name("arr1.npy.npy"); assert!(res.is_err()); } } Ok(()) }