Use when writing tests for RegJIT features, verifying compatibility with PCRE/RE2, and ensuring code quality
Write comprehensive tests for RegJIT regex engine to ensure correctness, prevent regressions, and verify compatibility with reference engines (PCRE, std::regex, RE2).
Test individual components in isolation
Example: Character matching
Initialize();
CompileRegex("a");
assert(Execute("a") == 1); // Match
assert(Execute("b") == 0); // No match
CleanUp();
Test full compilation pipeline with realistic patterns
Example: Anchor quantifier
Initialize();
CompileRegex("^+");
assert(Execute("") == 1); // Match at start
assert(Execute("xyz") == 1); // Match at start
CleanUp();
Test boundary conditions and special scenarios
Example: Empty string and boundaries
Initialize();
CompileRegex("^$");
assert(Execute("") == 1); // Match empty
assert(Execute("x") == 0); // Non-empty fails
CleanUp();
#include "../src/regjit.h"
#include <iostream>
#include <cassert>
int main() {
std::cout << "[Feature Name Tests]" << std::endl;
// Test case 1
Initialize();
CompileRegex("pattern");
assert(Execute("input") == expected);
CleanUp();
// Test case 2
Initialize();
CompileRegex("pattern2");
assert(Execute("input2") == expected2);
CleanUp();
std::cout << "[All tests passed]" << std::endl;
return 0;
}
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β
^ (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+
β
[abc] - Character set
β
[a-z] - Range matching
β
[^abc] - Negated class
β
[a-zA-Z0-9] - Multiple ranges
β
. - Any character
β
* - 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)
β
Anchors + Quantifiers
β
Character class + Quantifiers
β
Alternation + Anchors
β
Groups + Features
// GOOD - Intent clear
assert(Execute("a") == 1); // Single char matches
// UNCLEAR - Intent obscured
assert(1 == Execute("a")); // Backwards, hard to read
// GOOD - Single behavior
Initialize();
CompileRegex("a+");
assert(Execute("aaa") == 1);
CleanUp();
// WRONG - Multiple behaviors mixed
Initialize();
CompileRegex("a+");
assert(Execute("aaa") == 1 && Execute("b") == 0);
CleanUp();
// Pattern: [a-z]+
Initialize();
CompileRegex("[a-z]+");
assert(Execute("") == 0); // Empty
assert(Execute("a") == 1); // Minimum
assert(Execute("abc") == 1); // Normal
assert(Execute("z") == 1); // Boundary
assert(Execute("A") == 0); // Outside range
assert(Execute("a1b") == 0); // Non-matching char
CleanUp();
// After writing test, verify with PCRE:
// pcre: [a-z]+ matches "abc" β
// pcre: [a-z]+ does NOT match "A" β
// Our implementation matches PCRE β
# Specific test
make test_anchor_quant_edge
# Run test
./test_anchor_quant_edge
# All tests
make test_all
# Build specific test
make test_charclass
# Run after building
./test_charclass
# Clean and rebuild
make clean && make test_all
# View test output
make test_anchor 2>&1 | tail -20
// Pattern: $+
// Expected: Matches only at end, but attempts at all offsets
// PCRE: $+ on "abc" matches at position 3 β
// std::regex: $+ on "abc" matches β
// RE2: $+ on "abc" matches β
// RegJIT: Our implementation matches β
Initialize();
CompileRegex("$+");
assert(Execute("abc") == 1);
CleanUp();
| 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 |