qfall_math/integer/poly_over_z/
serialize.rs

1// Copyright © 2023 Marvin Beckmann
2//
3// This file is part of qFALL-math.
4//
5// qFALL-math is free software: you can redistribute it and/or modify it under
6// the terms of the Mozilla Public License Version 2.0 as published by the
7// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.
8
9//! This module contains implementations of functions
10//! important for serialization such as the [`Serialize`] and [`Deserialize`] trait.
11//!
12//! The explicit functions contain the documentation.
13
14use super::PolyOverZ;
15use crate::macros::serialize::{deserialize, serialize};
16use core::fmt;
17use serde::{
18    Deserialize, Serialize,
19    de::{Error, MapAccess, Unexpected, Visitor},
20    ser::SerializeStruct,
21};
22use std::str::FromStr;
23
24serialize!("poly", PolyOverZ);
25deserialize!("poly", Poly, PolyOverZ);
26
27#[cfg(test)]
28mod test_serialize {
29    use crate::integer::PolyOverZ;
30    use std::str::FromStr;
31
32    /// Tests whether the serialization of a positive [`PolyOverZ`] works.
33    #[test]
34    fn serialize_output_positive() {
35        let poly_str = "2  17 42";
36        let poly_z = PolyOverZ::from_str(poly_str).unwrap();
37        let cmp_str = format!("{{\"poly\":\"{poly_str}\"}}");
38
39        assert_eq!(cmp_str, serde_json::to_string(&poly_z).unwrap());
40    }
41
42    /// Tests whether the serialization of a negative [`PolyOverZ`] works.
43    #[test]
44    fn serialize_output_negative() {
45        let poly_str = "3  -17 -42 1";
46        let poly_z = PolyOverZ::from_str(poly_str).unwrap();
47        let cmp_str = format!("{{\"poly\":\"{poly_str}\"}}");
48
49        assert_eq!(cmp_str, serde_json::to_string(&poly_z).unwrap());
50    }
51
52    /// Tests whether the serialization of a positive large [`PolyOverZ`] works.
53    #[test]
54    fn serialize_output_positive_large() {
55        let poly_str = format!("3  -17 {} 1", u64::MAX);
56        let poly_z = PolyOverZ::from_str(&poly_str).unwrap();
57        let cmp_str = format!("{{\"poly\":\"{poly_str}\"}}");
58
59        assert_eq!(cmp_str, serde_json::to_string(&poly_z).unwrap());
60    }
61
62    /// Tests whether the serialization of a negative [`PolyOverZ`] works.
63    #[test]
64    fn serialize_output_negative_large() {
65        let poly_str = format!("3  -17 -{} 1", u64::MAX);
66        let poly_z = PolyOverZ::from_str(&poly_str).unwrap();
67        let cmp_str = format!("{{\"poly\":\"{poly_str}\"}}");
68
69        assert_eq!(cmp_str, serde_json::to_string(&poly_z).unwrap());
70    }
71}
72
73#[cfg(test)]
74mod test_deserialize {
75    use crate::integer::PolyOverZ;
76    use std::str::FromStr;
77
78    /// Tests whether the deserialization of a positive [`PolyOverZ`] works.
79    #[test]
80    fn deserialize_positive() {
81        let poly_str = "2  17 42";
82        let cmp_str = format!("{{\"poly\":\"{poly_str}\"}}");
83
84        let poly_z = PolyOverZ::from_str(poly_str).unwrap();
85        assert_eq!(poly_z, serde_json::from_str::<PolyOverZ>(&cmp_str).unwrap());
86    }
87
88    /// Tests whether the deserialization of a negative [`PolyOverZ`] works.
89    #[test]
90    fn deserialize_negative() {
91        let poly_str = "3  -17 -42 1";
92        let cmp_str = format!("{{\"poly\":\"{poly_str}\"}}");
93
94        let poly_z = PolyOverZ::from_str(poly_str).unwrap();
95        assert_eq!(poly_z, serde_json::from_str::<PolyOverZ>(&cmp_str).unwrap());
96    }
97
98    /// Tests whether the deserialization of a positive large [`PolyOverZ`] works.
99    #[test]
100    fn deserialize_positive_large() {
101        let poly_str = format!("3  -17 {} 1", u64::MAX);
102        let cmp_str = format!("{{\"poly\":\"{poly_str}\"}}");
103
104        let poly_z = PolyOverZ::from_str(&poly_str).unwrap();
105        assert_eq!(poly_z, serde_json::from_str::<PolyOverZ>(&cmp_str).unwrap());
106    }
107
108    /// Tests whether the deserialization of a negative large [`PolyOverZ`] works.
109    #[test]
110    fn deserialize_negative_large() {
111        let poly_str = format!("3  -17 -{} 1", u64::MAX);
112        let cmp_str = format!("{{\"poly\":\"{poly_str}\"}}");
113
114        let poly_z = PolyOverZ::from_str(&poly_str).unwrap();
115        assert_eq!(poly_z, serde_json::from_str::<PolyOverZ>(&cmp_str).unwrap());
116    }
117
118    /// Tests whether no fields 'poly' provided yield an error
119    #[test]
120    fn no_field_poly() {
121        let a: Result<PolyOverZ, serde_json::Error> =
122            serde_json::from_str("{{\"tree\":\"{2  17 42}\"}}");
123        assert!(a.is_err());
124
125        let b: Result<PolyOverZ, serde_json::Error> = serde_json::from_str("{{}}");
126        assert!(b.is_err());
127    }
128
129    /// Tests whether too many fields yield an error
130    #[test]
131    fn too_many_fields() {
132        let a: Result<PolyOverZ, serde_json::Error> =
133            serde_json::from_str("{{\"tree\":\"{2  17 42}\", \"poly\":\"{2  17 42}\"}}");
134        assert!(a.is_err());
135
136        let b: Result<PolyOverZ, serde_json::Error> =
137            serde_json::from_str("{{\"poly\":\"{}\", \"poly\":\"{2  17 42}\"}}");
138        assert!(b.is_err());
139    }
140}