qfall_math/integer/mat_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::MatZ;
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!("matrix", MatZ);
25deserialize!("matrix", Matrix, MatZ);
26
27#[cfg(test)]
28mod test_serialize {
29    use crate::integer::MatZ;
30    use std::str::FromStr;
31
32    /// Tests whether the serialization of a positive [`MatZ`] works.
33    #[test]
34    fn serialize_output_positive() {
35        let mat_poly_str = "[[17, 42],[1, 17]]";
36        let mat_poly_z = MatZ::from_str(mat_poly_str).unwrap();
37        let cmp_str = format!("{{\"matrix\":\"{mat_poly_str}\"}}");
38
39        assert_eq!(cmp_str, serde_json::to_string(&mat_poly_z).unwrap());
40    }
41
42    /// Tests whether the serialization of a negative [`MatZ`] works.
43    #[test]
44    fn serialize_output_negative() {
45        let mat_poly_str = "[[-17, -42, 1],[-13, -5, -42]]";
46        let mat_poly_z = MatZ::from_str(mat_poly_str).unwrap();
47        let cmp_str = format!("{{\"matrix\":\"{mat_poly_str}\"}}");
48
49        assert_eq!(cmp_str, serde_json::to_string(&mat_poly_z).unwrap());
50    }
51
52    /// Tests whether the serialization of a positive large [`MatZ`] works.
53    #[test]
54    fn serialize_output_positive_large() {
55        let mat_poly_str = format!("[[3, -17, {}, 1, 2, -13, 5]]", u64::MAX);
56        let mat_poly_z = MatZ::from_str(&mat_poly_str).unwrap();
57        let cmp_str = format!("{{\"matrix\":\"{mat_poly_str}\"}}");
58
59        assert_eq!(cmp_str, serde_json::to_string(&mat_poly_z).unwrap());
60    }
61
62    /// Tests whether the serialization of a negative large [`MatZ`] works.
63    #[test]
64    fn serialize_output_negative_large() {
65        let mat_poly_str = format!("[[3, -17, -{}, 1, 2, -13, 5]]", u64::MAX);
66        let mat_poly_z = MatZ::from_str(&mat_poly_str).unwrap();
67        let cmp_str = format!("{{\"matrix\":\"{mat_poly_str}\"}}");
68
69        assert_eq!(cmp_str, serde_json::to_string(&mat_poly_z).unwrap());
70    }
71}
72
73#[cfg(test)]
74mod test_deserialize {
75    use crate::integer::MatZ;
76    use std::str::FromStr;
77
78    /// Tests whether the deserialization of a positive [`MatZ`] works.
79    #[test]
80    fn deserialize_positive() {
81        let mat_poly_str = "[[17, 42],[1, 17]]";
82        let cmp_str = format!("{{\"matrix\":\"{mat_poly_str}\"}}");
83
84        let mat_poly_z = MatZ::from_str(mat_poly_str).unwrap();
85        assert_eq!(mat_poly_z, serde_json::from_str::<MatZ>(&cmp_str).unwrap());
86    }
87
88    /// Tests whether the deserialization of a negative [`MatZ`] works.
89    #[test]
90    fn deserialize_negative() {
91        let mat_poly_str = "[[-17, -42, 1],[-13, -5, -42]]";
92        let cmp_str = format!("{{\"matrix\":\"{mat_poly_str}\"}}");
93
94        let mat_poly_z = MatZ::from_str(mat_poly_str).unwrap();
95        assert_eq!(mat_poly_z, serde_json::from_str::<MatZ>(&cmp_str).unwrap());
96    }
97
98    /// Tests whether the deserialization of a positive large [`MatZ`] works.
99    #[test]
100    fn deserialize_positive_large() {
101        let mat_poly_str = format!("[[3, -17, {}, 1, 2, -13, 5]]", u64::MAX);
102        let cmp_str = format!("{{\"matrix\":\"{mat_poly_str}\"}}");
103
104        let mat_poly_z = MatZ::from_str(&mat_poly_str).unwrap();
105        assert_eq!(mat_poly_z, serde_json::from_str::<MatZ>(&cmp_str).unwrap());
106    }
107
108    /// Tests whether the deserialization of a negative large [`MatZ`] works.
109    #[test]
110    fn deserialize_negative_large() {
111        let mat_poly_str = format!("[[3, -17, -{}, 1, 2, -13, 5]]", u64::MAX);
112        let cmp_str = format!("{{\"matrix\":\"{mat_poly_str}\"}}");
113
114        let mat_poly_z = MatZ::from_str(&mat_poly_str).unwrap();
115        assert_eq!(mat_poly_z, serde_json::from_str::<MatZ>(&cmp_str).unwrap());
116    }
117
118    /// Tests whether no fields 'matrix' provided yield an error
119    #[test]
120    fn no_field_matrix() {
121        let a: Result<MatZ, serde_json::Error> =
122            serde_json::from_str("{{\"tree\":\"{[[2, 17, 42]]}\"}}");
123        assert!(a.is_err());
124
125        let b: Result<MatZ, 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<MatZ, serde_json::Error> = serde_json::from_str(
133            "{{\"tree\":\"{[[[2  17 42]]}\", \"matrix\":\"{[[2, 17, 42]]}\"}}",
134        );
135        assert!(a.is_err());
136
137        let b: Result<MatZ, serde_json::Error> =
138            serde_json::from_str("{{\"matrix\":\"{[[1  1]]}\", \"matrix\":\"{[[2, 17, 42]]}\"}}");
139        assert!(b.is_err());
140    }
141}