pbt 0.5.9

High-throughput property-based testing with `derive`, swarm-testing, precise sizing, and full graph-theoretic type analysis over mutually inductive and uninstantiable types.
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
//! Implementations for `serde_json` types.

use {
    crate::{
        Pbt,
        coin_flips::CoinFlips,
        fields::{Fields, Store},
        impls::integers::big_uint,
        multiset::Multiset,
        reflection::{Parts, Variant, Variants},
        registration::Registration,
    },
    core::{any::TypeId, iter, num::NonZero},
    num_bigint::BigUint,
    serde_json::{Map, Number, Value},
    wyrand::WyRand,
};

impl Pbt for Number {
    #[inline]
    fn construct<F>(
        Parts {
            mut fields,
            variant_index,
        }: Parts<F>,
    ) -> Self
    where
        F: Fields,
    {
        debug_assert_eq!(variant_index, None, "`serde_json::Number` is a literal");
        fields.field()
    }

    #[inline]
    fn deconstruct(self) -> Parts<Store> {
        let mut fields = Store::new();
        let () = fields.push(self);
        Parts {
            fields,
            variant_index: None,
        }
    }

    #[inline]
    fn register(_registration: &mut Registration<'_>) -> Variants<Self> {
        Variants::Literal {
            deserialize: |json| {
                let Value::Number(ref number) = *json else {
                    return None;
                };
                Some(number.clone())
            },
            generators: vec![json_number],
            serialize: |number| Value::Number(number.clone()),
            shrink: shrink_number,
        }
    }
}

impl Pbt for Map<String, Value> {
    #[inline]
    #[expect(
        clippy::expect_used,
        clippy::panic,
        reason = "end-users shouldn't be calling this"
    )]
    fn construct<F>(
        Parts {
            mut fields,
            variant_index,
        }: Parts<F>,
    ) -> Self
    where
        F: Fields,
    {
        let algebraic_index: usize = variant_index
            .expect("`serde_json::Map` is not a literal")
            .get();
        match algebraic_index {
            1 => Self::new(),
            2 => {
                let mut acc: Self = fields.field();
                let _old: Option<Value> = acc.insert(fields.field(), fields.field());
                acc
            }
            _ => panic!("can't instantiate variant #{algebraic_index} of `serde_json::Map`"),
        }
    }

    #[inline]
    #[expect(clippy::panic, reason = "end-users shouldn't be calling this")]
    fn deconstruct(mut self) -> Parts<Store> {
        let Some(key) = self.keys().next().cloned() else {
            return Parts {
                fields: Store::new(),
                variant_index: Some(const { NonZero::new(1).unwrap() }),
            };
        };
        let Some(value) = self.remove(&key) else {
            panic!("INTERNAL ERROR (`pbt`): TOCTOU");
        };
        let mut fields = Store::new();
        let () = fields.push(value);
        let () = fields.push(key);
        let () = fields.push(self);
        Parts {
            fields,
            variant_index: Some(const { NonZero::new(2).unwrap() }),
        }
    }

    #[inline]
    fn register(registration: &mut Registration<'_>) -> Variants<Self> {
        let () = registration.register::<String>();
        let () = registration.register::<Value>();
        Variants::Algebraic(vec![
            Variant {
                field_types: Multiset::new(),
            },
            Variant {
                field_types: [
                    TypeId::of::<Self>(),
                    TypeId::of::<String>(),
                    TypeId::of::<Value>(),
                ]
                .into_iter()
                .collect(),
            },
        ])
    }
}

impl Pbt for Value {
    #[inline]
    #[expect(
        clippy::expect_used,
        clippy::panic,
        reason = "end-users shouldn't be calling this"
    )]
    fn construct<F>(
        Parts {
            mut fields,
            variant_index,
        }: Parts<F>,
    ) -> Self
    where
        F: Fields,
    {
        let algebraic_index: usize = variant_index
            .expect("`serde_json::Value` is not a literal")
            .get();
        match algebraic_index {
            1 => Self::Null,
            2 => Self::Bool(fields.field()),
            3 => Self::Number(fields.field()),
            4 => Self::String(fields.field()),
            5 => Self::Array(fields.field()),
            6 => Self::Object(fields.field()),
            _ => panic!("can't instantiate variant #{algebraic_index} of `serde_json::Value`"),
        }
    }

    #[inline]
    fn deconstruct(self) -> Parts<Store> {
        let mut fields = Store::new();
        let variant_index = match self {
            Self::Null => const { NonZero::new(1).unwrap() },
            Self::Bool(bool) => {
                let () = fields.push(bool);
                const { NonZero::new(2).unwrap() }
            }
            Self::Number(number) => {
                let () = fields.push(number);
                const { NonZero::new(3).unwrap() }
            }
            Self::String(string) => {
                let () = fields.push(string);
                const { NonZero::new(4).unwrap() }
            }
            Self::Array(array) => {
                let () = fields.push(array);
                const { NonZero::new(5).unwrap() }
            }
            Self::Object(object) => {
                let () = fields.push(object);
                const { NonZero::new(6).unwrap() }
            }
        };
        Parts {
            fields,
            variant_index: Some(variant_index),
        }
    }

    #[inline]
    fn register(registration: &mut Registration<'_>) -> Variants<Self> {
        let () = registration.register::<bool>();
        let () = registration.register::<Number>();
        let () = registration.register::<String>();
        let () = registration.register::<Vec<Self>>();
        let () = registration.register::<Map<String, Self>>();
        Variants::Algebraic(vec![
            Variant {
                field_types: Multiset::new(),
            },
            Variant {
                field_types: iter::once(TypeId::of::<bool>()).collect(),
            },
            Variant {
                field_types: iter::once(TypeId::of::<Number>()).collect(),
            },
            Variant {
                field_types: iter::once(TypeId::of::<String>()).collect(),
            },
            Variant {
                field_types: iter::once(TypeId::of::<Vec<Self>>()).collect(),
            },
            Variant {
                field_types: iter::once(TypeId::of::<Map<String, Self>>()).collect(),
            },
        ])
    }
}

/// Generate a JSON number from a `BigUint`.
#[inline]
#[expect(
    clippy::expect_used,
    reason = "a decimal `BigUint` string is always a valid JSON number"
)]
fn number_from_biguint(biguint: &BigUint) -> Number {
    biguint
        .to_string()
        .parse()
        .expect("INTERNAL ERROR (`pbt`): invalid `BigUint` JSON number")
}

/// Generate an arbitrary `usize` with a geometric distribution.
#[inline]
#[expect(
    clippy::arithmetic_side_effects,
    reason = "the hardware will die before this overflows"
)]
fn geometric_usize(coin: &mut CoinFlips, prng: &mut WyRand) -> usize {
    let mut acc = 0_usize;
    while coin.flip(prng) {
        acc += 1;
    }
    acc
}

/// Generate a `serde_json::Number` by following exactly the RFC 8259 grammar.
///
/// RFC 8259 §6 defines JSON numbers as optional minus, then an integer
/// (`0` or a nonzero digit followed by digits), then optional fraction, then
/// optional exponent. Equivalently:
/// `-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?`.
///
/// Source: <https://datatracker.ietf.org/doc/html/rfc8259#section-6>.
#[inline]
#[expect(
    clippy::expect_used,
    reason = "we generate exactly the grammar accepted by `Number::from_str`"
)]
fn json_number(prng: &mut WyRand) -> Number {
    let mut coin = CoinFlips::new(prng);
    let mut acc = String::new();
    if coin.flip(prng) {
        acc.push('-');
    }
    push_integer(&mut coin, prng, &mut acc);
    if coin.flip(prng) {
        acc.push('.');
        push_digits(&mut coin, prng, &mut acc);
    }
    if coin.flip(prng) {
        acc.push(if coin.flip(prng) { 'e' } else { 'E' });
        if coin.flip(prng) {
            acc.push(if coin.flip(prng) { '+' } else { '-' });
        }
        push_digits(&mut coin, prng, &mut acc);
    }
    acc.parse()
        .expect("INTERNAL ERROR (`pbt`): generated invalid JSON number")
}

/// Push a nonempty sequence of decimal digits.
#[inline]
fn push_digits(coin: &mut CoinFlips, prng: &mut WyRand, out: &mut String) {
    let n_zeros = geometric_usize(coin, prng);
    let () = out.extend(iter::repeat_n('0', n_zeros));
    let () = out.push_str(&big_uint(coin, prng, 1).to_string());
}

/// Push an RFC 8259 integer component.
#[inline]
#[expect(clippy::arithmetic_side_effects, reason = "`BigUint` cannot overflow")]
fn push_integer(coin: &mut CoinFlips, prng: &mut WyRand, out: &mut String) {
    if coin.flip(prng) {
        out.push('0');
    } else {
        out.push_str(&(big_uint(coin, prng, 1) + BigUint::ONE).to_string());
    }
}

/// Shrink unsigned-integer JSON numbers using `BigUint` arithmetic.
#[inline]
#[expect(
    clippy::needless_pass_by_value,
    reason = "literal shrinking functions take owned values"
)]
#[expect(
    clippy::arithmetic_side_effects,
    reason = "`BigUint` cannot underflow here"
)]
fn shrink_number(number: Number) -> Box<dyn Iterator<Item = Number>> {
    let Ok(n) = number.to_string().parse::<BigUint>() else {
        return Box::new(iter::empty());
    };
    let mut shift = 0_usize;
    Box::new(iter::from_fn(move || {
        let delta = &n >> shift;
        if delta == BigUint::from(0_u8) {
            return None;
        }
        shift += 1;
        let shrunk = &n - delta;
        Some(number_from_biguint(&shrunk))
    }))
}

#[cfg(test)]
mod tests {
    #![expect(clippy::unwrap_used, reason = "failing tests ought to panic")]

    use {
        super::*,
        crate::{check_eta_expansion, check_serialization},
    };

    #[test]
    fn number_eta_expansion() {
        let () = check_eta_expansion::<Number>();
    }

    #[test]
    fn number_serialization() {
        let () = check_serialization::<Number>();
    }

    #[test]
    fn number_from_biguint_exceeds_u64() {
        let biguint: BigUint = "18446744073709551616".parse().unwrap();
        let number = number_from_biguint(&biguint);
        assert!(biguint > BigUint::from(u64::MAX));
        assert_eq!(number.to_string(), biguint.to_string());
    }

    #[test]
    fn deterministic_big_uint() {
        let mut prng = WyRand::new(123);
        let mut coin = CoinFlips::new(&mut prng);
        let generated: Vec<BigUint> = iter::repeat_with(|| big_uint(&mut coin, &mut prng, 1))
            .take(10_usize)
            .collect();
        let expected: Vec<BigUint> = [1_usize, 31, 0, 1, 5, 1, 1, 1, 2, 0]
            .into_iter()
            .map(BigUint::from)
            .collect();
        assert_eq!(generated, expected);
    }

    #[test]
    fn deterministic_geometric_usize() {
        let mut prng = WyRand::new(123);
        let mut coin = CoinFlips::new(&mut prng);
        let generated: Vec<usize> = iter::repeat_with(|| geometric_usize(&mut coin, &mut prng))
            .take(10_usize)
            .collect();
        assert_eq!(generated, vec![1, 9, 0, 1, 2, 2, 1, 1, 1, 2]);
    }

    #[test]
    fn deterministic_json_numbers() {
        let mut prng = WyRand::new(123);
        let generated: Vec<String> = iter::repeat_with(|| json_number(&mut prng).to_string())
            .take(5_usize)
            .collect();
        assert_eq!(generated, vec!["-32e+33", "0e+07", "-2", "1", "-2.000e+1"],);
    }

    #[test]
    fn deterministic_push_digits() {
        let mut prng = WyRand::new(123);
        let mut coin = CoinFlips::new(&mut prng);
        let generated: Vec<String> = iter::repeat_with(|| {
            let mut s = String::new();
            let () = push_digits(&mut coin, &mut prng, &mut s);
            s
        })
        .take(10_usize)
        .collect();
        assert_eq!(
            generated,
            vec!["031", "1", "0033", "0", "0", "02", "001", "012", "00", "1",],
        );
    }

    #[test]
    fn deterministic_push_integer() {
        let mut prng = WyRand::new(123);
        let mut coin = CoinFlips::new(&mut prng);
        let generated: Vec<String> = iter::repeat_with(|| {
            let mut s = String::new();
            let () = push_integer(&mut coin, &mut prng, &mut s);
            s
        })
        .take(10_usize)
        .collect();
        assert_eq!(
            generated,
            vec!["0", "32", "2", "0", "0", "34", "1", "1", "0", "3"],
        );
    }

    #[test]
    fn deterministic_shrink_number() {
        let original = number_from_biguint(&BigUint::from(1_000_usize));
        let generated: Vec<String> = shrink_number(original)
            .take(11_usize)
            .map(|candidate| candidate.to_string())
            .collect();
        assert_eq!(
            generated,
            vec![
                "0", "500", "750", "875", "938", "969", "985", "993", "997", "999"
            ],
        );
    }

    #[test]
    fn map_eta_expansion() {
        let () = check_eta_expansion::<Map<String, Value>>();
    }

    #[test]
    fn map_serialization() {
        let () = check_serialization::<Map<String, Value>>();
    }

    #[test]
    fn value_eta_expansion() {
        let () = check_eta_expansion::<Value>();
    }

    #[test]
    fn value_serialization() {
        let () = check_serialization::<Value>();
    }
}