rustfst/semirings/
macros.rs

1macro_rules! test_semiring_serializable {
2    ($sem_name:  tt, $semiring: ty, $( $weight: expr )* ) => {
3
4        #[cfg(test)]
5        mod $sem_name {
6            use super::*;
7
8            #[test]
9            fn test_serializable_binary() -> Result<()> {
10                for weight in &[ $( $weight ),* ] {
11                    let weight = weight.clone();
12                    let mut serialization = vec![];
13
14                    weight.write_binary(&mut serialization)?;
15
16                    let (_, weight_deserialized) = <$semiring>::parse_binary(serialization.as_slice())
17                        .map_err(|e| format_err!("Can't parse weight : {:?}", e))?;
18
19                    assert_eq!(weight_deserialized, weight);
20                }
21
22                Ok(())
23            }
24
25            #[test]
26            fn test_serializable_text() -> Result<()> {
27                for weight in &[ $( $weight ),* ] {
28                    let weight = weight.clone();
29                    let serialization = format!("{}", weight);
30
31                    let (_, weight_deserialized) = <$semiring>::parse_text(serialization.as_str())
32                        .map_err(|e| format_err!("Can't parse weight : {:?}", e))?;
33
34                    assert_eq!(weight_deserialized, weight);
35                }
36
37                Ok(())
38            }
39        }
40
41    };
42}