sane_array/
lib.rs

1//! Read and write SANE-encoded arrays
2//!
3//! This is an implementation of the Simple Array of Numbers Encoding (SANE) specification at:
4//! <https://github.com/considerate/sane>
5pub mod write;
6pub mod read;
7pub mod data;
8
9#[doc(inline)]
10pub use crate::read::{read_sane, read_sane_dyn, read_sane_arrays, read_sane_arrays_dyn, ReadSane};
11#[doc(inline)]
12pub use crate::write::{write_sane, write_sane_io, write_sane_arrays, write_sane_arrays_io, write_sane_arrays_dyn, WriteSane};
13#[doc(inline)]
14pub use crate::data::{SaneData, Sane};
15
16
17#[cfg(test)]
18mod tests {
19    use ndarray::{Ix2, Array, Ix3};
20
21    use crate::data::Sane;
22    use crate::write::{write_sane, write_sane_arrays};
23    use crate::read::{read_sane, read_sane_dyn, ParseError, read_sane_arrays};
24    use crate::{write_sane_arrays_dyn, read_sane_arrays_dyn};
25    extern crate quickcheck;
26    use std::io::Cursor;
27
28
29    #[test]
30    fn roundtrip() {
31        let arr = ndarray::array![[1,2,3], [4,5,6]];
32        let mut file = Cursor::new(Vec::new());
33        write_sane(&mut file, &arr).unwrap();
34        file.set_position(0);
35        let parsed = read_sane_dyn(&mut file).unwrap();
36        match parsed {
37            Sane::ArrayI32(arr2) => assert_eq!(arr.into_dyn(), arr2),
38            _ => assert!(false),
39        }
40    }
41
42    #[test]
43    fn roundtrip_typed() {
44        let arr = ndarray::array![[1,2,3], [4,5,6]];
45        let mut file = Cursor::new(Vec::new());
46        write_sane(&mut file, &arr).unwrap();
47        file.set_position(0);
48        let arr2 = read_sane(&mut file).unwrap();
49        assert_eq!(arr, arr2)
50    }
51
52    #[test]
53    fn roundtrip_typed_wrong_shape() {
54        // This array is rank 2
55        let arr = ndarray::array![[1,2,3], [4,5,6]];
56        let mut file = Cursor::new(Vec::new());
57        write_sane(&mut file, &arr).unwrap();
58        file.set_position(0);
59        // Parsing as rank 3 should fail with a ShapeError
60        let result : Result<Array<i32, Ix3>, _> = read_sane(&mut file);
61        match result {
62            Err(ParseError::ShapeError(_)) => assert!(true),
63            _ => assert!(false),
64        }
65    }
66
67    #[test]
68    fn roundtrip_arrays() {
69        let arr = ndarray::array![[1,2,3], [4,5,6]];
70        let arr2 = ndarray::array![[7,8], [9,10], [11,12]];
71        let mut file = Cursor::new(Vec::new());
72        let arrs = [arr, arr2];
73        write_sane_arrays(&mut file, &arrs).unwrap();
74        file.set_position(0);
75        let parsed: Vec<Array<i32, Ix2>> = read_sane_arrays(&mut file).unwrap();
76        assert_eq!(parsed, arrs);
77    }
78
79    #[test]
80    fn roundtrip_hetrogenous_types() {
81        use Sane::*;
82        let arrs = vec![
83            ArrayI32(ndarray::array![[1,2,3], [4,5,-6]].into_dyn()),
84            ArrayF32(ndarray::array![[1.0,2.0], [-4.0,5.0]].into_dyn()),
85            ArrayF64(ndarray::array![[1.0], [2.0], [3.0], [5.0]].into_dyn()),
86            ArrayU8(ndarray::array![[1], [2], [3], [5], [250]].into_dyn()),
87            ArrayI8(ndarray::array![[1], [-2], [3], [5], [-128]].into_dyn()),
88        ];
89        let mut file = Cursor::new(Vec::new());
90        write_sane_arrays_dyn(&mut file, &arrs).unwrap();
91        file.set_position(0);
92        let parsed: Vec<Sane> = read_sane_arrays_dyn(&mut file).unwrap();
93        assert_eq!(parsed, arrs)
94    }
95}