Skip to main content

micromap/set/
eq.rs

1// SPDX-FileCopyrightText: Copyright (c) 2023-2026 Yegor Bugayenko
2// SPDX-FileCopyrightText: Copyright (c) 2025 owtotwo
3// SPDX-License-Identifier: MIT
4
5use super::Set;
6
7impl<T: PartialEq, const N: usize, const M: usize> PartialEq<Set<T, M>> for Set<T, N> {
8    /// Two sets can be compared. (The capacity does not affect comparison.)
9    ///
10    /// # Examples
11    /// ```
12    /// let mut m1: micromap::Set<_, 5> = micromap::Set::new();
13    /// let mut m2: micromap::Set<_, 10> = micromap::Set::new();
14    /// m1.insert(1);
15    /// m2.insert(1);
16    /// assert_eq!(m1, m2);
17    /// // two sets with different order of key-value pairs are still equal:
18    /// m1.insert(2);
19    /// m1.insert(3);
20    /// m2.insert(3);
21    /// m2.insert(2);
22    /// assert_eq!(m1, m2);
23    /// ```
24    #[inline]
25    fn eq(&self, other: &Set<T, M>) -> bool {
26        self.map.eq(&other.map)
27    }
28}
29
30impl<T: Eq, const N: usize> Eq for Set<T, N> {}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn compares_two_sets() {
38        let mut s1: Set<i32, 1> = Set::new();
39        s1.insert(1);
40        let mut s2: Set<i32, 1> = Set::new();
41        s2.insert(1);
42        assert_eq!(s1, s2);
43    }
44
45    #[test]
46    fn compares_sets_with_different_order() {
47        let mut s1: Set<i32, 3> = Set::new();
48        s1.insert(1);
49        s1.insert(2);
50        s1.insert(3);
51        let mut s2: Set<i32, 3> = Set::new();
52        s2.insert(3);
53        s2.insert(2);
54        s2.insert(1);
55        assert_eq!(s1, s2);
56    }
57
58    #[test]
59    fn compares_sets_with_different_lengths() {
60        let mut s1: Set<i32, 3> = Set::new();
61        s1.insert(1);
62        s1.insert(2);
63        let mut s2: Set<i32, 3> = Set::new();
64        s2.insert(1);
65        assert_ne!(s1, s2);
66    }
67
68    #[test]
69    fn compares_sets_with_different_elements() {
70        let mut s1: Set<i32, 1> = Set::new();
71        s1.insert(1);
72        let mut s2: Set<i32, 1> = Set::new();
73        s2.insert(2);
74        assert_ne!(s1, s2);
75    }
76
77    #[test]
78    fn compares_sets_with_char_elements() {
79        let mut s1: Set<char, 2> = Set::new();
80        s1.insert('a');
81        s1.insert('b');
82        let mut s2: Set<char, 3> = Set::new();
83        s2.insert('a');
84        s2.insert('b');
85        assert_eq!(s1, s2);
86    }
87}