qfall_math/integer/mat_z/
serialize.rs1use 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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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}