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
use super::*;
mod prop_tests {
use super::*;
use proptest::prelude::*;
#[cfg(test)]
mod construction {
use super::*;
// ------------------------------------------------------------
// strategies
// ------------------------------------------------------------
prop_compose! {
fn point_strategy()(
x in prop_oneof![
Just(u8::MIN),
Just(u8::MIN.saturating_add(1)),
Just(0u8),
Just(1u8),
Just(2u8),
Just(u8::MAX.saturating_sub(2)),
Just(u8::MAX.saturating_sub(1)),
Just(u8::MAX),
any::<u8>(),
]
) -> u8 {
x
}
}
prop_compose! {
fn interval_strategy()(
a in point_strategy(),
b in point_strategy(),
) -> U8CO {
let start = a.min(b);
let end_excl = a.max(b).saturating_add(1);
// end_excl 至少比 start 大 1;若 a.max(b) == MAX,则 saturating_add(1) == MAX,
// 这时可能 start == end_excl,需要手动修正成一个合法非空区间。
if let Some(iv) = U8CO::try_new(start, end_excl) {
iv
} else {
// 唯一会落到这里的典型情形是 start == end_excl == MAX。
// 构造一个贴近上界的最小非空区间。
U8CO::try_new(u8::MAX - 1, u8::MAX).unwrap()
}
}
}
fn intervals_strategy() -> impl Strategy<Value = Vec<U8CO>> {
prop::collection::vec(interval_strategy(), 0..48)
}
fn probe_points_strategy() -> impl Strategy<Value = Vec<u8>> {
prop::collection::vec(point_strategy(), 0..96)
}
prop_compose! {
fn interval_array_8_strategy()(
xs in prop::array::uniform8(interval_strategy())
) -> [U8CO; 8] {
xs
}
}
// ------------------------------------------------------------
// helpers
// ------------------------------------------------------------
#[inline]
fn raw_contains_point(raw: &[U8CO], x: u8) -> bool {
raw.iter().any(|iv| iv.contains(x))
}
#[inline]
fn is_strictly_normalized(xs: &[U8CO]) -> bool {
xs.windows(2).all(|w| {
let a = w[0];
let b = w[1];
a.start() < b.start() && !a.is_contiguous_with(b)
})
}
#[inline]
fn sum_len(xs: &[U8CO]) -> u8 {
xs.iter().fold(0u8, |acc, iv| acc.saturating_add(iv.len()))
}
fn enrich_probes(raw: &[U8CO], extra: &[u8]) -> Vec<u8> {
#[inline]
fn midpoint_u8(a: u8, b: u8) -> u8 {
(a & b) + ((a ^ b) >> 1)
}
let mut out = Vec::with_capacity(extra.len() + raw.len() * 6 + 6);
out.push(u8::MIN);
out.push(u8::MIN.saturating_add(1));
out.push(0);
out.push(1);
out.push(u8::MAX.saturating_sub(1));
out.push(u8::MAX);
out.extend_from_slice(extra);
for &iv in raw {
let s = iv.start();
let e = iv.end_excl();
let ei = iv.end_incl();
out.push(s);
out.push(ei);
out.push(e);
out.push(s.saturating_sub(1));
out.push(ei.saturating_add(1));
out.push(midpoint_u8(s, ei));
}
out.sort_unstable();
out.dedup();
out
}
// ------------------------------------------------------------
// deterministic smoke tests
// ------------------------------------------------------------
#[test]
fn empty_input_builds_empty_set() {
let set = U8COBatchSet::default();
assert!(set.as_slice().is_empty());
assert_eq!(set.interval_count(), 0);
assert_eq!(set.point_count(), 0);
let set = U8COBatchSet::from(Vec::<U8CO>::new());
assert!(set.as_slice().is_empty());
assert_eq!(set.interval_count(), 0);
assert_eq!(set.point_count(), 0);
let set: U8COBatchSet = core::iter::empty::<U8CO>().collect();
assert!(set.as_slice().is_empty());
assert_eq!(set.interval_count(), 0);
assert_eq!(set.point_count(), 0);
}
// ------------------------------------------------------------
// main properties
// ------------------------------------------------------------
proptest! {
#[test]
fn prop_vec_constructor_normalizes_structure_and_preserves_point_membership(
raw in intervals_strategy(),
extra_probes in probe_points_strategy(),
) {
let set = U8COBatchSet::from(raw.clone());
// 构造后必须已经规范化:
// - start 严格递增
// - 相邻段不可再 contiguous
prop_assert!(is_strictly_normalized(set.as_slice()));
// 覆盖语义保持不变:
// 对采样点,构造前后 contains_point 等价。
let probes = enrich_probes(&raw, &extra_probes);
for x in probes {
prop_assert_eq!(
set.contains_point(x),
raw_contains_point(&raw, x),
"membership mismatch at point {:?}, raw={:?}, set={:?}",
x,
raw,
set,
);
}
}
#[test]
fn prop_vec_constructor_is_order_invariant(
raw in intervals_strategy(),
) {
let mut sorted = raw.clone();
sorted.sort_unstable_by(|a, b| {
a.start()
.cmp(&b.start())
.then(a.end_excl().cmp(&b.end_excl()))
});
let a = U8COBatchSet::from(raw);
let b = U8COBatchSet::from(sorted);
prop_assert_eq!(a, b);
}
#[test]
fn prop_interval_count_matches_internal_slice_len(
raw in intervals_strategy(),
) {
let set = U8COBatchSet::from(raw);
prop_assert_eq!(set.interval_count(), set.as_slice().len() as u8);
}
#[test]
fn prop_point_count_matches_sum_of_normalized_interval_lengths(
raw in intervals_strategy(),
) {
let set = U8COBatchSet::from(raw);
prop_assert_eq!(set.point_count(), sum_len(set.as_slice()));
}
#[test]
fn prop_from_iter_matches_vec_constructor(
raw in intervals_strategy(),
) {
let a = U8COBatchSet::from(raw.clone());
let b: U8COBatchSet = raw.into_iter().collect();
prop_assert_eq!(a, b);
}
#[test]
fn prop_array_constructor_matches_vec_constructor(
raw in interval_array_8_strategy(),
) {
let a = U8COBatchSet::from(raw);
let b = U8COBatchSet::from(Vec::from(raw));
prop_assert_eq!(a, b);
}
}
}
}