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
// =============================================================================
// Copyright (c) 2025 - 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Unit tests for the predicate module.
use qubit_function::predicates::{
ArcPredicate,
BoxPredicate,
Predicate,
RcPredicate,
};
use std::cell::RefCell;
use std::sync::{
Arc,
Mutex,
};
struct PositivePredicate;
impl Predicate<i32> for PositivePredicate {
fn test(&self, value: &i32) -> bool {
*value > 0
}
}
#[cfg(test)]
mod logical_operations_tests {
use super::{
ArcPredicate,
BoxPredicate,
Predicate,
RcPredicate,
};
// BoxPredicate NAND tests
#[test]
fn test_box_nand_basic() {
let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
let is_even = BoxPredicate::new(|x: &i32| x % 2 == 0);
let nand = is_positive.nand(is_even);
// NAND: true unless both are true
assert!(nand.test(&3)); // positive but odd: true && false = false, !false = true
assert!(nand.test(&-2)); // negative but even: false && true = false, !false = true
assert!(nand.test(&-1)); // negative and odd: false && false = false, !false = true
assert!(!nand.test(&4)); // positive and even: true && true = true, !true = false
}
// BoxPredicate XOR tests
#[test]
fn test_box_xor_basic() {
let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
let is_even = BoxPredicate::new(|x: &i32| x % 2 == 0);
let xor = is_positive.xor(is_even);
// XOR: true if exactly one is true
assert!(xor.test(&3)); // positive but odd: true ^ false = true
assert!(xor.test(&-2)); // negative but even: false ^ true = true
assert!(!xor.test(&-1)); // negative and odd: false ^ false = false
assert!(!xor.test(&4)); // positive and even: true ^ true = false
}
// BoxPredicate NOR tests
#[test]
fn test_box_nor_basic() {
let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
let is_even = BoxPredicate::new(|x: &i32| x % 2 == 0);
let nor = is_positive.nor(is_even);
// NOR: true only when both are false
assert!(nor.test(&-3)); // negative and odd: !(false || false) = true
assert!(!nor.test(&3)); // positive but odd: !(true || false) = false
assert!(!nor.test(&-2)); // negative but even: !(false || true) = false
assert!(!nor.test(&4)); // positive and even: !(true || true) = false
}
// RcPredicate NAND tests
#[test]
fn test_rc_nand_basic() {
let is_positive = RcPredicate::new(|x: &i32| *x > 0);
let is_even = RcPredicate::new(|x: &i32| x % 2 == 0);
let nand = is_positive.nand(is_even.clone());
assert!(nand.test(&3)); // positive but odd
assert!(nand.test(&-2)); // negative but even
assert!(nand.test(&-1)); // negative and odd
assert!(!nand.test(&4)); // positive and even
// Original predicates still usable
assert!(is_positive.test(&5));
assert!(is_even.test(&6));
}
// RcPredicate XOR tests
#[test]
fn test_rc_xor_basic() {
let is_positive = RcPredicate::new(|x: &i32| *x > 0);
let is_even = RcPredicate::new(|x: &i32| x % 2 == 0);
let xor = is_positive.xor(is_even.clone());
assert!(xor.test(&3)); // positive but odd
assert!(xor.test(&-2)); // negative but even
assert!(!xor.test(&-1)); // negative and odd
assert!(!xor.test(&4)); // positive and even
// Original predicates still usable
assert!(is_positive.test(&5));
assert!(is_even.test(&6));
}
// RcPredicate NOR tests
#[test]
fn test_rc_nor_basic() {
let is_positive = RcPredicate::new(|x: &i32| *x > 0);
let is_even = RcPredicate::new(|x: &i32| x % 2 == 0);
let nor = is_positive.nor(is_even.clone());
// NOR: true only when both are false
assert!(nor.test(&-3)); // negative and odd: !(false || false) = true
assert!(!nor.test(&3)); // positive but odd: !(true || false) = false
assert!(!nor.test(&-2)); // negative but even: !(false || true) = false
assert!(!nor.test(&4)); // positive and even: !(true || true) = false
// Original predicates still usable
assert!(is_positive.test(&5));
assert!(is_even.test(&6));
}
// ArcPredicate NAND tests
#[test]
fn test_arc_nand_basic() {
let is_positive = ArcPredicate::new(|x: &i32| *x > 0);
let is_even = ArcPredicate::new(|x: &i32| x % 2 == 0);
let nand = is_positive.nand(is_even.clone());
assert!(nand.test(&3)); // positive but odd
assert!(nand.test(&-2)); // negative but even
assert!(nand.test(&-1)); // negative and odd
assert!(!nand.test(&4)); // positive and even
// Original predicates still usable
assert!(is_positive.test(&5));
assert!(is_even.test(&6));
}
// ArcPredicate XOR tests
#[test]
fn test_arc_xor_basic() {
let is_positive = ArcPredicate::new(|x: &i32| *x > 0);
let is_even = ArcPredicate::new(|x: &i32| x % 2 == 0);
let xor = is_positive.xor(is_even.clone());
assert!(xor.test(&3)); // positive but odd
assert!(xor.test(&-2)); // negative but even
assert!(!xor.test(&-1)); // negative and odd
assert!(!xor.test(&4)); // positive and even
// Original predicates still usable
assert!(is_positive.test(&5));
assert!(is_even.test(&6));
}
// ArcPredicate NOR tests
#[test]
fn test_arc_nor_basic() {
let is_positive = ArcPredicate::new(|x: &i32| *x > 0);
let is_even = ArcPredicate::new(|x: &i32| x % 2 == 0);
let nor = is_positive.nor(is_even.clone());
// NOR: true only when both are false
assert!(nor.test(&-3)); // negative and odd: !(false || false) = true
assert!(!nor.test(&3)); // positive but odd: !(true || false) = false
assert!(!nor.test(&-2)); // negative but even: !(false || true) = false
assert!(!nor.test(&4)); // positive and even: !(true || true) = false
// Original predicates still usable
assert!(is_positive.test(&5));
assert!(is_even.test(&6));
}
// Box wrapper NAND tests
// Box wrapper XOR tests
// Box wrapper NOR tests
// Complex composition with NAND
// Complex composition with XOR
// NAND with string predicates
#[test]
fn test_nand_with_strings() {
let is_long = BoxPredicate::new(|s: &String| s.len() > 5);
let has_uppercase =
BoxPredicate::new(|s: &String| s.chars().any(|c| c.is_uppercase()));
let nand = is_long.nand(has_uppercase);
assert!(nand.test(&"hello".to_string())); // short, no uppercase: !(false && false) = true
assert!(nand.test(&"Hello".to_string())); // short, has uppercase: !(false && true) = true
assert!(nand.test(&"goodbye".to_string())); // long, no uppercase: !(true && false) = true
assert!(!nand.test(&"HelloWorld".to_string())); // long, has uppercase: !(true && true) = false
}
// XOR with string predicates
#[test]
fn test_xor_with_strings() {
let is_long = BoxPredicate::new(|s: &String| s.len() > 5);
let has_uppercase =
BoxPredicate::new(|s: &String| s.chars().any(|c| c.is_uppercase()));
let xor = is_long.xor(has_uppercase);
assert!(!xor.test(&"hello".to_string())); // short, no uppercase: false ^ false = false
assert!(xor.test(&"Hello".to_string())); // short, has uppercase: false ^ true = true
assert!(xor.test(&"goodbye".to_string())); // long, no uppercase: true ^ false = true
assert!(!xor.test(&"HelloWorld".to_string())); // long, has uppercase: true ^ true = false
}
// NOR with string predicates
#[test]
fn test_nor_with_strings() {
let is_long = BoxPredicate::new(|s: &String| s.len() > 5);
let has_uppercase =
BoxPredicate::new(|s: &String| s.chars().any(|c| c.is_uppercase()));
let nor = is_long.nor(has_uppercase);
assert!(nor.test(&"hello".to_string())); // short, no uppercase: !(false || false) = true
assert!(!nor.test(&"Hello".to_string())); // short, has uppercase: !(false || true) = false
assert!(!nor.test(&"goodbye".to_string())); // long, no uppercase: !(true || false) = false
assert!(!nor.test(&"HelloWorld".to_string())); // long, has uppercase: !(true || true) = false
}
}