qfall_math/utils/factorization/
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 [`Factorization`]
10//! into a [`String`].
11//!
12//! This includes the [`Display`](std::fmt::Display) trait.
13
14use super::Factorization;
15use crate::integer::Z;
16use std::fmt;
17use string_builder::Builder;
18
19impl fmt::Display for Factorization {
20    /// Allows to convert a factorization of type [`Factorization`] into a [`String`].
21    ///
22    /// Returns the factorization in form of a [`String`]. For factorization `2^3 * 5 * 7^2`
23    /// the String looks like this `[(2, 3), (5, 1), (7, 2)]`.
24    ///
25    /// # Examples
26    /// ```
27    /// use qfall_math::utils::Factorization;
28    /// use core::fmt;
29    ///
30    /// let mut fac = Factorization::from((1200, 20));
31    /// fac.refine();
32    ///
33    /// println!("{}", fac);
34    /// ```
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        let mut builder = Builder::default();
37
38        builder.append('[');
39
40        for (num, factor) in Vec::<(Z, u64)>::from(self).iter().enumerate() {
41            builder.append(format!("({}, {})", factor.0, factor.1));
42            if num < Vec::<(Z, u64)>::from(self).len() - 1 {
43                builder.append(", ");
44            }
45        }
46        builder.append(']');
47
48        write!(
49            f,
50            "{}",
51            builder
52                .string()
53                .expect("Vector string contains invalid bytes.")
54        )
55    }
56}
57
58#[cfg(test)]
59mod test_to_string {
60    use crate::utils::Factorization;
61
62    /// Tests whether a large positive integer works in a roundtrip.
63    #[test]
64    fn working_large_positive() {
65        let fac = Factorization::from(u64::MAX);
66
67        assert_eq!(format!("[({}, 1)]", u64::MAX), fac.to_string());
68    }
69
70    /// Tests whether a large negative integer works in a roundtrip.
71    #[test]
72    fn working_large_negative() {
73        let fac = Factorization::from(i64::MIN);
74
75        assert_eq!(format!("[({}, 1)]", i64::MIN), fac.to_string());
76    }
77
78    /// Tests whether a positive integer works in a roundtrip.
79    #[test]
80    fn working_positive() {
81        let fac = Factorization::from(42);
82
83        assert_eq!("[(42, 1)]", fac.to_string());
84    }
85
86    /// Tests whether a negative integer works in a roundtrip.
87    #[test]
88    fn working_negative() {
89        let fac = Factorization::from(-42);
90
91        assert_eq!("[(-42, 1)]", fac.to_string());
92    }
93
94    /// Tests whether to_string works for more than one entry.
95    #[test]
96    fn working_use_result_of_to_string_as_input() {
97        let mut fac = Factorization::from((1200, 20));
98
99        fac.refine();
100
101        assert_eq!("[(3, 1), (20, 3)]", fac.to_string());
102    }
103
104    /// Tests whether to_string works for refined negative values.
105    #[test]
106    fn working_refined_negative() {
107        let mut fac = Factorization::from((-1200, 20));
108
109        fac.refine();
110
111        assert_eq!("[(-1, 1), (3, 1), (20, 3)]", fac.to_string());
112    }
113}