qfall_math/rational/poly_over_q/
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 crate::{
15    macros::serialize::{deserialize, serialize},
16    rational::PolyOverQ,
17};
18use core::fmt;
19use serde::{
20    Deserialize, Serialize,
21    de::{Error, MapAccess, Unexpected, Visitor},
22    ser::SerializeStruct,
23};
24use std::str::FromStr;
25
26serialize!("poly", PolyOverQ);
27deserialize!("poly", Poly, PolyOverQ);
28
29#[cfg(test)]
30mod test_serialize {
31    use crate::rational::PolyOverQ;
32    use std::str::FromStr;
33
34    /// Tests whether the serialization of a positive [`PolyOverQ`] works.
35    #[test]
36    fn serialize_output_positive() {
37        let poly_str = "2  17/3 42/17";
38        let poly_z = PolyOverQ::from_str(poly_str).unwrap();
39        let cmp_str = format!("{{\"poly\":\"{poly_str}\"}}");
40
41        assert_eq!(cmp_str, serde_json::to_string(&poly_z).unwrap());
42    }
43
44    /// Tests whether the serialization of a negative [`PolyOverQ`] works.
45    #[test]
46    fn serialize_output_negative() {
47        let poly_str = "3  -17/3 -42/17 1";
48        let poly_z = PolyOverQ::from_str(poly_str).unwrap();
49        let cmp_str = format!("{{\"poly\":\"{poly_str}\"}}");
50
51        assert_eq!(cmp_str, serde_json::to_string(&poly_z).unwrap());
52    }
53
54    /// Tests whether the serialization of a positive large [`PolyOverQ`] works.
55    #[test]
56    fn serialize_output_positive_large() {
57        let poly_str = format!("3  -17/3 {}/2 1", u64::MAX);
58        let poly_z = PolyOverQ::from_str(&poly_str).unwrap();
59        let cmp_str = format!("{{\"poly\":\"{poly_str}\"}}");
60
61        assert_eq!(cmp_str, serde_json::to_string(&poly_z).unwrap());
62    }
63
64    /// Tests whether the serialization of a negative large [`PolyOverQ`] works.
65    #[test]
66    fn serialize_output_negative_large() {
67        let poly_str = format!("3  -17/3 -{}/2 1", u64::MAX);
68        let poly_z = PolyOverQ::from_str(&poly_str).unwrap();
69        let cmp_str = format!("{{\"poly\":\"{poly_str}\"}}");
70
71        assert_eq!(cmp_str, serde_json::to_string(&poly_z).unwrap());
72    }
73}
74
75#[cfg(test)]
76mod test_deserialize {
77    use crate::rational::PolyOverQ;
78    use std::str::FromStr;
79
80    /// Tests whether the deserialization of a positive [`PolyOverQ`] works.
81    #[test]
82    fn deserialize_positive() {
83        let poly_str = "2  17/3 42/17";
84        let cmp_str = format!("{{\"poly\":\"{poly_str}\"}}");
85
86        let poly_q = PolyOverQ::from_str(poly_str).unwrap();
87        assert_eq!(poly_q, serde_json::from_str::<PolyOverQ>(&cmp_str).unwrap());
88    }
89
90    /// Tests whether the deserialization of a negative [`PolyOverQ`] works.
91    #[test]
92    fn deserialize_negative() {
93        let poly_str = "3  -17/3 -42/17 1";
94        let cmp_str = format!("{{\"poly\":\"{poly_str}\"}}");
95
96        let poly_q = PolyOverQ::from_str(poly_str).unwrap();
97        assert_eq!(poly_q, serde_json::from_str::<PolyOverQ>(&cmp_str).unwrap());
98    }
99
100    /// Tests whether the deserialization of a positive large [`PolyOverQ`] works.
101    #[test]
102    fn deserialize_positive_large() {
103        let poly_str = format!("3  -17/3 {}/2 1", u64::MAX);
104        let cmp_str = format!("{{\"poly\":\"{poly_str}\"}}");
105
106        let poly_q = PolyOverQ::from_str(&poly_str).unwrap();
107        assert_eq!(poly_q, serde_json::from_str::<PolyOverQ>(&cmp_str).unwrap());
108    }
109
110    /// Tests whether the deserialization of a negative large [`PolyOverQ`] works.
111    #[test]
112    fn deserialize_negative_large() {
113        let poly_str = format!("3  -17/3 -{}/2 1", u64::MAX);
114        let cmp_str = format!("{{\"poly\":\"{poly_str}\"}}");
115
116        let poly_q = PolyOverQ::from_str(&poly_str).unwrap();
117        assert_eq!(poly_q, serde_json::from_str::<PolyOverQ>(&cmp_str).unwrap());
118    }
119
120    /// Tests whether no fields 'poly' provided yield an error
121    #[test]
122    fn no_field_value() {
123        let a: Result<PolyOverQ, serde_json::Error> =
124            serde_json::from_str("{{\"tree\":\"{2  17/3 42/17}\"}}");
125        assert!(a.is_err());
126
127        let b: Result<PolyOverQ, serde_json::Error> = serde_json::from_str("{{}}");
128        assert!(b.is_err());
129    }
130
131    /// Tests whether too many fields yield an error
132    #[test]
133    fn too_many_fields() {
134        let a: Result<PolyOverQ, serde_json::Error> =
135            serde_json::from_str("{{\"tree\":\"{2  17/3 42/17}\", \"poly\":\"{2  17/3 42/17}\"}}");
136        assert!(a.is_err());
137
138        let b: Result<PolyOverQ, serde_json::Error> =
139            serde_json::from_str("{{\"poly\":\"{}\", \"poly\":\"{2  17/3 42/17}\"}}");
140        assert!(b.is_err());
141    }
142}