1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright © 2023 Marvin Beckmann
//
// This file is part of qFALL-math.
//
// qFALL-math is free software: you can redistribute it and/or modify it under
// the terms of the Mozilla Public License Version 2.0 as published by the
// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.
//! Implementations to compare [`PolyOverZ`] with other values.
//! This uses the traits from [`std::cmp`].
use crate::{integer::Z, traits::CompareBase};
use flint_sys::fmpz_poly::fmpz_poly_equal;
use super::PolyOverZ;
impl PartialEq for PolyOverZ {
/// Checks if two polynomials over [`Z`](crate::integer::Z) are equal. Used by the `==` and `!=` operators.
///
/// Parameters:
/// - `other`: the other value that is used to compare the elements
///
/// Returns `true` if the elements are equal, otherwise `false`.
///
/// # Examples
/// ```
/// use qfall_math::integer::PolyOverZ;
/// use std::str::FromStr;
/// let a: PolyOverZ = PolyOverZ::from_str("2 42 1").unwrap();
/// let b: PolyOverZ = PolyOverZ::from_str("2 24 1").unwrap();
///
/// // These are all equivalent and return false.
/// let compared: bool = (a == b);
/// # assert!(!compared);
/// let compared: bool = (&a == &b);
/// # assert!(!compared);
/// let compared: bool = (a.eq(&b));
/// # assert!(!compared);
/// let compared: bool = (PolyOverZ::eq(&a, &b));
/// # assert!(!compared);
/// ```
fn eq(&self, other: &Self) -> bool {
unsafe { 1 == fmpz_poly_equal(&self.poly, &other.poly) }
}
}
// With the [`Eq`] trait, `a == a` is always true.
// This is not guaranteed by the [`PartialEq`] trait.
impl Eq for PolyOverZ {}
impl CompareBase<PolyOverZ> for PolyOverZ {}
impl<Integer: Into<Z>> CompareBase<Integer> for PolyOverZ {}
/// Test that the [`PartialEq`] trait is correctly implemented.
#[cfg(test)]
mod test_partial_eq {
// Test case structure:
// 1. Different ways to use equal and not equal.
// 2. Test different combinations of equal and not equal with different
// parameter length combinations.
// Not equal test are inverted equal tests.
use super::PolyOverZ;
use std::str::FromStr;
/// Demonstrate the different ways to use equal.
/// We assume that they behave the same in the other tests.
#[test]
#[allow(clippy::op_ref)]
fn equal_call_methods() {
let one_1 = PolyOverZ::from_str("2 24 1").unwrap();
let one_2 = PolyOverZ::from_str("2 24 1").unwrap();
assert!(one_1 == one_2);
assert!(&one_1 == &one_2);
assert!(one_1.eq(&one_2));
assert!(PolyOverZ::eq(&one_1, &one_2));
assert_eq!(one_1, one_2);
}
/// Demonstrate the different ways to use not equal.
/// We assume that they behave the same in the other tests.
#[test]
#[allow(clippy::op_ref)]
fn not_equal_call_methods() {
let one = PolyOverZ::from_str("2 24 1").unwrap();
let two = PolyOverZ::from_str("3 24 1 1").unwrap();
assert!(one != two);
assert!(&one != &two);
assert!(one.ne(&two));
assert!(PolyOverZ::ne(&one, &two));
assert_ne!(one, two);
}
/// Test equal with small positive and negative constant polynomials.
#[test]
fn equal_small() {
let small_1 = PolyOverZ::from(10);
let small_2 = PolyOverZ::from(10);
let negative = PolyOverZ::from(-1);
assert!(small_1 == small_2);
assert!(small_2 == small_1);
assert!(small_1 == small_1);
assert!(!(small_1 == negative));
assert!(!(negative == small_1));
}
/// Test not equal with small positive and negative constant polynomials.
#[test]
fn not_equal_small() {
let small_1 = PolyOverZ::from(10);
let small_2 = PolyOverZ::from(10);
let negative = PolyOverZ::from(-1);
assert!(!(small_1 != small_2));
assert!(!(small_2 != small_1));
assert!(!(small_1 != small_1));
assert!(small_1 != negative);
assert!(negative != small_1);
}
/// Test equal with a large [`PolyOverZ`]
/// (uses FLINT's pointer representation)
#[test]
fn equal_large() {
let max_1 = PolyOverZ::from(u64::MAX);
let max_2 = PolyOverZ::from(u64::MAX);
let min = PolyOverZ::from(i64::MIN);
assert!(max_1 == max_2);
assert!(max_2 == max_1);
assert!(max_1 == max_1);
assert!(min == min);
assert!(!(max_1 == min));
assert!(!(min == max_1));
}
/// Test not equal with a large [`PolyOverZ`]
/// (uses FLINT's pointer representation)
#[test]
fn not_equal_large() {
let max_1 = PolyOverZ::from(u64::MAX);
let max_2 = PolyOverZ::from(u64::MAX);
let min = PolyOverZ::from(i64::MIN);
assert!(!(max_1 != max_2));
assert!(!(max_2 != max_1));
assert!(!(max_1 != max_1));
assert!(!(min != min));
assert!(max_1 != min);
assert!(min != max_1);
}
/// Test equal with a large [`PolyOverZ`] (uses FLINT's pointer representation)
/// and small [`PolyOverZ`] (no pointer representation).
#[test]
fn equal_large_small() {
let max = PolyOverZ::from(u64::MAX);
let min = PolyOverZ::from(i64::MIN);
let small_positive = PolyOverZ::from(1);
let small_negative = PolyOverZ::from(-1);
assert!(!(max == small_negative));
assert!(!(small_negative == max));
assert!(!(max == small_positive));
assert!(!(small_positive == max));
assert!(!(min == small_negative));
assert!(!(small_negative == min));
assert!(!(min == small_positive));
assert!(!(small_positive == min));
}
/// Test not equal with a large [`PolyOverZ`] (uses FLINT's pointer representation)
/// and small [`PolyOverZ`] (no pointer representation).
#[test]
fn not_equal_large_small() {
let max = PolyOverZ::from(u64::MAX);
let min = PolyOverZ::from(i64::MIN);
let small_positive = PolyOverZ::from(1);
let small_negative = PolyOverZ::from(-1);
assert!(max != small_negative);
assert!(small_negative != max);
assert!(max != small_positive);
assert!(small_positive != max);
assert!(min != small_negative);
assert!(small_negative != min);
assert!(min != small_positive);
assert!(small_positive != min);
}
}
/// Test that the [`CompareBase`] trait uses the default implementation.
#[cfg(test)]
mod test_compare_base {
use crate::{
integer::{PolyOverZ, Z},
traits::CompareBase,
};
use std::str::FromStr;
/// Ensures that the [`CompareBase`] trait uses the default implementation
/// and is available for all types it would be checked against.
#[test]
fn availability() {
let one_1 = PolyOverZ::from_str("3 3 1 -7").unwrap();
assert!(one_1.compare_base(&Z::ONE));
assert!(one_1.compare_base(&PolyOverZ::from(1)));
assert!(one_1.compare_base(&0_i8));
assert!(one_1.compare_base(&0_i16));
assert!(one_1.compare_base(&0_i32));
assert!(one_1.compare_base(&0_i64));
assert!(one_1.compare_base(&0_u8));
assert!(one_1.compare_base(&0_u16));
assert!(one_1.compare_base(&0_u32));
assert!(one_1.compare_base(&0_u64));
assert!(one_1.call_compare_base_error(&PolyOverZ::from(1)).is_none());
assert!(one_1.call_compare_base_error(&Z::ONE).is_none());
assert!(one_1.call_compare_base_error(&0_i8).is_none());
assert!(one_1.call_compare_base_error(&0_i16).is_none());
assert!(one_1.call_compare_base_error(&0_i32).is_none());
assert!(one_1.call_compare_base_error(&0_i64).is_none());
assert!(one_1.call_compare_base_error(&0_u8).is_none());
assert!(one_1.call_compare_base_error(&0_u16).is_none());
assert!(one_1.call_compare_base_error(&0_u32).is_none());
assert!(one_1.call_compare_base_error(&0_u64).is_none());
}
}