qfall_math/integer/z/
cmp.rs1use super::Z;
13use crate::{integer_mod_q::Modulus, macros::for_others::implement_for_others};
14use flint_sys::fmpz::{fmpz, fmpz_cmp, fmpz_equal};
15use std::cmp::Ordering;
16
17impl PartialEq for Z {
18 fn eq(&self, other: &Self) -> bool {
42 unsafe { 1 == fmpz_equal(&self.value, &other.value) }
43 }
44}
45
46impl Eq for Z {}
49
50implement_for_others!(Z, Z, PartialEq for fmpz i8 i16 i32 i64 u8 u16 u32 u64);
51
52impl PartialOrd for Z {
53 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
73 Some(self.cmp(other))
74 }
75}
76
77impl Ord for Z {
78 fn cmp(&self, other: &Self) -> Ordering {
113 unsafe { fmpz_cmp(&self.value, &other.value).cmp(&0) }
114 }
115}
116
117impl PartialOrd<Modulus> for Z {
118 fn partial_cmp(&self, other: &Modulus) -> Option<Ordering> {
140 Some(unsafe { fmpz_cmp(&self.value, &other.modulus.n[0]).cmp(&0) })
141 }
142}
143
144implement_for_others!(Z, Z, PartialOrd for fmpz i8 i16 i32 i64 u8 u16 u32 u64);
145
146#[cfg(test)]
148mod test_partial_eq_z {
149 use super::Z;
156
157 #[test]
159 #[allow(clippy::op_ref)]
160 fn availability() {
161 let z = Z::ONE;
162
163 assert!(z == z);
164 assert!(&z == &z);
165 }
166
167 #[test]
170 #[allow(clippy::op_ref)]
171 fn equal_call_methods() {
172 let one_1 = Z::from(1);
173 let one_2 = Z::from(1);
174
175 assert!(one_1 == one_2);
176 assert!(&one_1 == &one_2);
177 assert!(one_1.eq(&one_2));
178 assert!(Z::eq(&one_1, &one_2));
179 assert_eq!(one_1, one_2);
180 }
181
182 #[test]
185 #[allow(clippy::op_ref)]
186 fn not_equal_call_methods() {
187 let one = Z::from(1);
188 let two = Z::from(2);
189
190 assert!(one != two);
191 assert!(&one != &two);
192 assert!(one.ne(&two));
193 assert!(Z::ne(&one, &two));
194 assert_ne!(one, two);
195 }
196
197 #[test]
199 fn equal_small() {
200 let small_1 = Z::from(10);
201 let small_2 = Z::from(10);
202
203 assert!(small_1 == small_2);
204 assert!(small_2 == small_1);
205 assert!(small_1 == small_1);
206 }
207
208 #[test]
210 fn not_equal_small() {
211 let small_1 = Z::from(10);
212 let negative = Z::from(-1);
213
214 assert!(small_1 != negative);
215 assert!(negative != small_1);
216 }
217
218 #[test]
221 fn equal_large() {
222 let max_1 = Z::from(u64::MAX);
223 let max_2 = Z::from(u64::MAX);
224 let min = Z::from(i64::MIN);
225
226 assert!(max_1 == max_2);
227 assert!(max_2 == max_1);
228 assert!(max_1 == max_1);
229 assert!(min == min);
230 }
231
232 #[test]
235 fn not_equal_large() {
236 let max_1 = Z::from(u64::MAX);
237 let min = Z::from(i64::MIN);
238
239 assert!(max_1 != min);
240 assert!(min != max_1);
241 }
242
243 #[test]
246 fn not_equal_large_small() {
247 let max = Z::from(u64::MAX);
248 let small_positive = Z::from(1);
249 let small_negative = Z::from(-1);
250 let min = Z::from(i64::MIN);
251
252 assert!(max != small_negative);
253 assert!(small_negative != max);
254 assert!(max != small_positive);
255 assert!(small_positive != max);
256
257 assert!(min != small_negative);
258 assert!(small_negative != min);
259 assert!(min != small_positive);
260 assert!(small_positive != min);
261 }
262}
263
264#[cfg(test)]
266mod test_partial_eq_z_other {
267 use super::Z;
268
269 #[test]
271 #[allow(clippy::op_ref)]
272 fn availability() {
273 let z = Z::from(2);
274
275 assert!(z == z.value);
276 assert!(z == 2i8);
277 assert!(z == 2u8);
278 assert!(z == 2i16);
279 assert!(z == 2u16);
280 assert!(z == 2i32);
281 assert!(z == 2u32);
282 assert!(z == 2i64);
283 assert!(z == 2u64);
284
285 assert!(z.value == z);
286 assert!(2i8 == z);
287 assert!(2u8 == z);
288 assert!(2i16 == z);
289 assert!(2u16 == z);
290 assert!(2i32 == z);
291 assert!(2u32 == z);
292 assert!(2i64 == z);
293 assert!(2u64 == z);
294
295 assert!(&z == &2i8);
296 assert!(&2i8 == &z);
297 }
298}
299
300#[cfg(test)]
302#[allow(clippy::neg_cmp_op_on_partial_ord)]
303mod test_partial_ord {
304 use super::Z;
305
306 #[test]
309 fn less_small() {
310 let small_positive_1 = Z::from(1);
311 let small_negative = Z::from(-1);
312
313 assert!(small_negative < small_positive_1);
314 }
315
316 #[test]
319 fn less_large_small() {
320 let max = Z::from(u64::MAX);
321 let small_positive = Z::from(1);
322 let small_negative = Z::from(-1);
323 let max_negative = Z::from(i64::MIN);
324
325 assert!(small_positive < max);
327 assert!(small_negative < max);
328
329 assert!(max_negative < small_positive);
331 assert!(max_negative < small_negative);
332 }
333
334 #[test]
337 fn less_large() {
338 let max_1 = Z::from(u64::MAX);
339 let max_negative = Z::from(i64::MIN);
340
341 assert!(max_negative < max_1);
342 }
343
344 #[test]
347 fn less_equal_small() {
348 let small_positive_1 = Z::from(1);
349 let small_positive_2 = Z::from(1);
350 let small_negative = Z::from(-1);
351
352 assert!(small_positive_1 <= small_positive_2);
353 assert!(small_positive_2 <= small_positive_1);
354 assert!(small_positive_1 <= small_positive_1);
355
356 assert!(small_negative <= small_positive_1);
357 assert!(small_negative <= small_negative);
358 }
359
360 #[test]
363 fn less_equal_large_small() {
364 let max = Z::from(u64::MAX);
365 let small_positive = Z::from(1);
366 let small_negative = Z::from(-1);
367 let max_negative = Z::from(i64::MIN);
368
369 assert!(small_positive <= max);
371 assert!(small_negative <= max);
372
373 assert!(max_negative <= small_positive);
375 assert!(max_negative <= small_negative);
376 }
377
378 #[test]
381 fn less_equal_large() {
382 let max_1 = Z::from(u64::MAX);
383 let max_2 = Z::from(u64::MAX);
384 let max_negative = Z::from(i64::MIN);
385
386 assert!(max_1 <= max_2);
387 assert!(max_2 <= max_1);
388 assert!(max_1 <= max_1);
389
390 assert!(max_negative <= max_1);
391 assert!(max_negative <= max_negative);
392 }
393
394 #[test]
397 fn greater_small() {
398 let small_positive_1 = Z::from(1);
399 let small_negative = Z::from(-1);
400
401 assert!(small_positive_1 > small_negative);
402 }
403
404 #[test]
407 fn greater_large_small() {
408 let max = Z::from(u64::MAX);
409 let small_positive = Z::from(1);
410 let small_negative = Z::from(-1);
411 let max_negative = Z::from(i64::MIN);
412
413 assert!(max > small_positive);
415 assert!(max > small_negative);
416
417 assert!(small_positive > max_negative);
419 assert!(small_negative > max_negative);
420 }
421
422 #[test]
425 fn greater_large() {
426 let max_1 = Z::from(u64::MAX);
427 let max_negative = Z::from(i64::MIN);
428
429 assert!(max_1 > max_negative);
430 }
431
432 #[test]
435 fn greater_equal_small() {
436 let small_positive_1 = Z::from(1);
437 let small_positive_2 = Z::from(1);
438 let small_negative = Z::from(-1);
439
440 assert!(small_positive_1 >= small_positive_2);
441 assert!(small_positive_2 >= small_positive_1);
442 assert!(small_positive_1 >= small_positive_1);
443
444 assert!(small_positive_1 >= small_negative);
445 assert!(small_negative >= small_negative);
446 }
447
448 #[test]
451 fn greater_equal_large_small() {
452 let max = Z::from(u64::MAX);
453 let small_positive = Z::from(1);
454 let small_negative = Z::from(-1);
455 let max_negative = Z::from(i64::MIN);
456
457 assert!(max >= small_positive);
459 assert!(max >= small_negative);
460
461 assert!(small_positive >= max_negative);
463 assert!(small_negative >= max_negative);
464 }
465
466 #[test]
469 fn greater_equal_large() {
470 let max_1 = Z::from(u64::MAX);
471 let max_2 = Z::from(u64::MAX);
472 let max_negative = Z::from(i64::MIN);
473
474 assert!(max_1 >= max_2);
475 assert!(max_2 >= max_1);
476 assert!(max_1 >= max_1);
477
478 assert!(max_1 >= max_negative);
479 assert!(max_negative >= max_negative);
480 }
481}
482
483#[cfg(test)]
485mod test_partial_ord_z_other {
486 use super::Z;
487 use crate::integer_mod_q::Modulus;
488
489 #[test]
491 #[allow(clippy::op_ref)]
492 fn availability() {
493 let z = Z::from(2);
494 let modulus = Modulus::from(2);
495
496 assert!(z <= modulus);
497 assert!(z <= z.value);
498 assert!(z <= 2i8);
499 assert!(z <= 2u8);
500 assert!(z <= 2i16);
501 assert!(z <= 2u16);
502 assert!(z <= 2i32);
503 assert!(z <= 2u32);
504 assert!(z <= 2i64);
505 assert!(z <= 2u64);
506
507 assert!(z.value >= z);
508 assert!(2i8 >= z);
509 assert!(2u8 >= z);
510 assert!(2i16 >= z);
511 assert!(2u16 >= z);
512 assert!(2i32 >= z);
513 assert!(2u32 >= z);
514 assert!(2i64 >= z);
515 assert!(2u64 >= z);
516
517 assert!(&z <= &modulus);
518 assert!(&z <= &2i8);
519 assert!(&2i8 >= &z);
520 }
521}
522
523#[cfg(test)]
524mod test_ord {
525 use super::Z;
526 use std::cmp::{max, min};
527
528 #[test]
533 fn default_implementations_small() {
534 let a: Z = Z::from(10);
535 let b: Z = Z::from(42);
536
537 assert_eq!(b, max(a.clone(), b.clone()));
538 assert_eq!(a, min(a.clone(), b.clone()));
539
540 assert_eq!(a, Z::ZERO.clamp(a.clone(), b.clone()));
541 assert_eq!(a, a.clone().clamp(Z::ZERO, b.clone()));
542 assert_eq!(a, b.clamp(Z::ZERO, a.clone()));
543 }
544
545 #[test]
548 fn default_implementations_large() {
549 let a: Z = Z::from(i64::MAX);
550 let b: Z = Z::from(u64::MAX);
551
552 assert_eq!(b, max(a.clone(), b.clone()));
553 assert_eq!(a, min(a.clone(), b.clone()));
554
555 assert_eq!(a, Z::ZERO.clamp(a.clone(), b.clone()));
556 assert_eq!(a, a.clone().clamp(Z::ZERO, b.clone()));
557 assert_eq!(a, b.clamp(Z::ZERO, a.clone()));
558 }
559}