| name | regjit-testing |
| description | Use when writing tests for RegJIT features, verifying compatibility with PCRE/RE2, and ensuring code quality |
| metadata | {"author":"acekingke"} |
RegJIT Testing
Overview
Write comprehensive tests for RegJIT regex engine to ensure correctness, prevent regressions, and verify compatibility with reference engines (PCRE, std::regex, RE2).
When to Use
- After implementing new features
- Before committing code
- When fixing bugs (write test first)
- When refactoring regex behavior
- When verifying anchor/quantifier edge cases
Test Types
Unit Tests
Test individual components in isolation
Example: Character matching
Initialize();
CompileRegex("a");
assert(Execute("a") == 1);
assert(Execute("b") == 0);
CleanUp();
Integration Tests
Test full compilation pipeline with realistic patterns
Example: Anchor quantifier
Initialize();
CompileRegex("^+");
assert(Execute("") == 1);
assert(Execute("xyz") == 1);
CleanUp();
Edge Case Tests
Test boundary conditions and special scenarios
Example: Empty string and boundaries
Initialize();
CompileRegex("^$");
assert(Execute("") == 1);
assert(Execute("x") == 0);
CleanUp();
RegJIT Test Structure
Template
#include "../src/regjit.h"
#include <iostream>
#include <cassert>
int main() {
std::cout << "[Feature Name Tests]" << std::endl;
Initialize();
CompileRegex("pattern");
assert(Execute("input") == expected);
CleanUp();
Initialize();
CompileRegex("pattern2");
assert(Execute("input2") == expected2);
CleanUp();
std::cout << "[All tests passed]" << std::endl;
return 0;
}
Test File Naming
test_anchor.cpp - Anchor features (^, $, \b, \B)
test_charclass.cpp - Character classes ([abc], [a-z])
test_quantifier.cpp - Quantifiers (*, +, {n})
test_anchor_quant_edge.cpp - Anchor + quantifier combinations
Test Categories for RegJIT
1. Anchor Tests
✅ ^ (start anchor)
- Matches at position 0
- With quantifiers: ^*, ^+, ^{n}
✅ $ (end anchor)
- Matches at string length
- With quantifiers: $*, $+, ${n}
✅ \b (word boundary)
- Between \w and \W characters
- With quantifiers: \b*, \b+
✅ \B (non-word boundary)
- NOT between \w and \W
- With quantifiers: \B*, \B+
2. Character Class Tests
✅ [abc] - Character set
✅ [a-z] - Range matching
✅ [^abc] - Negated class
✅ [a-zA-Z0-9] - Multiple ranges
✅ . - Any character
3. Quantifier Tests
✅ * - Zero or more (greedy)
✅ + - One or more (greedy)
✅ ? - Zero or one
✅ {n} - Exactly n times
✅ {n,} - At least n times
✅ {n,m} - Between n and m times
✅ *? - Zero or more (non-greedy)
✅ +? - One or more (non-greedy)
4. Combination Tests
✅ Anchors + Quantifiers
✅ Character class + Quantifiers
✅ Alternation + Anchors
✅ Groups + Features
Writing Good Tests
Principle 1: Clear Intent
assert(Execute("a") == 1);
assert(1 == Execute("a"));
Principle 2: Test One Thing
Initialize();
CompileRegex("a+");
assert(Execute("aaa") == 1);
CleanUp();
Initialize();
CompileRegex("a+");
assert(Execute("aaa") == 1 && Execute("b") == 0);
CleanUp();
Principle 3: Edge Cases Matter
Initialize();
CompileRegex("[a-z]+");
assert(Execute("") == 0);
assert(Execute("a") == 1);
assert(Execute("abc") == 1);
assert(Execute("z") == 1);
assert(Execute("A") == 0);
assert(Execute("a1b") == 0);
CleanUp();
Principle 4: Verify Against Reference
Test Execution
Building Tests
make test_anchor_quant_edge
./test_anchor_quant_edge
make test_all
Common Commands
make test_charclass
./test_charclass
make clean && make test_all
make test_anchor 2>&1 | tail -20
PCRE/RE2 Compatibility Testing
Verification Checklist
Example: Anchor Quantifier Compatibility
Initialize();
CompileRegex("$+");
assert(Execute("abc") == 1);
CleanUp();
Test Maintenance
When to Add Tests
When to Update Tests
When to Remove Tests
Quick Reference
| Pattern | Test Files | Purpose |
|---|
| Anchors | test_anchor.cpp | ^ $ \b \B |
| Charclass | test_charclass.cpp | [...] . |
| Quantifiers | test_quantifier.cpp | * + ? {n} |
| Edge Cases | test_anchor_quant_edge.cpp | Complex combinations |
Success Criteria
Converted and distributed by TomeVault — claim your Tome and manage your conversions.