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
// =============================================================================
// Copyright (c) 2025 - 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Comprehensive tests for StatefulBiConsumer types
//!
//! This module provides exhaustive test coverage for all StatefulBiConsumer
//! implementations including BoxStatefulBiConsumer, ArcStatefulBiConsumer,
//! RcStatefulBiConsumer, and their conditional variants.
use qubit_function::{
ArcStatefulBiConsumer,
BiConsumerOnce,
BoxStatefulBiConsumer,
RcStatefulBiConsumer,
StatefulBiConsumer,
};
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::{
Arc,
Mutex,
};
// ============================================================================
// BoxStatefulBiConsumer Tests
// ============================================================================
/// Custom struct for testing StatefulBiConsumer trait default implementations
#[derive(Clone)]
struct CustomStatefulBiConsumer {
multiplier: i32,
log: Arc<Mutex<Vec<i32>>>,
}
impl StatefulBiConsumer<i32, i32> for CustomStatefulBiConsumer {
fn accept(&mut self, first: &i32, second: &i32) {
self.multiplier += 1;
let result = (*first + *second) * self.multiplier;
self.log
.lock()
.expect("mutex should not be poisoned")
.push(result);
}
}
#[cfg(test)]
mod wrapper_composition_tests {
use super::{
Arc,
ArcStatefulBiConsumer,
BoxStatefulBiConsumer,
Mutex,
Rc,
RcStatefulBiConsumer,
RefCell,
StatefulBiConsumer,
};
// Test and_then() on closure
#[test]
fn test_closure_and_then() {
let log = Arc::new(Mutex::new(Vec::new()));
let l1 = log.clone();
let l2 = log.clone();
let mut chained =
BoxStatefulBiConsumer::new(move |x: &i32, y: &i32| {
l1.lock()
.expect("mutex should not be poisoned")
.push(*x + *y);
})
.and_then(move |x: &i32, y: &i32| {
l2.lock()
.expect("mutex should not be poisoned")
.push(*x * *y);
});
chained.accept(&5, &3);
assert_eq!(
*log.lock().expect("mutex should not be poisoned"),
vec![8, 15]
);
}
// Test and_then() with multiple closures
#[test]
fn test_closure_and_then_chain() {
let log = Arc::new(Mutex::new(Vec::new()));
let l1 = log.clone();
let l2 = log.clone();
let l3 = log.clone();
let mut chained =
BoxStatefulBiConsumer::new(move |x: &i32, y: &i32| {
l1.lock()
.expect("mutex should not be poisoned")
.push(*x + *y);
})
.and_then(move |x: &i32, y: &i32| {
l2.lock()
.expect("mutex should not be poisoned")
.push(*x * *y);
})
.and_then(move |x: &i32, y: &i32| {
l3.lock()
.expect("mutex should not be poisoned")
.push(*x - *y);
});
chained.accept(&5, &3);
assert_eq!(
*log.lock().expect("mutex should not be poisoned"),
vec![8, 15, 2]
);
}
// Test and_then() with BoxStatefulBiConsumer
#[test]
fn test_closure_and_then_with_box() {
let log = Arc::new(Mutex::new(Vec::new()));
let l1 = log.clone();
let l2 = log.clone();
let box_consumer =
BoxStatefulBiConsumer::new(move |x: &i32, y: &i32| {
l2.lock()
.expect("mutex should not be poisoned")
.push(*x * *y);
});
let mut chained =
BoxStatefulBiConsumer::new(move |x: &i32, y: &i32| {
l1.lock()
.expect("mutex should not be poisoned")
.push(*x + *y);
})
.and_then(box_consumer);
chained.accept(&5, &3);
assert_eq!(
*log.lock().expect("mutex should not be poisoned"),
vec![8, 15]
);
}
// Test and_then() with ArcStatefulBiConsumer
#[test]
fn test_closure_and_then_with_arc() {
let log = Arc::new(Mutex::new(Vec::new()));
let l1 = log.clone();
let l2 = log.clone();
let arc_consumer =
ArcStatefulBiConsumer::new(move |x: &i32, y: &i32| {
l2.lock()
.expect("mutex should not be poisoned")
.push(*x * *y);
});
let mut chained =
BoxStatefulBiConsumer::new(move |x: &i32, y: &i32| {
l1.lock()
.expect("mutex should not be poisoned")
.push(*x + *y);
})
.and_then(arc_consumer);
chained.accept(&5, &3);
assert_eq!(
*log.lock().expect("mutex should not be poisoned"),
vec![8, 15]
);
}
// Test and_then() with RcStatefulBiConsumer
#[test]
fn test_closure_and_then_with_rc() {
let log = Rc::new(RefCell::new(Vec::new()));
let l1 = log.clone();
let l2 = log.clone();
let rc_consumer = RcStatefulBiConsumer::new(move |x: &i32, y: &i32| {
l2.borrow_mut().push(*x * *y);
});
let mut chained =
BoxStatefulBiConsumer::new(move |x: &i32, y: &i32| {
l1.borrow_mut().push(*x + *y);
})
.and_then(rc_consumer);
chained.accept(&5, &3);
assert_eq!(*log.borrow(), vec![8, 15]);
}
// Test and_then() returns BoxStatefulBiConsumer
}
// ============================================================================
// Closure Implementation Tests (StatefulBiConsumer trait for closures)
// ============================================================================