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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//! Basic quantifiers such as `forall`, `exists`, `none`, `exactly_one`, and `all_equal`.
//! These functions express simple logical evaluations over a single iterable.
//!
//! Useful in validation, invariant checks, and test assertions.
use crate::;
/// Checks if all elements satisfy the predicate.
///
/// Equivalent to **_∀a ∈ iter: pred(a)_**.
/// When `a` is `∅`, this returns `ok(())` because there is no counterexample.
/// ## Arguments
/// - `iter` - The collection to be checked.
/// - `pred` - The predicate to test each element against.
/// ## Returns
/// - `Ok(())` if all elements satisfy the predicate.
/// - `Err(QuantorError::PredicateFailed { kind, index })` if an element fails the predicate, with the index of the first failure.
/// ## Example
/// ```
/// use quantor::{quantifiers::forall, error::QuantorResultExt};
///
/// let numbers = vec!(0, 2, 4, 6);
/// assert!(forall(&numbers, |x| x % 2 == 0).is_ok());
///
/// let bad = vec!(0, 1, 2, 4, 6);
/// let err = forall(&bad, |x| x % 2 == 0);
///
/// if let Some(index) = err.failing_index() {
/// assert_eq!(1, index);
/// }
/// ```
/// Checks if at least one element satisfies the predicate.
///
/// Equivalent to **_∃a ∈ iter: pred(a)_**.
/// When `a` is `∅`, this returns `QuantorError` because there is no element that can satisfy `pred`.
/// ## Arguments
/// - `iter` - The collection to be checked.
/// - `pred` - The predicate to test each element against.
/// ## Returns
/// - `Ok(())` if any element satisfies the predicate.
/// - `Err(QuantorError::NoMatch { kind })` if no element satisfies the predicate.
/// ## Example
/// ```
/// use quantor::quantifiers::exists;
///
/// let numbers = vec!(0, 1, 3, 5);
/// assert!(exists(&numbers, |x| x % 2 == 0).is_ok());
///
/// let bad = vec!(1, 3, 5, 7);
/// assert!(exists(&bad, |x| x % 2 == 0).is_err());
/// ```
/// Checks if no element satisfies the predicate.
///
/// Equivalent to **_∀a ∈ iter: ¬pred(a)_**.
/// ## Arguments
/// - `iter` - The collection to be checked.
/// - `pred` - The predicate to test each element against.
/// ## Returns
/// - `Ok(())` if no elements satisfy the predicate.
/// - `Err(QuantorError::UnexpectedMatch { kind, index })` if at least one element satisfies the predicate, with the `index`.
///
/// ## Example
/// ```
/// use quantor::{quantifiers::none, error::QuantorResultExt};
///
/// let numbers = vec!(1, 3, 5, 7);
/// assert!(none(&numbers, |x| x % 2 == 0).is_ok());
///
/// let bad = vec!(1, 3, 5, 7, 0);
/// let err = none(&bad, |x| x % 2 == 0);
///
/// if let Some(index) = err.failing_index() {
/// assert_eq!(4, index);
/// }
/// ```
/// Checks if exactly one element satisfies the predicate.
///
/// Equivalent to **_∃!a ∈ iter: pred(a)_**.
/// ## Arguments
/// - `iter` - The collection to be checked.
/// - `pred` - The predicate to test each element against.
/// ## Returns
/// - `Ok(())` if exactly one element satisfies the predicate.
/// - `Err(QuantorError::UnexpectedMatch { kind, index })` when there is more than one element which satisfies the predicate, with the `index` of the second passing element.
/// ## Example
/// ```
/// use quantor::{quantifiers::exactly_one, error::QuantorResultExt};
///
/// let numbers = vec!(0, 1, 3, 5);
/// assert!(exactly_one(&numbers, |x| x % 2 == 0).is_ok());
///
/// let bad = vec!(0, 1, 3, 5, 6);
/// let err = exactly_one(&bad, |x| x % 2 == 0);
///
/// if let Some(index) = err.failing_index() {
/// assert_eq!(4, index);
/// }
/// ```
/// Checks if all elements are equal to each other.
///
/// Equivalent to **_∀a,b ∈ iter: a = b_**.
/// ## Arguments
/// - `iter` - The collection to be checked.
/// ## Returns
/// - `Ok(())` if all elements are equal to each other.
/// - `Err(QuantorError::NotAllEqual { kind, index })` if an element at `index` is not equal to the first element.
/// ## Example
/// ```
/// use quantor::{quantifiers::all_equal, error::QuantorResultExt};
///
/// let empty: Vec<usize> = vec!();
///
/// let ones = vec!(1, 1, 1);
/// let natural = vec!(1, 2, 3);
///
/// assert!(all_equal(&empty).is_ok());
/// assert!(all_equal(&ones).is_ok());
///
/// let err = all_equal(&natural);
///
/// if let Some(index) = err.failing_index() {
/// assert_eq!(1, index);
/// }
/// ```
/// Checks if exactly `n` elements in the iterator satisfy the predicate.
///
/// Equivalent to **_|{x ∈ iter | pred(x)}| = n_**
///
/// ## Arguments
/// - `iter` - The collection to be checked.
/// - `n` - The number of assumed elements to satisfy `pred`.
/// - `pred` - The predicate to test each element against.
/// ## Returns
/// - `Ok(())` if exactly `n` elements match.
/// - `Err(QuantorError::ExactlyNFailed { kind, found, expected })` otherwise.
/// ## Example
/// ```
/// use quantor::quantifiers::exactly_n;
/// use quantor::error::QuantorResultExt;
///
/// let values = vec![1, 2, 4, 6];
/// let result = exactly_n(&values, 3, |x| x % 2 == 0);
///
/// assert!(result.is_ok());
///
/// let err = exactly_n(&values, 2, |x| x % 2 == 0);
///
/// if let Some(expected) = err.match_count() {
/// assert_eq!(expected, 3);
/// }
/// ```