pmat 3.11.0

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# C++ Mutation Testing

**Production-ready AST-based mutation testing for C++17+.**

## Features

- ๐ŸŽฏ **80%+ mutation scores achievable** - Quantify test suite quality  
- โšก **Fast generation** - Expected ~5ms for 75+ mutants  
- ๐Ÿ” **Real test execution** - Works with CMake/CTest and Google Test  
- ๐Ÿงฌ **7 mutation operators** - Binary, relational, logical, bitwise, unary, pointer, member access  
- ๐Ÿ”ท **C++-specific features** - Pointer operators, member access, update expressions  
- ๐Ÿ“Š **Identifies test gaps** - Surviving mutants reveal actual weaknesses  
- ๐Ÿ”„ **Full automation** - Source โ†’ mutants โ†’ tests โ†’ score  

---

## Quick Start

### Prerequisites

```bash
# Install CMake
sudo apt-get install cmake  # Ubuntu/Debian
brew install cmake          # macOS

# Compiler (GCC/Clang/MSVC)
# Google Test (auto-fetched via CMake FetchContent)
```

### Example Workflow

```bash
cargo run --example cpp_mutation_workflow --features cpp-ast
```

**Output:**
```
๐Ÿ”ท C++ Mutation Testing Workflow

๐Ÿ“ Reading source file: calculator.cpp
   Size: 2847 bytes

๐Ÿ”ง Generating mutants...
   Generated: 75 mutants
   Time: 4.2ms

โœ… Running baseline tests...
   Baseline tests passed โœ…

๐Ÿงช Testing 75 mutants...
   [Progress...]

๐Ÿ“Š Mutation Testing Results
   Total Mutants:    75
   Killed:           62 (82%)
   Survived:         13 (17%)
   Timeout/Error:    0

๐ŸŽฏ Mutation Score: 82% โœ… EXCELLENT!
```

---

## Mutation Operators

### 1. Binary Operator Replacement (AOR)

**Replaces:** `+, -, *, /, %`

```cpp
// Original
int Add(int a, int b) {
    return a + b;
}

// Mutants
return a - b;  // + โ†’ -
return a * b;  // + โ†’ *
return a / b;  // + โ†’ /
return a % b;  // + โ†’ %
```

### 2. Relational Operator Replacement (ROR)

**Replaces:** `<, >, <=, >=, ==, !=`

```cpp
// Original
bool GreaterThan(int a, int b) {
    return a > b;
}

// Mutants
return a < b;   // > โ†’ <
return a >= b;  // > โ†’ >=
return a <= b;  // > โ†’ <=
return a == b;  // > โ†’ ==
return a != b;  // > โ†’ !=
```

### 3. Logical Operator Replacement (LOR)

**Replaces:** `&&, ||`

```cpp
// Original
bool And(bool a, bool b) {
    return a && b;
}

// Mutant
return a || b;  // && โ†’ ||
```

### 4. Bitwise Operator Replacement (BOR)

**Replaces:** `&, |, ^, <<, >>, ~`

```cpp
// Original
int BitwiseAnd(int a, int b) {
    return a & b;
}

// Mutants
return a | b;  // & โ†’ |
return a ^ b;  // & โ†’ ^
```

### 5. Unary Operator Replacement (UOR)

**Replaces:** `-, +, ++, --`

```cpp
// Original
int Negate(int value) {
    return -value;
}

// Mutant
return +value;  // - โ†’ +

// Original
int PreIncrement(int value) {
    return ++value;
}

// Mutant
return --value;  // ++ โ†’ --
```

### 6. Pointer Operator Replacement (POR) โญ C++ SPECIFIC

**Detects:** `*, &, ->`

**Note:** Pointer operators are detected but NOT mutated due to semantic complexity.

```cpp
// Detected (not mutated)
int value = *ptr;        // Dereference
int* ptr = &value;       // Address-of
obj->method();           // Arrow operator
```

**Rationale:** Pointer mutations require type analysis to avoid semantic errors.

### 7. Member Access Replacement (MAR) โญ C++ SPECIFIC

**Detects:** `.`, `::`

**Note:** Member access operators are detected but NOT mutated.

```cpp
// Detected (not mutated)
obj.member = 10;                // Instance member
Class::staticMethod();          // Static/namespace member
```

**Rationale:** `.` and `::` have different semantics (instance vs static) requiring type information.

---

## C++-Specific Features

### Pre/Post Increment Mutations

```cpp
// Pre-increment
int result = ++i;  // Mutated to: --i

// Post-increment
int result = i++;  // Mutated to: i--
```

### Bitwise Operations

```cpp
// Shift operators
int left = a << 2;   // Mutated to: a >> 2
int right = a >> 2;  // Mutated to: a << 2

// Bitwise NOT (detected but not mutated)
int complement = ~value;
```

### CMake/CTest Integration

Works seamlessly with standard C++ build systems:

```cmake
enable_testing()
add_executable(test_calculator test_calculator.cpp)
target_link_libraries(test_calculator gtest_main)
gtest_discover_tests(test_calculator)
```

---

## Installation

### Add to Cargo.toml

```toml
[dependencies]
pmat = "2.154.0"

[features]
cpp-ast = ["pmat/cpp-ast"]
```

### Verify Installation

```bash
cargo build --features cpp-ast
cargo run --example cpp_mutation_workflow --features cpp-ast
```

---

## Usage

### Basic API

```rust
use pmat::services::mutation::CppMutationGenerator;

let generator = CppMutationGenerator::with_default_operators();
let mutants = generator.generate_mutants(&source, "calculator.cpp")?;

println!("Generated {} mutants", mutants.len());

for mutant in &mutants {
    println!("{}: {} at line {}", 
        mutant.id, 
        mutant.operator, 
        mutant.location.line
    );
}
```

### Testing Workflow

1. **Generate mutants** from source
2. **Run baseline tests** to ensure they pass
3. **Test each mutant** by:
   - Replacing source file
   - Rebuilding with CMake
   - Running CTest
   - Checking if tests fail (mutant killed) or pass (mutant survived)
4. **Calculate mutation score**: `(killed / total) * 100%`

### Example Test Suite (Google Test)

```cpp
TEST(CalculatorTest, Add) {
    EXPECT_EQ(Calculator::Add(2, 3), 5);
    EXPECT_EQ(Calculator::Add(-1, -1), -2);
    EXPECT_EQ(Calculator::Add(0, 5), 5);
    EXPECT_EQ(Calculator::Add(-3, 5), 2);
}

TEST(CalculatorTest, GreaterThan) {
    EXPECT_TRUE(Calculator::GreaterThan(5, 3));
    EXPECT_FALSE(Calculator::GreaterThan(3, 5));
    EXPECT_FALSE(Calculator::GreaterThan(3, 3));
}
```

---

## Troubleshooting

### CMake Not Found

```
โš ๏ธ  CMake not available, skipping test execution
   Install from: https://cmake.org/download/
```

**Solution:**
```bash
# Ubuntu/Debian
sudo apt-get install cmake

# macOS
brew install cmake

# Windows
# Download from https://cmake.org/download/
```

### Baseline Tests Fail

```
โŒ Baseline tests failed! Fix tests before mutation testing.
```

**Solution:** Ensure all tests pass before mutation testing:
```bash
cd build
ctest --output-on-failure
```

### Build Errors with Mutants

Mutants that cause compilation errors are marked as `Timeout/Error` and excluded from score calculation.

### Slow Test Execution

**Problem:** Each mutant requires rebuild and test execution (~100-500ms per mutant).

**Solutions:**
1. Use incremental builds (CMake caches unchanged files)
2. Run tests in parallel (future enhancement)
3. Use ML prediction to skip equivalent mutants (optional)

---

## Best Practices

### 1. Write Comprehensive Tests

```cpp
// BAD: Single test case
TEST(CalculatorTest, Add) {
    EXPECT_EQ(Calculator::Add(2, 3), 5);
}

// GOOD: Multiple test cases covering edge cases
TEST(CalculatorTest, Add) {
    EXPECT_EQ(Calculator::Add(2, 3), 5);      // Positive
    EXPECT_EQ(Calculator::Add(-1, -1), -2);   // Negative
    EXPECT_EQ(Calculator::Add(0, 5), 5);      // Zero
    EXPECT_EQ(Calculator::Add(INT_MAX, 0), INT_MAX);  // Boundary
}
```

### 2. Test Boundary Conditions

```cpp
TEST(CalculatorTest, Divide) {
    EXPECT_EQ(Calculator::Divide(6, 3), 2);
    EXPECT_EQ(Calculator::Divide(5, 2), 2);   // Integer division
    EXPECT_EQ(Calculator::Divide(5, 0), 0);   // Division by zero
}
```

### 3. Test All Operators

Ensure every operator in your code is covered by tests:

```cpp
// If you have: return a > b && a > 0
// Test both conditions:
TEST(Test, BothConditions) {
    EXPECT_TRUE(Compare(5, 3));   // Both true
    EXPECT_FALSE(Compare(-1, -2)); // First true, second false
    EXPECT_FALSE(Compare(3, 5));   // First false
}
```

### 4. Use Google Test Assertions

```cpp
EXPECT_EQ(actual, expected);     // Equality
EXPECT_NE(actual, expected);     // Inequality
EXPECT_TRUE(condition);          // Boolean true
EXPECT_FALSE(condition);         // Boolean false
EXPECT_LT(val1, val2);           // Less than
EXPECT_GT(val1, val2);           // Greater than
```

---

## Performance

### Generation Speed

| Language   | Mutants | Time   | Speed        |
|------------|---------|--------|--------------|
| **C++**    | 75      | ~5ms   | 15,000/sec   |
| Go         | 62      | 3ms    | 20,666/sec โšก|
| Python     | 56      | 5.2ms  | 10,769/sec   |
| TypeScript | 67      | 14ms   | 4,785/sec    |

**Why C++ is fast:**
- Simple AST structure (no type inference needed)
- Efficient tree-sitter-cpp parser
- Minimal operator complexity
- Direct byte-level mutations

### Test Execution Speed

**Per-Mutant Cost:**
- CMake rebuild: ~50-200ms (incremental)
- CTest execution: ~50-300ms
- **Total: ~100-500ms per mutant**

**For 75 mutants:** ~7.5-37.5 seconds

**Optimization:** CMake caches unchanged files, making incremental builds fast.

---

## Comparison with Other Tools

### vs mull (LLVM-based)

| Feature | PMAT C++ | mull |
|---------|----------|------|
| Installation | Cargo (easy) | LLVM setup (complex) |
| Speed | ~5ms generation | Slower (IR analysis) |
| Operators | 7 (focused) | Many (comprehensive) |
| Integration | CMake/CTest | LLVM toolchain |
| Portability | Cross-platform | LLVM-dependent |

### vs mutate_cpp

| Feature | PMAT C++ | mutate_cpp |
|---------|----------|------------|
| Parser | tree-sitter AST | Regex-based |
| Accuracy | High | Lower (regex) |
| Speed | Fast | Very fast |
| Maintainability | Good | Limited |

### Advantages of PMAT

1. **Easy installation** - No LLVM dependencies
2. **Fast generation** - Tree-sitter AST parsing
3. **Standard workflow** - Works with existing CMake projects
4. **Cross-language** - Same architecture for TypeScript, Python, Go, C++
5. **Active development** - Part of larger PMAT ecosystem

---

## Advanced Usage

### Custom Operators

```rust
use pmat::services::mutation::tree_sitter_operators::TreeSitterMutationOperator;

struct CustomCppOperator;

impl TreeSitterMutationOperator for CustomCppOperator {
    fn name(&self) -> &str { "CustomOp" }
    fn can_mutate(&self, node: &Node, source: &[u8]) -> bool {
        // Custom logic
    }
    fn mutate(&self, node: &Node, source: &[u8]) -> Vec<MutatedSource> {
        // Custom mutations
    }
}

let generator = CppMutationGenerator::new(vec![
    Box::new(CustomCppOperator),
]);
```

### Filtering Mutants

```rust
let mutants = generator.generate_mutants(&source, "file.cpp")?;

// Filter by operator type
let arithmetic_only = mutants.iter()
    .filter(|m| matches!(m.operator, MutationOperatorType::ArithmeticReplacement))
    .collect::<Vec<_>>();

// Filter by location
let critical_section = mutants.iter()
    .filter(|m| m.location.line >= 100 && m.location.line <= 200)
    .collect::<Vec<_>>();
```

### Integration with CI/CD

```yaml
# GitHub Actions example
name: Mutation Testing

on: [push]

jobs:
  mutation:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      
      - name: Install CMake
        run: sudo apt-get install cmake
      
      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
      
      - name: Run Mutation Testing
        run: |
          cargo run --example cpp_mutation_workflow --features cpp-ast
          
      - name: Check Mutation Score
        run: |
          # Parse output and fail if score < 80%
          if [ mutation_score -lt 80 ]; then
            echo "Mutation score too low!"
            exit 1
          fi
```

---

## FAQ

**Q: Why aren't pointer operators mutated?**  
A: Pointer mutations (`*` โ†” `&`, `->` โ†” `.`) require type information to avoid semantic errors. They are detected for awareness but not mutated.

**Q: Why is C++ slower than Go?**  
A: CMake rebuild overhead (~50-200ms per mutant) vs Go's fast compilation (~5ms).

**Q: Can I use with Catch2 or other test frameworks?**  
A: Yes! Any test framework that integrates with CTest works. Modify the workflow example to use your test command.

**Q: How many mutants should I expect?**  
A: Roughly 4-8 mutants per operator occurrence. 100 LOC with 20 operators โ‰ˆ 80-160 mutants.

**Q: What's a good mutation score?**  
A: 80%+ is excellent, 60-80% is good, <60% needs improvement.

---

## References

- [tree-sitter-cpp]https://github.com/tree-sitter/tree-sitter-cpp - Parser library
- [Google Test]https://github.com/google/googletest - Test framework
- [CMake]https://cmake.org/ - Build system
- [mull]https://github.com/mull-project/mull - LLVM-based mutation testing
- [mutate_cpp]https://github.com/nlohmann/mutate_cpp - Regex-based mutation testing

---

## Contributing

Found a bug or want to add a feature? See [CONTRIBUTING.md](../../CONTRIBUTING.md).

---

## Changelog

### v2.154.0 (Current)
- โœ… Initial C++ mutation testing release
- โœ… 7 mutation operators (5 active, 2 detection-only)
- โœ… CMake/CTest integration
- โœ… Google Test support
- โœ… Tree-sitter AST parsing
- โœ… ~5ms generation for 75+ mutants

---

*Last Updated: 2025-10-09 | Version: 2.154.0*