qfall_math/integer/mat_poly_over_z/to_string.rs
1// Copyright © 2023 Marcel Luca Schmidt
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 all options to convert a matrix of type
10//! [`MatPolyOverZ`] into a [`String`].
11//!
12//! This includes the [`Display`](std::fmt::Display) trait.
13
14use super::MatPolyOverZ;
15use crate::{macros::for_others::implement_for_owned, utils::parse::matrix_to_string};
16use core::fmt;
17
18impl From<&MatPolyOverZ> for String {
19 /// Converts a [`MatPolyOverZ`] into its [`String`] representation.
20 ///
21 /// Parameters:
22 /// - `value`: specifies the matrix that will be represented as a [`String`]
23 ///
24 /// Returns a [`String`] of the form `"[[row_0],[row_1],...[row_n]]"`.
25 ///
26 /// # Examples
27 /// ```
28 /// use qfall_math::integer::MatPolyOverZ;
29 /// use std::str::FromStr;
30 /// let matrix = MatPolyOverZ::from_str("[[1 17, 1 5],[2 1 7, 1 2]]").unwrap();
31 ///
32 /// let string: String = matrix.into();
33 /// ```
34 fn from(value: &MatPolyOverZ) -> Self {
35 value.to_string()
36 }
37}
38
39implement_for_owned!(MatPolyOverZ, String, From);
40
41impl fmt::Display for MatPolyOverZ {
42 /// Allows to convert a matrix of type [`MatPolyOverZ`] into a [`String`].
43 ///
44 /// Returns the Matrix in form of a [`String`]. For matrix
45 /// `[[0, 1 42, 2 42 24],[3 17 24 42, 1 17, 1 42]]` the String looks
46 /// like this `[[0, 1 42, 2 42 24],[3 17 24 42, 1 17, 1 42]]`.
47 ///
48 /// # Examples
49 /// ```
50 /// use qfall_math::integer::MatPolyOverZ;
51 /// use core::fmt;
52 /// use std::str::FromStr;
53 ///
54 /// let matrix = MatPolyOverZ::from_str("[[0, 1 42, 2 42 24],[3 17 24 42, 1 17, 1 42]]").unwrap();
55 /// println!("{matrix}");
56 /// ```
57 ///
58 /// ```
59 /// use qfall_math::integer::MatPolyOverZ;
60 /// use core::fmt;
61 /// use std::str::FromStr;
62 ///
63 /// let matrix = MatPolyOverZ::from_str("[[0, 1 42, 2 42 24],[3 17 24 42, 1 17, 1 42]]").unwrap();
64 /// let matrix_string = matrix.to_string();
65 /// ```
66 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67 write!(f, "{}", matrix_to_string(self))
68 }
69}
70
71crate::macros::serialize::matrix_pretty_string!(MatPolyOverZ, PolyOverZ);
72
73#[cfg(test)]
74mod test_to_string {
75 use crate::integer::MatPolyOverZ;
76 use std::str::FromStr;
77
78 /// Tests whether a matrix with a large entry works in a roundtrip
79 #[test]
80 fn working_large_positive() {
81 let cmp = MatPolyOverZ::from_str(&format!(
82 "[[0, 1 {}, 2 42 24],[3 17 24 42, 1 17, 1 42]]",
83 u64::MAX
84 ))
85 .unwrap();
86
87 assert_eq!(
88 format!(
89 "[[0, 1 {}, 2 42 24],[3 17 24 42, 1 17, 1 42]]",
90 u64::MAX
91 ),
92 cmp.to_string()
93 )
94 }
95
96 /// Tests whether a matrix with a large negative entry works in a roundtrip
97 #[test]
98 fn working_large_negative() {
99 let cmp = MatPolyOverZ::from_str(&format!(
100 "[[0, 1 -{}, 2 42 24],[3 17 24 42, 1 17, 1 42]]",
101 u64::MAX
102 ))
103 .unwrap();
104
105 assert_eq!(
106 format!(
107 "[[0, 1 -{}, 2 42 24],[3 17 24 42, 1 17, 1 42]]",
108 u64::MAX
109 ),
110 cmp.to_string()
111 )
112 }
113
114 /// Tests whether a matrix with positive entries works in a roundtrip
115 #[test]
116 fn working_positive() {
117 let cmp =
118 MatPolyOverZ::from_str("[[0, 1 42, 2 42 24],[3 17 24 42, 1 17, 1 42]]").unwrap();
119
120 assert_eq!(
121 "[[0, 1 42, 2 42 24],[3 17 24 42, 1 17, 1 42]]",
122 cmp.to_string()
123 )
124 }
125
126 /// Tests whether a matrix with negative entries works in a roundtrip
127 #[test]
128 fn working_negative() {
129 let cmp =
130 MatPolyOverZ::from_str("[[0, 1 -42, 2 42 24],[3 17 24 42, 1 -17, 1 42]]").unwrap();
131
132 assert_eq!(
133 "[[0, 1 -42, 2 42 24],[3 17 24 42, 1 -17, 1 42]]",
134 cmp.to_string()
135 )
136 }
137
138 /// Tests whether a large matrix works in a roundtrip
139 #[test]
140 fn working_large_dimensions() {
141 let cmp_1 = MatPolyOverZ::from_str(&format!(
142 "[{}[3 17 24 42, 1 -17, 1 42]]",
143 "[0, 1 42, 2 42 24],".repeat(99)
144 ))
145 .unwrap();
146 let cmp_2 = MatPolyOverZ::from_str(&format!("[[{}1 42]]", "1 42, ".repeat(99))).unwrap();
147
148 assert_eq!(
149 format!(
150 "[{}[3 17 24 42, 1 -17, 1 42]]",
151 "[0, 1 42, 2 42 24],".repeat(99)
152 ),
153 cmp_1.to_string()
154 );
155 assert_eq!(
156 format!("[[{}1 42]]", "1 42, ".repeat(99)),
157 cmp_2.to_string()
158 );
159 }
160
161 /// Tests whether a matrix that is created using a string, returns a
162 /// string that can be used to create a [`MatZ`]
163 #[test]
164 fn working_use_result_of_to_string_as_input() {
165 let cmp =
166 MatPolyOverZ::from_str("[[0, 1 -42, 2 42 24],[3 17 24 42, 1 -17, 1 42]]").unwrap();
167
168 let cmp_str_2 = cmp.to_string();
169
170 assert!(MatPolyOverZ::from_str(&cmp_str_2).is_ok());
171 }
172
173 /// Ensures that the `Into<String>` trait works properly
174 #[test]
175 fn into_works_properly() {
176 let cmp = "[[1 17, 1 5],[2 1 7, 1 2]]";
177 let matrix = MatPolyOverZ::from_str(cmp).unwrap();
178
179 let string: String = matrix.clone().into();
180 let borrowed_string: String = (&matrix).into();
181
182 assert_eq!(cmp, string);
183 assert_eq!(cmp, borrowed_string);
184 }
185}