nsip 0.4.0

NSIP Search API client for nsipsearch.nsip.org/api
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
# Property-Based Testing Guide

## Overview

Property-based testing validates code properties hold for all inputs, not just specific examples. Uses [proptest](https://github.com/proptest-rs/proptest) and [quickcheck](https://github.com/BurntSushi/quickcheck).

**Philosophy:** Instead of testing specific cases, test universal properties.

## Traditional vs Property-Based

### Traditional (Example-Based)

```rust
#[test]
fn test_reverse() {
    assert_eq!(reverse("hello"), "olleh");
    assert_eq!(reverse("rust"), "tsur");
    assert_eq!(reverse(""), "");
}
```

**Coverage:** 3 specific cases

### Property-Based

```rust
use proptest::prelude::*;

proptest! {
    #[test]
    fn reverse_inverts(s: String) {
        assert_eq!(reverse(&reverse(&s)), s);
    }
}
```

**Coverage:** Hundreds of generated strings

## Setup

### Add Dependencies

```toml
[dev-dependencies]
proptest = "1.5"
quickcheck = "1.0"
quickcheck_macros = "1.0"
```

## Using Proptest

### Basic Property Test

```rust
use proptest::prelude::*;

proptest! {
    #[test]
    fn test_addition_commutative(a: i32, b: i32) {
        // Property: a + b == b + a
        prop_assert_eq!(a + b, b + a);
    }

    #[test]
    fn test_multiplication_associative(a: i32, b: i32, c: i32) {
        // Property: (a * b) * c == a * (b * c)
        prop_assert_eq!((a * b) * c, a * (b * c));
    }
}
```

### Custom Strategies

Generate specific input types:

```rust
use proptest::prelude::*;

fn valid_email() -> impl Strategy<Value = String> {
    "[a-z]{1,20}@[a-z]{1,10}\\.[a-z]{2,3}"
        .prop_map(|s| s.to_string())
}

proptest! {
    #[test]
    fn test_email_parser(email in valid_email()) {
        let parsed = parse_email(&email);
        prop_assert!(parsed.is_ok());
    }
}
```

### Constrained Inputs

```rust
proptest! {
    #[test]
    fn test_positive_division(
        a in 1..1000i32,  // 1 to 999
        b in 1..100i32    // 1 to 99
    ) {
        let result = a / b;
        prop_assert!(result >= 0);
        prop_assert!(result <= a);
    }
}
```

### Shrinking

When a test fails, proptest **shrinks** the input to find minimal failing case:

```rust
proptest! {
    #[test]
    fn test_fails_large_numbers(n: u32) {
        prop_assert!(n < 1000);  // Fails for n >= 1000
    }
}
```

**Output:**
```
Test failed for input: n = 1000
(shrunk from initial failure: n = 4294967295)
```

## Common Properties

### 1. Idempotence

**Property:** Applying operation twice = applying once

```rust
proptest! {
    #[test]
    fn sort_is_idempotent(mut vec: Vec<i32>) {
        vec.sort();
        let first = vec.clone();
        vec.sort();
        prop_assert_eq!(vec, first);
    }
}
```

### 2. Round-Trip (Encode/Decode)

**Property:** Decode(Encode(x)) == x

```rust
proptest! {
    #[test]
    fn serialize_roundtrip(data: MyStruct) {
        let bytes = serialize(&data);
        let decoded = deserialize(&bytes).unwrap();
        prop_assert_eq!(data, decoded);
    }
}
```

### 3. Invariants

**Property:** Certain conditions always hold

```rust
proptest! {
    #[test]
    fn heap_maintains_max(ops: Vec<HeapOp>) {
        let mut heap = MaxHeap::new();
        for op in ops {
            match op {
                HeapOp::Push(n) => heap.push(n),
                HeapOp::Pop => { heap.pop(); }
            }
            // Invariant: top element is maximum
            if let Some(top) = heap.peek() {
                for item in heap.iter() {
                    prop_assert!(*top >= *item);
                }
            }
        }
    }
}
```

### 4. Commutativity

**Property:** Order doesn't matter

```rust
proptest! {
    #[test]
    fn set_insertion_commutative(a: i32, b: i32) {
        let mut set1 = HashSet::new();
        set1.insert(a);
        set1.insert(b);

        let mut set2 = HashSet::new();
        set2.insert(b);
        set2.insert(a);

        prop_assert_eq!(set1, set2);
    }
}
```

### 5. Associativity

**Property:** Grouping doesn't matter

```rust
proptest! {
    #[test]
    fn string_concat_associative(a: String, b: String, c: String) {
        let left = format!("{}{}{}", a, b, c);
        let right = format!("{}{}{}", a, b, c);
        prop_assert_eq!(left, right);
    }
}
```

### 6. Monotonicity

**Property:** Output increases with input

```rust
proptest! {
    #[test]
    fn absolute_value_monotonic(a: i32, b: i32) {
        if a <= b {
            prop_assert!(a.abs() <= b.abs() || a.abs() >= b.abs());
        }
    }
}
```

### 7. Oracle (Test Against Known Implementation)

```rust
proptest! {
    #[test]
    fn optimized_matches_reference(input: Vec<i32>) {
        let optimized = fast_sort(&input);
        let reference = input.clone().sorted();
        prop_assert_eq!(optimized, reference);
    }
}
```

## Using QuickCheck

Alternative to proptest with simpler API:

```rust
use quickcheck_macros::quickcheck;

#[quickcheck]
fn reverse_reverse_is_identity(vec: Vec<i32>) -> bool {
    let mut v = vec.clone();
    v.reverse();
    v.reverse();
    v == vec
}

#[quickcheck]
fn adding_zero_is_identity(n: i32) -> bool {
    n + 0 == n && 0 + n == n
}
```

## Advanced Strategies

### Composite Types

```rust
#[derive(Debug, Clone)]
struct User {
    id: u64,
    name: String,
    age: u8,
}

fn user_strategy() -> impl Strategy<Value = User> {
    (
        any::<u64>(),
        "[a-zA-Z]{1,20}",
        1u8..120u8
    ).prop_map(|(id, name, age)| User {
        id,
        name: name.to_string(),
        age,
    })
}

proptest! {
    #[test]
    fn test_user_validation(user in user_strategy()) {
        prop_assert!(validate_user(&user).is_ok());
    }
}
```

### Recursive Structures

```rust
#[derive(Debug, Clone, PartialEq)]
enum Tree {
    Leaf(i32),
    Node(Box<Tree>, Box<Tree>),
}

fn tree_strategy() -> impl Strategy<Value = Tree> {
    let leaf = any::<i32>().prop_map(Tree::Leaf);
    leaf.prop_recursive(
        8,   // max depth
        256, // max nodes
        10,  // items per collection
        |inner| {
            (inner.clone(), inner).prop_map(|(l, r)| {
                Tree::Node(Box::new(l), Box::new(r))
            })
        },
    )
}
```

### Weighted Strategies

```rust
fn operation_strategy() -> impl Strategy<Value = Op> {
    prop_oneof![
        3 => Just(Op::Add),     // 30% probability
        3 => Just(Op::Remove),  // 30%
        2 => Just(Op::Clear),   // 20%
        2 => Just(Op::Reset),   // 20%
    ]
}
```

## Configuration

### Number of Test Cases

```rust
proptest! {
    #![proptest_config(ProptestConfig::with_cases(10000))]

    #[test]
    fn extensive_test(n: u64) {
        // Runs 10,000 test cases instead of default 256
    }
}
```

### Reproducible Failures

```rust
// Failed seed from test output
proptest! {
    #![proptest_config(ProptestConfig {
        rng_algorithm: RngAlgorithm::ChaCha,
        seed: Some([0xdeadbeef; 4]),
        ..Default::default()
    })]

    #[test]
    fn reproducible_test(n: i32) {
        // Uses fixed seed for reproduction
    }
}
```

## Best Practices

### 1. Start Simple

```rust
// Start with basic property
proptest! {
    #[test]
    fn len_after_push(mut vec: Vec<i32>, n: i32) {
        let original_len = vec.len();
        vec.push(n);
        prop_assert_eq!(vec.len(), original_len + 1);
    }
}
```

### 2. Test Properties, Not Implementation

```rust
// ❌ Bad - tests implementation details
proptest! {
    #[test]
    fn uses_quicksort(vec: Vec<i32>) {
        assert!(is_using_quicksort(&vec));
    }
}

// ✅ Good - tests behavior
proptest! {
    #[test]
    fn result_is_sorted(vec: Vec<i32>) {
        let sorted = my_sort(&vec);
        prop_assert!(is_sorted(&sorted));
    }
}
```

### 3. Combine with Example Tests

```rust
// Specific edge cases
#[test]
fn test_empty_vec() {
    assert_eq!(process(&[]), vec![]);
}

// General properties
proptest! {
    #[test]
    fn output_length_matches(input: Vec<i32>) {
        prop_assert_eq!(process(&input).len(), input.len());
    }
}
```

### 4. Use Preconditions

```rust
proptest! {
    #[test]
    fn division_property(a: i32, b: i32) {
        prop_assume!(b != 0);  // Skip if precondition fails
        let result = a / b;
        prop_assert_eq!(result * b, a - (a % b));
    }
}
```

## Common Patterns

### Testing Parsers

```rust
proptest! {
    #[test]
    fn parser_roundtrip(ast: AST) {
        let text = format_ast(&ast);
        let parsed = parse(&text).unwrap();
        prop_assert_eq!(parsed, ast);
    }
}
```

### Testing Data Structures

```rust
proptest! {
    #[test]
    fn map_operations(ops: Vec<MapOp>) {
        let mut map = MyMap::new();
        let mut reference = HashMap::new();

        for op in ops {
            match op {
                MapOp::Insert(k, v) => {
                    map.insert(k, v);
                    reference.insert(k, v);
                }
                MapOp::Remove(k) => {
                    map.remove(&k);
                    reference.remove(&k);
                }
            }
        }

        prop_assert_eq!(map.len(), reference.len());
    }
}
```

### Testing Concurrent Code

```rust
proptest! {
    #[test]
    fn concurrent_counter(ops: Vec<CounterOp>) {
        let counter = Arc::new(AtomicUsize::new(0));
        let handles: Vec<_> = ops.into_iter().map(|op| {
            let c = counter.clone();
            thread::spawn(move || {
                match op {
                    CounterOp::Inc => c.fetch_add(1, Ordering::SeqCst),
                    CounterOp::Dec => c.fetch_sub(1, Ordering::SeqCst),
                }
            })
        }).collect();

        for h in handles {
            h.join().unwrap();
        }

        // Invariant: counter value is consistent
        prop_assert!(counter.load(Ordering::SeqCst) >= 0);
    }
}
```

## Debugging Failures

### Reproduce Specific Case

```rust
#[test]
fn debug_specific_case() {
    // From proptest failure message
    let input = vec![1, 2, 3];
    assert!(my_function(&input));
}
```

### Print Debugging

```rust
proptest! {
    #[test]
    fn debug_test(n: i32) {
        println!("Testing with n = {}", n);
        prop_assert!(n >= 0 || n < 0);
    }
}
```

## Performance Considerations

Property tests are slower than example tests:
- Run 100-1000+ cases vs 1-10 examples
- Generation overhead

**Optimize:**
- Use in CI, not on every save
- Reduce cases for fast feedback: `with_cases(100)`
- Increase cases for thorough testing: `with_cases(10000)`

## Links

- [Proptest Documentation]https://docs.rs/proptest/
- [QuickCheck Documentation]https://docs.rs/quickcheck/
- [Property-Based Testing Book]https://fsharpforfunandprofit.com/posts/property-based-testing/
- [Hypothesis (Python PBT)]https://hypothesis.readthedocs.io/ - Similar concepts