File: parity.rs

package info (click to toggle)
rust-malachite-base 0.4.16-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 10,036 kB
  • sloc: makefile: 2
file content (138 lines) | stat: -rw-r--r-- 3,945 bytes parent folder | download
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
// Copyright © 2024 Mikhail Hogrefe
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.

use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_base::num::basic::signeds::PrimitiveSigned;
use malachite_base::num::basic::unsigneds::PrimitiveUnsigned;
use malachite_base::test_util::generators::{signed_gen, unsigned_gen};

fn even_primitive_helper<T: PrimitiveInt>() {
    let test = |n: T, out| {
        assert_eq!(n.even(), out);
    };
    test(T::ZERO, true);
    test(T::ONE, false);
    test(T::TWO, true);
    test(T::exact_from(123), false);
    test(T::exact_from(124), true);
    test(T::MAX, false);
}

fn even_signed_helper<T: PrimitiveSigned>() {
    let test = |n: T, out| {
        assert_eq!(n.even(), out);
    };
    test(T::NEGATIVE_ONE, false);
    test(T::exact_from(-123), false);
    test(T::exact_from(-124), true);
    test(T::MIN, true);
}

#[test]
fn test_even() {
    apply_fn_to_primitive_ints!(even_primitive_helper);
    apply_fn_to_signeds!(even_signed_helper);
}

fn odd_primitive_helper<T: PrimitiveInt>() {
    let test = |n: T, out| {
        assert_eq!(n.odd(), out);
    };
    test(T::ZERO, false);
    test(T::ONE, true);
    test(T::TWO, false);
    test(T::exact_from(123), true);
    test(T::exact_from(124), false);
    test(T::MAX, true);
}

fn odd_signed_helper<T: PrimitiveSigned>() {
    let test = |n: T, out| {
        assert_eq!(n.odd(), out);
    };
    test(T::NEGATIVE_ONE, true);
    test(T::exact_from(-123), true);
    test(T::exact_from(-124), false);
    test(T::MIN, false);
}

#[test]
fn test_odd() {
    apply_fn_to_primitive_ints!(odd_primitive_helper);
    apply_fn_to_signeds!(odd_signed_helper);
}

fn even_properties_helper_unsigned<T: PrimitiveUnsigned>() {
    unsigned_gen::<T>().test_properties(|x| {
        let even = x.even();
        assert_eq!(x.divisible_by(T::TWO), even);
        assert_eq!(!x.odd(), even);
        if x != T::MAX {
            assert_eq!((x + T::ONE).odd(), even);
        }
        if x != T::ZERO {
            assert_eq!((x - T::ONE).odd(), even);
        }
    });
}

fn even_properties_helper_signed<T: PrimitiveSigned>() {
    signed_gen::<T>().test_properties(|x| {
        let even = x.even();
        assert_eq!(x.divisible_by(T::TWO), even);
        assert_eq!(!x.odd(), even);
        if x != T::MAX {
            assert_eq!((x + T::ONE).odd(), even);
        }
        if x != T::MIN {
            assert_eq!((x - T::ONE).odd(), even);
            assert_eq!((-x).even(), even);
        }
    });
}

#[test]
fn even_properties() {
    apply_fn_to_unsigneds!(even_properties_helper_unsigned);
    apply_fn_to_signeds!(even_properties_helper_signed);
}

fn odd_properties_helper_unsigned<T: PrimitiveUnsigned>() {
    unsigned_gen::<T>().test_properties(|x| {
        let odd = x.odd();
        assert_ne!(x.divisible_by(T::TWO), odd);
        assert_eq!(!x.even(), odd);
        if x != T::MAX {
            assert_eq!((x + T::ONE).even(), odd);
        }
        if x != T::ZERO {
            assert_eq!((x - T::ONE).even(), odd);
        }
    });
}

fn odd_properties_helper_signed<T: PrimitiveSigned>() {
    signed_gen::<T>().test_properties(|x| {
        let odd = x.odd();
        assert_ne!(x.divisible_by(T::TWO), odd);
        assert_eq!(!x.even(), odd);
        if x != T::MAX {
            assert_eq!((x + T::ONE).even(), odd);
        }
        if x != T::MIN {
            assert_eq!((x - T::ONE).even(), odd);
            assert_eq!((-x).odd(), odd);
        }
    });
}

#[test]
fn odd_properties() {
    apply_fn_to_unsigneds!(odd_properties_helper_unsigned);
    apply_fn_to_signeds!(odd_properties_helper_signed);
}