qubit-function 0.17.0

Functional programming traits and Box/Rc/Arc adapters for Rust, inspired by Java functional interfaces
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// =============================================================================
//    Copyright (c) 2025 - 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================

#[cfg(test)]
mod tests {
    use qubit_function::predicates::{
        ArcBiPredicate,
        BiPredicate,
        BoxBiPredicate,
        RcBiPredicate,
    };
    use std::thread;

    // ========================================================================
    // BiPredicate Trait Tests - Test closure and function pointer
    // implementations
    // ========================================================================

    mod bi_predicate_ext_tests {
        use super::BiPredicate;
    }

    // ========================================================================
    // BoxBiPredicate Tests
    // ========================================================================

    mod generic_constraint_tests {
        use super::{
            ArcBiPredicate,
            BiPredicate,
            BoxBiPredicate,
            RcBiPredicate,
            thread,
        };

        fn filter_pairs<P>(
            pairs: Vec<(i32, i32)>,
            predicate: &P,
        ) -> Vec<(i32, i32)>
        where
            P: BiPredicate<i32, i32>,
        {
            pairs
                .into_iter()
                .filter(|(x, y)| predicate.test(x, y))
                .collect()
        }

        #[test]
        fn test_generic_function_accepts_closure() {
            let pairs = vec![(1, 2), (-1, 3), (5, -6)];
            let result = filter_pairs(pairs, &|x: &i32, y: &i32| x + y > 0);
            assert_eq!(result, vec![(1, 2), (-1, 3)]);
        }

        #[test]
        fn test_generic_function_accepts_box_bi_predicate() {
            let pairs = vec![(1, 2), (-1, 3), (5, -6)];
            let pred = BoxBiPredicate::new(|x: &i32, y: &i32| x + y > 0);
            let result = filter_pairs(pairs, &pred);
            assert_eq!(result, vec![(1, 2), (-1, 3)]);
        }

        #[test]
        fn test_generic_function_accepts_arc_bi_predicate() {
            let pairs = vec![(1, 2), (-1, 3), (5, -6)];
            let pred = ArcBiPredicate::new(|x: &i32, y: &i32| x + y > 0);
            let result = filter_pairs(pairs, &pred);
            assert_eq!(result, vec![(1, 2), (-1, 3)]);
        }

        #[test]
        fn test_generic_function_accepts_rc_bi_predicate() {
            let pairs = vec![(1, 2), (-1, 3), (5, -6)];
            let pred = RcBiPredicate::new(|x: &i32, y: &i32| x + y > 0);
            let result = filter_pairs(pairs, &pred);
            assert_eq!(result, vec![(1, 2), (-1, 3)]);
        }

        #[test]
        fn test_generic_function_accepts_function_pointer() {
            fn sum_positive(x: &i32, y: &i32) -> bool {
                x + y > 0
            }

            let pairs = vec![(1, 2), (-1, 3), (5, -6)];
            let result = filter_pairs(pairs, &sum_positive);
            assert_eq!(result, vec![(1, 2), (-1, 3)]);
        }

        #[test]
        fn test_generic_count_with_different_bi_predicate_types() {
            fn count_matching<P>(pairs: &[(i32, i32)], pred: &P) -> usize
            where
                P: BiPredicate<i32, i32>,
            {
                pairs.iter().filter(|(x, y)| pred.test(x, y)).count()
            }

            let pairs = [(1, 2), (-1, 3), (5, -6), (3, 4)];

            let box_pred = BoxBiPredicate::new(|x: &i32, y: &i32| x + y > 0);
            assert_eq!(count_matching(&pairs, &box_pred), 3);

            let arc_pred = ArcBiPredicate::new(|x: &i32, y: &i32| x + y > 0);
            assert_eq!(count_matching(&pairs, &arc_pred), 3);

            let rc_pred = RcBiPredicate::new(|x: &i32, y: &i32| x + y > 0);
            assert_eq!(count_matching(&pairs, &rc_pred), 3);
        }

        #[test]
        fn test_generic_with_combined_bi_predicates() {
            let x_positive = ArcBiPredicate::new(|x: &i32, _y: &i32| *x > 0);
            let y_positive = ArcBiPredicate::new(|_x: &i32, y: &i32| *y > 0);
            let combined = x_positive.and(y_positive);

            let pairs = [(1, 2), (-1, 3), (5, -6), (3, 4)];
            let result = filter_pairs(pairs.to_vec(), &combined);
            assert_eq!(result, vec![(1, 2), (3, 4)]);
        }

        #[test]
        fn test_generic_with_string_bi_predicates() {
            fn filter_string_pairs<P>(
                pairs: Vec<(String, usize)>,
                predicate: &P,
            ) -> Vec<(String, usize)>
            where
                P: BiPredicate<String, usize>,
            {
                pairs
                    .into_iter()
                    .filter(|(s, len)| predicate.test(s, len))
                    .collect()
            }

            let pairs = vec![
                (String::from("hello"), 3),
                (String::from("hi"), 5),
                (String::from("world"), 4),
            ];

            let pred =
                BoxBiPredicate::new(|s: &String, len: &usize| s.len() > *len);
            let result = filter_string_pairs(pairs, &pred);
            assert_eq!(result.len(), 2);
        }

        #[test]
        fn test_bi_predicate_as_struct_field() {
            struct Validator<P> {
                predicate: P,
            }

            impl<P> Validator<P>
            where
                P: BiPredicate<i32, i32>,
            {
                fn validate(&self, x: i32, y: i32) -> bool {
                    self.predicate.test(&x, &y)
                }
            }

            let validator = Validator {
                predicate: BoxBiPredicate::new(|x: &i32, y: &i32| x + y > 0),
            };

            assert!(validator.validate(5, 3));
            assert!(!validator.validate(-5, -3));
        }

        #[test]
        fn test_returning_bi_predicate_from_function() {
            fn create_sum_checker(
                threshold: i32,
            ) -> impl BiPredicate<i32, i32> {
                move |x: &i32, y: &i32| x + y > threshold
            }

            let checker = create_sum_checker(10);
            assert!(checker.test(&6, &5));
            assert!(!checker.test(&3, &4));
        }

        #[test]
        fn test_thread_safety_with_arc_bi_predicate() {
            fn process_in_thread<P>(pred: P, x: i32, y: i32) -> bool
            where
                P: BiPredicate<i32, i32> + Send + 'static,
            {
                thread::spawn(move || pred.test(&x, &y))
                    .join()
                    .expect("thread should not panic")
            }

            let pred = ArcBiPredicate::new(|x: &i32, y: &i32| x + y > 0);
            assert!(process_in_thread(pred, 5, 3));
        }

        #[test]
        fn test_mixed_bi_predicate_types_in_sequence() {
            let pairs = [(1, 2), (-1, 3), (5, -6), (3, 4)];

            // Use different types in sequence
            let box_pred = BoxBiPredicate::new(|x: &i32, y: &i32| x + y > 0);
            let count1 =
                pairs.iter().filter(|(x, y)| box_pred.test(x, y)).count();

            let arc_pred = ArcBiPredicate::new(|x: &i32, _y: &i32| *x > 0);
            let count2 =
                pairs.iter().filter(|(x, y)| arc_pred.test(x, y)).count();

            assert_eq!(count1, 3);
            assert_eq!(count2, 3);
        }

        #[test]
        fn test_generic_with_custom_types() {
            #[derive(Debug, Clone, PartialEq)]
            struct Point {
                x: i32,
                y: i32,
            }

            fn filter_points<P>(
                points: Vec<(Point, Point)>,
                pred: &P,
            ) -> Vec<(Point, Point)>
            where
                P: BiPredicate<Point, Point>,
            {
                points
                    .into_iter()
                    .filter(|(p1, p2)| pred.test(p1, p2))
                    .collect()
            }

            let points = vec![
                (Point { x: 1, y: 2 }, Point { x: 3, y: 4 }),
                (Point { x: -1, y: 2 }, Point { x: 1, y: -4 }),
            ];

            let pred =
                BoxBiPredicate::new(|p1: &Point, p2: &Point| p1.x + p2.x > 0);
            let result = filter_points(points, &pred);
            assert_eq!(result.len(), 1);
        }
    }

    // ========================================================================
    // Default Implementation Tests - Test that custom types can use
    // default implementations of into_xxx methods
    // ========================================================================

    mod default_implementation_tests {
        use super::BiPredicate;

        // Custom bi-predicate type that only implements the core
        // test method and relies on default implementations for
        // all conversion methods
        #[derive(Clone)]
        struct CustomBiPredicate<T, U>
        where
            T: 'static,
            U: 'static,
        {
            threshold: i32,
            _phantom: std::marker::PhantomData<(T, U)>,
        }

        impl CustomBiPredicate<i32, i32> {
            fn new(threshold: i32) -> Self {
                Self {
                    threshold,
                    _phantom: std::marker::PhantomData,
                }
            }
        }

        // Only implement the core test method - all into_xxx and to_xxx
        // methods will use default implementations
        impl BiPredicate<i32, i32> for CustomBiPredicate<i32, i32> {
            fn test(&self, first: &i32, second: &i32) -> bool {
                first + second > self.threshold
            }

            // All other methods (into_box, into_rc, into_arc, into_fn,
            // to_box, to_rc, to_arc, to_fn) use default implementations
            // automatically
        }

        #[test]
        fn test_custom_type_basic_test() {
            let pred = CustomBiPredicate::new(10);
            assert!(pred.test(&6, &5));
            assert!(pred.test(&10, &1));
            assert!(!pred.test(&5, &5));
            assert!(!pred.test(&3, &4));
        }

        #[test]
        fn test_custom_type_can_be_used_in_generic_context() {
            fn accepts_predicate<P>(pred: &P, x: i32, y: i32) -> bool
            where
                P: BiPredicate<i32, i32>,
            {
                pred.test(&x, &y)
            }

            let pred = CustomBiPredicate::new(10);
            assert!(accepts_predicate(&pred, 6, 5));
            assert!(!accepts_predicate(&pred, 3, 4));
        }

        // ========================================================================
        // Test default to_xxx implementations
        // ========================================================================
    }

    // ========================================================================
    // Edge Case Tests
    // ========================================================================

    mod edge_case_tests {
        use super::{
            BiPredicate,
            BoxBiPredicate,
        };

        #[test]
        fn test_with_zero() {
            let sum_positive =
                BoxBiPredicate::new(|x: &i32, y: &i32| x + y > 0);
            assert!(!sum_positive.test(&0, &0));
            assert!(sum_positive.test(&1, &0));
            assert!(sum_positive.test(&0, &1));
        }

        #[test]
        fn test_always_true() {
            let always_true = BoxBiPredicate::new(|_x: &i32, _y: &i32| true);
            assert!(always_true.test(&5, &3));
            assert!(always_true.test(&-5, &-3));
            assert!(always_true.test(&0, &0));
        }

        #[test]
        fn test_always_false() {
            let always_false = BoxBiPredicate::new(|_x: &i32, _y: &i32| false);
            assert!(!always_false.test(&5, &3));
            assert!(!always_false.test(&-5, &-3));
            assert!(!always_false.test(&0, &0));
        }

        #[test]
        fn test_double_negation() {
            let sum_positive =
                BoxBiPredicate::new(|x: &i32, y: &i32| x + y > 0);
            let not_not = !(!sum_positive);
            assert!(not_not.test(&5, &3));
            assert!(!not_not.test(&-5, &-3));
        }

        #[test]
        fn test_with_empty_string() {
            let is_empty = BoxBiPredicate::new(|s1: &String, s2: &String| {
                s1.is_empty() && s2.is_empty()
            });
            assert!(is_empty.test(&String::new(), &String::new()));
            assert!(!is_empty.test(&String::from("a"), &String::new()));
        }

        #[test]
        fn test_with_large_numbers() {
            let sum_overflow_safe = BoxBiPredicate::new(|x: &i64, y: &i64| {
                x.checked_add(*y).is_some()
            });
            let max_minus_one = i64::MAX - 1;
            assert!(sum_overflow_safe.test(&max_minus_one, &1));
            assert!(!sum_overflow_safe.test(&i64::MAX, &1));
        }

        #[test]
        fn test_with_floating_point() {
            let close_enough =
                BoxBiPredicate::new(|x: &f64, y: &f64| (*x - *y).abs() < 0.01);
            assert!(close_enough.test(&1.0, &1.005));
            assert!(!close_enough.test(&1.0, &1.02));
        }

        #[test]
        fn test_complex_chain() {
            let p1 = BoxBiPredicate::new(|x: &i32, _y: &i32| *x > 0);
            let p2 = BoxBiPredicate::new(|_x: &i32, y: &i32| *y > 0);
            let p3 = BoxBiPredicate::new(|x: &i32, y: &i32| x + y > 10);

            let complex = p1.and(p2).or(p3);
            assert!(complex.test(&5, &3)); // Both positive
            assert!(complex.test(&50, &-30)); // Sum > 10 (50 + (-30) = 20)
            assert!(!complex.test(&-5, &3)); // Not both positive, sum not > 10
        }
    }

    // ========================================================================
    // Mixed Type Combination Tests
    // ========================================================================

    mod mixed_type_combination_tests {
        use super::{
            ArcBiPredicate,
            BiPredicate,
            BoxBiPredicate,
            RcBiPredicate,
        };

        #[test]
        fn test_box_with_closure() {
            let box_pred = BoxBiPredicate::new(|x: &i32, y: &i32| x + y > 0);
            let combined = box_pred.and(|x: &i32, _y: &i32| *x > 0);
            assert!(combined.test(&5, &3));
            assert!(!combined.test(&-5, &10));
        }

        #[test]
        fn test_arc_preserves_original() {
            let arc1 = ArcBiPredicate::new(|x: &i32, y: &i32| x + y > 0);
            let arc2 = ArcBiPredicate::new(|x: &i32, _y: &i32| *x > 0);

            let _combined = arc1.clone().and(arc2.clone());

            // Originals still usable
            assert!(arc1.test(&-5, &10));
            assert!(arc2.test(&5, &-10));
        }

        #[test]
        fn test_rc_preserves_original() {
            let rc1 = RcBiPredicate::new(|x: &i32, y: &i32| x + y > 0);
            let rc2 = RcBiPredicate::new(|x: &i32, _y: &i32| *x > 0);

            let _combined = rc1.clone().and(rc2.clone());

            // Originals still usable
            assert!(rc1.test(&-5, &10));
            assert!(rc2.test(&5, &-10));
        }
    }
}