//! Compression and decompression tests for tensors

#[cfg(test)]
mod compression_tests {
    use approx::assert_abs_diff_eq;
    use ndarray::Array2;
    use taco_format::{PrecisionMode, compress_tensor, decompress_tensor};

    #[test]
    fn test_tensor_compression_lossless() {
        // Create a test tensor
        let mut tensor = Array2::<f32>::zeros((10, 3));
        for i in 0..10 {
            for j in 0..3 {
                tensor[[i, j]] = (i as f32) * 3.0 + (j as f32);
            }
        }

        // Compress with lossless compression
        let compressed =
            compress_tensor(&tensor, PrecisionMode::Full, 3).expect("Failed to compress");

        // Decompress
        let decompressed =
            decompress_tensor(&compressed, PrecisionMode::Full).expect("Failed to decompress");

        // Compare
        assert_eq!(tensor.shape(), decompressed.shape());
        for i in 0..10 {
            for j in 0..3 {
                assert_eq!(tensor[[i, j]], decompressed[[i, j]]);
            }
        }
    }

    #[test]
    fn test_tensor_compression_half() {
        // Create a test tensor
        let mut tensor = Array2::<f32>::zeros((10, 3));
        for i in 0..10 {
            for j in 0..3 {
                tensor[[i, j]] = (i as f32) * 3.0 + (j as f32) + 0.12345;
            }
        }

        // Compress with half precision
        let compressed =
            compress_tensor(&tensor, PrecisionMode::Half, 3).expect("Failed to compress");

        // Decompress
        let decompressed =
            decompress_tensor(&compressed, PrecisionMode::Half).expect("Failed to decompress");

        // Compare - should be close but not exactly equal due to half precision
        assert_eq!(tensor.shape(), decompressed.shape());
        for i in 0..10 {
            for j in 0..3 {
                // Half precision has limited accuracy, so we use a larger epsilon
                assert_abs_diff_eq!(tensor[[i, j]], decompressed[[i, j]], epsilon = 0.01);
            }
        }
    }
}
