use std::io::{self, Cursor}; use std::fs::File; use npyz::WriterBuilder; #[test] fn unicode_files() { // Files created by: // // ```python // np.save( // "test-data/unicode/ok.npy", // np.array(["αβout"], dtype='(path: &str) -> io::Result> { let file = File::open(path).unwrap_or_else(|e| panic!("{}: {}", path, e)); let reader = npyz::NpyFile::new(file).unwrap(); reader.into_vec::() } assert_eq!( read_file::("test-data/unicode/ok.npy").unwrap(), vec!["αβout".to_string()], ); assert!(read_file::("test-data/unicode/surrogate.npy").is_err()); assert!(read_file::("test-data/unicode/surrogate-pair.npy").is_err()); assert_eq!( read_file::>("test-data/unicode/ok.npy").unwrap(), vec!["αβout".chars().collect::>()], ); assert!(read_file::>("test-data/unicode/surrogate.npy").is_err()); assert!(read_file::>("test-data/unicode/surrogate-pair.npy").is_err()); assert_eq!( read_file::>("test-data/unicode/ok.npy").unwrap(), vec!["αβout".chars().map(|x| x as u32).collect::>()], ); assert_eq!( read_file::>("test-data/unicode/surrogate.npy").unwrap(), vec![vec![0xD805]], ); assert_eq!( read_file::>("test-data/unicode/surrogate-pair.npy").unwrap(), vec![vec![0xD834, 0xDD1E]], ); } #[test] fn writing_strings() { let strings = vec![ "abc".to_string(), "αβout".to_string(), "\u{1D11E}".to_string(), ]; let utf32_strings: Vec> = strings.iter().map(|str| str.chars().collect()).collect(); fn check_writing( strings_to_write: &[T], expected_utf32s: &[Vec], ) { let max_len = expected_utf32s.iter().map(|utf32| utf32.len()).max().unwrap(); let dtype = npyz::DType::new_scalar(format!(">().unwrap(); assert_eq!(read_utf32s, expected_utf32s); } check_writing(&utf32_strings, &utf32_strings); check_writing(&strings, &utf32_strings); }