qubit-function 0.8.3

Common functional programming type aliases for Rust, providing Java-style functional interfaces
Documentation
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/*******************************************************************************
 *
 *    Copyright (c) 2025 - 2026.
 *    Haixing Hu, Qubit Co. Ltd.
 *
 *    All rights reserved.
 *
 ******************************************************************************/

//! Tests for BiConsumerOnce types

use qubit_function::{
    BiConsumerOnce,
    BoxBiConsumerOnce,
    FnBiConsumerOnceOps,
};
use std::sync::{
    Arc,
    Mutex,
};

#[test]
fn test_bi_consumer_once_default_conversions_allow_relaxed_generic_types() {
    #[derive(Debug)]
    struct BorrowedRc<'a> {
        value: &'a str,
    }

    #[derive(Clone, Debug)]
    struct BorrowedRcBiConsumerOnce;

    impl<'a> BiConsumerOnce<BorrowedRc<'a>, BorrowedRc<'a>> for BorrowedRcBiConsumerOnce {
        fn accept(self, first: &BorrowedRc<'a>, second: &BorrowedRc<'a>) {
            assert_eq!(first.value, "left");
            assert_eq!(second.value, "right");
        }
    }

    let left = String::from("left");
    let right = String::from("right");
    let first = BorrowedRc {
        value: left.as_str(),
    };
    let second = BorrowedRc {
        value: right.as_str(),
    };
    let consumer = BorrowedRcBiConsumerOnce;

    consumer.clone().into_box().accept(&first, &second);
    consumer.clone().into_fn()(&first, &second);

    consumer.to_box().accept(&first, &second);
    consumer.to_fn()(&first, &second);
}

#[cfg(test)]
mod box_bi_consumer_once_tests {
    use super::*;

    #[test]
    fn test_new_and_accept() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let l = log.clone();
        let consumer = BoxBiConsumerOnce::new(move |x: &i32, y: &i32| {
            l.lock().unwrap().push(*x + *y);
        });
        consumer.accept(&5, &3);
        assert_eq!(*log.lock().unwrap(), vec![8]);
    }

    #[test]
    fn test_and_then() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let l1 = log.clone();
        let l2 = log.clone();
        let chained = BoxBiConsumerOnce::new(move |x: &i32, y: &i32| {
            l1.lock().unwrap().push(*x + *y);
        })
        .and_then(move |x: &i32, y: &i32| {
            l2.lock().unwrap().push(*x * *y);
        });

        chained.accept(&5, &3);
        assert_eq!(*log.lock().unwrap(), vec![8, 15]);
    }

    #[test]
    fn test_noop() {
        let noop = BoxBiConsumerOnce::<i32, i32>::noop();
        noop.accept(&42, &10);
        // Should not panic
    }

    #[test]
    fn test_when_true() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let l = log.clone();
        let consumer = BoxBiConsumerOnce::new(move |x: &i32, y: &i32| {
            l.lock().unwrap().push(*x + *y);
        });
        let conditional = consumer.when(|x: &i32, y: &i32| *x > 0 && *y > 0);

        conditional.accept(&5, &3);
        assert_eq!(*log.lock().unwrap(), vec![8]);
    }

    #[test]
    fn test_when_false() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let l = log.clone();
        let consumer = BoxBiConsumerOnce::new(move |x: &i32, y: &i32| {
            l.lock().unwrap().push(*x + *y);
        });
        let conditional = consumer.when(|x: &i32, y: &i32| *x > 0 && *y > 0);

        conditional.accept(&-5, &3);
        assert_eq!(*log.lock().unwrap(), vec![]);
    }

    #[test]
    fn test_when_or_else() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let l1 = log.clone();
        let l2 = log.clone();
        let consumer = BoxBiConsumerOnce::new(move |x: &i32, _y: &i32| {
            l1.lock().unwrap().push(*x);
        });
        let conditional =
            consumer
                .when(|x: &i32, y: &i32| *x > *y)
                .or_else(move |_x: &i32, y: &i32| {
                    l2.lock().unwrap().push(*y);
                });

        conditional.accept(&5, &3);
        assert_eq!(*log.lock().unwrap(), vec![5]);
    }

    #[test]
    fn test_when_or_else_false_branch() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let l1 = log.clone();
        let l2 = log.clone();
        let consumer = BoxBiConsumerOnce::new(move |x: &i32, _y: &i32| {
            l1.lock().unwrap().push(*x);
        });
        let conditional =
            consumer
                .when(|x: &i32, y: &i32| *x > *y)
                .or_else(move |_x: &i32, y: &i32| {
                    l2.lock().unwrap().push(*y);
                });

        // Condition is false (3 is not > 5), so else branch should execute
        conditional.accept(&3, &5);
        assert_eq!(*log.lock().unwrap(), vec![5]);
    }

    #[test]
    fn test_into_box() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let l = log.clone();
        let closure = move |x: &i32, y: &i32| {
            l.lock().unwrap().push(*x + *y);
        };
        let box_consumer = closure.into_box();
        box_consumer.accept(&5, &3);
        assert_eq!(*log.lock().unwrap(), vec![8]);
    }

    #[test]
    fn test_into_fn() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let l = log.clone();
        let consumer = BoxBiConsumerOnce::new(move |x: &i32, y: &i32| {
            l.lock().unwrap().push(*x + *y);
        });
        let func = consumer.into_fn();
        func(&5, &3);
        assert_eq!(*log.lock().unwrap(), vec![8]);
    }

    #[test]
    fn test_name() {
        let mut consumer = BoxBiConsumerOnce::<i32, i32>::noop();
        assert_eq!(consumer.name(), None);

        consumer.set_name("test_consumer");
        assert_eq!(consumer.name(), Some("test_consumer"));
    }

    #[test]
    fn test_moved_value() {
        let data = [1, 2, 3];
        let consumer = BoxBiConsumerOnce::new(move |_x: &i32, _y: &i32| {
            // data is moved into the closure
            println!("Data length: {}", data.len());
        });
        consumer.accept(&5, &3);
        // data is no longer available here
    }

    #[test]
    fn test_new_with_name() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let l = log.clone();
        let consumer =
            BoxBiConsumerOnce::new_with_name("test_consumer", move |x: &i32, y: &i32| {
                l.lock().unwrap().push(*x + *y);
            });
        assert_eq!(consumer.name(), Some("test_consumer"));
        consumer.accept(&5, &3);
        assert_eq!(*log.lock().unwrap(), vec![8]);
    }

    #[test]
    fn test_conditional_into_box() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let l = log.clone();
        let consumer = BoxBiConsumerOnce::new(move |x: &i32, y: &i32| {
            l.lock().unwrap().push(*x + *y);
        });
        let conditional = consumer.when(|x: &i32, y: &i32| *x > 0 && *y > 0);
        let boxed = conditional.into_box();
        boxed.accept(&5, &3);
        assert_eq!(*log.lock().unwrap(), vec![8]);
    }

    #[test]
    fn test_conditional_into_fn() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let l = log.clone();
        let consumer = BoxBiConsumerOnce::new(move |x: &i32, y: &i32| {
            l.lock().unwrap().push(*x + *y);
        });
        let conditional = consumer.when(|x: &i32, y: &i32| *x > 0 && *y > 0);
        let func = conditional.into_fn();
        func(&5, &3);
        assert_eq!(*log.lock().unwrap(), vec![8]);
    }

    #[test]
    fn test_conditional_into_box_false_branch() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let l = log.clone();
        let consumer = BoxBiConsumerOnce::new(move |x: &i32, y: &i32| {
            l.lock().unwrap().push(*x + *y);
        });
        let conditional = consumer.when(|x: &i32, y: &i32| *x > 0 && *y > 0);
        let boxed = conditional.into_box();
        // Test false branch: should not execute when condition is not met
        boxed.accept(&-5, &3);
        assert_eq!(*log.lock().unwrap(), vec![]);
    }

    #[test]
    fn test_conditional_into_fn_false_branch() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let l = log.clone();
        let consumer = BoxBiConsumerOnce::new(move |x: &i32, y: &i32| {
            l.lock().unwrap().push(*x + *y);
        });
        let conditional = consumer.when(|x: &i32, y: &i32| *x > 0 && *y > 0);
        let func = conditional.into_fn();
        // Test false branch: should not execute when condition is not met
        func(&-5, &3);
        assert_eq!(*log.lock().unwrap(), vec![]);
    }

    #[test]
    fn test_conditional_and_then() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let l1 = log.clone();
        let l2 = log.clone();
        let consumer = BoxBiConsumerOnce::new(move |x: &i32, y: &i32| {
            l1.lock().unwrap().push(*x + *y);
        });
        let conditional = consumer.when(|x: &i32, y: &i32| *x > 0 && *y > 0);
        let chained = conditional.and_then(move |x: &i32, y: &i32| {
            l2.lock().unwrap().push(*x * *y);
        });
        chained.accept(&5, &3);
        assert_eq!(*log.lock().unwrap(), vec![8, 15]);
    }

    #[test]
    fn test_box_consumer_once_into_box_identity() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let l = log.clone();
        let consumer = BoxBiConsumerOnce::new(move |x: &i32, y: &i32| {
            l.lock().unwrap().push(*x + *y);
        });
        // into_box should return self for BoxBiConsumerOnce
        let boxed = consumer.into_box();
        boxed.accept(&5, &3);
        assert_eq!(*log.lock().unwrap(), vec![8]);
    }

    #[test]
    fn test_box_consumer_once_into_fn_unwrap() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let l = log.clone();
        let consumer = BoxBiConsumerOnce::new(move |x: &i32, y: &i32| {
            l.lock().unwrap().push(*x + *y);
        });
        // into_fn should unwrap the Box and return the inner function
        let func = consumer.into_fn();
        func(&5, &3);
        assert_eq!(*log.lock().unwrap(), vec![8]);
    }
}

#[cfg(test)]
mod closure_tests {
    use super::*;

    #[test]
    fn test_closure_accept() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let l = log.clone();
        let closure = move |x: &i32, y: &i32| {
            l.lock().unwrap().push(*x + *y);
        };
        closure.accept(&5, &3);
        assert_eq!(*log.lock().unwrap(), vec![8]);
    }

    #[test]
    fn test_closure_and_then() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let l1 = log.clone();
        let l2 = log.clone();
        let chained = (move |x: &i32, y: &i32| {
            l1.lock().unwrap().push(*x + *y);
        })
        .and_then(move |x: &i32, y: &i32| {
            l2.lock().unwrap().push(*x * *y);
        });

        chained.accept(&5, &3);
        assert_eq!(*log.lock().unwrap(), vec![8, 15]);
    }

    #[test]
    fn test_closure_into_fn() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let l = log.clone();
        let closure = move |x: &i32, y: &i32| {
            l.lock().unwrap().push(*x + *y);
        };
        let func = closure.into_fn();
        func(&5, &3);
        assert_eq!(*log.lock().unwrap(), vec![8]);
    }

    #[test]
    fn test_closure_to_box() {
        // Create a cloneable closure using a function pointer
        fn add_values(x: &i32, y: &i32) {
            println!("Sum: {}", x + y);
        }

        // Function pointers implement Clone
        let closure: fn(&i32, &i32) = add_values;

        // Test to_box - should not consume the closure since it's cloneable
        let box_consumer = closure.to_box();
        box_consumer.accept(&5, &3);

        // Verify we can still use the closure again
        let box_consumer2 = closure.to_box();
        box_consumer2.accept(&10, &20);
    }

    #[test]
    fn test_closure_to_fn() {
        // Create a cloneable closure using a function pointer
        fn multiply_values(x: &i32, y: &i32) {
            println!("Product: {}", x * y);
        }

        // Function pointers implement Clone
        let closure: fn(&i32, &i32) = multiply_values;

        // Test to_fn - should not consume the closure since it's cloneable
        let func = closure.to_fn();
        func(&5, &3);

        // Verify we can still use the closure again
        let func2 = closure.to_fn();
        func2(&10, &20);
    }

    #[test]
    fn test_closure_to_box_with_state() {
        let log = Arc::new(Mutex::new(Vec::new()));

        // Create a cloneable struct that implements the closure behavior
        #[derive(Clone)]
        struct LoggingConsumer {
            log: Arc<Mutex<Vec<i32>>>,
        }

        impl LoggingConsumer {
            fn consume(&self, x: &i32, y: &i32) {
                self.log.lock().unwrap().push(*x + *y);
            }
        }

        let consumer = LoggingConsumer { log: log.clone() };
        let consumer_clone = consumer.clone();

        // Create a cloneable closure wrapper
        let closure = move |x: &i32, y: &i32| consumer.consume(x, y);

        // Test to_box
        let box_consumer = closure.to_box();
        box_consumer.accept(&5, &3);
        assert_eq!(*log.lock().unwrap(), vec![8]);

        // Use the cloned consumer
        let closure2 = move |x: &i32, y: &i32| consumer_clone.consume(x, y);
        let box_consumer2 = closure2.to_box();
        box_consumer2.accept(&10, &20);
        assert_eq!(*log.lock().unwrap(), vec![8, 30]);
    }

    #[test]
    fn test_closure_to_fn_with_state() {
        let log = Arc::new(Mutex::new(Vec::new()));

        // Create a cloneable struct that implements the closure behavior
        #[derive(Clone)]
        struct LoggingConsumer {
            log: Arc<Mutex<Vec<i32>>>,
        }

        impl LoggingConsumer {
            fn consume(&self, x: &i32, y: &i32) {
                self.log.lock().unwrap().push(*x * *y);
            }
        }

        let consumer = LoggingConsumer { log: log.clone() };
        let consumer_clone = consumer.clone();

        // Create a cloneable closure wrapper
        let closure = move |x: &i32, y: &i32| consumer.consume(x, y);

        // Test to_fn
        let func = closure.to_fn();
        func(&5, &3);
        assert_eq!(*log.lock().unwrap(), vec![15]);

        // Use the cloned consumer
        let closure2 = move |x: &i32, y: &i32| consumer_clone.consume(x, y);
        let func2 = closure2.to_fn();
        func2(&10, &20);
        assert_eq!(*log.lock().unwrap(), vec![15, 200]);
    }
}

#[cfg(test)]
mod debug_display_tests {
    use super::*;

    #[test]
    fn test_debug() {
        let consumer = BoxBiConsumerOnce::new(|_x: &i32, _y: &i32| {});
        let debug_str = format!("{:?}", consumer);
        assert!(debug_str.contains("BoxBiConsumerOnce"));
    }

    #[test]
    fn test_debug_with_name() {
        let mut consumer = BoxBiConsumerOnce::new(|_x: &i32, _y: &i32| {});
        consumer.set_name("test_consumer");
        let debug_str = format!("{:?}", consumer);
        assert!(debug_str.contains("BoxBiConsumerOnce"));
        assert!(debug_str.contains("test_consumer"));
    }

    #[test]
    fn test_display() {
        let consumer = BoxBiConsumerOnce::new(|_x: &i32, _y: &i32| {});
        let display_str = format!("{}", consumer);
        assert_eq!(display_str, "BoxBiConsumerOnce");
    }

    #[test]
    fn test_display_with_name() {
        let mut consumer = BoxBiConsumerOnce::new(|_x: &i32, _y: &i32| {});
        consumer.set_name("my_consumer");
        let display_str = format!("{}", consumer);
        assert_eq!(display_str, "BoxBiConsumerOnce(my_consumer)");
    }

    #[test]
    fn test_name_methods() {
        let mut consumer = BoxBiConsumerOnce::new(|_x: &i32, _y: &i32| {});
        assert_eq!(consumer.name(), None);

        consumer.set_name("test");
        assert_eq!(consumer.name(), Some("test"));
    }
}

// ============================================================================
// Type Conversion Tests
// ============================================================================

#[cfg(test)]
mod type_conversion_tests {
    use super::*;

    #[test]
    fn test_box_into_box() {
        let consumer = BoxBiConsumerOnce::new(|x: &i32, y: &i32| {
            println!("x: {}, y: {}", x, y);
        });
        let boxed = consumer.into_box();
        boxed.accept(&10, &20);
    }

    #[test]
    fn test_box_into_fn() {
        let consumer = BoxBiConsumerOnce::new(|x: &i32, y: &i32| {
            println!("x: {}, y: {}", x, y);
        });
        let func = consumer.into_fn();
        func(&10, &20);
    }

    #[test]
    fn test_when_or_else_conversion() {
        use std::sync::Arc;
        use std::sync::Mutex;

        let result = Arc::new(Mutex::new(0));
        let result_clone1 = result.clone();
        let result_clone2 = result.clone();

        let consumer = BoxBiConsumerOnce::new(move |x: &i32, _y: &i32| {
            *result_clone1.lock().unwrap() = *x;
        })
        .when(|x: &i32, y: &i32| x > y)
        .or_else(move |_x: &i32, y: &i32| {
            *result_clone2.lock().unwrap() = *y;
        });
        consumer.accept(&5, &3);
        assert_eq!(*result.lock().unwrap(), 5);
    }
}

// ============================================================================
// Default Implementation Tests
// ============================================================================

#[cfg(test)]
mod default_implementation_tests {
    use super::*;

    /// Custom struct that implements BiConsumerOnce
    /// to test the default implementations of into_box, into_fn, to_box, to_fn
    struct CustomBiConsumerOnce {
        log: Arc<Mutex<Vec<i32>>>,
        multiplier: i32,
    }

    impl CustomBiConsumerOnce {
        fn new(log: Arc<Mutex<Vec<i32>>>, multiplier: i32) -> Self {
            Self { log, multiplier }
        }
    }

    impl BiConsumerOnce<i32, i32> for CustomBiConsumerOnce {
        fn accept(self, first: &i32, second: &i32) {
            self.log
                .lock()
                .unwrap()
                .push((*first + *second) * self.multiplier);
        }
    }

    impl Clone for CustomBiConsumerOnce {
        fn clone(&self) -> Self {
            Self {
                log: self.log.clone(),
                multiplier: self.multiplier,
            }
        }
    }

    #[test]
    fn test_custom_into_box() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let consumer = CustomBiConsumerOnce::new(log.clone(), 2);

        // Test default into_box implementation
        let boxed = consumer.into_box();
        boxed.accept(&5, &3);
        assert_eq!(*log.lock().unwrap(), vec![16]); // (5 + 3) * 2 = 16
    }

    #[test]
    fn test_custom_into_fn() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let consumer = CustomBiConsumerOnce::new(log.clone(), 3);

        // Test default into_fn implementation
        let func = consumer.into_fn();
        func(&4, &2);
        assert_eq!(*log.lock().unwrap(), vec![18]); // (4 + 2) * 3 = 18
    }

    #[test]
    fn test_custom_to_box() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let consumer = CustomBiConsumerOnce::new(log.clone(), 4);

        // Test default to_box implementation
        let boxed = consumer.to_box();
        boxed.accept(&3, &2);
        assert_eq!(*log.lock().unwrap(), vec![20]); // (3 + 2) * 4 = 20

        // Verify original consumer is still usable (not consumed)
        consumer.accept(&1, &1);
        assert_eq!(*log.lock().unwrap(), vec![20, 8]); // (1 + 1) * 4 = 8
    }

    #[test]
    fn test_custom_to_fn() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let consumer = CustomBiConsumerOnce::new(log.clone(), 5);

        // Test default to_fn implementation
        let func = consumer.to_fn();
        func(&2, &3);
        assert_eq!(*log.lock().unwrap(), vec![25]); // (2 + 3) * 5 = 25

        // Verify original consumer is still usable (not consumed)
        consumer.accept(&1, &0);
        assert_eq!(*log.lock().unwrap(), vec![25, 5]); // (1 + 0) * 5 = 5
    }

    #[test]
    fn test_custom_consumer_composition() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let consumer1 = CustomBiConsumerOnce::new(log.clone(), 2);

        // Test composing with another consumer using and_then
        let l2 = log.clone();
        let chained = consumer1.into_box().and_then(move |x: &i32, y: &i32| {
            l2.lock().unwrap().push(*x * *y);
        });

        chained.accept(&5, &3);
        assert_eq!(*log.lock().unwrap(), vec![16, 15]); // (5+3)*2=16, 5*3=15
    }

    #[test]
    fn test_custom_consumer_with_predicate() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let consumer = CustomBiConsumerOnce::new(log.clone(), 10);

        // Test using when with custom consumer
        let conditional = consumer.into_box().when(|x: &i32, y: &i32| *x > *y);

        conditional.accept(&5, &3);
        assert_eq!(*log.lock().unwrap(), vec![80]); // (5+3)*10=80

        // Test when condition is false
        let log2 = Arc::new(Mutex::new(Vec::new()));
        let consumer2 = CustomBiConsumerOnce::new(log2.clone(), 10);
        let conditional2 = consumer2.into_box().when(|x: &i32, y: &i32| *x > *y);

        conditional2.accept(&3, &5);
        assert_eq!(*log2.lock().unwrap(), vec![]); // Condition false, not executed
    }

    #[test]
    fn test_custom_consumer_multiple_clones() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let consumer = CustomBiConsumerOnce::new(log.clone(), 2);

        // Clone multiple times and use each clone
        let boxed1 = consumer.to_box();
        let boxed2 = consumer.to_box();
        let boxed3 = consumer.to_box();

        boxed1.accept(&1, &1);
        boxed2.accept(&2, &2);
        boxed3.accept(&3, &3);

        assert_eq!(*log.lock().unwrap(), vec![4, 8, 12]);
        // (1+1)*2=4, (2+2)*2=8, (3+3)*2=12
    }

    #[test]
    fn test_custom_consumer_fn_multiple_uses() {
        let log = Arc::new(Mutex::new(Vec::new()));
        let consumer = CustomBiConsumerOnce::new(log.clone(), 3);

        // Get multiple functions from the same consumer
        let fn1 = consumer.to_fn();
        let fn2 = consumer.to_fn();

        fn1(&2, &2);
        fn2(&3, &3);

        assert_eq!(*log.lock().unwrap(), vec![12, 18]);
        // (2+2)*3=12, (3+3)*3=18
    }
}