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
//! Module that holds laws for Semigroup implementations
//!
//! # Examples
//!
//! ```
//! # extern crate quickcheck;
//! # extern crate frunk_laws;
//! # extern crate frunk;
//! # use quickcheck::quickcheck;
//! # use frunk::semigroup::*;
//! # fn main() {
//! use frunk_laws::semigroup_laws::*;
//! quickcheck(associativity as fn(Vec<i8>, Vec<i8>, Vec<i8>) -> bool)
//! # }
//! ```

use frunk::semigroup::*;

/// Function for checking adherence to the associativity law
///
///  (x <> y) <> z = x <> (y <> z)
///
/// # Examples
///
/// ```
/// # extern crate quickcheck;
/// # extern crate frunk_laws;
/// # extern crate frunk;
/// # use quickcheck::quickcheck;
/// # use frunk::semigroup::*;
/// # fn main() {
/// use frunk_laws::semigroup_laws::*;
/// quickcheck(associativity as fn(Vec<i8>, Vec<i8>, Vec<i8>) -> bool)
/// # }
/// ```
pub fn associativity<A: Semigroup + Eq>(a: A, b: A, c: A) -> bool {
    a.combine(&b).combine(&c) == a.combine(&b.combine(&c))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::wrapper::*;
    use quickcheck::quickcheck;
    #[cfg(feature = "std")]
    use std::collections::{HashMap, HashSet};

    #[test]
    #[cfg(feature = "std")]
    fn string_prop() {
        quickcheck(associativity as fn(String, String, String) -> bool)
    }

    #[test]
    #[cfg(feature = "std")]
    fn option_prop() {
        quickcheck(associativity as fn(Option<String>, Option<String>, Option<String>) -> bool)
    }

    #[test]
    #[cfg(feature = "std")]
    fn vec_prop() {
        quickcheck(associativity as fn(Vec<i8>, Vec<i8>, Vec<i8>) -> bool)
    }

    #[test]
    #[cfg(feature = "std")]
    fn hashset_prop() {
        quickcheck(associativity as fn(HashSet<i8>, HashSet<i8>, HashSet<i8>) -> bool)
    }

    #[test]
    #[cfg(feature = "std")]
    fn hashmap_prop() {
        quickcheck(
            associativity
                as fn(HashMap<i8, String>, HashMap<i8, String>, HashMap<i8, String>) -> bool,
        )
    }

    #[test]
    fn max_prop() {
        quickcheck(
            associativity as fn(Wrapper<Max<i8>>, Wrapper<Max<i8>>, Wrapper<Max<i8>>) -> bool,
        )
    }

    #[test]
    fn min_prop() {
        quickcheck(
            associativity as fn(Wrapper<Min<i8>>, Wrapper<Min<i8>>, Wrapper<Min<i8>>) -> bool,
        )
    }

    #[test]
    fn any_prop() {
        quickcheck(
            associativity as fn(Wrapper<Any<bool>>, Wrapper<Any<bool>>, Wrapper<Any<bool>>) -> bool,
        )
    }

    #[test]
    fn all_prop() {
        quickcheck(
            associativity as fn(Wrapper<All<bool>>, Wrapper<All<bool>>, Wrapper<All<bool>>) -> bool,
        )
    }
}